octocore-cassandra 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGES.md +12 -0
  3. data/CONTRIBUTING +0 -0
  4. data/Gemfile +10 -0
  5. data/LICENSE +202 -0
  6. data/MAINTAINERS +0 -0
  7. data/NOTICE +8 -0
  8. data/README.md +69 -0
  9. data/Rakefile +142 -0
  10. data/bin/fakestream-cassandra +258 -0
  11. data/bin/octocore-admin-cassandra +54 -0
  12. data/lib/octocore-cassandra.rb +152 -0
  13. data/lib/octocore-cassandra/baseline.rb +131 -0
  14. data/lib/octocore-cassandra/callbacks.rb +117 -0
  15. data/lib/octocore-cassandra/config.rb +39 -0
  16. data/lib/octocore-cassandra/config/config.yml +1 -0
  17. data/lib/octocore-cassandra/config/search/index/user.yml +42 -0
  18. data/lib/octocore-cassandra/counter.rb +265 -0
  19. data/lib/octocore-cassandra/counter/helpers.rb +168 -0
  20. data/lib/octocore-cassandra/email.rb +63 -0
  21. data/lib/octocore-cassandra/featureflag.rb +79 -0
  22. data/lib/octocore-cassandra/helpers.rb +6 -0
  23. data/lib/octocore-cassandra/helpers/api_consumer_helper.rb +51 -0
  24. data/lib/octocore-cassandra/helpers/api_helper.rb +65 -0
  25. data/lib/octocore-cassandra/helpers/api_logger.rb +14 -0
  26. data/lib/octocore-cassandra/helpers/client_helper.rb +104 -0
  27. data/lib/octocore-cassandra/helpers/kong_helper.rb +164 -0
  28. data/lib/octocore-cassandra/helpers/sinatra_helper.rb +22 -0
  29. data/lib/octocore-cassandra/kafka_bridge.rb +60 -0
  30. data/lib/octocore-cassandra/kldivergence.rb +14 -0
  31. data/lib/octocore-cassandra/mailer.rb +1 -0
  32. data/lib/octocore-cassandra/mailer/subscriber_mailer.rb +30 -0
  33. data/lib/octocore-cassandra/message_parser.rb +114 -0
  34. data/lib/octocore-cassandra/models.rb +274 -0
  35. data/lib/octocore-cassandra/models/contactus.rb +42 -0
  36. data/lib/octocore-cassandra/models/enterprise.rb +76 -0
  37. data/lib/octocore-cassandra/models/enterprise/adapter_details.rb +18 -0
  38. data/lib/octocore-cassandra/models/enterprise/api_event.rb +14 -0
  39. data/lib/octocore-cassandra/models/enterprise/api_hit.rb +20 -0
  40. data/lib/octocore-cassandra/models/enterprise/api_key.rb +11 -0
  41. data/lib/octocore-cassandra/models/enterprise/api_track.rb +13 -0
  42. data/lib/octocore-cassandra/models/enterprise/app_init.rb +13 -0
  43. data/lib/octocore-cassandra/models/enterprise/app_login.rb +12 -0
  44. data/lib/octocore-cassandra/models/enterprise/app_logout.rb +12 -0
  45. data/lib/octocore-cassandra/models/enterprise/authorization.rb +67 -0
  46. data/lib/octocore-cassandra/models/enterprise/category.rb +14 -0
  47. data/lib/octocore-cassandra/models/enterprise/category_baseline.rb +19 -0
  48. data/lib/octocore-cassandra/models/enterprise/category_hit.rb +26 -0
  49. data/lib/octocore-cassandra/models/enterprise/category_trend.rb +19 -0
  50. data/lib/octocore-cassandra/models/enterprise/conversions.rb +69 -0
  51. data/lib/octocore-cassandra/models/enterprise/ctr.rb +54 -0
  52. data/lib/octocore-cassandra/models/enterprise/dimension_choice.rb +21 -0
  53. data/lib/octocore-cassandra/models/enterprise/engagement_time.rb +43 -0
  54. data/lib/octocore-cassandra/models/enterprise/funnel_data.rb +20 -0
  55. data/lib/octocore-cassandra/models/enterprise/funnel_tracker.rb +19 -0
  56. data/lib/octocore-cassandra/models/enterprise/funnels.rb +129 -0
  57. data/lib/octocore-cassandra/models/enterprise/gcm.rb +21 -0
  58. data/lib/octocore-cassandra/models/enterprise/newsfeed_hit.rb +52 -0
  59. data/lib/octocore-cassandra/models/enterprise/notification_hit.rb +42 -0
  60. data/lib/octocore-cassandra/models/enterprise/page.rb +15 -0
  61. data/lib/octocore-cassandra/models/enterprise/page_view.rb +14 -0
  62. data/lib/octocore-cassandra/models/enterprise/pageload_time.rb +43 -0
  63. data/lib/octocore-cassandra/models/enterprise/product.rb +22 -0
  64. data/lib/octocore-cassandra/models/enterprise/product_baseline.rb +20 -0
  65. data/lib/octocore-cassandra/models/enterprise/product_hit.rb +26 -0
  66. data/lib/octocore-cassandra/models/enterprise/product_page_view.rb +13 -0
  67. data/lib/octocore-cassandra/models/enterprise/product_trend.rb +18 -0
  68. data/lib/octocore-cassandra/models/enterprise/push_key.rb +15 -0
  69. data/lib/octocore-cassandra/models/enterprise/rules.rb +45 -0
  70. data/lib/octocore-cassandra/models/enterprise/segment.rb +65 -0
  71. data/lib/octocore-cassandra/models/enterprise/segment_data.rb +22 -0
  72. data/lib/octocore-cassandra/models/enterprise/tag.rb +14 -0
  73. data/lib/octocore-cassandra/models/enterprise/tag_baseline.rb +19 -0
  74. data/lib/octocore-cassandra/models/enterprise/tag_hit.rb +26 -0
  75. data/lib/octocore-cassandra/models/enterprise/tag_trend.rb +19 -0
  76. data/lib/octocore-cassandra/models/enterprise/template.rb +18 -0
  77. data/lib/octocore-cassandra/models/plans.rb +17 -0
  78. data/lib/octocore-cassandra/models/subscribe.rb +13 -0
  79. data/lib/octocore-cassandra/models/user.rb +15 -0
  80. data/lib/octocore-cassandra/models/user/push_token.rb +15 -0
  81. data/lib/octocore-cassandra/models/user/user_browser_details.rb +16 -0
  82. data/lib/octocore-cassandra/models/user/user_location_history.rb +15 -0
  83. data/lib/octocore-cassandra/models/user/user_persona.rb +101 -0
  84. data/lib/octocore-cassandra/models/user/user_phone_details.rb +17 -0
  85. data/lib/octocore-cassandra/models/user/user_profile.rb +20 -0
  86. data/lib/octocore-cassandra/models/user/user_timeline.rb +111 -0
  87. data/lib/octocore-cassandra/record.rb +20 -0
  88. data/lib/octocore-cassandra/schedeuleable.rb +20 -0
  89. data/lib/octocore-cassandra/scheduler.rb +72 -0
  90. data/lib/octocore-cassandra/search.rb +5 -0
  91. data/lib/octocore-cassandra/search/client.rb +33 -0
  92. data/lib/octocore-cassandra/search/indexer.rb +0 -0
  93. data/lib/octocore-cassandra/search/searchable.rb +18 -0
  94. data/lib/octocore-cassandra/search/setup.rb +71 -0
  95. data/lib/octocore-cassandra/segment.rb +287 -0
  96. data/lib/octocore-cassandra/stats.rb +33 -0
  97. data/lib/octocore-cassandra/trendable.rb +88 -0
  98. data/lib/octocore-cassandra/trends.rb +158 -0
  99. data/lib/octocore-cassandra/utils.rb +90 -0
  100. data/lib/octocore-cassandra/version.rb +4 -0
  101. data/spec/lib/stats_spec.rb +20 -0
  102. data/spec/spec_helper.rb +103 -0
  103. metadata +490 -0
