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
@@ -4,7 +4,11 @@ require 'gorillib/data_munging'
4
4
  require 'pry'
5
5
  require 'rake'
6
6
 
7
+ <<<<<<< HEAD
8
+ PROJ_ROOT = (ENV['PROJ_CONTENTS'] || File.expand_path('..', File.dirname(__FILE__)))
9
+ =======
7
10
  BOOK_ROOT = (ENV['BOOK_CONTENTS'] || File.expand_path('..', File.dirname(__FILE__)))
11
+ >>>>>>> produce_consume
8
12
 
9
13
  Settings.define :mini, type: :boolean, default: false, description: "use sample data or full data?"
10
14
  Settings.resolve!
@@ -14,11 +18,19 @@ Settings[:mini_slug] = Settings.mini ? "-sample" : ""
14
18
  task :force
15
19
 
16
20
  Pathname.register_paths(
21
+ <<<<<<< HEAD
22
+ proj_root: PROJ_ROOT,
23
+ root: [:proj_root],
24
+ code: [:proj_root, 'code'],
25
+ data: [:proj_root, 'data'],
26
+ work: [:proj_root, 'tmp'],
27
+ =======
17
28
  book_root: BOOK_ROOT,
18
29
  root: [:book_root],
19
30
  code: [:book_root, 'code'],
20
31
  data: [:book_root, 'data'],
21
32
  work: [:book_root, 'tmp'],
33
+ >>>>>>> produce_consume
22
34
  )
23
35
 
24
36
  require 'rake/name_space'
