earth 0.4.12 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. data/.gitignore +1 -0
  2. data/Gemfile +1 -1
  3. data/earth.gemspec +7 -3
  4. data/features/automobile_make_model_year_variant.feature +10 -10
  5. data/features/step_definitions/data_steps.rb +1 -1
  6. data/features/support/env.rb +2 -2
  7. data/features/support/imports/automobile_fuel_good.csv +3 -3
  8. data/features/support/imports/automobile_make_model_year_variant_bad.csv +2 -2
  9. data/features/support/imports/automobile_make_model_year_variant_good.csv +2 -2
  10. data/lib/earth/air.rb +2 -1
  11. data/lib/earth/air/aircraft.rb +21 -8
  12. data/lib/earth/air/aircraft/data_miner.rb +85 -180
  13. data/lib/earth/air/aircraft_class.rb +9 -1
  14. data/lib/earth/air/aircraft_class/data_miner.rb +57 -20
  15. data/lib/earth/air/aircraft_fuel_use_equation.rb +26 -0
  16. data/lib/earth/air/aircraft_fuel_use_equation/data_miner.rb +26 -0
  17. data/lib/earth/air/aircraft_manufacturer.rb +0 -2
  18. data/lib/earth/air/aircraft_manufacturer/data_miner.rb +4 -1
  19. data/lib/earth/air/airline.rb +2 -11
  20. data/lib/earth/air/airline/data_miner.rb +10 -51
  21. data/lib/earth/air/airport.rb +2 -29
  22. data/lib/earth/air/airport/data_miner.rb +61 -56
  23. data/lib/earth/air/bts_aircraft.rb +7 -0
  24. data/lib/earth/air/bts_aircraft/data_miner.rb +15 -0
  25. data/lib/earth/air/data_miner.rb +2 -1
  26. data/lib/earth/air/flight_seat_class.rb +1 -4
  27. data/lib/earth/air/flight_segment.rb +16 -10
  28. data/lib/earth/air/flight_segment/data_miner.rb +170 -129
  29. data/lib/earth/automobile/automobile_make/data_miner.rb +12 -3
  30. data/lib/earth/automobile/automobile_make_model/data_miner.rb +4 -1
  31. data/lib/earth/automobile/automobile_make_model_year/data_miner.rb +4 -1
  32. data/lib/earth/automobile/automobile_make_model_year_variant/data_miner.rb +29 -16
  33. data/lib/earth/automobile/automobile_make_year/data_miner.rb +8 -2
  34. data/lib/earth/automobile/automobile_size_class/data_miner.rb +4 -1
  35. data/lib/earth/automobile/automobile_type_fuel_age/data_miner.rb +4 -1
  36. data/lib/earth/automobile/automobile_type_fuel_year/data_miner.rb +4 -2
  37. data/lib/earth/automobile/automobile_type_fuel_year_age/data_miner.rb +4 -1
  38. data/lib/earth/automobile/automobile_type_year/data_miner.rb +4 -1
  39. data/lib/earth/fuel/fuel/data_miner.rb +4 -1
  40. data/lib/earth/hospitality/lodging_class.rb +0 -16
  41. data/lib/earth/hospitality/lodging_class/data_miner.rb +11 -1
  42. data/lib/earth/inflectors.rb +1 -1
  43. data/lib/earth/locality/country/data_miner.rb +14 -14
  44. data/lib/earth/locality/urbanity/data_miner.rb +4 -1
  45. data/lib/earth/residence/air_conditioner_use/data_miner.rb +4 -1
  46. data/lib/earth/residence/clothes_machine_use/data_miner.rb +4 -1
  47. data/lib/earth/residence/dishwasher_use/data_miner.rb +4 -1
  48. data/lib/earth/residence/residence_appliance/data_miner.rb +5 -2
  49. data/lib/earth/residence/residence_class/data_miner.rb +4 -1
  50. data/lib/earth/version.rb +1 -1
  51. data/spec/earth_spec.rb +2 -2
  52. data/test/test_aircraft_match.rb +732 -0
  53. metadata +87 -51
  54. data/features/flight_segment.feature +0 -14
  55. data/features/support/imports/flight_segment_bad.csv +0 -8
  56. data/features/support/imports/flight_segment_good.csv +0 -8
  57. data/lib/earth/air/flight_fuel_type.rb +0 -12
  58. data/lib/earth/air/flight_fuel_type/data_miner.rb +0 -12
