aboutsummaryrefslogtreecommitdiffstats
path: root/lib/tasks/referential.rake
blob: b3202035b9b2117a7374ab2580e1732a58d9a5fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# coding: utf-8
# execution example: rake 'referential:create[3, 1896, '01-01-2017', '01-04-2017']'

namespace :referential do
  desc 'Create a referential and accompanying data'
  task :create, [:workbench_id, :start_date, :end_date] => [:environment] do |t, args|
    args.with_defaults(workbench_id: 1, start_date: Date.today.strftime, end_date: (Date.today + 30).strftime)

    Referential.transaction do
      workbench = Workbench.find_by!(id: args[:workbench_id])
      line = workbench.line_referential.lines.order("random()").first
      name = "Referential #{Faker::Name.unique.name}"

      referential = workbench.referentials.create!(name: name, slug: name.downcase.parameterize.underscore, organisation: workbench.organisation,
                                                   prefix: Faker::Lorem.unique.characters(10))
      ReferentialMetadata.create!(referential: referential, line_ids: [line.id], periodes: [Date.parse(args[:start_date])..Date.parse(args[:end_date])])
      referential.switch

      print "✓ Created Referential ".green, name, "(#{referential.id})".blue, "\n"
      puts "  For inspection of data in the console, do a: `Referential.last.switch'".blueish

      stop_areas = workbench.stop_area_referential.stop_areas.last(10)

      4.times do |i|
        name = Faker::Name.unique.name
        route_attrs = { line: line, name: "Route #{name}", published_name: "Published #{name}" }

        if i.even?
          route_attrs[:wayback] = :outbound
          route = Chouette::Route.create!(route_attrs)
          route.stop_areas = stop_areas
        else
          route_attrs[:wayback] = :inbound
          route_attrs[:opposite_route] = Chouette::Route.last if i == 3
          route = Chouette::Route.create!(route_attrs)
          route.stop_areas = stop_areas.reverse
        end
        route.save!
        print "  ✓ Created Route ".green, route.name, "(#{route.id}), ".blue, "Line (#{line.id}) has #{line.routes.count} routes\n"

        journey_pattern = Chouette::JourneyPattern.create!(route: route, name: "Journey Pattern #{Faker::Name.unique.name}")
        print "✓ Created JourneyPattern ".green, journey_pattern.name, "(#{journey_pattern.id})".blue, "\n"

        journey_pattern.stop_points = stop_areas.inject([]) { |stop_points, stop_area| stop_points += stop_area.stop_points }

        time_tables = []
        2.times do |j|
          name = "Test #{Faker::Name.unique.name}"
          time_table = Chouette::TimeTable.create!(comment: name, start_date: Date.parse(args[:start_date]) + j.days,
                                                   end_date: Date.parse(args[:end_date]) - j.days)
          print "✓ Created TimeTable ".green, time_table.comment, "(#{time_table.id})".blue, "\n"
          time_tables << time_table
        end

        25.times do |j|
          vehicle_journey = Chouette::VehicleJourney.create!(journey_pattern: journey_pattern, route: route, number: Faker::Number.unique.number(4), time_tables: time_tables)
          print "✓ Created VehicleJourney ".green, vehicle_journey.number, "(#{vehicle_journey.id})".blue, "\n"

          time = Time.current.at_noon + j.minutes
          journey_pattern.stop_points.each_with_index do |stop_point, k|
            vehicle_journey.vehicle_journey_at_stops.create!(stop_point: stop_point, arrival_time: time + k.minutes, departure_time: time + k.minutes + 30.seconds)
          end
        end

      end

      referential.update(ready: true)
    end
  end

  def update_checksums_for_referential referential
    thing = %w(\\ | / —)
    Referential.force_register_models_with_checksum
    puts "\n \e[33m***\e[0m Referential #{referential.name}"
    referential.switch do
      Referential.models_with_checksum.each do |klass|
        i = 0
        j = 0
        prev_size = 1
        head =  "Updating checksums for #{klass.name}: "
        print head
        print "⎯"*(80-head.size)
        print "  "
        count = klass.count
        klass.find_each do |o|
          o.update_checksum!
          if j%10 == 0
            out = "#{"\b"*prev_size}\e[33m#{thing[i]}\e[0m (#{j}/#{count})"
            prev_size = out.size - prev_size - 9
            print out
            i = (i+1) % thing.size
          end
          j += 1
        end
        print "#{"\b"*prev_size}\e[32m✓\e[0m (#{count}/#{count})\n"
      end
    end
  end

  desc 'Update all the checksums in the given referential'
  task :update_checksums_in_referential, [:slug] => :environment do |t, args|
    referential = Referential.find_by_slug(args[:slug])
    update_checksums_for_referential referential
  end

  desc 'Update all the checksums in the given organisation'
  task :update_checksums_in_organisation, [:organisation_id] => :environment do |t, args|
    thing = %w(\\ | / —)
    Organisation.find(args[:organisation_id]).referentials.find_each do |referential|
      update_checksums_for_referential referential
    end
  end
end