request_response_stats 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1d93ef2e0bb7c1e31551811923a3b6393721c46f
4
+ data.tar.gz: a411d96d3df46bbd8832332d23a8dc469371d65b
5
+ SHA512:
6
+ metadata.gz: d407ec5d98944bdc216d91bbb34e1bdf8af27eb56ead08a11f1e45253bb59415bc1b08f3e943c8ca5fff04eead3671dfa9ea968c81ba3bc60aa191508648cde4
7
+ data.tar.gz: 6723271a4816804f5d4d660a5492b57f3f1bde43e53835f15ef33623adc79af9f9b0292002836591dbdadb85b2176545bdcbfd22f47d0eb946851bca56edb109
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
14
+ **/.DS_Store
15
+ **/*.swp
16
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at munishapc@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in request_response_stats.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Munish Goyal
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # RequestResponseStats
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/request_response_stats`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'request_response_stats'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install request_response_stats
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/request_response_stats. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the RequestResponseStats project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/request_response_stats/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "request_response_stats"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,65 @@
1
+ # File: app/controllers/req_res_stat_controller.rb
2
+
3
+ # TODO: This controller is yet to be tested
4
+ class ReqResStatController < ApplicationController
5
+
6
+ # params[:start_time] format: "2009-06-24 12:39:54 +09:00"
7
+ # params[:end_time] format: "2009-06-24 12:39:54 +09:00"
8
+ # params[:stat_key] popular choices: "request_count", "error_count", "min_time", "max_time", "avg_time"
9
+ def get_stats
10
+ stat_key = params[:stat_key].to_sym
11
+ start_time = parse_date_time_zone params[:start_time]
12
+ end_time = parse_date_time_zone params[:end_time]
13
+ granularity_in_hours = params[:granularity_in_hours].to_i.hours if params[:granularity_in_hours].present?
14
+
15
+ min_values = ReqResStat.get_min(stat_key, start_time, end_time, granularity_in_hours)
16
+ max_values = ReqResStat.get_max(stat_key, start_time, end_time, granularity_in_hours)
17
+ avg_values = ReqResStat.get_avg(stat_key, start_time, end_time, granularity_in_hours)
18
+
19
+ return_value = {
20
+ start_time: start_time,
21
+ end_time: end_time,
22
+ granularity_in_hours: granularity_in_hours,
23
+ min_values: min_values,
24
+ max_values: max_values,
25
+ avg_values: avg_values,
26
+ }
27
+
28
+ render json: return_value
29
+
30
+ rescue Exception => ex
31
+ error_message = [ex.message, ex.backtrace.join("\n")].join("\n")
32
+ render json: {error_message: error_message}
33
+ end
34
+
35
+ def get_details
36
+ stat_key = params[:stat_key].to_sym
37
+ start_time = parse_date_time_zone params[:start_time]
38
+ end_time = parse_date_time_zone params[:end_time]
39
+ granularity_in_hours = params[:granularity_in_hours].to_i.hours if params[:granularity_in_hours].present?
40
+
41
+ details = ReqResStat.get_details(stat_key, start_time, end_time, nil, granularity_in_hours)
42
+
43
+ return_value = {
44
+ details: details
45
+ }
46
+
47
+ render json: return_value
48
+
49
+ rescue Exception => ex
50
+ error_message = [ex.message, ex.backtrace.join("\n")].join("\n")
51
+ render json: {error_message: error_message}
52
+ end
53
+
54
+ private
55
+
56
+ # time format: "2009-06-24 12:39:54 +09:00"
57
+ def parse_date_time_zone(date_time_zone)
58
+ date, time, zone = date_time_zone.split(" ")
59
+ date = date.split("-")
60
+ time = time.split(":")
61
+
62
+ Time.new(*date, *time, zone)
63
+ end
64
+ end
65
+
@@ -0,0 +1,35 @@
1
+ # File: lib/request_response_stats/controller_concern.rb
2
+
3
+ require_relative 'request_response'
4
+
5
+ module RequestResponseStats
6
+ module ControllerConcern
7
+ def self.included(base)
8
+ include RequestResponseStats
9
+
10
+ base.class_eval do
11
+ around_action :log_request_response_stats
12
+ # before_action :log_request_response_stats_before
13
+ # after_action :log_request_response_stats_end
14
+
15
+ def log_request_response_stats
16
+ if defined?(RR_INBOUND_STATS) && RR_INBOUND_STATS
17
+ begin
18
+ rrs = RequestResponse.new(request, response, {redis_connection: $redis, gather_stats: true, mongoid_doc_model: ReqResStat})
19
+ rrs.capture_request_response_cycle_start_info
20
+ yield
21
+ rrs.capture_request_response_cycle_end_info
22
+ rescue Exception => ex
23
+ rrs.try(:capture_request_response_cycle_error_info)
24
+ raise ex
25
+ end
26
+ else
27
+ yield
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,76 @@
1
+ # File: lib/request_response_stats/custom_client.rb
2
+
3
+ require_relative 'request_response'
4
+ require_relative 'dummy_request'
5
+ require_relative 'dummy_response'
6
+
7
+ module RequestResponseStats
8
+ module CustomClient
9
+ RENAME_NAMESPACE = "original_"
10
+ RENAMED_METHODS = [:get, :post, :patch, :put, :delete, :head, :client]
11
+ MAX_URL_LENGTH = 250 # Note that value of -1 will include whole of the url
12
+
13
+ # By default args is assumed to be an array with first element as uri
14
+ # If args is an hash contained within one-element array, then you can specify the key
15
+ # that is to be used to fetch the uri
16
+ def custom_uri_key
17
+ false
18
+ end
19
+
20
+ def method_missing(name, *args, &block)
21
+ if RENAMED_METHODS.include?(name)
22
+ resp = log_request_response_stats(name, args) { self.public_send("#{RENAME_NAMESPACE}#{name.to_s}".to_sym, *args, &block) }
23
+ return resp
24
+ else
25
+ super
26
+ end
27
+ end
28
+ # module_function :method_missing
29
+
30
+ def log_request_response_stats(name, args, &original_call)
31
+ if defined?(RR_OUTBOUND_STATS) && RR_OUTBOUND_STATS
32
+ begin
33
+ uri = args.is_a?(Array) ? args.first : args
34
+ uri = uri[custom_uri_key] if (uri.is_a?(Hash) && custom_uri_key && uri[custom_uri_key])
35
+ sanitized_uri = uri.to_s[0..(MAX_URL_LENGTH - 1)]
36
+ rrs = RequestResponse.new(
37
+ DummyRequest.new({method: name, path: sanitized_uri}),
38
+ DummyResponse.new,
39
+ {redis_connection: $redis, gather_stats: true, mongoid_doc_model: ReqResStat}
40
+ )
41
+ rrs.capture_request_response_cycle_start_info
42
+ resp = original_call.call
43
+ rrs.capture_request_response_cycle_end_info
44
+ return resp
45
+ rescue Exception => ex
46
+ # Rails.logger.info "Following exception is raised:"
47
+ # Rails.logger.info ex
48
+ rrs.try(:capture_request_response_cycle_error_info)
49
+ raise ex
50
+ end
51
+ else
52
+ resp = original_call.call
53
+ end
54
+ end
55
+ # module_function :log_request_response_stats
56
+
57
+ def self.included(base)
58
+ class << base
59
+ RENAMED_METHODS.each do |key|
60
+ if method_defined?(key)
61
+ alias_method("#{RENAME_NAMESPACE}#{key.to_s}".to_sym, key)
62
+ remove_method key
63
+ end
64
+ end
65
+ end
66
+ base.class_eval do
67
+ const_set("RENAME_NAMESPACE", RENAME_NAMESPACE)
68
+ const_set("RENAMED_METHODS", RENAMED_METHODS)
69
+
70
+ module_function :method_missing
71
+ module_function :log_request_response_stats
72
+ module_function :custom_uri_key
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,6 @@
1
+ # File: lib/request_response_stats/dummy_request.rb
2
+
3
+ module RequestResponseStats
4
+ class DummyResponse
5
+ end
6
+ end
@@ -0,0 +1,15 @@
1
+ # File: lib/request_response_stats/dummy_response.rb
2
+
3
+ module RequestResponseStats
4
+ class DummyRequest
5
+ attr_accessor :path, :method, :parameters
6
+
7
+ def initialize(options)
8
+ @path = options[:path]
9
+ @method = options[:method]
10
+ @controller = options[:parameters].try(:[], "controller") || "external_controller"
11
+ @action = options[:parameters].try(:[], "action") || "external_action"
12
+ @parameters = {"controller" => @controller, "action" => @action}
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,226 @@
1
+ # File: lib/request_response_stats/redis_record.rb
2
+
3
+ require 'active_support/time'
4
+ require 'active_support/core_ext/hash/indifferent_access'
5
+ require 'json'
6
+
7
+ module RequestResponseStats
8
+ class RedisRecord
9
+ attr_accessor :key, :value
10
+
11
+ REDIS_RR_KEY_NAMESPACE = "api_req_res"
12
+
13
+ PERMITTED_KEYS = [
14
+ :key_name,
15
+ :server_name,
16
+ :api_name,
17
+ :api_verb,
18
+ :api_controller,
19
+ :api_action,
20
+ :request_count,
21
+ :min_time,
22
+ :max_time,
23
+ :avg_time,
24
+ :start_time,
25
+ :end_time,
26
+ :error_count,
27
+ :min_used_memory_MB,
28
+ :max_used_memory_MB,
29
+ :avg_used_memory_MB,
30
+ :min_swap_memory_MB,
31
+ :max_swap_memory_MB,
32
+ :avg_swap_memory_MB,
33
+ :avg_gc_stat_diff,
34
+ :min_gc_stat_diff,
35
+ :max_gc_stat_diff,
36
+ ].map(&:to_s)
37
+
38
+ AT_MAX_TIME = nil unless defined? AT_MAX_TIME
39
+ AT_ERROR_COUNT = nil unless defined? AT_ERROR_COUNT
40
+ AT_MAX_SWAP_MEMORY_MB = nil unless defined? AT_MAX_SWAP_MEMORY_MB
41
+ ALERT_THRESHOLD = {
42
+ max_time: AT_MAX_TIME || 30,
43
+ error_count: AT_ERROR_COUNT || 2,
44
+ max_swap_memory_MB: AT_MAX_SWAP_MEMORY_MB || 200,
45
+ }.stringify_keys
46
+
47
+ class << self
48
+ # it returns the redis connection
49
+ # this method must be redefined for `RedisRecord` to be useable
50
+ def redis
51
+ raise StandardError, "UNDEFINED #{__method__}"
52
+ end
53
+
54
+ # get value from redis
55
+ def get(key)
56
+ redis.get(key)
57
+ end
58
+
59
+ # set value to redis
60
+ def set(key, value, options={})
61
+ redis.set(key, value, options)
62
+ end
63
+
64
+ # delete value from redis
65
+ def del(key)
66
+ redis.del(key)
67
+ end
68
+
69
+ def all_keys(opts={})
70
+ support = opts[:support] || false
71
+ if support
72
+ regex = /^#{REDIS_RR_KEY_NAMESPACE}/
73
+ else
74
+ regex = /^#{REDIS_RR_KEY_NAMESPACE}_PUBLIC/
75
+ end
76
+
77
+ redis.keys.select{|k| k =~ regex}.sort.reverse
78
+ end
79
+
80
+ # return parsed value from redis
81
+ def parsed_get(key)
82
+ JSON.parse(redis.get(key) || "{}")
83
+ end
84
+
85
+ # it returns parsed result into the format required for Mongo dump
86
+ def formatted_parsed_get_for_mongo(key)
87
+ data = parsed_get(key)
88
+ data["start_time"] = date_time_str_to_obj(data["start_time"])
89
+ data["end_time"] = date_time_str_to_obj(data["end_time"])
90
+
91
+ data
92
+ end
93
+
94
+ def hashify_all_data(opts={})
95
+ support = opts[:support] || false
96
+ req_res_stat = ActiveSupport::HashWithIndifferentAccess.new
97
+ all_keys(support: support).each do |key|
98
+ req_res_stat[key] = ActiveSupport::HashWithIndifferentAccess.new(parsed_get key)
99
+ end
100
+
101
+ req_res_stat
102
+ end
103
+
104
+ def flush_all_keys
105
+ redis.del(*all_keys(support: true)) if all_keys.present?
106
+ end
107
+
108
+ def group_stats_by_time_duration
109
+ raise StandardError, "UNDEFINED #{__method__}"
110
+ end
111
+
112
+ def support_key(server_name, key_name="default")
113
+ [REDIS_RR_KEY_NAMESPACE, "SUPPORT", server_name.to_s, key_name].join("_")
114
+ end
115
+
116
+ def req_key(server_name, req_object_id)
117
+ support_key(server_name, ["REQ_OBJ", req_object_id].join("_"))
118
+ end
119
+
120
+ def req_res_key(server_name, api_name, api_http_verb)
121
+ ["#{REDIS_RR_KEY_NAMESPACE}_PUBLIC_#{server_name}_#{api_name}_#{api_http_verb}", get_time_slot_name].compact.join("_")
122
+ end
123
+
124
+ def get_slot_range_for_key(redis_key)
125
+ date_slot_string = redis_key.split("_")[-1]
126
+
127
+ get_slot_range_for_date_slot_string(date_slot_string)
128
+ end
129
+
130
+ # set jsonified value to redis and raise alerts
131
+ def jsonified_set(key, value, options={}, custom_options={strict_key_check: true})
132
+ value.select!{|k,v| PERMITTED_KEYS.include? k.to_s} if custom_options[:strict_key_check]
133
+
134
+ # set jsonified value to redis
135
+ redis.set(key, value.to_json, options)
136
+
137
+ # get alerts collection
138
+ alerts = ALERT_THRESHOLD.select{ |k, v| value[k] >= v if value[k] }.map{|k,v| {
139
+ redis_key: key,
140
+ alarm_key: k,
141
+ alarm_value: v,
142
+ actual_value: value[k]
143
+ }}
144
+ alerts_data = {data: alerts}.to_json
145
+ raise_alert(alerts_data) if alerts.present?
146
+
147
+ # return alerts
148
+ alerts_data
149
+ end
150
+
151
+ def freezed_keys
152
+ all_keys.map{|k| self.new(k)}.select{|k| k.is_key_freezed?}.map{|rr| rr.key}
153
+ end
154
+
155
+ # lets you fetch records for given conditions from redis
156
+ def query(params={})
157
+ # to implement
158
+ end
159
+
160
+ private
161
+
162
+ # Example: for a value of `1.hour` for `group_stats_by_time_duration`, the data for each endpoint is
163
+ # divided into `1 day / 1.hour = 24` time slots within a day
164
+ def get_time_slot_name(current_time = Time.now)
165
+ current_time_utc = current_time.utc
166
+ seconds_since_beginning_of_today = (current_time_utc - Time.utc(current_time_utc.year, current_time_utc.month, current_time_utc.day)).to_i
167
+ # seconds_in_a_day = 24 * 60 * 60
168
+ num_of_slots_in_a_day = 24.hours / group_stats_by_time_duration
169
+ seconds_in_a_time_slot = group_stats_by_time_duration.seconds.to_i
170
+ current_slot = (seconds_since_beginning_of_today / seconds_in_a_time_slot).to_i
171
+ slot_str_length = num_of_slots_in_a_day.to_s.size
172
+ current_slot_name = "%0#{slot_str_length}d" % current_slot
173
+ current_slot_full_name = [
174
+ current_time_utc.year,
175
+ "%02d" % current_time_utc.month, # current_time_utc.month
176
+ "%02d" % current_time_utc.day, # current_time_utc.day
177
+ current_slot_name
178
+ ].join("-")
179
+
180
+ current_slot_full_name
181
+ end
182
+
183
+ # returns time range of given date_slot_stringe
184
+ # which is [start_time_of_slot, end_time_of_slot]
185
+ def get_slot_range_for_date_slot_string(date_slot_string)
186
+ year_num, month_num, date_num, slot_num = date_slot_string.split("-").map(&:to_i)
187
+ time = Time.utc(year_num, month_num, date_num)
188
+
189
+ # num_of_slots_in_a_day = 24.hours / group_stats_by_time_duration
190
+ seconds_in_a_time_slot = group_stats_by_time_duration.seconds.to_i
191
+ seconds_already_passed = slot_num * seconds_in_a_time_slot
192
+ starting_time = time + seconds_already_passed
193
+ ending_time = starting_time + seconds_in_a_time_slot
194
+
195
+ [starting_time, ending_time]
196
+ end
197
+
198
+ # Input example: "2017-11-06 09:01:00 UTC"
199
+ def date_time_str_to_obj(date_time_str)
200
+ date, time, zone = date_time_str.split(" ")
201
+ date = date.split("-").map(&:to_i)
202
+ time = time.split(":").map(&:to_i)
203
+ zone = "+00:00" if zone == ["UTC"]
204
+ DateTime.new(*date, *time, zone)
205
+ end
206
+
207
+ def raise_alert(data)
208
+ name.split("::")[0].constantize.custom_alert_code(data)
209
+ end
210
+
211
+ end
212
+
213
+ def initialize(key, value=nil)
214
+ @key = key
215
+ @value = value
216
+ end
217
+
218
+ # if a key is freezed then no more data will be written to it
219
+ def is_key_freezed?
220
+ return nil unless self.class.group_stats_by_time_duration
221
+
222
+ self.class.get_slot_range_for_key(key)[1] < Time.now
223
+ end
224
+
225
+ end
226
+ end