trueandco_analytics 0.3.0 → 0.4.0

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.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -6
  3. data/app/assets/javascripts/analitics_in_point.js +28 -0
  4. data/app/assets/javascripts/analitics_script.js +165 -0
  5. data/app/assets/javascripts/trueandco_analytics.js +2 -169
  6. data/app/views/trueandco_analytics/{receiver → site/receiver}/pull_user_statistic.erb +0 -0
  7. data/config/routes.rb +1 -1
  8. data/db/migrate/20170802133950_create_metric_users.rb +1 -1
  9. data/exe/trueandco_analytics +1 -10
  10. data/lib/generators/trueandco_analytics/templates/trueandco_analytics.rb +3 -3
  11. data/lib/trueandco_analytics.rb +44 -26
  12. data/lib/trueandco_analytics/{locales → common/locales}/en.yml +0 -0
  13. data/{app/models/trueandco_analytics → lib/trueandco_analytics/common/models}/application_record.rb +0 -0
  14. data/{app/models/trueandco_analytics → lib/trueandco_analytics/common/models}/metric_user.rb +0 -0
  15. data/{app/models/trueandco_analytics → lib/trueandco_analytics/common/models}/metric_user_session.rb +0 -0
  16. data/lib/trueandco_analytics/common/models/metric_user_visit.rb +8 -0
  17. data/lib/trueandco_analytics/{modules → common/modules}/info.rb +0 -0
  18. data/lib/trueandco_analytics/{reports → common/reports}/application_report.rb +0 -0
  19. data/lib/trueandco_analytics/{reports → common/reports}/details_report.rb +0 -0
  20. data/lib/trueandco_analytics/{reports → common/reports}/page_buy_in_date_range_report.rb +0 -0
  21. data/lib/trueandco_analytics/{services → common/services}/logger.rb +0 -0
  22. data/lib/trueandco_analytics/{services → common/services}/redis_connect.rb +0 -0
  23. data/lib/trueandco_analytics/{services → common/services}/reports.rb +1 -6
  24. data/lib/trueandco_analytics/{services → common/services}/workers.rb +0 -0
  25. data/lib/trueandco_analytics/config/params.rb +7 -7
  26. data/lib/trueandco_analytics/{cli → console/cli}/common.rb +0 -0
  27. data/lib/trueandco_analytics/{cli → console/cli}/report.rb +0 -0
  28. data/lib/trueandco_analytics/console/commands/report_c/generate.rb +29 -0
  29. data/lib/{extension_string.rb → trueandco_analytics/console/extension_string.rb} +0 -0
  30. data/lib/trueandco_analytics/engine.rb +2 -6
  31. data/{app/commands/trueandco_analytics → lib/trueandco_analytics/site/commands}/arr_jsons_to_arr_hash.rb +1 -1
  32. data/lib/trueandco_analytics/site/commands/metric_c/add_list.rb +63 -0
  33. data/lib/trueandco_analytics/site/commands/session_c/create.rb +63 -0
  34. data/lib/trueandco_analytics/site/commands/session_c/put_data.rb +49 -0
  35. data/lib/trueandco_analytics/site/commands/user_c/create_or_update_user_if_exist.rb +39 -0
  36. data/{app/controllers/trueandco_analytics → lib/trueandco_analytics/site/controllers}/application_controller.rb +2 -2
  37. data/{app/controllers/trueandco_analytics → lib/trueandco_analytics/site/controllers}/reciver_controller.rb +5 -7
  38. data/lib/trueandco_analytics/site/helpers/trueandco_analytics_helper.rb +18 -0
  39. data/lib/trueandco_analytics/site/workers/user_session_metric_worker.rb +20 -0
  40. data/lib/trueandco_analytics/version.rb +1 -1
  41. metadata +31 -29
  42. data/app/commands/trueandco_analytics/metric_c/add_list.rb +0 -65
  43. data/app/commands/trueandco_analytics/report_c/generate.rb +0 -31
  44. data/app/commands/trueandco_analytics/session_c/create.rb +0 -65
  45. data/app/commands/trueandco_analytics/session_c/put_data.rb +0 -46
  46. data/app/commands/trueandco_analytics/user_c/create_or_update_user_if_exist.rb +0 -41
  47. data/app/models/trueandco_analytics/metric_user_visit.rb +0 -8
  48. data/app/workers/trueandco_analytics/user_session_metric_worker.rb +0 -21
  49. data/lib/trueandco_analytics/helpers/trueandco_analytics_helper.rb +0 -35
