octocore-mongo 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGES.md +12 -0
  3. data/CONTRIBUTING +0 -0
  4. data/Gemfile +11 -0
  5. data/Gemfile.lock +150 -0
  6. data/LICENSE +202 -0
  7. data/MAINTAINERS +0 -0
  8. data/NOTICE +8 -0
  9. data/README.md +69 -0
  10. data/Rakefile +51 -0
  11. data/bin/fakestream-mongo +258 -0
  12. data/bin/octocore-admin-mongo +54 -0
  13. data/lib/octocore-mongo.rb +157 -0
  14. data/lib/octocore-mongo/baseline.rb +131 -0
  15. data/lib/octocore-mongo/callbacks.rb +117 -0
  16. data/lib/octocore-mongo/config.rb +39 -0
  17. data/lib/octocore-mongo/config/config.yml +1 -0
  18. data/lib/octocore-mongo/config/search/index/user.yml +42 -0
  19. data/lib/octocore-mongo/counter.rb +265 -0
  20. data/lib/octocore-mongo/counter/helpers.rb +168 -0
  21. data/lib/octocore-mongo/email.rb +63 -0
  22. data/lib/octocore-mongo/featureflag.rb +79 -0
  23. data/lib/octocore-mongo/helpers.rb +6 -0
  24. data/lib/octocore-mongo/helpers/api_consumer_helper.rb +51 -0
  25. data/lib/octocore-mongo/helpers/api_helper.rb +65 -0
  26. data/lib/octocore-mongo/helpers/api_logger.rb +14 -0
  27. data/lib/octocore-mongo/helpers/client_helper.rb +104 -0
  28. data/lib/octocore-mongo/helpers/kong_helper.rb +164 -0
  29. data/lib/octocore-mongo/helpers/sinatra_helper.rb +22 -0
  30. data/lib/octocore-mongo/kafka_bridge.rb +60 -0
  31. data/lib/octocore-mongo/kldivergence.rb +14 -0
  32. data/lib/octocore-mongo/mailer.rb +1 -0
  33. data/lib/octocore-mongo/mailer/subscriber_mailer.rb +32 -0
  34. data/lib/octocore-mongo/message_parser.rb +114 -0
  35. data/lib/octocore-mongo/models.rb +275 -0
  36. data/lib/octocore-mongo/models/contactus.rb +42 -0
  37. data/lib/octocore-mongo/models/enterprise.rb +75 -0
  38. data/lib/octocore-mongo/models/enterprise/adapter_details.rb +18 -0
  39. data/lib/octocore-mongo/models/enterprise/api_event.rb +14 -0
  40. data/lib/octocore-mongo/models/enterprise/api_hit.rb +20 -0
  41. data/lib/octocore-mongo/models/enterprise/api_key.rb +11 -0
  42. data/lib/octocore-mongo/models/enterprise/api_track.rb +13 -0
  43. data/lib/octocore-mongo/models/enterprise/app_init.rb +13 -0
  44. data/lib/octocore-mongo/models/enterprise/app_login.rb +12 -0
  45. data/lib/octocore-mongo/models/enterprise/app_logout.rb +12 -0
  46. data/lib/octocore-mongo/models/enterprise/authorization.rb +67 -0
  47. data/lib/octocore-mongo/models/enterprise/category.rb +14 -0
  48. data/lib/octocore-mongo/models/enterprise/category_baseline.rb +19 -0
  49. data/lib/octocore-mongo/models/enterprise/category_hit.rb +26 -0
  50. data/lib/octocore-mongo/models/enterprise/category_trend.rb +19 -0
  51. data/lib/octocore-mongo/models/enterprise/conversions.rb +69 -0
  52. data/lib/octocore-mongo/models/enterprise/ctr.rb +54 -0
  53. data/lib/octocore-mongo/models/enterprise/dimension_choice.rb +21 -0
  54. data/lib/octocore-mongo/models/enterprise/engagement_time.rb +43 -0
  55. data/lib/octocore-mongo/models/enterprise/funnel_data.rb +20 -0
  56. data/lib/octocore-mongo/models/enterprise/funnel_tracker.rb +19 -0
  57. data/lib/octocore-mongo/models/enterprise/funnels.rb +129 -0
  58. data/lib/octocore-mongo/models/enterprise/gcm.rb +21 -0
  59. data/lib/octocore-mongo/models/enterprise/newsfeed_hit.rb +52 -0
  60. data/lib/octocore-mongo/models/enterprise/notification_hit.rb +42 -0
  61. data/lib/octocore-mongo/models/enterprise/page.rb +15 -0
  62. data/lib/octocore-mongo/models/enterprise/page_view.rb +14 -0
  63. data/lib/octocore-mongo/models/enterprise/pageload_time.rb +43 -0
  64. data/lib/octocore-mongo/models/enterprise/product.rb +22 -0
  65. data/lib/octocore-mongo/models/enterprise/product_baseline.rb +20 -0
  66. data/lib/octocore-mongo/models/enterprise/product_hit.rb +26 -0
  67. data/lib/octocore-mongo/models/enterprise/product_page_view.rb +13 -0
  68. data/lib/octocore-mongo/models/enterprise/product_trend.rb +18 -0
  69. data/lib/octocore-mongo/models/enterprise/push_key.rb +15 -0
  70. data/lib/octocore-mongo/models/enterprise/rules.rb +45 -0
  71. data/lib/octocore-mongo/models/enterprise/segment.rb +65 -0
  72. data/lib/octocore-mongo/models/enterprise/segment_data.rb +22 -0
  73. data/lib/octocore-mongo/models/enterprise/tag.rb +14 -0
  74. data/lib/octocore-mongo/models/enterprise/tag_baseline.rb +19 -0
  75. data/lib/octocore-mongo/models/enterprise/tag_hit.rb +26 -0
  76. data/lib/octocore-mongo/models/enterprise/tag_trend.rb +19 -0
  77. data/lib/octocore-mongo/models/enterprise/template.rb +18 -0
  78. data/lib/octocore-mongo/models/plans.rb +17 -0
  79. data/lib/octocore-mongo/models/subscribe.rb +13 -0
  80. data/lib/octocore-mongo/models/user.rb +13 -0
  81. data/lib/octocore-mongo/models/user/push_token.rb +15 -0
  82. data/lib/octocore-mongo/models/user/user_browser_details.rb +16 -0
  83. data/lib/octocore-mongo/models/user/user_location_history.rb +15 -0
  84. data/lib/octocore-mongo/models/user/user_persona.rb +101 -0
  85. data/lib/octocore-mongo/models/user/user_phone_details.rb +17 -0
  86. data/lib/octocore-mongo/models/user/user_profile.rb +20 -0
  87. data/lib/octocore-mongo/models/user/user_timeline.rb +111 -0
  88. data/lib/octocore-mongo/record.rb +20 -0
  89. data/lib/octocore-mongo/schedeuleable.rb +20 -0
  90. data/lib/octocore-mongo/scheduler.rb +72 -0
  91. data/lib/octocore-mongo/search.rb +5 -0
  92. data/lib/octocore-mongo/search/client.rb +33 -0
  93. data/lib/octocore-mongo/search/indexer.rb +0 -0
  94. data/lib/octocore-mongo/search/searchable.rb +18 -0
  95. data/lib/octocore-mongo/search/setup.rb +71 -0
  96. data/lib/octocore-mongo/segment.rb +287 -0
  97. data/lib/octocore-mongo/stats.rb +33 -0
  98. data/lib/octocore-mongo/trendable.rb +88 -0
  99. data/lib/octocore-mongo/trends.rb +158 -0
  100. data/lib/octocore-mongo/utils.rb +90 -0
  101. data/lib/octocore-mongo/version.rb +4 -0
  102. data/octocore-mongo.gemspec +50 -0
  103. data/spec/lib/stats_spec.rb +20 -0
  104. data/spec/spec_helper.rb +103 -0
  105. metadata +545 -0