@@ -1,7 +1,15 @@
1
1
  class AircraftClass < ActiveRecord::Base
2
2
  set_primary_key :code
3
3
 
4
- # has_many :aircraft, :foreign_key => 'class_code', :primary_key => 'code'
4
+ has_many :aircraft, :foreign_key => 'class_code', :primary_key => 'code'
5
+
6
+ def fuel_use_coefficients
7
+ [m3, m2, m1, b]
8
+ end
9
+
10
+ def valid_fuel_use_equation?
11
+ fuel_use_coefficients.all?(&:present?) and fuel_use_coefficients.any?(&:nonzero?)
12
+ end
5
13
 
6
14
  data_miner do
7
15
  tap "Brighter Planet's aircraft class data", Earth.taps_server
@@ -1,30 +1,67 @@
1
1
  AircraftClass.class_eval do
2
2
  data_miner do
3
3
  schema Earth.database_options do
4
- string 'code'
5
- string 'name'
6
- float 'm1'
7
- float 'm2'
8
- float 'm3'
9
- float 'endpoint_fuel'
10
- float 'seats'
4
+ string 'code'
5
+ float 'm3'
6
+ string 'm3_units'
7
+ float 'm2'
8
+ string 'm2_units'
9
+ float 'm1'
10
+ string 'm1_units'
11
+ float 'b'
12
+ string 'b_units'
13
+ float 'seats'
11
14
  end
12
15
 
13
- import "a list of Brighter Planet-defined aircraft classes",
14
- :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdGNBbHFibmxJUFprQkUwZHp6VU51Smc&hl=en&gid=0&output=csv' do
15
- key 'code', :field_name => 'aircraft_class_code'
16
- store 'name'
16
+ process "Ensure Aircraft is populated" do
17
+ Aircraft.run_data_miner!
17
18
  end
18
19
 
19
- process "Derive some average aircraft chraracteristics from aircraft" do
20
- Aircraft.run_data_miner!
21
- aircraft = Aircraft.arel_table
22
- aircraft_classes = AircraftClass.arel_table
23
- conditional_relation = aircraft_classes[:code].eq(aircraft[:class_code])
24
-
25
- %w{ m1 m2 m3 endpoint_fuel seats }.each do |column|
26
- relation = Aircraft.weighted_average_relation(column).where(conditional_relation)
27
- update_all "#{column} = (#{relation.to_sql})"
20
+ process "Derive aircraft classes from Aircraft" do
21
+ connection.select_values("SELECT DISTINCT class_code FROM aircraft WHERE aircraft.class_code IS NOT NULL").each do |class_code|
22
+ AircraftClass.find_or_create_by_code(class_code)
23
+ end
24
+ end
25
+
26
+ process "Derive some average characteristics from Aircraft" do
27
+ AircraftClass.find_each do |aircraft_class|
28
+ %w{ m3 m2 m1 b }.each do |coefficient|
29
+ value = AircraftFuelUseEquation.where("aircraft.class_code = '#{aircraft_class.code}'").
30
+ weighted_average(:"#{coefficient}", :weighted_by => [:aircraft, :passengers])
31
+ aircraft_class.send("#{coefficient}=", value)
32
+ end
33
+ # # do this in sql because we want to preserve nils and weighted_average returns 0 when it gets NULL
34
+ # connection.execute %{
35
+ # UPDATE aircraft_classes
36
+ # SET aircraft_classes.#{coefficient} = (
37
+ # SELECT sum(aircraft_fuel_use_equations.#{coefficient} * aircraft.passengers) / sum(aircraft.passengers)
38
+ # FROM aircraft_fuel_use_equations
39
+ # INNER JOIN aircraft
40
+ # ON aircraft.fuel_use_code = aircraft_fuel_use_equations.code
41
+ # WHERE aircraft.class_code = '#{aircraft_class.code}'
42
+ # AND aircraft_fuel_use_equations.#{coefficient} IS NOT NULL
43
+ # )
44
+ # WHERE aircraft_classes.code = '#{aircraft_class.code}'
45
+ # }
46
+ # end
47
+
48
+ aircraft_class.seats = aircraft_class.aircraft.weighted_average(:seats, :weighted_by => :passengers)
49
+ # # do this in sql because we want to preserve nils and weighted_average returns 0 when it gets NULL
50
+ # connection.execute %{
51
+ # UPDATE aircraft_classes
52
+ # SET aircraft_classes.seats = (
53
+ # SELECT sum(aircraft.seats * aircraft.passengers) / sum(aircraft.passengers)
54
+ # FROM aircraft
55
+ # WHERE aircraft.class_code = '#{aircraft_class.code}'
56
+ # )
57
+ # WHERE aircraft_classes.code = '#{aircraft_class.code}'
58
+ # }
59
+
60
+ aircraft_class.m3_units = 'kilograms_per_cubic_nautical_mile'
61
+ aircraft_class.m2_units = 'kilograms_per_square_nautical_mile'
62
+ aircraft_class.m1_units = 'kilograms_per_nautical_mile'
63
+ aircraft_class.b_units = 'kilograms'
64
+ aircraft_class.save
28
65
  end