@@ -1,31 +1,49 @@
1
- require 'colorize'
2
- require 'csv'
3
- require 'fnv'
4
- require 'byebug'
5
-
6
- if defined?(Rails) == 'constant'
7
- require 'sidekiq/api'
8
- require 'sidekiq'
9
- require 'ipaddress'
10
- require 'browser'
11
- require 'browser/aliases'
12
- require 'trueandco_analytics/engine'
13
- else
14
- require 'extension_string'
15
- end
16
- require 'trueandco_analytics/config/params'
17
- require 'trueandco_analytics/modules/info'
1
+ module TrueandcoAnalytics
2
+ ROOT = File.expand_path('../..', __FILE__)
3
+ COMMON_PATH = "#{TrueandcoAnalytics::ROOT}/lib/trueandco_analytics/common"
4
+ SITE_PATH = "#{TrueandcoAnalytics::ROOT}/lib/trueandco_analytics/site"
5
+ CONSOLE_PATH = "#{TrueandcoAnalytics::ROOT}/lib/trueandco_analytics/console"
18
6
 
19
- I18n.available_locales = [:en]
20
- I18n.config.enforce_available_locales = true
21
- I18n.load_path += Dir[File.join(File.dirname(__FILE__), 'trueandco_analytics/locales', '*.yml').to_s]
7
+ require 'colorize'
8
+ require 'csv'
9
+ require 'fnv'
10
+ require 'byebug'
11
+ require 'trueandco_analytics/config/params'
12
+ require 'trueandco_analytics/common/modules/info'
22
13
 
23
- module TrueandcoAnalytics
24
- ROOT = File.expand_path('../..', __FILE__)
25
- autoload :Reports, 'trueandco_analytics/services/reports'
26
- autoload :Logger, 'trueandco_analytics/services/logger'
27
- autoload :Workers, 'trueandco_analytics/services/workers'
28
- autoload :RedisConnect, 'trueandco_analytics/services/redis_connect'
14
+ # Add the translations
15
+ I18n.available_locales = [:en]
16
+ I18n.config.enforce_available_locales = true
17
+ I18n.load_path += Dir[File.join(File.dirname(__FILE__), 'trueandco_analytics/common/locales', '*.yml').to_s]
18
+
19
+ # Add models
20
+ require "#{COMMON_PATH}/models/application_record"
21
+ Dir["#{COMMON_PATH}/models/*.rb"].each { |f| require f }
22
+
23
+ if defined?(Rails) == 'constant'
24
+ require 'sidekiq/api'
25
+ require 'sidekiq'
26
+ require 'ipaddress'
27
+ require 'browser'
28
+ require 'browser/aliases'
29
+
30
+ require "#{SITE_PATH}/controllers/application_controller"
31
+ Dir["#{SITE_PATH}/**/*.rb"].each { |f| require f }
32
+
33
+ require 'trueandco_analytics/engine'
34
+ else
35
+ Dir["#{CONSOLE_PATH}/**/*.rb"].each { |f| require f }
36
+ end
37
+ # Add service for reports
38
+ autoload :Reports, 'trueandco_analytics/common/services/reports'
39
+ # Add list of reports
40
+ autoload :ApplicationReport, 'trueandco_analytics/common/reports/application_report'
41
+ autoload :DetailsReport, 'trueandco_analytics/common/reports/details_report'
42
+ autoload :PageBuyInDateRangeReport, 'trueandco_analytics/common/reports/page_buy_in_date_range_report'
43
+
44
+ autoload :Logger, 'trueandco_analytics/common/services/logger'
45
+ autoload :Workers, 'trueandco_analytics/common/services/workers'
46
+ autoload :RedisConnect, 'trueandco_analytics/common/services/redis_connect'
29
47
 
30
48
  def self.table_name_prefix
31
49
  'metric_'
@@ -0,0 +1,8 @@
1
+ module TrueandcoAnalytics
2
+ class MetricUserVisit < ApplicationRecord
3
+ self.table_name = 'metric_user_visits'
4
+
5
+ belongs_to :metric_user_session
6
+ belongs_to :metric_user
7
+ end
8
+ end
@@ -1,14 +1,9 @@
1
- report_dir = "#{TrueandcoAnalytics::ROOT}/lib/trueandco_analytics/reports"
2
- require "#{report_dir}/application_report"
3
- Dir["#{report_dir}/*.rb"].each do |f|
4
- require f
5
- end
6
-
7
1
  module TrueandcoAnalytics