@@ -0,0 +1,129 @@
1
+ require 'mongo_mapper'
2
+ require 'octocore-mongo/record'
3
+ require 'set'
4
+
5
+ module Octo
6
+
7
+ # Stores the funnel for the enterprise
8
+ class Funnel
9
+
10
+ Octo.featureflag self, true
11
+
12
+ include MongoMapper::Document
13
+ include Octo::Record
14
+
15
+ belongs_to :enterprise, class_name: 'Octo::Enterprise'
16
+
17
+ key :name_slug, String
18
+ key :funnel, Array
19
+
20
+ key :name, String
21
+ key :active, Boolean
22
+
23
+ timestamps!
24
+
25
+ before_create :create_name_slug, :activate_funnel
26
+
27
+ after_create :populate_with_fake_data
28
+
29
+ # Creates name slug
30
+ def create_name_slug
31
+ self.name_slug = self.name.to_slug
32
+ end
33
+
34
+ # Activates a funnel
35
+ def activate_funnel
36
+ self.active = true
37
+ end
38
+
39
+ # Generates a new funnel from the pages provided
40
+ # @param [Array] pages The pages array. This array could contain instances
41
+ # of either String, Octo::Product or Octo::Page.
42
+ # If string, it will be assumed that these are routeurls for pages or
43
+ # products.
44
+ # If the class is explicitly specified, it will be used.
45
+ # @param [Hash] opts The options for creating funnel
46
+ # @option opts [String] :name The name of the funnel
47
+ # @option opts [String] :enterprise_id The enterprise ID for whom funnel is
48
+ # being created
49
+ # @return [Octo::Funnel] The funnel created
50
+ def self.from_pages(pages, opts = {})
51
+ funnel_length = pages.count
52
+ return nil if funnel_length.zero?
53
+
54
+ funnel = Array.new
55
+ enterprise_id = opts.fetch(:enterprise_id, nil)
56
+
57
+ # Check if they are Octo::Product or Octo::Page instantces and handle
58
+ if ::Set.new([Octo::Product, Octo::Page]).include?(pages[0].class)
59
+ funnel = pages.collect { |p| p.routeurl }
60
+ enterprise_id = pages[0].enterprise_id
61
+ elsif pages[0].class == String
62
+ funnel = pages
63
+ end
64
+
65
+ # Create a new funnel
66
+ self.new(
67
+ enterprise_id: enterprise_id,
68
+ name: opts.fetch(:name),
69
+ funnel: funnel
70
+ ).save!
71
+ end
72
+
73
+ # Populates a newly created funnel with some fake data
74
+ # @param [Fixnum] days The number of days for which data to be faked
75
+ def populate_with_fake_data(interval_days = 7)
76
+ if self.enterprise.fakedata?
77
+ today = Time.now.beginning_of_day
78
+ (today - interval_days.days).to(today, 24.hour).each do |ts|
79
+ Octo::FunnelData.new(
80
+ enterprise_id: self.enterprise_id,
81
+ funnel_slug: self.name_slug,
82
+ ts: ts,
83
+ value: fake_data(self.funnel.count)
84
+ ).save!
85
+ end
86
+ end
87
+ end
88
+
89
+ # Returns data for a funnel
90
+ # @return [Octo::FunnelData] The Octo funnel data
91
+ def data(ts = Time.now.floor)
92
+ args = {
93
+ enterprise_id: self.enterprise._id,
94
+ funnel_slug: self.name_slug,
95
+ ts: ts
96
+ }
97
+ res = Octo::FunnelData.where(args)
98
+ if res.count > 0
99
+ res.first
100
+ elsif self.enterprise.fakedata?
101
+ args.merge!({ value: fake_data(self.funnel.count) })
102
+ Octo::FunnelData.new(args).save!
103
+ end
104
+ end
105
+
106
+ private
107
+
108
+ # Generates fake data for funnel
109
+ # @param [Fixnum] n The length of funnel
110
+ # @return [Array] An array containing the funnel value
111
+ def fake_data(n)
112
+ fun = Array.new(n)
113
+ max_dropoff = 100/n
114
+ n.times do |i|
115
+ if i == 0
116
+ fun[i] = 100.0
117
+ else
118
+ fun[i] = fun[i-1] - rand(1..max_dropoff)
119
+ if fun[i] < 0
120
+ fun[i] = rand(0...fun[i].abs)
121
+ end
122
+ end
123
+ end
124
+ fun
125
+ end
126
+
127
+ end
128
+ end
129
+
@@ -0,0 +1,21 @@
1
+ require 'mongo_mapper'
2
+
3
+ module Octo
4
+
5
+ # Storage for Notifications
6
+ class GcmNotification
7
+ include MongoMapper::Document
8
+
9
+ belongs_to :enterprise, class_name: 'Octo::Enterprise'
10
+
11
+ key :gcmid, String
12
+ key :userid, Integer
13
+
14
+ key :score, Float
15
+ key :ack, Boolean
16
+ key :sent_at, Time
17
+ key :recieved_at, Time
18
+
19
+ end
20
+ end
21
+
@@ -0,0 +1,52 @@
1
+ require 'mongo_mapper'
2
+
3
+ require 'octocore-mongo/counter'
4
+ require 'octocore-mongo/schedeuleable'
5
+
6
+ module Octo
7
+
8
+ class NewsfeedHit
9
+ include MongoMapper::Document
10
+ extend Octo::Counter
11
+
12
+ extend Octo::Scheduleable
13
+
14
+ COUNTERS = 20
15
+
16
+ # Specify that we do not want any granularity more than
17
+ # TYPE_DAY
18
+ max_type Octo::Counter::TYPE_DAY
19
+
20
+ belongs_to :enterprise, class_name: 'Octo::Enterprise'
21
+
22
+ countables
23
+
24
+ def self.fakedata(args)
25
+ res = self.where(args)
26
+
27
+ enterprise = Octo::Enterprise.find_by_id(args[:enterprise_id])
28
+ if res.count == 0 and enterprise.fakedata?
29
+ unless args.has_key?(:uid)
30
+ args[:uid] = 'newsfeed'
31
+ end
32
+ res = []
33
+ ts = args.fetch(:ts, 7.days.ago..Time.now)
34
+ if ts.class == Range
35
+ start_ts = ts.begin.beginning_of_day
36
+ end_ts = ts.end.end_of_day
37
+ start_ts.to(end_ts, 1.day).each do |_ts|
38
+ _args = args.merge({ ts: _ts, count: rand(400..700) })
39
+ res << self.new(_args).save!
40
+ end
41
+ elsif ts.class == Time
42
+ _args = args.merge({ count: rand(400..800) })
43
+ res << self.new(_args).save!
44
+ end
45
+ end
46
+ res
47
+ end
48
+
49
+
50
+ end
51
+ end
52
+
@@ -0,0 +1,42 @@
1
+ require 'mongo_mapper'
2
+
3
+ require 'octocore-mongo/counter'
4
+ require 'octocore-mongo/schedeuleable'
5
+
6
+ module Octo
7
+
8
+ # Counters for notifications sent
9
+ class NotificationHit
10
+
11
+ include MongoMapper::Document
12
+ extend Octo::Counter
13
+ extend Octo::Scheduleable
14
+
15
+ max_type Octo::Counter::TYPE_DAY
16
+
17
+ belongs_to :enterprise, class_name: 'Octo::Enterprise'
18
+
19
+ countables
20
+
21
+ def self.time_slots
22
+ Array.new(6) { |i| "s_#{ i }" }
23
+ end
24
+
25
+ def self.fakedata(args)
26
+ opts = {
27
+ bod: false,
28
+ step: 1.day
29
+ }
30
+ self.time_slots.concat([:ios, :android]).inject([]) do |res, uid|
31
+ values = {
32
+ count: rand(500..900),
33
+ }
34
+ _args = args.merge(uid: uid)
35
+ res << self.fake_data_with(_args, values, opts)
36
+ res.flatten
37
+ end
38
+ end
39
+
40
+ end
41
+ end
42
+
@@ -0,0 +1,15 @@
1
+ require 'mongo_mapper'
2
+
3
+ module Octo
4
+ class Page
5
+ include MongoMapper::Document
6
+
7
+ belongs_to :enterprise, class_name: 'Octo::Enterprise'
8
+
9
+ key :routeurl, String
10
+
11
+ key :categories, Array
12
+ key :tags, Array
13
+ end
14
+ end
15
+
@@ -0,0 +1,14 @@
1
+ require 'mongo_mapper'
2
+
3
+ module Octo
4
+ class PageView
5
+ include MongoMapper::Document
6
+
7
+ belongs_to :enterprise, class_name: 'Octo::Enterprise'
8
+
9
+ key :userid, Integer
10
+ key :created_at, Time, order: :desc
11
+
12
+ key :routeurl, String
13
+ end
14
+ end
@@ -0,0 +1,43 @@
1
+ require 'mongo_mapper'
2
+
3
+ module Octo
4
+
5
+ class PageloadTime
6
+ include MongoMapper::Document
7
+
8
+ # Types of pageloads
9
+ NEWSFEED = 0
10
+
11
+ belongs_to :enterprise, class_name: 'Octo::Enterprise'
12
+ key :pageload_type, Integer
13
+ key :counter_type, Integer
14
+ key :ts, Time
15
+
16
+ key :time, Integer
17
+
18
+ timestamps!
19
+
20
+ def self.fakedata(args)
21
+ res = self.where(args)
22
+ enterprise = Octo::Enterprise.find_by_id(args[:enterprise_id])
23
+ if res.count == 0 and enterprise.fakedata?
24
+ res = []
25
+ ts = args.fetch(ts, 7.days.ago..Time.now.floor)
26
+ if ts.class == Range
27
+ start_time = ts.begin.beginning_of_day
28
+ end_time = ts.end.end_of_day
29
+ start_time.to(end_time, 1.day).each do |_ts|
30
+ _args = args.merge({ ts: _ts, time: (rand(5.0..7.0)*60).to_i })
31
+ res << self.new(_args).save!
32
+ end
33
+ elsif ts.class == Time
34
+ _args = args.merge({ time: (rand(5.0..7.0)).to_i })
35
+ res << self.new(args).save!
36
+ end
37
+ end
38
+ res
39
+ end
40
+
41
+ end
42
+ end
43
+
@@ -0,0 +1,22 @@
1
+ require 'mongo_mapper'
2
+ require 'octocore-mongo/record'
3
+
4
+ module Octo
5
+ class Product
6
+ include MongoMapper::Document
7
+ include Octo::Record
8
+
9
+ belongs_to :enterprise, class_name: 'Octo::Enterprise'
10
+
11
+ key :_id, Integer
12
+
13
+ key :price, Float
14
+ key :name, String
15
+ key :routeurl, String
16
+
17
+ key :categories, Array
18
+ key :tags, Array
19
+
20
+ timestamps!
21
+ end
22
+ end
@@ -0,0 +1,20 @@
1
+ require 'mongo_mapper'
2
+
3
+ require 'octocore-mongo/baseline'
4
+ require 'octocore-mongo/schedeuleable'
5
+
6
+ module Octo
7
+ class ProductBaseline
8
+ include MongoMapper::Document
9
+ extend Octo::Baseline
10
+ extend Octo::Scheduleable
11
+
12
+ belongs_to :enterprise, class_name: 'Octo::Enterprise'
13
+
14
+ baselineable
15
+
16
+ # Specify the type of counter
17
+ baseline_for 'Octo::ProductHit'
18
+
19
+ end
20
+ end
@@ -0,0 +1,26 @@
1
+ require 'mongo_mapper'
2
+
3
+ require 'octocore-mongo/counter'
4
+ require 'octocore-mongo/trendable'
5
+ require 'octocore-mongo/schedeuleable'
6
+
7
+ module Octo
8
+
9
+ class ProductHit
10
+ include MongoMapper::Document
11
+ extend Octo::Counter
12
+ extend Octo::Trendable
13
+ extend Octo::Scheduleable
14
+
15
+ COUNTERS = 30
16
+
17
+ belongs_to :enterprise, class_name: 'Octo::Enterprise'
18
+
19
+ countables
20
+ trendables
21
+
22
+ baseline 'Octo::ProductBaseline'
23
+ trends_class 'Octo::ProductTrend'
24
+
25
+ end
26
+ end
@@ -0,0 +1,13 @@
1
+ require 'mongo_mapper'
2
+
3
+ module Octo
4
+ class ProductPageView
5
+ include MongoMapper::Document
6
+ belongs_to :enterprise, class_name: 'Octo::Enterprise'
7
+
8
+ key :userid, Integer
9
+ key :created_at, Time, order: :desc
10
+
11
+ key :product_id, Integer
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ require 'mongo_mapper'
2
+ require 'octocore-mongo/trends'
3
+
4
+ module Octo
5
+
6
+ # Class for storing trending product
7
+ class ProductTrend
8
+ include MongoMapper::Document
9
+ extend Octo::Trends
10
+
11
+ belongs_to :enterprise, class_name: 'Octo::Enterprise'
12
+
13
+ trendable
14
+
15
+ trend_for 'Octo::ProductHit'
16
+ trend_class 'Octo::Product'
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ require 'mongo_mapper'
2
+
3
+ module Octo
4
+ class PushKey
5
+ include MongoMapper::Document
6
+
7
+ belongs_to :enterprise, class_name: 'Octo::Enterprise'
8
+
9
+ key :push_type, Integer
10
+ key :key, String
11
+
12
+ timestamps!
13
+ end
14
+ end
15
+