@@ -0,0 +1,168 @@
1
+ module Octo
2
+ module Counter
3
+ module Helper
4
+
5
+
6
+ #The prefix to use when converting a type constant into an
7
+ #aggregator method name
8
+ METHOD_PREFIX = 'aggregate'
9
+
10
+
11
+ # Defined the method names for the type counters.
12
+ def type_counters_method_names(type = nil)
13
+ if type.nil?
14
+ get_typecounters.map do |typ|
15
+ [METHOD_PREFIX, typ.to_s.downcase].join('_')
16
+ end
17
+ else
18
+ [METHOD_PREFIX, type.to_s.downcase].join('_')
19
+ end
20
+ end
21
+
22
+ # Get all the type counters i.e. TYPE_MINUTE_30 etc from
23
+ # Counter class.
24
+ # @return [Array] Array of all the constants that define a counter type
25
+ def get_typecounters
26
+ max = max_type
27
+ Counter.constants.select do |x|
28
+ if x.to_s.start_with?('TYPE')
29
+ Counter.const_get(x) <= max
30
+ else
31
+ false
32
+ end
33
+ end
34
+ end
35
+
36
+ # Define the max granularity that should exist
37
+ def max_type(type = nil)
38
+ if @max_type
39
+ @max_type
40
+ else
41
+ if type
42
+ @max_type = type
43
+ else
44
+ @max_type = 9
45
+ end
46
+ end
47
+ @max_type
48
+ end
49
+
50
+ # Coverts the method name to the constant type
51
+ # @param [String] method_name The method name to convert into constant
52
+ # @return [Symbol] The constant
53
+ def method_names_type_counter(method_name)
54
+ prefix, *counterType = method_name.to_s.split('_')
55
+ if prefix == METHOD_PREFIX
56
+ cnst = counterType.join('_').upcase.to_sym
57
+ string_to_const_val(cnst)
58
+ end
59
+ end
60
+
61
+ # Converts a string (which may represent a counter constant) into its
62
+ # corresponding constant
63
+ # @param [String] cnst The string which may represent a constant
64
+ # @return [Fixnum] The constant value; iff the string represent a constant.
65
+ # Nil otherwise
66
+ def string_to_const_val(cnst)
67
+ index = Counter.constants.index(cnst)
68
+ if index
69
+ Counter.const_get(cnst)
70
+ end
71
+ end
72
+
73
+ # Generates a fromtype for a totype. This defines the relation
74
+ # of aggregation needed for the counters.
75
+ def get_fromtype_for_totype(totype)
76
+ case totype
77
+ when TYPE_MINUTE_30
78
+ TYPE_MINUTE
79
+ when TYPE_HOUR
80
+ TYPE_MINUTE_30
81
+ when TYPE_HOUR_3
82
+ TYPE_HOUR
83
+ when TYPE_HOUR_6
84
+ TYPE_HOUR_3
85
+ when TYPE_HOUR_12
86
+ TYPE_HOUR_6
87
+ when TYPE_DAY
88
+ TYPE_HOUR_6
89
+ when TYPE_DAY_3
90
+ TYPE_DAY
91
+ when TYPE_DAY_6
92
+ TYPE_DAY_3
93
+ when TYPE_WEEK
94
+ TYPE_DAY_3
95
+ else
96
+ TYPE_WEEK
97
+ end
98
+ end
99
+
100
+ # Gets the duration for a particular counter type. This helps in
101
+ # aggregation.
102
+ # @param [Fixnum] type The counter type
103
+ # @param [Time] ts The time at wich duration needs to be passed
104
+ # @return [Time] The time or time range for the given specs
105
+ def get_duration_for_counter_type(type, ts=Time.now.ceil)
106
+ start_time, step = case type
107
+ when TYPE_MINUTE
108
+ [2.minute.ago, 1.minute]
109
+ when TYPE_MINUTE_30
110
+ [30.minute.ago, 1.minute]
111
+ when TYPE_HOUR
112
+ [1.hour.ago, 30.minute]
113
+ when TYPE_HOUR_3
114
+ [3.hour.ago, 1.hour]
115
+ when TYPE_HOUR_6
116
+ [6.hour.ago, 3.hour]
117
+ when TYPE_HOUR_12
118
+ [12.hour.ago, 6.hour]
119
+ when TYPE_DAY
120
+ [1.day.ago, 6.hour]
121
+ when TYPE_DAY_3
122
+ [3.day.ago, 1.day]
123
+ when TYPE_DAY_6
124
+ [6.day.ago, 3.day]
125
+ when TYPE_WEEK
126
+ [1.week.ago, 1.week]
127
+ end
128
+ start_time.ceil.to(ts, step)
129
+ end
130
+
131
+ # Returns the mapping for counters as text
132
+ # @return [Hash]
133
+ def self.counter_text
134
+ {
135
+ TYPE_MINUTE => 'Near Real Time',
136
+ TYPE_MINUTE_30 => '30 Minute',
137
+ TYPE_HOUR => 'Hourly',
138
+ TYPE_HOUR_3 => '3 Hourly',
139
+ TYPE_HOUR_6 => '6 Hourly',
140
+ TYPE_HOUR_12 => '12 Hourly',
141
+ TYPE_DAY => 'Daily',
142
+ TYPE_DAY_3 => '3 Days',
143
+ TYPE_DAY_6 => '6 Days',
144
+ TYPE_WEEK => 'Weekly'
145
+ }
146
+ end
147
+
148
+ # Generate aggregator methods for a class. You can pass
149
+ # your own block to generator for all custom needs.
150
+ # Check out the implementation at Octo::Counter#countables
151
+ # or Octo::Trends#trendable
152
+ # @param [Block] block The block to be evaluated while executing
153
+ # the method
154
+ def generate_aggregators(&block)
155
+ @stored_block = block
156
+ type_counters_method_names.each do |method_name|
157
+ singleton_class.module_eval(<<-RUBY, __FILE__, __LINE__+1)
158
+ def #{ method_name } (ts=Time.now.floor)
159
+ bl = self.instance_variable_get(:@stored_block)
160
+ instance_exec(ts, __method__, &bl) if bl
161
+ end
162
+ RUBY
163
+ end
164
+ end
165
+
166
+ end
167
+ end
168
+ end
@@ -0,0 +1,63 @@
1
+ require 'mandrill'
2
+ require 'resque'
3
+ require 'resque-scheduler'
4
+
5
+ module Octo
6
+
7
+ # Octo Email Sender
8
+ module Email
9
+
10
+ # Send Emails using mandrill api
11
+ # @param [Text] email Email Address of the receiver
12
+ # @param [Text] subject Subject of Email
13
+ # @param [Hash] opt Hash contain other message details
14
+ def send(email, subject, opts = {})
15
+ if email.nil? or subject.nil?
16
+ raise ArgumentError, 'Email Address or Subject is missing'
17
+ else
18
+ message = {
19
+ from_name: Octo.get_config(:email_sender).fetch(:name),
20
+ from_email: Octo.get_config(:email_sender).fetch(:email),
21
+
22
+ subject: subject,
23
+
24
+ text: opts.fetch('text', nil),
25
+ html: opts.fetch('html', nil),
26
+
27
+ to: [{
28
+ email: email,
29
+ name: opts.fetch('name', nil)
30
+ }]
31
+ }
32
+ # Pass the message to resque only when mandrill key is present
33
+ _mandrill_config = ENV['MANDRILL_API_KEY'] || Octo.get_config(:mandrill_api_key)
34
+ if _mandrill_config and !_mandrill_config.empty?
35
+ enqueue_msg(message)
36
+ end
37
+ end
38
+ end
39
+
40
+ # Adding Email details to Resque Queue
41
+ # @param [Hash] message Hash contain message details
42
+ def enqueue_msg(message)
43
+ Resque.enqueue(Octo::EmailSender, message)
44
+ end
45
+
46
+ end
47
+
48
+ # Class to perform Resque operations for sending email
49
+ class EmailSender
50
+
51
+ @queue = :email_sender
52
+
53
+ # Resque Perform method
54
+ # @param [Hash] message The details of email
55
+ def self.perform(message)
56
+ _mandrill_config = ENV['MANDRILL_API_KEY'] || Octo.get_config(:mandrill_api_key)
57
+ m = Mandrill::API.new _mandrill_config
58
+ m.messages.send message
59
+ end
60
+
61
+ end
62
+ end
63
+
@@ -0,0 +1,79 @@
1
+ require 'set'
2
+
3
+ module Octo
4
+
5
+ # The FeatureFlag Module
6
+ #
7
+ # This module flags some functionalities as OFF or DISABLED state.
8
+ module FeatureFlag
9
+
10
+ def self.included(base)
11
+ base.extend ClassMethods
12
+ end
13
+
14
+ module ClassMethods
15
+
16
+ # Set the featureflag for a module or class to the state. If the
17
+ # featureflag is set to true, it means that the feature is disabled. If
18
+ # the featureflag is set to false, it means that the feature is enabled.
19
+ #
20
+ # It also defined a `is_flagged?` method on the module which returns
21
+ # the state of featureflag
22
+ #
23
+ # @param [Module] klass The class or module to be feature flagged
24
+ # @param [Boolean] state The boolean state to set for the class
25
+ #
26
+ def featureflag(klass, state)
27
+ if state
28
+ unless flags.include?klass
29
+ flags << klass
30
+ klass.instance_eval do
31
+ def is_flagged?
32
+ true
33
+ end
34
+ end
35
+ end
36
+ else
37
+ if flags.include?klass
38
+ flags.delete(klass)
39
+ klass.instance_eval do
40
+ def is_flagged?
41
+ false
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ # Get the list of all flags
49
+ # @return [Set] A set of all flags
50
+ #
51
+ def flags
52
+ @flags ||= Set.new([])
53
+ end
54
+
55
+ # Helper method to find if a module is feature flagged or not
56
+ # @param [Module] feature The module to be tested
57
+ # @return [Boolean] Boolean value specifying if the feature is flagged
58
+ # or not
59
+ #
60
+ def is_flagged?(feature)
61
+ flags.include?feature
62
+ end
63
+
64
+ # Returns if the flag is not set
65
+ # @param [Module] feature The module to be tested
66
+ # @return [Boolean] Boolean value specifying the status
67
+ #
68
+ def is_not_flagged?(feature)
69
+ !is_flagged?feature
70
+ end
71
+ end
72
+
73
+ end
74
+
75
+ #include FeatureFlag
76
+ end
77
+
78
+ Octo.send(:include, Octo::FeatureFlag)
79
+
@@ -0,0 +1,6 @@
1
+ require 'octocore-cassandra/helpers/api_helper'
2
+ require 'octocore-cassandra/helpers/api_logger'
3
+ require 'octocore-cassandra/helpers/api_consumer_helper'
4
+ require 'octocore-cassandra/helpers/client_helper'
5
+ require 'octocore-cassandra/helpers/kong_helper'
6
+ require 'octocore-cassandra/helpers/sinatra_helper'
@@ -0,0 +1,51 @@
1
+ require 'redis'
2
+ require 'set'
3
+
4
+ module Octo
5
+ module Helpers
6
+
7
+ # Helper Module for Octo Consumer
8
+ module ApiConsumerHelper
9
+
10
+ # Get all the valid events
11
+ # @return [Set<Symbol>] Valid events globally
12
+ def valid_events
13
+ Set.new(Octo.get_config(:allowed_events))
14
+ end
15
+
16
+ # Get the API events. These are the ones that the client is billed for
17
+ # This should eventually be placed under kong helpers when that is
18
+ # ready.
19
+ # @return [Set<Symbol>] Set of api_events
20
+ # def api_events
21
+ # Set.new(%w(app.init app.login app.logout page.view productpage.view update.profile))
22
+ # end
23
+
24
+ # Handles Kafka messages and perform required operations
25
+ # @param [Hash] msg Hash Message
26
+ def handle(msg)
27
+ msg_obj = Octo::Message::Message.new(msg)
28
+ msg = msg_obj.to_h
29
+ begin
30
+ eventName = msg[:event_name]
31
+ if (valid_events.include?eventName)
32
+ call_hooks(eventName, msg)
33
+ end
34
+ rescue Exception => e
35
+ puts e.message
36
+ puts e.backtrace.inspect
37
+ end
38
+ end
39
+
40
+ # Set Octo callbacks
41
+ # @param [String] event Event name
42
+ # @param [Hash] *args Points to the messsage hash
43
+ def call_hooks(event, *args)
44
+ hook = [:after, event.gsub('.', '_')].join('_').to_sym
45
+ Octo::Callbacks.run_hook(hook, *args)
46
+ end
47
+
48
+ end
49
+ end
50
+ end
51
+
@@ -0,0 +1,65 @@
1
+ require 'securerandom'
2
+ require 'octocore-cassandra'
3
+ require 'octocore-cassandra/stats'
4
+
5
+ module Octo
6
+ module Helpers
7
+ module ApiHelper
8
+
9
+ include Octo::Stats
10
+
11
+ KONG_HEADERS = %w(HTTP_X_CONSUMER_ID HTTP_X_CONSUMER_CUSTOM_ID HTTP_X_CONSUMER_USERNAME)
12
+
13
+ # Get enterprise details from the HTTP headers that Kong sets
14
+ # @return [Hash] The hash of enterprise details
15
+ def enterprise_details
16
+ kong_config = Octo.get_config :kong
17
+ if kong_config[:enabled]
18
+ KONG_HEADERS.inject({}) do |r, header|
19
+ key = header.gsub('HTTP_X_CONSUMER_', '').downcase
20
+ r[key] = request.env.fetch(header, nil)
21
+ r
22
+ end
23
+ else
24
+ apikey = request.env.fetch('HTTP_APIKEY')
25
+ {custom_id: settings.redis.get(apikey)}
26
+ end
27
+ end
28
+
29
+ # Gets the POSTed parameters from rack env
30
+ # @return [Hash] A hash of POSTed parameters
31
+ def post_params
32
+ instrument(:json_parse) do
33
+ JSON.parse(request.env['rack.input'].read)
34
+ end
35
+ end
36
+
37
+ # Generate a UUID for each response
38
+ # @return [String] UUID
39
+ def uuid
40
+ SecureRandom.uuid
41
+ end
42
+
43
+ # Process an incoming request
44
+ # @param [String] event_name The name of the event
45
+ # @return [JSON] The json return value after processing
46
+ def process_request(event_name)
47
+ postparams = post_params
48
+ opts = {
49
+ event_name: event_name,
50
+ enterprise: enterprise_details,
51
+ uuid: uuid
52
+ }
53
+ postparams.merge!(opts)
54
+ kafka_config = Octo.get_config :kafka
55
+ if kafka_config[:enabled]
56
+ settings.kafka_bridge.push(postparams)
57
+ else
58
+ settings.queue.push(JSON.dump(postparams))
59
+ end
60
+ { eventId: opts[:uuid] }.to_json
61
+ end
62
+
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,14 @@
1
+ require 'logger'
2
+
3
+ module Octo
4
+ class ApiLogger
5
+ def self.logger
6
+ if @_logger.nil?
7
+ @_logger = Logger.new STDOUT
8
+ @_logger.level = Logger::INFO
9
+ @_logger.datetime_format = '%a %d-%m-%Y %H%M '
10
+ end
11
+ @_logger
12
+ end
13
+ end
14
+ end