@@ -0,0 +1,7 @@
1
+ # This is a minimal Gemfile that lets Wukong boot up (but not work).
2
+ gem 'configliere'
3
+ gem 'gorillib'
4
+ gem 'multi_json'
5
+ gem 'vayacondios-client'
6
+ gem 'log4r'
7
+ gem 'eventmachine'
@@ -0,0 +1,6 @@
1
+ This directory contains the skeleton of a Ruby project. It's used to
2
+ test the behavior of Wukong's command line tools within such a
3
+ context.
4
+
5
+ The Gemfile here contains the minimal subset of gems that let Wukong
6
+ boot.
File without changes
@@ -0,0 +1,82 @@
1
+ #!/usr/bin/env ruby
2
+ require 'wukong'
3
+ require 'gorillib/type/ip_address'
4
+ require 'gorillib/pathname'
5
+ require 'gorillib/model/serialization'
6
+ require 'gorillib/model/positional_fields'
7
+
8
+ Pathname.register_paths(
9
+ maxmind: '../../../data/server_logs/maxmind',
10
+ geolite_ip_blocks: [:maxmind, 'GeoLiteCity_20121002/GeoLiteCity-Blocks.csv'],
11
+ geolite_locations: [:maxmind, 'GeoLiteCity_20121002/GeoLiteCity-Location.csv'],
12
+ )
13
+
14
+ class IpGeo
15
+ include Gorillib::Model
16
+ include Gorillib::Model::PositionalFields
17
+ #
18
+ field :location_id, Integer
19
+ field :longitude, Float # note: longitude, latitude
20
+ field :latitude, Float
21
+ #
22
+ field :country_id, String, blankish: ["", nil, '""']
23
+ field :admin1_id, String, blankish: ["", nil, '""']
24
+ field :city, String, blankish: ["", nil, '""']
25
+ #
26
+ field :postal_code, String, blankish: ["", nil, '""']
27
+ field :metro_code, String, blankish: ["", nil, '""']
28
+ field :area_code, String, blankish: ["", nil, '""']
29
+ end
30
+
31
+ lines = 0
32
+ LOCATIONS = {}
33
+ locations_file = Pathname.of(:geolite_locations).open(encoding: "ISO-8859-1")
34
+ locations_file.readline; locations_file.readline
35
+ locations_file.
36
+ # readlines[0..1000].
37
+ each do |line|
38
+ location_id, country_id, admin1_id, city, postal_code, latitude, longitude, metro_code, area_code = line.chomp.gsub(/"/, '').split(',', 9)
39
+ LOCATIONS[location_id.to_i] = [ location_id.to_i, longitude.to_f, latitude.to_f, country_id, admin1_id, city, postal_code, metro_code, area_code ]
40
+ end
41
+
42
+ module IpCensus
43
+ class IpBlocksMapper < Wukong::Streamer::RecordStreamer
44
+
45
+ def initialize(*)
46
+ super
47
+ @last = IpNumeric.from_dotted('0.116.0.0')
48
+ end
49
+
50
+ def recordize line
51
+ beg_ip, end_ip, location_id = line.gsub(/\"/, '').split(",", 3)
52
+ [IpNumeric.new(Integer(beg_ip)), IpNumeric.new(Integer(end_ip)), Integer(location_id)]
53
+ end
54
+
55
+ # Use the regex to break line into fields
56
+ # Emit each record as flat line
57
+ def process(beg_ip, end_ip, location_id, &block)
58
+ emit_range(IpRange.new(@last, beg_ip), 0, &block) if @last != beg_ip.to_int
59
+ emit_range IpRange.new(beg_ip, end_ip), location_id, &block
60
+ @last = end_ip.to_i + 1
61
+ end
62
+
63
+ def emit_range(rng, location_id)
64
+ location = LOCATIONS[location_id]
65
+ warn "No location #{location_id.inspect}" unless location || (location_id == 0)
66
+ rng.bitness_blocks(16).each do |blk_min, blk_max|
67
+ # raise [blk_min, blk_max, blk_min.to_hex[0..-5], blk_max.to_hex[0..-5]].inspect if blk_min.to_hex[0..-3] != blk_max.to_hex[0..-3]
68
+ yield [
69
+ blk_min.to_hex[0..3],
70
+ blk_min.to_hex[4..-1],
71
+ blk_max.to_hex[4..-1],
72
+ blk_min.to_i,
73
+ blk_max.to_i,
74
+ location,
75
+ ].flatten
76
+ end
77
+ end
78
+
79
+ end
80
+ end
81
+
82
+ Wukong::Script.new(IpCensus::IpBlocksMapper, nil).run
@@ -0,0 +1,102 @@
1
+ # # Parse logs to TSV
2
+ #
3
+ # bzcat data/star_wars_kid.log.bz2 | head -n 100200 | tail -n 100 > data/swk-100.log
4
+ # cat data/swk-100.tsv
5
+ # cat data/swk-100.log | ./apache_log_parser.rb --map | wu-lign | cutc 150
6
+ # cat data/swk-100.log | ./apache_log_parser.rb --map > data/swk-100.tsv
7
+ # ./histograms.rb --run data/star_wars_kid.log data/star_wars_kid.tsv
8
+
9
+ # # Histograms
10
+ #
11
+ # cat data/swk-100.tsv | ./histograms.rb --map | wu-lign
12
+ # cat data/swk-hist-map.tsv | ./histograms.rb --reduce
13
+ # ./histograms.rb --run data/star_wars_kid.tsv data/star_wars_kid-pages_by_hour.tsv
14
+
15
+ # # Sessionize
16
+ #
17
+ # cat data/swk-100.tsv | ./histograms.rb --map | wu-lign
18
+ # cat data/swk-hist-map.tsv | ./histograms.rb --reduce
19
+ # ./histograms.rb --run data/star_wars_kid.tsv data/star_wars_kid-pages_by_hour.tsv
20
+
21
+ class Logline
22
+ include Gorillib::Model
23
+ include Gorillib::Model::PositionalFields
24
+
25
+ field :ip_address, String
26
+ field :rfc_1413, String
27
+ field :userid, String
28
+ #
29
+ field :requested_at, Time
30
+ field :http_method, String
31
+ field :path, String
32
+ field :protocol, String
33
+ field :response_code, Integer
34
+ field :bytesize, Integer, blankish: ['', nil, '-']
35
+ field :referer, String
36
+ field :user_agent, String
37
+
38
+ def day_hr
39
+ [visit_time.year, visit_time.month, visit_time.day, visit_time.hour].join
40
+ end
41
+
42
+ def receive_requested_at(val)
43
+ if %r{(\d+)/(\w+)/(\d+):(\d+):(\d+):(\d+)\s([\+\-]\d\d)(\d\d)} === val
44
+ day, mo, yr, hour, min, sec, tz1, tz2 = [$1, $2, $3, $4, $5, $6, $7, $8]
45
+ val = Time.new(yr.to_i, MONTHS[mo], day.to_i,
46
+ hour.to_i, min.to_i, sec.to_i, "#{tz1}:#{tz2}")
47
+ end
48
+ super(val)
49
+ end
50
+
51
+ # Use the regex to break line into fields
52
+ # Emit each record as flat line
53
+ def self.parse(line)
54
+ mm = LOG_RE.match(line.chomp) or return BadRecord.new('no match', line)
55
+ new(* mm.captures)
56
+ end
57
+
58
+ def page_type
59
+ file_ext = path[FILE_EXT_RE]
60
+ case file_ext
61
+ when nil then 'page'
62
+ when '.wmv' then 'video'
63
+ when '.html','.shtml' then 'page'
64
+ when '.css', '.js' then 'asset'
65
+ when '.png', '.gif', '.ico' then 'image'
66
+ when '.wmv' then 'image'
67
+ when '.pl','.asp','.jsp','.cgi' then 'page'
68
+ else 'other'
69
+ end
70
+ end
71
+
72
+ # Matches a file extension
73
+ FILE_EXT_RE = %r{\.[^/]+\z}
74
+
75
+ # Map of abbreviated months to date number.
76
+ 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, }
77
+
78
+ #
79
+ # Regular expression to parse an apache log line.
80
+ #
81
+ # 83.240.154.3 - - [07/Jun/2008:20:37:11 +0000] "GET /faq HTTP/1.1" 200 569 "http://infochimps.org/search?query=your_mom" "Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.0.16) Gecko/2009120208 Firefox/3.0.16"
82
+ #
83
+ LOG_RE = Regexp.compile(%r{\A
84
+ (\S+) # ip_address 83.240.154.3
85
+ \s(\S+) # rfc_1413 -
86
+ \s(\S+) # userid -
87
+
88
+ \s\[(\d+/\w+/\d+ # date part [07/Jun/2008
89
+ :\d+:\d+:\d+ # time part :20:37:11
90
+ \s[\+\-]\S*)\] # timezone +0000]
91
+
92
+ \s\"(?:(\S+) # http_method "GET
93
+ \s(\S+) # path /faq
94
+ \s(HTTP/[\d\.]+)|-)\" # protocol HTTP/1.1"
95
+
96
+ \s(\d+) # response_code 200
97
+ \s(\d+|-) # bytesize 569
98
+ \s\"([^\"]*)\" # referer "http://infochimps.org/search?query=CAC"
99
+ \s\"([^\"]*)\" # user_agent "Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.0.16) Gecko/2009120208 Firefox/3.0.16"
100
+ \z}x)
101
+
102
+ end
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../../lib', File.dirname(__FILE__))
2
+ require 'configliere'
3
+ require 'wukong/script'
4
+ require_relative '../models/logline'
@@ -0,0 +1,48 @@
1
+
2
+
3
+ LOAD common_pages FROM 'data/common_pages' AS (ip:chararray, from_path:chararray, into_path:chararray);
4
+
5
+ --
6
+ -- Build adjacency list <A pr B,C,D> from edges (<A B>, <A C>, <A D>)
7
+ --
8
+
9
+ adj_list_j = GROUP common_pages BY from_path;
10
+ adj_list = FOREACH adj_list_j GENERATE
11
+ group AS from_path,
12
+ 1.0F AS pagerank:float,
13
+ common_pages.(dest) AS into_paths
14
+ ;
15
+ STORE adj_list INTO 'data/pagerank/pr_iter_00';
16
+
17
+
18
+ --
19
+ -- Iterate pagerank <A pr_00 B,C,D> to become <A pr_01 B,C,D>
20
+ --
21
+
22
+ -- find partial share: A.rank / A.into_paths.length
23
+ -- dispatch <into_path partial_share> to each page
24
+ sent_shares = FOREACH adj_list GENERATE
25
+ FLATTEN(into_paths) AS path,
26
+ (float)(pagerank / (float)SIZE(into_paths)) AS share:float;
27
+
28
+ -- dispatch <from_path into_paths> to yourself, so you have the links still around
29
+ sent_edges = FOREACH adj_list GENERATE
30
+ from_path AS path, into_paths;
31
+
32
+ -- assemble all the received shared, and the self-sent edge list;
33
+ rcvd_shares = COGROUP sent_edges BY path INNER, sent_shares BY path PARALLEL $PARALLEL;
34
+
35
+ -- calculate the new rank, and emit a record that looked just like the input.
36
+ next_iter = FOREACH rcvd_shares {
37
+ raw_rank = (float)SUM(sent_shares.share);
38
+ -- treat the case that a node has no in links
39
+ damped_rank = ((raw_rank IS NOT NULL AND raw_rank > 1.0e-12f) ? raw_rank*0.85f + 0.15f : 0.0f);
40
+ GENERATE
41
+ group AS from_path,
42
+ damped_rank AS rank,
43
+ FLATTEN(sent_edges.into_paths)
44
+ ; };
45
+
46
+ STORE next_iter INTO 'data/pagerank/pr_iter_01';
47
+
48
+
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative './common'
3
+
4
+ class ApacheLogParser < Wukong::Streamer::LineStreamer
5
+ # create a Logline object from each record and serialize it flat to disk
6
+ def process line
7
+ yield Logline.parse(line)
8
+ end
9
+ end
10
+
11
+ Wukong.run( ApacheLogParser )
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative './common'
3
+
4
+ # cat data/swk-100.tsv | ./histograms.rb --map | sort > data/swk-hist-map.tsv
5
+ # cat data/swk-hist-map.tsv | ./histograms.rb --reduce > data/swk-hist.tsv
6
+
7
+ class HistogramsMapper < Wukong::Streamer::ModelStreamer
8
+ self.model_klass = Logline
9
+ def process visit
10
+ yield [visit.path, visit.day_hr]
11
+ end
12
+ end
13
+
14
+ class HistogramsReducer < Wukong::Streamer::Reducer
15
+ def get_key(path, day_hr)
16
+ [path, day_hr]
17
+ end
18
+ def start!(*args)
19
+ @count = 0
20
+ super
21
+ end
22
+ def accumulate(path, day_hr)
23
+ @count += 1
24
+ end
25
+ def finalize
26
+ yield [key, @count]
27
+ end
28
+ end
29
+
30
+ # Wukong.run( HistogramsMapper )
31
+ Wukong.run( HistogramsMapper, HistogramsReducer, :sort_fields => 3 )
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative './common'
3
+
4
+ class HistogramsMapper < Wukong::Streamer::ModelStreamer
5
+ self.model_klass = Logline
6
+ def process visit
7
+ yield [visit.path, visit.day_hr]
8
+ end
9
+ end
10
+
11
+ # Wukong.run( HistogramsMapper )
12
+ Wukong.run( HistogramsMapper )
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env ruby
2
+ require 'configliere'
3
+ Settings.define :page_types, type: Array, default: ['page', 'video'], description: "Acceptable page types"
4
+ require_relative './common'
5
+
6
+ #
7
+ # Group all visitors, and then troll through all the pages they've visited
8
+ # breaking each into distinct visits (where more than an [hour|day|whatever]
9
+ # separate subsequent pageviews
10
+ #
11
+
12
+ #
13
+ # Mapper parses log files and created a visitor_id from the visitor's user_id,
14
+ # cookie or ip. It emits
15
+ #
16
+ # <visitor_id> <datetime> <url_path>
17
+ #
18
+ # where the partition key is visitor_id, and we sort by visitor_id and datetime.
19
+ #
20
+ class BreadcrumbsMapper < Wukong::Streamer::ModelStreamer
21
+ self.model_klass = Logline
22
+ def process visit, *args
23
+ # return unless Settings.page_types.include?(visit.page_type)
24
+ yield [visit.ip, visit.day_hr, visit.requested_at.to_i, visit.path]
25
+ end
26
+ end
27
+
28
+ #
29
+ # Reducer:
30
+ #
31
+ # The reducer is given all page requests for the given visitor id, sorted by
32
+ # timestamp.
33
+ #
34
+ # It group by visits (pageviews separated by more than DISTINCT_VISIT_TIMEGAP)
35
+ # and emits
36
+ #
37
+ # trail <visitor_id> <n_pages_in_visit> <duration> <timestamp> < page1,page2,... >
38
+ #
39
+ # where the last is a comma-separated string of URL encoded paths (any internal comma is converted to %2C).
40
+ #
41
+ # You can instead emit
42
+ #
43
+ # page_trails <page1> <n_pages_in_visit> <duration> <timestamp> < page1,page2,... >
44
+ # page_trails <page2> <n_pages_in_visit> <duration> <timestamp> < page1,page2,... >
45
+ # ....
46
+ # page_trails <pagen> <n_pages_in_visit> <duration> <timestamp> < page1,page2,... >
47
+ #
48
+ # to discover all trails passing through a given page.
49
+ class BreadcrumbsReducer < Wukong::Streamer::Reducer
50
+ def get_key ip, day_hr, itime, path, *args
51
+ [ip]
52
+ end
53
+ def start!(*args)
54
+ @path_times = []
55
+ super
56
+ end
57
+ def accumulate ip, day_hr, itime, path, *args
58
+ @path_times << "(#{itime},#{path})"
59
+ end
60
+ def finalize
61
+ path_times_str = ("{" << @path_times.join(",") << "}")
62
+ yield [key, path_times_str]
63
+ end
64
+ end
65
+
66
+
67
+ Wukong.run( BreadcrumbsMapper, BreadcrumbsReducer, :sort_fields => 2 )
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative './common'
3
+
4
+ class BreadcrumbsMapper < Wukong::Streamer::ModelStreamer
5
+ self.model_klass = Logline
6
+ def process visit, *args
7
+ # return unless Settings.page_types.include?(visit.page_type)
8
+ yield [visit.ip, visit.requested_at.to_i, visit.path]
9
+ end
10
+ end
11
+
12
+ class BreadcrumbEdgesReducer < Wukong::Streamer::Reducer
13
+ def get_key(ip, itime, path)
14
+ [ip]
15
+ end
16
+ def start!(*args)
17
+ @paths = Set.new
18
+ super
19
+ end
20
+ def accumulate ip, itime, path
21
+ @paths << path
22
+ end
23
+
24
+ # for each pair of paths, emit the edge in both directions
25
+ def finalize
26
+ @paths = @paths.to_a
27
+ while @paths.present?
28
+ from = @paths.shift
29
+ @paths.each do |into|
30
+ yield [key, from, into]
31
+ yield [key, into, from]
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+
38
+ Wukong.run( BreadcrumbsMapper, BreadcrumbEdgesReducer, :sort_fields => 2 )