wukong 3.0.0.pre2 → 3.0.0.pre3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. data/Gemfile +13 -0
  2. data/README.md +182 -6
  3. data/bin/wu-local +13 -5
  4. data/bin/wu-server +1 -1
  5. data/examples/Gemfile +2 -1
  6. data/examples/basic/string_reverser.rb +23 -0
  7. data/examples/{tiny_count.rb → basic/tiny_count.rb} +0 -0
  8. data/examples/{word_count → basic/word_count}/accumulator.rb +0 -0
  9. data/examples/{word_count → basic/word_count}/tokenizer.rb +0 -0
  10. data/examples/{word_count → basic/word_count}/word_count.rb +0 -0
  11. data/examples/deploy_pack/Gemfile +7 -0
  12. data/examples/deploy_pack/README.md +6 -0
  13. data/examples/{text/latinize_text.rb → deploy_pack/a/b/c/.gitkeep} +0 -0
  14. data/examples/deploy_pack/app/processors/string_reverser.rb +5 -0
  15. data/examples/deploy_pack/config/environment.rb +1 -0
  16. data/examples/{dataflow → dsl/dataflow}/fibonacci_series.rb +0 -0
  17. data/examples/dsl/dataflow/scraper_macro_flow.rb +28 -0
  18. data/examples/{dataflow → dsl/dataflow}/simple.rb +0 -0
  19. data/examples/{dataflow → dsl/dataflow}/telegram.rb +0 -0
  20. data/examples/{workflow → dsl/workflow}/cherry_pie.dot +0 -0
  21. data/examples/{workflow → dsl/workflow}/cherry_pie.md +0 -0
  22. data/examples/{workflow → dsl/workflow}/cherry_pie.png +0 -0
  23. data/examples/{workflow → dsl/workflow}/cherry_pie.rb +0 -0
  24. data/examples/empty/.gitkeep +0 -0
  25. data/examples/graph/implied_geolocation/README.md +63 -0
  26. data/examples/graph/{minimum_spanning_tree.rb → minimum_spanning_tree/airfares_graphviz.rb} +0 -0
  27. data/examples/munging/airline_flights/indexable.rb +75 -0
  28. data/examples/munging/airline_flights/indexable_spec.rb +90 -0
  29. data/examples/munging/geo/geonames_models.rb +29 -0
  30. data/examples/munging/wikipedia/dbpedia/dbpedia_common.rb +1 -0
  31. data/examples/munging/wikipedia/dbpedia/extract_links-cruft.rb +66 -0
  32. data/examples/munging/wikipedia/dbpedia/extract_links.rb +213 -146
  33. data/examples/rake_helper.rb +12 -0
  34. data/examples/ruby_project/Gemfile +7 -0
  35. data/examples/ruby_project/README.md +6 -0
  36. data/examples/ruby_project/a/b/c/.gitkeep +0 -0
  37. data/examples/serverlogs/geo_ip_mapping/munge_geolite.rb +82 -0
  38. data/examples/serverlogs/models/logline.rb +102 -0
  39. data/examples/{dataflow/parse_apache_logs.rb → serverlogs/parser/apache_parser_widget.rb} +0 -0
  40. data/examples/serverlogs/visit_paths/common.rb +4 -0
  41. data/examples/serverlogs/visit_paths/page_counts.pig +48 -0
  42. data/examples/serverlogs/visit_paths/serverlogs-01-parse-script.rb +11 -0
  43. data/examples/serverlogs/visit_paths/serverlogs-02-histograms-full.rb +31 -0
  44. data/examples/serverlogs/visit_paths/serverlogs-02-histograms-mapper.rb +12 -0
  45. data/examples/serverlogs/visit_paths/serverlogs-03-breadcrumbs-full.rb +67 -0
  46. data/examples/serverlogs/visit_paths/serverlogs-04-page_page_edges-full.rb +38 -0
  47. data/examples/text/{pig_latin.rb → pig_latin/pig_latinizer.rb} +0 -0
  48. data/examples/{dataflow/pig_latinizer.rb → text/pig_latin/pig_latinizer_widget.rb} +0 -0
  49. data/lib/hanuman/graph.rb +6 -1
  50. data/lib/wu/geo.rb +4 -0
  51. data/lib/wu/geo/geo_grids.numbers +0 -0
  52. data/lib/wu/geo/geolocated.rb +331 -0
  53. data/lib/wu/geo/quadtile.rb +69 -0
  54. data/{examples → lib/wu}/graph/union_find.rb +0 -0
  55. data/lib/wu/model/reconcilable.rb +63 -0
  56. data/{examples/munging/wikipedia/utils/munging_utils.rb → lib/wu/munging.rb} +7 -4
  57. data/lib/wu/social/models/twitter.rb +31 -0
  58. data/{examples/models/wikipedia.rb → lib/wu/wikipedia/models.rb} +0 -0
  59. data/lib/wukong.rb +9 -4
  60. data/lib/wukong/boot.rb +10 -1
  61. data/lib/wukong/driver.rb +65 -71
  62. data/lib/wukong/logger.rb +93 -0
  63. data/lib/wukong/processor.rb +38 -29
  64. data/lib/wukong/runner.rb +144 -0
  65. data/lib/wukong/server.rb +119 -0
  66. data/lib/wukong/spec_helpers.rb +1 -0
  67. data/lib/wukong/spec_helpers/integration_driver.rb +22 -9
  68. data/lib/wukong/spec_helpers/integration_driver_matchers.rb +26 -4
  69. data/lib/wukong/spec_helpers/processor_helpers.rb +4 -10
  70. data/lib/wukong/spec_helpers/shared_examples.rb +12 -13
  71. data/lib/wukong/version.rb +1 -1
  72. data/lib/wukong/widget/processors.rb +13 -0
  73. data/lib/wukong/widget/serializers.rb +55 -65
  74. data/lib/wukong/widgets.rb +0 -2
  75. data/spec/hanuman/graph_spec.rb +14 -0
  76. data/spec/spec_helper.rb +4 -30
  77. data/spec/support/{wukong_test_helpers.rb → example_test_helpers.rb} +29 -2
  78. data/spec/support/integration_helper.rb +38 -0
  79. data/spec/support/model_test_helpers.rb +115 -0
  80. data/spec/wu/geo/geolocated_spec.rb +247 -0
  81. data/spec/wu/model/reconcilable_spec.rb +152 -0
  82. data/spec/wukong/widget/processors_spec.rb +0 -1
  83. data/spec/wukong/widget/serializers_spec.rb +88 -62
  84. data/spec/wukong/wu_local_spec.rb +125 -0
  85. data/wukong.gemspec +3 -16
  86. metadata +72 -266
  87. data/examples/dataflow/apache_log_line.rb +0 -100
  88. data/examples/jabberwocky.txt +0 -36
  89. data/examples/munging/Gemfile +0 -8
  90. data/examples/munging/airline_flights/airline.rb +0 -57
  91. data/examples/munging/airline_flights/airport.rb +0 -211
  92. data/examples/munging/airline_flights/flight.rb +0 -156
  93. data/examples/munging/airline_flights/models.rb +0 -4
  94. data/examples/munging/airline_flights/parse.rb +0 -26
  95. data/examples/munging/airline_flights/route.rb +0 -35
  96. data/examples/munging/airline_flights/timezone_fixup.rb +0 -62
  97. data/examples/munging/airports/40_wbans.txt +0 -40
  98. data/examples/munging/airports/filter_weather_reports.rb +0 -37
  99. data/examples/munging/airports/join.pig +0 -31
  100. data/examples/munging/airports/to_tsv.rb +0 -33
  101. data/examples/munging/airports/usa_wbans.pig +0 -19
  102. data/examples/munging/airports/usa_wbans.txt +0 -2157
  103. data/examples/munging/airports/wbans.pig +0 -19
  104. data/examples/munging/airports/wbans.txt +0 -2310
  105. data/examples/munging/rake_helper.rb +0 -62
  106. data/examples/munging/weather/.gitignore +0 -1
  107. data/examples/munging/weather/Gemfile +0 -4
  108. data/examples/munging/weather/Rakefile +0 -28
  109. data/examples/munging/weather/extract_ish.rb +0 -13
  110. data/examples/munging/weather/models/weather.rb +0 -119
  111. data/examples/munging/weather/utils/noaa_downloader.rb +0 -46
  112. data/examples/munging/wikipedia/README.md +0 -34
  113. data/examples/munging/wikipedia/Rakefile +0 -193
  114. data/examples/munging/wikipedia/n1_subuniverse/n1_nodes.pig +0 -18
  115. data/examples/munging/wikipedia/page_metadata/extract_page_metadata.rb +0 -21
  116. data/examples/munging/wikipedia/page_metadata/extract_page_metadata.rb.old +0 -27
  117. data/examples/munging/wikipedia/pagelinks/augment_pagelinks.pig +0 -29
  118. data/examples/munging/wikipedia/pagelinks/extract_pagelinks.rb +0 -14
  119. data/examples/munging/wikipedia/pagelinks/extract_pagelinks.rb.old +0 -25
  120. data/examples/munging/wikipedia/pagelinks/undirect_pagelinks.pig +0 -29
  121. data/examples/munging/wikipedia/pageviews/augment_pageviews.pig +0 -32
  122. data/examples/munging/wikipedia/pageviews/extract_pageviews.rb +0 -85
  123. data/examples/munging/wikipedia/pig_style_guide.md +0 -25
  124. data/examples/munging/wikipedia/redirects/redirects_page_metadata.pig +0 -19
  125. data/examples/munging/wikipedia/subuniverse/sub_articles.pig +0 -23
  126. data/examples/munging/wikipedia/subuniverse/sub_page_metadata.pig +0 -24
  127. data/examples/munging/wikipedia/subuniverse/sub_pagelinks_from.pig +0 -22
  128. data/examples/munging/wikipedia/subuniverse/sub_pagelinks_into.pig +0 -22
  129. data/examples/munging/wikipedia/subuniverse/sub_pagelinks_within.pig +0 -26
  130. data/examples/munging/wikipedia/subuniverse/sub_pageviews.pig +0 -29
  131. data/examples/munging/wikipedia/subuniverse/sub_undirected_pagelinks_within.pig +0 -24
  132. data/examples/munging/wikipedia/utils/get_namespaces.rb +0 -86
  133. data/examples/munging/wikipedia/utils/namespaces.json +0 -1
  134. data/examples/string_reverser.rb +0 -26
  135. data/examples/twitter/locations.rb +0 -29
  136. data/examples/twitter/models.rb +0 -24
  137. data/examples/twitter/pt1-fiddle.pig +0 -8
  138. data/examples/twitter/pt2-simple_parse.pig +0 -31
  139. data/examples/twitter/pt2-simple_parse.rb +0 -18
  140. data/examples/twitter/pt3-join_on_zips.pig +0 -39
  141. data/examples/twitter/pt4-strong_links.rb +0 -20
  142. data/examples/twitter/pt5-lnglat_and_strong_links.pig +0 -16
  143. data/examples/twitter/states.tsv +0 -50
  144. data/examples/workflow/package_gem.rb +0 -55
  145. data/lib/wukong/widget/sink.rb +0 -16
  146. data/lib/wukong/widget/source.rb +0 -14