29
66
  end
30
67
 
@@ -0,0 +1,26 @@
1
+ class AircraftFuelUseEquation < ActiveRecord::Base
2
+ set_primary_key :code
3
+
4
+ has_many :aircraft, :foreign_key => 'fuel_use_code', :primary_key => 'code'
5
+
6
+ falls_back_on :m3 => lambda { weighted_average(:m3, :weighted_by => [:aircraft, :passengers]) }, # 9.73423082858437e-08 r7110: 8.6540464368905e-8 r6972: 8.37e-8
7
+ :m2 => lambda { weighted_average(:m2, :weighted_by => [:aircraft, :passengers]) }, # -0.000134350543484608 r7110: -0.00015337661447817 r6972: -4.09e-5
8
+ :m1 => lambda { weighted_average(:m1, :weighted_by => [:aircraft, :passengers]) }, # 6.7728101555467 r7110: 4.7781966869412 r6972: 7.85
9
+ :b => lambda { weighted_average(:b, :weighted_by => [:aircraft, :passengers]) }, # 1527.81790006167 r7110: 1065.3476555284 r6972: 1.72e3
10
+ :m3_units => 'kilograms_per_cubic_nautical_mile',
11
+ :m2_units => 'kilograms_per_square_nautical_mile',
12
+ :m1_units => 'kilograms_per_nautical_mile',
13
+ :b_units => 'kilograms'
14
+
15
+ def fuel_use_coefficients
16
+ [m3, m2, m1, b]
17
+ end
18
+
19
+ def valid_fuel_use_equation?
20
+ fuel_use_coefficients.all?(&:present?) and fuel_use_coefficients.any?(&:nonzero?)
21
+ end
22
+
23
+ data_miner do
24
+ tap "Brighter Planet's sanitized aircraft fuel use data", Earth.taps_server
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ AircraftFuelUseEquation.class_eval do
2
+ data_miner do
3
+ schema Earth.database_options do
4
+ string 'code'
5
+ string 'aircraft_description'
6
+ float 'm3'
7
+ string 'm3_units'
8
+ float 'm2'
9
+ string 'm2_units'
10
+ float 'm1'
11
+ string 'm1_units'
12
+ float 'b'
13
+ string 'b_units'
14
+ end
15
+
16
+ import "aircraft fuel use equations derived from EMEP/EEA and ICAO",
17
+ :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdDltUVZVekVobEJPYlpFNUpWNkwyYXc&output=csv' do
18
+ key 'code'
19
+ store 'aircraft_description'
20
+ store 'm3', :units_field_name => 'm3_units'
21
+ store 'm2', :units_field_name => 'm2_units'
22
+ store 'm1', :units_field_name => 'm1_units'
23
+ store 'b', :units_field_name => 'b_units'
24
+ end
25
+ end
26
+ end
@@ -1,8 +1,6 @@
1
1
  class AircraftManufacturer < ActiveRecord::Base
2
2
  set_primary_key :name
3
3
 
4
- # has_many :aircraft, :foreign_key => 'name', :primary_key => 'bp_code'
5
-
6
4
  data_miner do
7
5
  tap "Brighter Planet's aircraft manufacturer data", Earth.taps_server
8
6
  end
@@ -8,8 +8,11 @@ AircraftManufacturer.class_eval do
8
8
  string 'name'
9
9
  end
10
10
 
11
- process "Derive a list of aircraft manufacturers from aircraft" do
11
+ process "Ensure Aircraft is populated" do
12
12
  Aircraft.run_data_miner!