8
2
  class Reports
9
3
  include TrueandcoAnalytics::Modules::Info
10
4
 
11
5
  def self.available_reports
6
+ Dir["#{COMMON_PATH}/reports/*.rb"].each { |f| require f }
12
7
  TrueandcoAnalytics::ApplicationReport.reports
13
8
  end
14
9
 
@@ -8,8 +8,8 @@ module TrueandcoAnalytics
8
8
  @time_dead_session = Rails.env.development? ? 30 : 600
9
9
  end
10
10
  @user_method_check = false
11
- @buy_selector_class = 'buy'
12
- @buy_selector_class_check = false
11
+ @buy_selector = ''
12
+ @buy_selector_check = false
13
13
  @connect_redis = { redis_db: 1, }
14
14
  @connect_redis_check = false
15
15
  @sidekiq_configure_client_url = 'redis://localhost:6379/1'
@@ -41,18 +41,18 @@ module TrueandcoAnalytics
41
41
  end
42
42
  end
43
43
 
44
- attr_reader :time_survey, :time_dead_session, :buy_selector_class,
44
+ attr_reader :time_survey, :time_dead_session, :buy_selector,
45
45
  :sidekiq_configure_client_url, :sidekiq_configure_server_url, :sidekiq_configure_namespace,
46
46
  :connect_db, :connect_redis
47
47
 
48
48
  attr_writer_only_once :time_survey, :user_method,
49
49
  :sidekiq_configure_client_url, :sidekiq_configure_server_url, :sidekiq_configure_namespace,
50
- :connect_db,:connect_redis
50
+ :connect_db, :connect_redis
51
51
 
52
- def buy_selector_class=(value)
52
+ def buy_selector=(value)
53
53
  return if value.nil?
54
- @buy_selector_class = value.delete('.') unless @buy_selector_class_check
55
- @buy_selector_class_check = true
54
+ @buy_selector = value.delete('.') unless @buy_selector_check
55
+ @buy_selector_check = true
56
56
  end
57
57
 
58
58
  def user_method
@@ -0,0 +1,29 @@
1
+ module TrueandcoAnalytics::ReportC
2
+ class Generate
3
+ include TrueandcoAnalytics::Modules::Info
4
+
5
+ def initialize(name: nil, format: nil, datetime_start: nil, datetime_end: nil, path: nil)
6
+ @name = name
7
+ @format = format
8
+ @datetime_start = datetime_start
9
+ @datetime_end = datetime_end
10
+ @path = path || CURRENT_PATH
11
+ end
12
+
13
+ def execute
14
+ report = ::TrueandcoAnalytics::Reports.get_report_obj(name, format, datetime_start, datetime_end)
15
+ return if report.nil? || report.data.empty?
16
+ @path += report.report_name if File.directory?(path)
17
+ dirname = File.dirname(path)
18
+ unless File.directory?(dirname)
19
+ FileUtils.mkdir_p(dirname)
20
+ end
21
+ File.open(path, 'w+') {|f| f.write(report.data)}
22
+ result(I18n.t('trueandco_analytics.result.done', path: path))
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :name, :format, :datetime_start, :datetime_end, :path
28
+ end
29
+ end
@@ -1,5 +1,3 @@
1
- require "trueandco_analytics/helpers/trueandco_analytics_helper"
2
-
3
1
  module TrueandcoAnalytics
4
2
 
5
3
  class Engine < ::Rails::Engine
@@ -16,14 +14,12 @@ module TrueandcoAnalytics
16
14
  initializer :action_controller do
17
15
  ::TrueandcoAnalytics::Workers.init
18
16
  ActiveSupport.on_load(:action_controller) do
19
- self.try(:helper, ::TrueandcoAnalytics::TrueandcoAnalyticsHelper) ||
20
- self.try(:include, ::TrueandcoAnalytics::TrueandcoAnalyticsHelper)
17
+ self.try(:helper, ::TrueandcoAnalytics::Site::TrueandcoAnalyticsHelper) ||
18
+ self.try(:include, ::TrueandcoAnalytics::Site::TrueandcoAnalyticsHelper)
21
19
  end
22
20
  end