@@ -1,100 +0,0 @@
1
- #
2
- # Parses logs in either the [Apache Common Log Format](http://en.wikipedia.org/wiki/Common_Log_Format)
3
- # or [Apache Combined Log Format](http://httpd.apache.org/docs/2.2/logs.html#combined)
4
- #
5
- # Common: `%h %l %u %t "%r" %>s %b`
6
- # Combined: `%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-agent}i"`
7
- #
8
- class ApacheLogLine
9
- include Gorillib::Model
10
-
11
- field :client, Hostname
12
- field :rfc_1413, String
13
- field :userid, String
14
- field :log_timestamp, Time
15
- field :http_method, String
16
- field :rsrc, String
17
- field :protocol, String
18
- field :response_code, Integer
19
- field :size, Integer
20
- # field :referer, String
21
- # field :user_agent, String
22
-
23
- def page_type
24
- case
25
- when rsrc =~ /\.(css|js)$/ then :asset
26
- when rsrc =~ /\.(png|gif|ico)$/ then :image
27
- when rsrc =~ /\.(pl|s?html?|asp|jsp|cgi)$/ then :page
28
- else :other
29
- end
30
- end
31
-
32
- #
33
- # Regular expression to parse an apache log line.
34
- #
35
- # local - - [24/Oct/1994:13:43:13 -0600] "GET index.html HTTP/1.0" 200 3185
36
- # 83.240.154.3 - - [07/Jun/2008:20:37:11 +0000] "GET /faq HTTP/1.1" 200 569 "http://infochimps.org/search?query=CAC" "Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.0.16) Gecko/2009120208 Firefox/3.0.16"
37
- # whidbey.whidbey.com - - [04/Sep/1995:00:30:18 -0400] "GET /pub/sshay/images/btthumb.jpg" 200 4624
38
- # jgbustam-ppp.clark.net - - [04/Sep/1995:00:00:28 -0400] "GET /pub/jgbustam/famosos/alpha.html HTTP/1.0" 304 -
39
- #
40
- COMMON_LOG_RE = Regexp.compile(%r{\A
41
- (\S+) # client 83.240.154.3
42
- \s(\S+) # rfc_1413 -
43
- \s(\S+) # userid -
44
- \s\[([\w\:\+\-\ \/]+)\] # date part [07/Jun/2008:20:37:11 +0000]
45
- \s\"(?:(\S+) # http_method "GET
46
- \s(.+?) # rsrc /faq
47
- (?:\s(HTTP/\d+\.\d+))?|-)\" # protocol HTTP/1.1"
48
- \s(\d+|-) # response_code 200
49
- \s(\d+|-) # size 569
50
- \z
51
- }x)
52
-
53
- COMBINED_LOG_RE = Regexp.compile(%r{\A
54
- (\S+) # client 83.240.154.3
55
- \s(\S+) # rfc_1413 -
56
- \s(\S+) # userid -
57
- \s\[([\w\:\+\-\ \/]+)\] # date part [07/Jun/2008:20:37:11 +0000]
58
- \s\"(?:(\S+) # http_method "GET
59
- \s([^\"]+?) # rsrc /faq
60
- (?:\s(HTTP/\d+\.\d+))?|-)\" # protocol HTTP/1.1"
61
- \s(\d+|-) # response_code 200
62
- \s(\d+|-) # size 569
63
- (?:\s\"([^\"]*)\") # referer "http://infochimps.org/search?query=CAC"
64
- (?:\s\"([^\"]*)\") # ua "Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.0.16) Gecko/2009120208 Firefox/3.0.16"
65
- \z
66
- }x)
67
-
68
- # LOG_RE = Regexp.compile(%r{\A(\S+)\s})
69
-
70
- MONTHS = { 'Jan' => 1, 'Feb' => 2, 'Mar' => 3, 'Apr' => 4, 'May' => 5, 'Jun' => 6, 'Jul' => 7, 'Aug' => 8, 'Sep' => 9, 'Oct' => 10, 'Nov' => 11, 'Dec' => 12, }
71
-
72
- # Converts a time like `10/Apr/2007:10:58:27 +0300` to something parseable
73
- def receive_log_timestamp(raw_ts)
74
- return super(nil) if raw_ts.nil?
75
- match = %r{(\d+)/(\w+)/(\d+):(\d+):(\d+):(\d+)\s([\+\-\w]+)}.match(raw_ts)
76
- unless match then warn "Can't parse date #{raw_ts}" ; return super(nil) ; end
77
- #
78
- day, month_name, year, hour, min, sec, tz = match.captures
79
- month = MONTHS[month_name]
80
- tz.insert(3, ':') # -0600 to -06:00
81
- #
82
- # super "#{year}-#{month}-#{day} #{hour}:#{min}:#{sec} #{tz}"
83
- super Time.new(year.to_i, month, day.to_i, hour.to_i, min.to_i, sec.to_i, tz)
84
- end
85
-
86
- # @returns the log_timestamp in the common log format
87
- def unparsed_log_timestamp
88
- return if log_timestamp.blank?
89
- log_timestamp.strftime("%d/%b/%Y:%H:%M:%S %z")
90
- end
91
-
92
- # Use the regex to break line into fields
93
- # Emit each record as flat line
94
- def self.make(line)
95
- m = COMMON_LOG_RE.match(line) or return
96
- from_tuple *m.captures
97
- rescue ArgumentError => err
98
- raise unless err.message =~ /invalid byte sequence in UTF-8/
99
- end
100
- end
@@ -1,36 +0,0 @@
1
- "Jabberwocky"
2
-
3
- 'Twas brillig, and the slithy toves
4
- Did gyre and gimble in the wabe;
5
- All mimsy were the borogoves,
6
- And the mome raths outgrabe.
7
-
8
- "Beware the Jabberwock, my son!
9
- The jaws that bite, the claws that catch!
10
- Beware the Jubjub bird, and shun
11
- The frumious Bandersnatch!"
12
-
13
- He took his vorpal sword in hand:
14
- Long time the manxome foe he sought—
15
- So rested he by the Tumtum tree,
16
- And stood awhile in thought.
17
-
18
- And as in uffish thought he stood,
19
- The Jabberwock, with eyes of flame,
20
- Came whiffling through the tulgey wood,
21
- And burbled as it came!
22
-
23
- One, two! One, two! and through and through
24
- The vorpal blade went snicker-snack!
25
- He left it dead, and with its head
26
- He went galumphing back.
27
-
28
- "And hast thou slain the Jabberwock?
29
- Come to my arms, my beamish boy!
30
- O frabjous day! Callooh! Callay!"
31
- He chortled in his joy.
32
-
33
- 'Twas brillig, and the slithy toves
34
- Did gyre and gimble in the wabe;
35
- All mimsy were the borogoves,
36
- And the mome raths outgrabe.
@@ -1,8 +0,0 @@
1
- source 'http://rubygems.org'
2
-
3
- gem 'gorillib', :path => '/home/dlaw/dev/gorillib'
4
- gem 'ruby-progressbar'
5
- gem 'crack'
6
- gem 'rake'
7
- gem 'wukong', :path => '/home/dlaw/dev/wukong_og'
8
- gem 'json'
@@ -1,57 +0,0 @@
1
- class Airline
2
- include Gorillib::Model
3
- field :icao_id, String, doc: "3-letter ICAO code, if available", identifier: true, length: 2
4
- field :iata_id, String, doc: "2-letter IATA code, if available", identifier: true, length: 2
5
- field :airline_ofid, Integer, doc: "Unique OpenFlights identifier for this airline.", identifier: true
6
- field :active, :boolean, doc: 'true if the airline is or has until recently been operational, false if it is defunct. (This is only a rough indication and should not be taken as 100% accurate)'
7
- field :country, String, doc: "Country or territory where airline is incorporated"
8
- field :name, String, doc: "Airline name."
9
- field :callsign, String, doc: "Airline callsign", identifier: true
10
- field :alias, String, doc: "Alias of the airline. For example, 'All Nippon Airways' is commonly known as 'ANA'"
11
- end
12
-
13
- #
14
- # As of January 2012, the OpenFlights Airlines Database contains 5888
15
- # airlines. If you enjoy this data, please consider [visiting their page and
16
- # donating](http://openflights.org/data.html)
17
- #
18
- # > Notes: Airlines with null codes/callsigns/countries generally represent
19
- # > user-added airlines. Since the data is intended primarily for current
20
- # > flights, defunct IATA codes are generally not included. For example,
21
- # > "Sabena" is not listed with a SN IATA code, since "SN" is presently used by
22
- # > its successor Brussels Airlines.
23
- #
24
- # Sample entries
25
- #
26
- # 324,"All Nippon Airways","ANA All Nippon Airways","NH","ANA","ALL NIPPON","Japan","Y"
27
- # 412,"Aerolineas Argentinas",\N,"AR","ARG","ARGENTINA","Argentina","Y"
28
- # 413,"Arrowhead Airways",\N,"","ARH","ARROWHEAD","United States","N"
29
- #
30
- class RawOpenflightAirline
31
- include Gorillib::Model
32
- include Gorillib::Model::LoadFromCsv
33
- BLANKISH_STRINGS = ["", nil, "NULL", '\\N', "NONE", "NA", "Null", "..."]
34
-
35
- field :airline_ofid, Integer, blankish: BLANKISH_STRINGS, doc: "Unique OpenFlights identifier for this airline.", identifier: true
36
- field :name, String, blankish: BLANKISH_STRINGS, doc: "Airline name."
37
- field :alias, String, blankish: BLANKISH_STRINGS, doc: "Alias of the airline. For example, 'All Nippon Airways' is commonly known as 'ANA'"
38
- field :iata_id, String, blankish: BLANKISH_STRINGS, doc: "2-letter IATA code, if available", identifier: true, length: 2
39
- field :icao_id, String, blankish: BLANKISH_STRINGS, doc: "3-letter ICAO code, if available", identifier: true, length: 2
40
- field :callsign, String, blankish: BLANKISH_STRINGS, doc: "Airline callsign"
41
- field :country, String, blankish: BLANKISH_STRINGS, doc: "Country or territory where airline is incorporated"
42
- field :active, :boolean, blankish: BLANKISH_STRINGS, doc: 'true if the airline is or has until recently been operational, false if it is defunct. (This is only a rough indication and should not be taken as 100% accurate)'
43
-
44
- def receive_iata_id(val) super if val =~ /\A\w+\z/ ; end
45
- def receive_icao_id(val) super if val =~ /\A\w+\z/ ; end
46
- def receive_active(val)
47
- super(case val.to_s when "Y" then true when "N" then false else val ; end)
48
- end
49
-
50
- def to_airline
51
- Airline.receive(self.compact_attributes)
52
- end
53
-
54
- def self.load_airlines(filename)
55
- load_csv(filename){|raw_airline| yield(raw_airline.to_airline) }
56
- end
57
- end
@@ -1,211 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- ### @export "airport_model"
4
- class Airport
5
- include Gorillib::Model
6
-
7
- field :icao, String, doc: "4-letter ICAO code, or blank if not assigned.", length: 4, identifier: true, :blankish => ["", nil]
8
- field :iata, String, doc: "3-letter IATA code, or blank if not assigned.", length: 3, identifier: true, :blankish => ["", nil]
9
- field :faa, String, doc: "3-letter FAA code, or blank if not assigned.", length: 3, identifier: true, :blankish => ["", nil]
10
- field :utc_offset, Float, doc: "Hours offset from UTC. Fractional hours are expressed as decimals, eg. India is 5.5.", validates: { inclusion: (-12...12) }
11
- field :dst_rule, String, doc: "Daylight savings time rule. One of E (Europe), A (US/Canada), S (South America), O (Australia), Z (New Zealand), N (None) or U (Unknown). See the readme for more.", validates: { inclusion: %w[E A S O Z N U] }
12
- field :longitude, Float, doc: "Decimal degrees, usually to six significant digits. Negative is West, positive is East.", validates: { inclusion: (-180...180) }
13
- field :latitude, Float, doc: "Decimal degrees, usually to six significant digits. Negative is South, positive is North.", validates: { inclusion: (-90.0...90.0) }
14
- field :altitude, Float, doc: "Elevation in meters."
15
- field :name, String, doc: "Name of airport."
16
- field :country, String, doc: "Country or territory where airport is located.", length: 2
17
- field :state, String, doc: "State in which the airport is located", length: 2
18
- field :city, String, doc: "Main city served by airport. This is the logical city it serves; so, for example SFO gets 'San Francisco', not 'San Bruno'"
19
- field :airport_ofid, String, doc: "OpenFlights identifier for this airport.", identifier: true
20
- end
21
- ### @export "nil"
22
- class Airport
23
- EXEMPLARS = %w[
24
- ANC ATL AUS BDL BNA BOI BOS BWI CLE CLT
25
- CMH DCA DEN DFW DTW EWR FLL HNL IAD IAH
26
- IND JAX JFK LAS LAX LGA MCI MCO MDW MIA
27
- MSP MSY OAK ORD PDX PHL PHX PIT PVD RDU
28
- SAN SEA SFO SJC SJU SLC SMF STL TPA YYZ
29
- ]
30
-
31
- def utc_time_for(tm)
32
- utc_time = tm.get_utc + utc_offset
33
- utc_time += (60*60) if TimezoneFixup.dst?(tm)
34
- utc_time
35
- end
36
-
37
- BLANKISH_STRINGS = ["", nil, "NULL", '\\N', "NONE", "NA", "Null", "..."]
38
- OK_CHARS_RE = /[^a-zA-Z0-9\:\ \/\.\,\-\(\)\'ÁÂÄÅÇÉÍÎÑÓÖØÚÜÞàáâãäåæçèéêëìíîïðñóôõöøúüýĀāăĆćČčēėęěğīİıŁłńņňŌōőřŞşŠšţťūůųźŽžơț]/
39
-
40
- def lint
41
- errors = {}
42
- errors["ICAO is wrong length"] = icao if icao.present? && icao.length != 4
43
- if (icao && faa && (icao =~ /^K.../))
44
- errors["ICAO != K+FAA yet ICAO is a K..."] = [icao, faa] if (icao != "K#{faa}")
45
- end
46
- # errors["ICAO present for piddlyshit airport"] = icao if icao.present? && ((faa.to_s.length == 4) || (faa.to_s =~ /\d/))
47
- errors[:spaces] ||= []
48
- errors[:funny] ||= []
49
- attributes.each do |attr, val|
50
- next if val.blank?
51
- errors["#{attr} looks blankish"] = val if BLANKISH_STRINGS.include?(val)
52
- if (val.is_a?(String))
53
- errors[:spaces] << [attr, val] if (val.strip != val)
54
- errors[:funny] << [attr, val] if val =~ OK_CHARS_RE
55
- end
56
- end
57
- errors.compact_blank
58
- end
59
-
60
- def to_s
61
- str = "#<Airport "
62
- str << [icao, iata, faa,
63
- (latitude && "%4.1f" % latitude), (longitude && "%5.1f" % longitude), state, country,
64
- "%-30s" % name, country, city].join("\t")
65
- str << ">"
66
- end
67
-
68
- def faa_controlled?
69
- icao =~ /^(?:K|P[ABFGHJKMOPW]|T[IJ]|NS(AS|FQ|TU))/
70
- end
71
- end
72
- ### @export "airport_load"
73
- class Airport
74
- include Gorillib::Model::LoadFromTsv
75
- self.tsv_options.merge!(num_fields: 10..20)
76
- def self.load_airports(filename)
77
- load_tsv(filename){|airport| yield(airport) }
78
- end
79
-
80
- end
81
- ### @export "nil"
82
-
83
- #
84
- # As of January 2012, the OpenFlights Airports Database contains 6977 airports
85
- # [spanning the globe](http://openflights.org/demo/openflights-apdb-2048.png).
86
- # If you enjoy this data, please consider [visiting their page and
87
- # donating](http://openflights.org/data.html)
88
- #
89
- # > Note: Rules for daylight savings time change from year to year and from
90
- # > country to country. The current data is an approximation for 2009, built on
91
- # > a country level. Most airports in DST-less regions in countries that
92
- # > generally observe DST (eg. AL, HI in the USA, NT, QL in Australia, parts of
93
- # > Canada) are marked incorrectly.
94
- #
95
- # Sample entries
96
- #
97
- # 507,"Heathrow","London","United Kingdom","LHR","EGLL",51.4775,-0.461389,83,0,"E"
98
- # 26,"Kugaaruk","Pelly Bay","Canada","YBB","CYBB",68.534444,-89.808056,56,-6,"A"
99
- # 3127,"Pokhara","Pokhara","Nepal","PKR","VNPK",28.200881,83.982056,2712,5.75,"N"
100
- #
101
-
102
- ### @export "raw_openflight_airport"
103
-
104
- module RawAirport
105
- COUNTRIES = { 'Puerto Rico' => 'us', 'Canada' => 'ca', 'USA' => 'us', 'United States' => 'us',
106
- 'Northern Mariana Islands' => 'us', 'N Mariana Islands' => 'us',
107
- 'Federated States of Micronesia' => 'fm',
108
- 'Thailand' => 'th', 'Palau' => 'pw',
109
- 'American Samoa' => 'as', 'Wake Island' => 'us', 'Virgin Islands' => 'vi', 'Guam' => 'gu'
110
- }
111
- BLANKISH_STRINGS = ["", nil, "NULL", '\\N', "NONE", "NA", "Null", "..."]
112
- OK_CHARS_RE = /[^a-zA-Z0-9\:\ \/\.\,\-\(\)\'ÁÂÄÅÇÉÍÎÑÓÖØÚÜÞàáâãäåæçèéêëìíîïðñóôõöøúüýĀāăĆćČčēėęěğīİıŁłńņňŌōőřŞşŠšţťūůųźŽžơț]/
113
-
114
- def receive_city(val)
115
- super.tap{|val| if val then val.strip! ; val.gsub!(/\\+/, '') ; end }
116
- end
117
-
118
- def receive_country(val)
119
- super(COUNTRIES[val] || val)
120
- end
121
-
122
- def receive_name(val)
123
- super.tap do |val|
124
- if val
125
- val.strip!
126
- val.gsub!(/\\+/, '')
127
- val.gsub!(/\s*\[(military|private)\]/, '')
128
- val.gsub!(/\b(Int\'l|International)\b/, 'Intl')
129
- val.gsub!(/\b(Intercontinental)\b/, 'Intcntl')
130
- val.gsub!(/\b(Airpt)\b/, 'Airport')
131
- val.gsub!(/ Airport$/, '')
132
- end
133
- end
134
- end
135
- end
136
-
137
- #
138
- class RawOpenflightAirport
139
- include Gorillib::Model
140
- include Gorillib::Model::LoadFromCsv
141
- include RawAirport
142
- #
143
- field :airport_ofid, String, doc: "Unique OpenFlights identifier for this airport."
144
- field :name, String, doc: "Name of airport. May or may not contain the City name."
145
- field :city, String, blankish: BLANKISH_STRINGS, doc: "Main city served by airport. May be spelled differently from Name."
146
- field :country, String, doc: "Country or territory where airport is located."
147
- field :iata_faa, String, blankish: BLANKISH_STRINGS, doc: "3-letter FAA code, for airports located in the USA. For all other airports, 3-letter IATA code, or blank if not assigned."
148
- field :icao, String, blankish: BLANKISH_STRINGS, doc: "4-letter ICAO code; Blank if not assigned."
149
- field :latitude, Float, doc: "Decimal degrees, usually to six significant digits. Negative is South, positive is North."
150
- field :longitude, Float, doc: "Decimal degrees, usually to six significant digits. Negative is West, positive is East."
151
- field :altitude_ft, Float, blankish: ['', nil, 0, '0'], doc: "In feet."
152
- field :utc_offset, Float, doc: "Hours offset from UTC. Fractional hours are expressed as decimals, eg. India is 5.5."
153
- field :dst_rule, String, doc: "Daylight savings time rule. One of E (Europe), A (US/Canada), S (South America), O (Australia), Z (New Zealand), N (None) or U (Unknown). See the readme for more."
154
-
155
- UNRELIABLE_OPENFLIGHTS_IATA_VALUES = /^(7AK|AGA|AUQ|BDJ|BGW|BME|BPM|BXH|BZY|CAT|CEE|CEJ|CFS|CGU|CIO|CLV|CNN|DEE|DIB|DNM|DUH|DUR|FKI|GES|GSM|HKV|HOJ|HYD|IEO|IFN|IKA|IZA|JCU|JGS|KMW|KNC|LGQ|LUM|MCU|MCY|MDO|MOH|MON|MPH|MVF|NAY|NMA|NOE|NQY|OTU|OUI|PBV|PCA|PCB|PGK|PHO|PIF|PKN|PKY|PMK|PTG|PZO|QAS|QKT|QVY|RCM|RJL|RTG|SBG|SDZ|SFG|SIC|SIQ|SJI|SRI|STP|STU|SWQ|TJQ|TJS|TMC|TYA|UKC|VIY|VQS|VTS|WDH|WKM|WPR|WPU|ZQF)$/
156
-
157
- def id_is_faa?
158
- (icao =~ /^(?:K)/) || (icao.blank? && country == 'us')
159
- end
160
-
161
- def iata ; (id_is_faa? ? nil : iata_faa) unless iata_faa =~ UNRELIABLE_OPENFLIGHTS_IATA_VALUES end
162
- def faa ; (id_is_faa? ? iata_faa : nil ) end
163
- def altitude
164
- altitude_ft && (0.3048 * altitude_ft).round(1)
165
- end
166
-
167
- def receive_country(val)
168
- country = Geo::CountryNameLookup.for_alt_name(val, nil)
169
- p val unless country
170
- super(country ? country.country_id : val)
171
- end
172
-
173
- def to_airport
174
- attrs = self.compact_attributes.except(:altitude_ft)
175
- attrs[:altitude] = altitude
176
- attrs[:iata] = iata unless iata.to_s =~ UNRELIABLE_OPENFLIGHTS_IATA_VALUES
177
- attrs[:faa] = faa
178
- Airport.receive(attrs)
179
- end
180
-
181
- def self.load_airports(filename)
182
- load_csv(filename){|raw_airport| yield(raw_airport.to_airport) }
183
- end
184
- end
185
-
186
- ### @export "raw_dataexpo_airport"
187
- class RawDataexpoAirport
188
- include Gorillib::Model
189
- include Gorillib::Model::LoadFromCsv
190
- include RawAirport
191
- self.csv_options = self.csv_options.merge(pop_headers: true)
192
-
193
- field :faa, String, doc: "the international airport abbreviation code"
194
- field :name, String, doc: "Airport name"
195
- field :city, String, blankish: ["NA"], doc: "city in which the airport is located"
196
- field :state, String, blankish: ["NA"], doc: "state in which the airport is located"
197
- field :country, String, doc: "country in which airport is located"
198
- field :latitude, Float, doc: "latitude of the airport"
199
- field :longitude, Float, doc: "longitude of the airport"
200
-
201
- def to_airport
202
- attrs = self.compact_attributes
203
- attrs[:icao] = "K#{faa}" if faa =~ /[A-Z]{3}/ && (not ['PR', 'AK', 'CQ', 'HI', 'AS', 'GU', 'VI'].include?(state)) && (country == 'us')
204
- Airport.receive(attrs)
205
- end
206
-
207
- def self.load_airports(filename)
208
- load_csv(filename){|raw_airport| yield(raw_airport.to_airport) }
209
- end
210
- end
211
- ### @export "nil"
@@ -1,156 +0,0 @@
1
- # Raw data:
2
- # Year,Month,DayofMonth,DayOfWeek,DepTime,CRSDepTime,ArrTime,CRSArrTime,UniqueCarrier,FlightNum,TailNum,ActualElapsedTime,CRSElapsedTime,AirTime,ArrDelay,DepDelay,Origin,Dest,Distance,TaxiIn,TaxiOut,Can
3
- # 2007,1,1,1,1232,1225,1341,1340,WN,2891,N351,69,75,54,1,7,SMF,ONT,389,4,11,0,,0,0,0,0,0,0
4
-
5
- class RawAirlineFlight
6
- include Gorillib::Model
7
-
8
- field :date_year, Integer, position: 1, doc: "Year (1987-2008)"
9
- field :date_month, Integer, position: 2, doc: "Month (1-12)"
10
- field :date_day, Integer, position: 3, doc: "Day of month (1-31)"
11
- field :day_of_week, Integer, position: 4, doc: "Day of week -- 1 (Monday) - 7 (Sunday)"
12
- #
13
- field :act_dep_tod, String, position: 5, doc: "time of day for actual departure (local, hhmm)", blankish: [nil, '', 'NA']
14
- field :crs_dep_tod, String, position: 6, doc: "time of day for scheduled departure (local, hhmm)"
15
- field :act_arr_tod, String, position: 7, doc: "time of day for actual arrival (local, hhmm). Not adjusted for wrap-around.", blankish: [nil, '', 'NA']
16
- field :crs_arr_tod, String, position: 8, doc: "time of day for scheduled arrival (local, hhmm). Not adjusted for wrap-around."
17
- #
18
- field :unique_carrier, String, position: 9, doc: "unique carrier code", validates: { length: { in: 0..5 } }
19
- field :flight_num, Integer, position: 10, doc: "flight number"
20
- field :tail_num, String, position: 11, doc: "plane tail number", validates: { length: { in: 0..8 } }
21
- #
22
- field :act_duration, Integer, position: 12, doc: "actual flight time, in minutes", blankish: [nil, '', 'NA']
23
- field :crs_duration, Integer, position: 13, doc: "CRS flight time, in minutes"
24
- field :air_duration, Integer, position: 14, doc: "Air time, in minutes", blankish: [nil, '', 'NA']
25
- field :arr_delay, Integer, position: 15, doc: "arrival delay, in minutes", blankish: [nil, '', 'NA']
26
- field :dep_delay, Integer, position: 16, doc: "departure delay, in minutes", blankish: [nil, '', 'NA']
27
- field :from_airport, String, position: 17, doc: "Origin IATA airport code", validates: { length: { in: 0..3 } }
28
- field :into_airport, String, position: 18, doc: "Destination IATA airport code", validates: { length: { in: 0..3 } }
29
- field :distance_mi, Integer, position: 19, doc: "Flight distance, in miles"
30
- field :taxi_in_duration, Integer, position: 20, doc: "taxi in time, in minutes", blankish: [nil, '', 'NA']
31
- field :taxi_out_duration, Integer, position: 21, doc: "taxi out time in minutes", blankish: [nil, '', 'NA']
32
- #
33
- field :is_cancelled, :boolean_10, position: 22, doc: "was the flight cancelled?"
34
- field :cancellation_code, String, position: 23, doc: "Reason for cancellation (A = carrier, B = weather, C = NAS, D = security, Z = no cancellation)"
35
- field :is_diverted, :boolean_10, position: 24, doc: "Was the plane diverted?"
36
- field :carrier_delay, Integer, position: 25, doc: "in minutes"
37
- field :weather_delay, Integer, position: 26, doc: "in minutes"
38
- field :nas_delay, Integer, position: 27, doc: "in minutes"
39
- field :security_delay, Integer, position: 28, doc: "in minutes"
40
- field :late_aircraft_delay, Integer, position: 29, doc: "in minutes"
41
-
42
- def flight_date
43
- Time.new(date_year, date_month, date_day)
44
- end
45
-
46
- # uses the year / month / day, along with an "hhmm" string, to
47
- def inttime_from_hhmm(val, fencepost=nil)
48
- hour, minutes = [val.to_i / 100, val.to_i % 100]
49
- res = Time.utc(date_year, date_month, date_day, hour, minutes)
50
- # if before fencepost, we wrapped around in time
51
- res += (24 * 60 * 60) if fencepost && (res.to_i < fencepost)
52
- res.to_i
53
- end
54
-
55
- def act_dep_itime ; @act_dep_itime = inttime_from_hhmm(act_dep_tod) if act_dep_tod ; end
56
- def crs_dep_itime ; @crs_dep_itime = inttime_from_hhmm(crs_dep_tod) ; end
57
- def act_arr_itime ; @act_arr_itime = inttime_from_hhmm(act_arr_tod, act_dep_itime) if act_arr_tod ; end
58
- def crs_arr_itime ; @crs_arr_itime = inttime_from_hhmm(crs_arr_tod, crs_dep_itime) ; end
59
-
60
- def receive_tail_num(val) ; val = nil if val.to_s == "0" ; super(val) ; end
61
- def arr_delay(val) val = nil if val.to_s == 0 ; super(val) ; end
62
-
63
- def receive_cancellation_code(val) ; if val == "" then super("Z") else super(val) ; end ; end
64
-
65
- def to_airline_flight
66
- attrs = self.attributes.reject{|attr,val| [:year, :month, :day, :distance_mi].include?(attr) }
67
- attrs[:flight_datestr] = flight_date.strftime("%Y%m%d")
68
- attrs[:distance_km] = (distance_mi * 1.609_344).to_i
69
-
70
- attrs[:act_dep_tod] = "%04d" % act_dep_tod.to_i if act_dep_tod
71
- attrs[:crs_dep_tod] = "%04d" % crs_dep_tod.to_i if crs_dep_tod
72
- attrs[:act_arr_tod] = "%04d" % act_arr_tod.to_i if act_arr_tod
73
- attrs[:crs_arr_tod] = "%04d" % crs_arr_tod.to_i if crs_arr_tod
74
-
75
- attrs[:act_dep_itime] = act_dep_itime
76
- attrs[:crs_dep_itime] = crs_dep_itime
77
- attrs[:act_arr_itime] = act_arr_itime
78
- attrs[:crs_arr_itime] = crs_arr_itime
79
-
80
- AirlineFlight.receive(attrs)
81
- end
82
- end
83
-
84
- class AirlineFlight
85
- include Gorillib::Model
86
-
87
- # Identifier
88
- field :flight_datestr, String, position: 0, doc: "Date, YYYYMMDD. Use flight_date method if you want a date"
89
- field :unique_carrier, String, position: 1, doc: "Unique Carrier Code. When the same code has been used by multiple carriers, a numeric suffix is used for earlier users, for example, PA, PA(1), PA(2).", validates: { length: { in: 0..5 } }
90
- field :flight_num, Integer, position: 2, doc: "flight number"
91
- # Flight
92
- field :from_airport, String, position: 3, doc: "Origin IATA airport code", validates: { length: { in: 0..3 } }
93
- field :into_airport, String, position: 4, doc: "Destination IATA airport code", validates: { length: { in: 0..3 } }
94
- field :tail_num, String, position: 5, doc: "Plane tail number", validates: { length: { in: 0..8 } }
95
- field :distance_km, Integer, position: 6, doc: "Flight distance, in kilometers"
96
- field :day_of_week, Integer, position: 7, doc: "Day of week -- 1 (Monday) - 7 (Sunday)"
97
- # Departure and Arrival Absolute Time
98
- field :crs_dep_itime, IntTime, position: 8, doc: "scheduled departure time (utc epoch seconds)"
99
- field :crs_arr_itime, IntTime, position: 9, doc: "scheduled arrival time (utc epoch seconds)"
100
- field :act_dep_itime, IntTime, position: 10, doc: "actual departure time (utc epoch seconds)"
101
- field :act_arr_itime, IntTime, position: 11, doc: "actual arrival time (utc epoch seconds)"
102
- # Departure and Arrival Local Time of Day
103
- field :crs_dep_tod, String, position: 12, doc: "time of day for scheduled departure (local, hhmm)"
104
- field :crs_arr_tod, String, position: 13, doc: "time of day for scheduled arrival (local, hhmm). Not adjusted for wrap-around."
105
- field :act_dep_tod, String, position: 14, doc: "time of day for actual departure (local, hhmm)"
106
- field :act_arr_tod, String, position: 15, doc: "time of day for actual arrival (local, hhmm). Not adjusted for wrap-around."
107
- # Duration
108
- field :crs_duration, Integer, position: 16, doc: "CRS flight time, in minutes"
109
- field :act_duration, Integer, position: 17, doc: "Actual flight time, in minutes"
110
- field :air_duration, Integer, position: 18, doc: "Air time, in minutes"
111
- field :taxi_in_duration, Integer, position: 19, doc: "taxi in time, in minutes"
112
- field :taxi_out_duration, Integer, position: 20, doc: "taxi out time in minutes"
113
- # Delay
114
- field :is_diverted, :boolean_10, position: 21, doc: "Was the plane diverted? The actual_duration column remains NULL for all diverted flights."
115
- field :is_cancelled, :boolean_10, position: 22, doc: "was the flight cancelled?"
116
- field :cancellation_code, String, position: 23, doc: "Reason for cancellation (A = carrier, B = weather, C = NAS, D = security, Z = no cancellation)"
117
- field :dep_delay, Integer, position: 24, doc: "Difference in minutes between scheduled and actual departure time. Early departures show negative numbers. "
118
- field :arr_delay, Integer, position: 25, doc: "Difference in minutes between scheduled and actual arrival time. Early arrivals show negative numbers."
119
- field :carrier_delay, Integer, position: 26, doc: "Carrier delay, in minutes"
120
- field :weather_delay, Integer, position: 27, doc: "Weather delay, in minutes"
121
- field :nas_delay, Integer, position: 28, doc: "National Air System delay, in minutes"
122
- field :security_delay, Integer, position: 29, doc: "Security delay, in minutes"
123
- field :late_aircraft_delay, Integer, position: 30, doc: "Late Aircraft delay, in minutes"
124
-
125
- def to_tsv
126
- attrs = attributes
127
- attrs[:is_cancelled] = is_cancelled ? 1 : 0
128
- attrs[:is_diverted] = is_diverted ? 1 : 0
129
- attrs[:act_dep_itime] ||= ' '
130
- attrs[:act_arr_itime] ||= ' '
131
-
132
- # FIXME
133
- attrs[:act_duration] = ((crs_arr_itime - crs_dep_itime) / 60.0).to_i
134
- attrs[:air_duration] = attrs[:act_duration] - attrs[:crs_duration]
135
- attrs.each{|key, val| attrs[key] = val.to_s[-7..-1] if val.to_s.length > 7 } # FIXME: for testing
136
-
137
- attrs.values.join("\t")
138
- end
139
-
140
- def flight_date
141
- @flight_date ||= Gorillib::Factory::DateFactory.receive(flight_datestr)
142
- end
143
-
144
- # checks that the record is sane
145
- def lint
146
- {
147
- act_duration: (!act_arr_itime) || (act_arr_itime - act_dep_itime == act_duration * 60),
148
- crs_duration: (!crs_arr_itime) || (crs_arr_itime - crs_dep_itime == crs_duration * 60),
149
- cancelled_has_code: (is_cancelled == (cancellation_code != "Z")),
150
- cancellation_code: (%w[A B C D Z].include?(cancellation_code)),
151
- act_duration: (!act_duration) || (act_duration == (air_duration + taxi_in_duration + taxi_out_duration)),
152
- dep_delay: (!act_dep_itime) || (dep_delay == (act_dep_itime - crs_dep_itime)/60.0),
153
- arr_delay: (!act_arr_itime) || (arr_delay == (act_arr_itime - crs_arr_itime)/60.0),
154
- }
155
- end
156
- end