13
+ end
14
+
15
+ process "Derive a list of aircraft manufacturers from aircraft" do
13
16
  INSERT_IGNORE %{INTO aircraft_manufacturers(name)
14
17
  SELECT aircraft.manufacturer_name FROM aircraft WHERE aircraft.manufacturer_name IS NOT NULL
15
18
  }
@@ -1,16 +1,7 @@
1
1
  class Airline < ActiveRecord::Base
2
- set_primary_key :iata_code
3
-
4
- # has_many :airline_aircraft, :class_name => 'AirlineAircraft'
5
- # has_many :seat_classes, :class_name => 'AirlineSeatClass'
6
- # has_many :segments, :foreign_key => 'airline_iata_code', :primary_key => 'iata_code', :class_name => "FlightSegment"
7
- # has_many :airline_aircraft_seat_classes, :class_name => 'AirlineAircraftSeatClass'
8
-
2
+ set_primary_key :name
3
+
9
4
  data_miner do
10
5
  tap "Brighter Planet's sanitized airlines data", Earth.taps_server
11
6
  end
12
-
13
- def all_flights_domestic?
14
- !international?
15
- end
16
7
  end
@@ -1,59 +1,18 @@
1
- require 'errata'
2
-
3
1
  Airline.class_eval do
4
- class Airline::Guru
5
- # needed by errata
6
- def is_not_coral_air?(row); row['Code'].to_i != 19155; end # 19155 Coral Air Inc.: COR
7
- def is_not_aviacion_y_comercio?(row); row['Code'].to_i != 19452; end # 19452 Aviacion Y Comercio S.A.: AO
8
- def is_not_air_china?(row); row['Code'].to_i != 19543; end # 19543 Air China: CA
9
- def is_not_south_african_airways?(row); row['Code'].to_i != 19570; end # 19570 South African Airways: SA
10
- def is_not_continental_airlines?(row); row['Code'].to_i != 19704; end # 19704 Continental Air Lines Inc.: CO
11
- def is_not_sallee_s_aviation?(row); row['Code'].to_i != 19740; end # 19740 Sallee's Aviation: SAL
12
- def is_not_air_berlin?(row); row['Code'].to_i != 21361; end # "21361","Air Berlin PLC and CO: AB"
13
- end
14
-
15
2
  data_miner do
16
3
  schema Earth.database_options do
17
- string 'iata_code'
18
- string 'name'
19
- string 'dot_airline_id_code'
20
- boolean 'international'
21
- float 'seats'
22
- float 'distance'
23
- string 'distance_units'
24
- float 'load_factor'
25
- float 'freight_share'
26
- float 'payload'
27
- string 'payload_units'
28
- end
29
-
30
- import "the T100 AIRLINE_ID lookup table, which also includes IATA codes",
31
- :url => 'http://www.transtats.bts.gov/Download_Lookup.asp?Lookup=L_AIRLINE_ID',
32
- :errata => { :url => 'http://static.brighterplanet.com/science/data/transport/air/airlines/errata.csv', :responder => Airline::Guru.new } do
33
- key 'iata_code', :field_name => 'Description', :split => { :pattern => /:/, :keep => 1 }
34
- store 'dot_airline_id_code', :field_name => 'Code'
35
- store 'name', :field_name => 'Description', :split => { :pattern => /:/, :keep => 0 }
36
- end
37
-
38
- process "Determine whether airlines fly internationally by looking at flight segments" do
39
- FlightSegment.run_data_miner!
40
- update_all 'international = 1', '(SELECT COUNT(*) FROM flight_segments WHERE flight_segments.airline_iata_code = airlines.iata_code AND flight_segments.origin_country_iso_3166_code != flight_segments.destination_country_iso_3166_code AND flight_segments.origin_country_iso_3166_code IS NOT NULL AND flight_segments.destination_country_iso_3166_code IS NOT NULL) > 0'
4
+ string 'name'
5
+ string 'bts_code'
6
+ string 'iata_code'
7
+ string 'icao_code'
41
8
  end
42
9
 