23
21
 
24
22
  config.generators do |g|
25
- g.test_framework :rspec, :fixture => false
26
- g.fixture_replacement :factory_girl, :dir => 'spec/factories'
27
23
  g.assets false
28
24
  g.helper false
29
25
  end
@@ -1,4 +1,4 @@
1
- module TrueandcoAnalytics
1
+ module TrueandcoAnalytics::Site
2
2
  class ArrJsonsToArrHash
3
3
 
4
4
  def initialize(arr_json)
@@ -0,0 +1,63 @@
1
+ module TrueandcoAnalytics::Site::MetricC
2
+ class AddList
3
+
4
+ def initialize(metric_user, session, user_session_data)
5
+ if metric_user.nil? || session.nil? || user_session_data.nil?
6
+ raise ArgumentError.new("#{self.to_s} metric_user=#{metric_user}
7
+ session=#{session} user_session_data=#{user_session_data}")
8
+ end
9
+ @metric_user = metric_user
10
+ @session = session
11
+ @user_session_data = user_session_data
12
+ end
13
+
14
+ def execute
15
+ uniq_pages = info_uniq_page
16
+ return if uniq_pages.nil?
17
+ uniq_pages.each do |page|
18
+ data = {
19
+ metric_user: metric_user,
20
+ metric_user_session: session,
21
+ page_path: page['page_path'],
22
+ user_action: page['clicks']['clickDetails'].to_json,
23
+ time_s: page['time']['timeOnPage'],
24
+ is_buy: page['clicks']['clickDetails'].length > 0
25
+ }
26
+ ::TrueandcoAnalytics::MetricUserVisit.create(data)
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ attr_reader :metric_user, :session, :user_session_data
33
+
34
+ def info_uniq_page
35
+ length = user_session_data.length
36
+ return user_session_data if length < 2
37
+ results = []
38
+ page = user_session_data[0]['page_path']
39
+ clickDetails = []
40
+ index = 0
41
+ while index < length - 1
42
+ unless user_session_data[index]['clicks']['clickDetails'].empty?
43
+ clickDetails = user_session_data[index]['clicks']['clickDetails']
44
+ end
45
+ index += 1
46
+ unless page == user_session_data[index]['page_path']
47
+ data = user_session_data[index - 1]
48
+ data['clicks']['clickDetails'] = clickDetails
49
+ results << data
50
+ page = user_session_data[index]['page_path']
51
+ clickDetails = []
52
+ end
53
+ end
54
+ data = user_session_data[index]
55
+ unless data['clicks']['clickDetails'].empty?
56
+ clickDetails = data['clicks']['clickDetails']
57
+ end
58
+ data['clicks']['clickDetails'] = clickDetails
59
+ results << data
60
+ results
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,63 @@
1
+ module TrueandcoAnalytics::Site::SessionC
2
+ class Create
3
+ def initialize(metric_user, first_point, timestamp_end, request_info)
4
+
5
+ if metric_user.nil? || first_point.nil? || timestamp_end.nil? ||request_info.nil?
6
+ raise ArgumentError.new("#{self.to_s} metric_user=#{metric_user}
7
+ first_point=#{first_point} timestamp_end=#{timestamp_end} request_info=#{request_info}")
8
+ end
9
+
10
+ @metric_user = metric_user
11
+ @ip = request_info['remote_ip']
12
+ @user_agent = request_info['user_agent']
13
+ @session_start = first_point['time']['timestamp']
14
+ @referer = first_point['userReferrer']
15
+ @live_time_s = timestamp_end - @session_start
16
+ end
17
+
18
+ def execute
19
+ Browser::Base.include(Browser::Aliases)
20
+ browser = Browser.new(user_agent, accept_language: "en-us")
21
+ data = {
22
+ metric_user: metric_user,
23
+ user_agent: user_agent,
24
+ referer: referer,
25
+ session_start: datetime_session_start,
26
+ live_time_s: 60,
27
+ ip_4: ip_v4(ip),
28
+ ip_6: ip_v6(ip),
29
+ browser: browser.name,
30
+ is_mobile: browser.mobile? || false
31
+ }
32
+ ::TrueandcoAnalytics::MetricUserSession.create(data)
33
+ end
34
+
35
+ private
36
+
37
+ attr_reader :metric_user, :ip, :user_agent, :session_start, :live_time_s, :referer
38
+
39
+ def datetime_session_start
40
+ DateTime.strptime(session_start.to_s, '%s')
41
+ end
42
+
43
+ def ip_v4(ip)
44
+ ip_o = get_ip_object(ip)
45
+ result = 0
46
+ result = ip_o.to_i if ip_o.ipv4?
47
+ result
48
+ end
49
+
50
+
51
+ def ip_v6(ip)
52
+ ip_o = get_ip_object(ip)
53
+ result = 0
54
+ result = ip_o.data if ip_o.ipv6?
55
+ result
56
+ end
57
+
58
+ def get_ip_object(ip)
59
+ @ip_o ||= IPAddress.parse ip
60
+ end
61
+ end
62
+
63
+ end
@@ -0,0 +1,49 @@
1
+ module TrueandcoAnalytics::Site::SessionC
2
+ class PutData
3
+
4
+ def initialize(json_data, request_info, userInfo)
5
+ @json_data = json_data
6
+ @userInfo = userInfo
7
+ @request_info = request_info
8
+ @redis = ::TrueandcoAnalytics::RedisConnect.get
9
+ @time_survey = ::TrueandcoAnalytics::Config::Params.time_survey
10
+ @time_dead_session = ::TrueandcoAnalytics::Config::Params.time_dead_session
11
+ end
12
+
13
+ def execute
14
+ 1.times do
15
+ break if userInfo.nil? || userInfo.empty?
16
+ user_key = ::FNV.new.fnv1_32 userInfo.to_s
17
+ mange_job(user_key)
18
+ redis.rpush(user_key, json_data)
19
+ redis.expire(user_key, time_dead_session + 3)
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ attr_reader :json_data, :userInfo, :request_info, :redis, :time_dead_session, :time_survey
26
+
27
+ def datetime_dead_session
28
+ ::DateTime.now + time_dead_session.seconds
29
+ end
30
+
31
+ def mange_job(user_key)
32
+ user_job_key = "#{user_key}:job_key"
33
+ unless redis.exists user_job_key
34
+ create_job(user_key, user_job_key)
35
+ end
36
+ if redis.ttl(user_job_key) < time_survey * 3
37
+ job_id = redis.get(user_job_key)
38
+ Sidekiq::ScheduledSet.new.find_job(job_id)&.delete
39
+ create_job(user_key, user_job_key)
40
+ end
41
+ end
42
+
43
+ def create_job(user_key, user_job_key)
44
+ job_id = ::TrueandcoAnalytics::Site::UserSessionMetricWorker.perform_at(time_dead_session.seconds.from_now, user_key, request_info)
45
+ redis.setex(user_job_key, time_dead_session, job_id)
46
+ end
47
+ end
48
+
49
+ end
@@ -0,0 +1,39 @@
1
+ module TrueandcoAnalytics::Site::UserC
2
+ class CreateOrUpdateUserIfExist
3
+
4
+ def initialize(user_agent, timestamp_end)
5
+ if user_agent.nil? || timestamp_end.nil?
6
+ raise ArgumentError.new("#{self.to_s} user_agent=#{user_agent} timestamp_end=#{timestamp_end}")
7
+ end
8
+ @user_agent = user_agent
9
+ @timestamp_end = timestamp_end
10
+ @user_id = ::TrueandcoAnalytics::Config::Params.user_method&.id
11
+ end
12
+
13
+ def execute
14
+ user = nil
15
+ user = ::TrueandcoAnalytics::MetricUser.find_by(user_id: user_id) if user_id
16
+ user = ::TrueandcoAnalytics::MetricUser.find_by(hash_id: hash_id) if user.nil?
17
+ user = if user.nil?
18
+ ::TrueandcoAnalytics::MetricUser.create(user_id: user_id || 0, datetime_create: datetime_end,
19
+ last_active_datetime: datetime_end, hash_id: hash_id)
20
+ else
21
+ user.update_attribute(:last_active_datetime, datetime_end)
22
+ user
23
+ end
24
+ user
25
+ end
26
+
27
+ private
28
+
29
+ attr_reader :user_agent, :timestamp_end, :user_id
30
+
31
+ def datetime_end
32
+ DateTime.strptime(timestamp_end.to_s, '%s')
33
+ end
34
+
35
+ def hash_id
36
+ @hash_id ||= FNV.new.fnv1_32(user_agent)
37
+ end
38
+ end
39
+ end