43
- process "Derive some average flight characteristics from flight segments" do
44
- FlightSegment.run_data_miner!
45
- airlines = Airline.arel_table
46
- segments = FlightSegment.arel_table
47
-
48
- conditional_relation = airlines[:iata_code].eq(segments[:airline_iata_code])
49
-
50
- # sabshere 2/1/11 these need units
51
- update_all "seats = (#{FlightSegment.weighted_average_relation(:seats, :weighted_by => :passengers ).where(conditional_relation).to_sql})"
52
- update_all "distance = (#{FlightSegment.weighted_average_relation(:distance, :weighted_by => :passengers ).where(conditional_relation).to_sql})"
53
- update_all "load_factor = (#{FlightSegment.weighted_average_relation(:load_factor, :weighted_by => :passengers ).where(conditional_relation).to_sql})"
54
- update_all "freight_share = (#{FlightSegment.weighted_average_relation(:freight_share, :weighted_by => :passengers ).where(conditional_relation).to_sql})"
55
- update_all "payload = (#{FlightSegment.weighted_average_relation(:payload, :weighted_by => :passengers, :disaggregate_by => :departures_performed).where(conditional_relation).to_sql})"
10
+ import "a Brighter Planet-curated list of airlines",
11
+ :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdDhoVHZmSTlZcHBsRUtPR0dPd0prMkE&output=csv' do
12
+ key 'name'
13
+ store 'bts_code', :nullify => true
14
+ store 'iata_code', :nullify => true
15
+ store 'icao_code', :nullify => true
56
16
  end
57
17
  end
58
18
  end
59
-
@@ -1,27 +1,12 @@
1
1
  class Airport < ActiveRecord::Base
2
2
  set_primary_key :iata_code
3
3
 
4
- # --------------------------------
5
- # virtual has_many association
6
- # has_many :segments won't work because there's no general way to specify the correct conditions
7
- # even if you get clever with it, like
8
- # has_many :segments,
9
- # :class_name => 'FlightSegment',
10
- # :foreign_key => 'origin_airport_id',
11
- # :conditions => 'flight_segments.destination_airport_id = #{id}'
12
- # you get queries like "`flight_segments`.origin_airport_id = 3654 AND (flight_segments.destination_airport_id = 3654))"
13
- # in which you notice the AND which must be an OR
14
- # and you can't just do finder_sql, because that breaks any other :select
15
- def segments
16
- FlightSegment.scoped :conditions => ['origin_airport_id = ? OR destination_airport_id = ?', id, id]
17
- end
18
- # --------------------------------
19
-
20
4
  belongs_to :country, :foreign_key => 'country_iso_3166_code', :primary_key => 'iso_3166_code'
5
+
21
6
  acts_as_mappable :default_units => :nms,
22
7
  :lat_column_name => :latitude,
23
8
  :lng_column_name => :longitude
24
-
9
+
25
10
  data_miner do
26
11
  tap "Brighter Planet's sanitized airports data", Earth.taps_server
27
12
 
@@ -29,16 +14,4 @@ class Airport < ActiveRecord::Base
29
14
  run_data_miner_on_belongs_to_associations
30
15
  end
31
16
  end
32
-
33
- def all_flights_from_here_domestic?
34
- !international_origin?
35
- end
36
-
37
- def all_flights_to_here_domestic?
38
- !international_destination?
39
- end
40
-
41
- def united_states?
42
- country == Country.united_states
43
- end
44
17
  end
@@ -1,4 +1,19 @@
1
1
  Airport.class_eval do
2
+ class Airport::Guru
3
+ def method_missing(method_id, *args, &block)
4
+ if method_id.to_s =~ /\A(id|iata)_is_([a-z]{3}|\d{1,4})\?$/
5
+ regexp = Regexp.new($2, Regexp::IGNORECASE)
6
+ if $1 == "iata"
7
+ args.first['iata_code'] =~ regexp # row['iata_code'] =~ /meh/i
8
+ else
9
+ args.first[$1] =~ regexp # row['id'] =~ /1234/i
10
+ end
11
+ else
12
+ super
13
+ end
14
+ end
15
+ end
16
+
2
17
  data_miner do
3
18
  schema Earth.database_options do
4
19
  string 'iata_code'
@@ -8,74 +23,64 @@ Airport.class_eval do
8
23
  string 'country_iso_3166_code'
9
24
  float 'latitude'
10
25
  float 'longitude'
11
- float 'seats'
12
- float 'distance'
13
- string 'distance_units'
14
- float 'load_factor'
15
- float 'freight_share'
16
- float 'payload'
17
- string 'payload_units'
18
- boolean 'international_origin'
19
- boolean 'international_destination'
20
26
  end
21
27
 
22
28
  import "the OpenFlights.org airports database",
23
29
  :url => 'https://openflights.svn.sourceforge.net/svnroot/openflights/openflights/data/airports.dat',
24
- :headers => false,
25
- :select => lambda { |row| row[4].present? } do
26
- key 'iata_code', :field_number => 4
27
- store 'name', :field_number => 1
28
- store 'city', :field_number => 2
29
- store 'country_name', :field_number => 3
30
- store 'country_iso_3166_code', :field_number => 3, :upcase => true, :dictionary => { :input => 'name', :output => 'iso_3166_code', :url => 'http://data.brighterplanet.com/countries.csv' }
31
- store 'latitude', :field_number => 6
32
- store 'longitude', :field_number => 7
30
+ :headers => %w{ id name city country_name iata_code icao_code latitude longitude altitude timezone daylight_savings },
31
+ :errata => { :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdFc2UzhQYU5PWEQ0N21yWFZGNmc2a3c&gid=0&output=csv',
32
+ :responder => Airport::Guru.new } do
33
+ key 'iata_code'
34
+ store 'name'
35
+ store 'city'
36
+ store 'country_name'
37
+ store 'latitude'
38
+ store 'longitude'
33
39
  end
34
40
 
35
- # step.await :other_class => FlightSegment do |deferred|
36
- # deferred.derive :country # this uses a heuristic that depends on flight segments
37
- # class << self
38
- # def derive_country
39
- # update_all('country_id = (SELECT flight_segments.origin_country_id FROM flight_segments WHERE flight_segments.origin_airport_id = airports.id AND flight_segments.origin_country_id IS NOT NULL LIMIT 1)', 'airports.country_id IS NULL')
40
- # update_all('country_id = (SELECT flight_segments.destination_country_id FROM flight_segments WHERE flight_segments.destination_airport_id = airports.id AND flight_segments.destination_country_id IS NOT NULL LIMIT 1)', 'airports.country_id IS NULL')
41
- # Country.all.each do |c|
42
- # next if c.name.blank?
43
- # update_all("country_id = #{c.id}", ["airports.country_id IS NULL AND airports.country_name LIKE ?", "%#{c.name.upcase}%"])
44
- # end
45
- # Airport.all(:conditions => 'country_id IS NULL AND country_name IS NOT NULL').each do |a|
46
- # c = Country.find(:first, :conditions => ["name like ?", "%#{a.country_name}%"])
47
- # a.update_attributes(:country_id => c.id) if c
48
- # end
49
- # end
50
- # end
41
+ import "airports missing from the OpenFlights.org database",
42
+ :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdHpyR3NudEl5V21ZcEdXQXFDNU8zTWc&output=csv' do
43
+ key 'iata_code'
44
+ store 'name'
45
+ store 'city'
46
+ store 'country_name'
47
+ store 'latitude'
48
+ store 'longitude'
49
+ end
51
50
 
52
- process "Determine whether each airport serves international flights" do
53
- FlightSegment.run_data_miner!
54
- update_all 'international_origin = 1','(SELECT COUNT(*) FROM flight_segments WHERE flight_segments.origin_airport_iata_code = airports.iata_code AND flight_segments.origin_country_iso_3166_code != flight_segments.destination_country_iso_3166_code AND flight_segments.origin_country_iso_3166_code IS NOT NULL AND flight_segments.destination_country_iso_3166_code IS NOT NULL LIMIT 1) > 0'
55
- update_all 'international_destination = 1', '(SELECT COUNT(*) FROM flight_segments WHERE flight_segments.destination_airport_iata_code = airports.iata_code AND flight_segments.origin_country_iso_3166_code != flight_segments.destination_country_iso_3166_code AND flight_segments.origin_country_iso_3166_code IS NOT NULL AND flight_segments.destination_country_iso_3166_code IS NOT NULL LIMIT 1) > 0'
51
+ process "Ensure Country is populated" do
52
+ Country.run_data_miner!
56
53
  end
57
54
 
58
- # sabshere 5/24/10 using temporary tables because the WHERE clause has a very slow OR condition: iata_code = destination_iata_code OR iata_code = origin_iata_code
59
- process "Derive some average flight characteristics from flight segments" do
60
- FlightSegment.run_data_miner!
61
- segments = FlightSegment.arel_table
62
- airports = Airport.arel_table
63
-
64
- find_in_batches do |batch|
65
- batch.each do |airport|
66
- targeting_relation = airports[:iata_code].eq airport.iata_code
67
- conditional_relation = segments[:origin_airport_iata_code].eq(airport.iata_code).or(segments[:destination_airport_iata_code].eq(airport.iata_code))
68
- connection.execute "CREATE TEMPORARY TABLE tmp1 #{FlightSegment.where(conditional_relation).to_sql}"
69
- update_all "seats = (#{FlightSegment.weighted_average_relation(:seats, :weighted_by => :passengers ).to_sql.gsub('flight_segments', 'tmp1')})", targeting_relation.to_sql
70
- update_all "distance = (#{FlightSegment.weighted_average_relation(:distance, :weighted_by => :passengers ).to_sql.gsub('flight_segments', 'tmp1')})", targeting_relation.to_sql
71
- update_all "load_factor = (#{FlightSegment.weighted_average_relation(:load_factor, :weighted_by => :passengers ).to_sql.gsub('flight_segments', 'tmp1')})", targeting_relation.to_sql
72
- update_all "freight_share = (#{FlightSegment.weighted_average_relation(:freight_share, :weighted_by => :passengers ).to_sql.gsub('flight_segments', 'tmp1')})", targeting_relation.to_sql
73
- update_all "payload = (#{FlightSegment.weighted_average_relation(:payload, :weighted_by => :passengers, :disaggregate_by => :departures_performed).to_sql.gsub('flight_segments', 'tmp1')})", targeting_relation.to_sql
74
- connection.execute 'DROP TABLE tmp1'
75
- end
55
+ process "Replace country names with country ISO 3166 codes" do
56
+ connection.select_values("SELECT DISTINCT country_name FROM airports WHERE country_name IS NOT NULL").each do |name|
57
+ code = Country.find_by_name(name).iso_3166_code
58
+ update_all %{country_iso_3166_code = "#{code}"}, %{country_name = "#{name}"}
76
59
  end
77
60
  end
78
61
 
62
+ # 10/14/2011 cutting this b/c we don't use it - Ian
63
+ # sabshere 5/24/10 using temporary tables because the WHERE clause has a very slow OR condition: iata_code = destination_iata_code OR iata_code = origin_iata_code
64
+ # process "Derive some average flight characteristics from flight segments" do
65
+ # FlightSegment.run_data_miner!
66
+ # segments = FlightSegment.arel_table
67
+ # airports = Airport.arel_table
68
+ #
69
+ # find_in_batches do |batch|
70
+ # batch.each do |airport|
71
+ # targeting_relation = airports[:iata_code].eq airport.iata_code
72
+ # conditional_relation = segments[:origin_airport_iata_code].eq(airport.iata_code).or(segments[:destination_airport_iata_code].eq(airport.iata_code))
73
+ # connection.execute "CREATE TEMPORARY TABLE tmp1 #{FlightSegment.where(conditional_relation).to_sql}"
74
+ # update_all "seats = (#{FlightSegment.weighted_average_relation(:seats, :weighted_by => :passengers ).to_sql.gsub('flight_segments', 'tmp1')})", targeting_relation.to_sql
75
+ # update_all "distance = (#{FlightSegment.weighted_average_relation(:distance, :weighted_by => :passengers ).to_sql.gsub('flight_segments', 'tmp1')})", targeting_relation.to_sql
76
+ # update_all "load_factor = (#{FlightSegment.weighted_average_relation(:load_factor, :weighted_by => :passengers ).to_sql.gsub('flight_segments', 'tmp1')})", targeting_relation.to_sql
77
+ # update_all "freight_share = (#{FlightSegment.weighted_average_relation(:freight_share, :weighted_by => :passengers ).to_sql.gsub('flight_segments', 'tmp1')})", targeting_relation.to_sql
78
+ # update_all "payload = (#{FlightSegment.weighted_average_relation(:payload, :weighted_by => :passengers, :disaggregate_by => :departures_performed).to_sql.gsub('flight_segments', 'tmp1')})", targeting_relation.to_sql
79
+ # connection.execute 'DROP TABLE tmp1'
80
+ # end
81
+ # end
82
+ # end
83
+
79
84
  # FIXME TODO verify this
80
85
  end
81
86
  end