Module: CommonHelper

Defined in:
lib/common_helper.rb

Instance Method Summary collapse

Instance Method Details

#add_custom_params_to_loggerObject



31
32
33
34
# File 'lib/common_helper.rb', line 31

def add_custom_params_to_logger
  Rails.logger.() if Rails.logger.respond_to?(:add_metadata)
  redis_push(Rails.logger.instance_variable_get("@mongo_record").to_json)
end

#common_params_meta_logObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/common_helper.rb', line 67

def common_params_meta_log
  ll = {
    referer_service: params[:referer_service],
    jwt_cookie_hash: jwt_header_token,
    property: current_micro_service_name,
    referrer_property: params[:referer_service]
  }
  if !params[:referer_service]
    if request.referer && (URI(request.referer).path=="" || URI(request.referer).path=="/")
      if request.cookies['REARCHID']=="rearch"
        ll[:referrer_property] = "Search Page"
      elsif request.cookies['REARCHID']=="nonrearch"
        ll[:referrer_property] = "Old Home Page"
      end
    elsif request.referer && URI(request.referer).path.start_with?('/learn')
      ll[:referrer_property] = "Learn-Frontend"
    elsif request.referer && URI(request.referer).path.start_with?('/profile')
      ll[:referrer_property] = "Profile-Frontend"
    elsif request.referer && request.referer.match(/practice\/solve\//)
      ll[:referrer_property] = "Practice-Frontend"
    elsif request.referer && request.referer.match(/test\//)
      ll[:referrer_property] = "Test-Frontend"
    elsif request.referer && URI(request.referer).path.start_with?('/ask')
      ll[:referrer_property] = "Ask-Frontend"
    elsif request.referer && URI(request.referer).path.start_with?('/rankup')
      ll[:referrer_property] = "Rankup-Frontend"
    elsif request.referer && URI(request.referer).path.start_with?('/jump')
      ll[:referrer_property] = "Jump-Frontend"
    elsif request.referer && URI(request.referer).path.start_with?('/exams')
      ll[:referrer_property] = "Exams-Wordpress"
    elsif request.referer && URI(request.referer).path.start_with?('/news')
      ll[:referrer_property] = "News-Wordpress"
    end
  end
  ll
end

#current_micro_service_nameObject



2
3
4
# File 'lib/common_helper.rb', line 2

def current_micro_service_name
  @_current_micro_service_name ||= Rails.configuration.database_configuration[Rails.env]['mongodb_logger']['application_name']  
end

#custom_meta_data_logObject



104
105
106
# File 'lib/common_helper.rb', line 104

def 
  user_meta_log.merge(user_agent_meta_log).merge(custom_params_meta_log).merge(common_params_meta_log)
end

#custom_params_meta_logObject



63
64
65
# File 'lib/common_helper.rb', line 63

def custom_params_meta_log
  {c_source: params[:C_source], c_id: params[:C_id]}
end

#kafka_db_log_to_same_topicObject



42
43
44
45
46
47
48
# File 'lib/common_helper.rb', line 42

def kafka_db_log_to_same_topic
  custom_log_data = self.as_json
  db_name = self.class.name.split("::").map(&:downcase).join("-")
  topic_name = "#{db_name}-db-#{Rails.env}"
  redis_format_data = {class: "TestAppDbKafkaLogWorker", args: [custom_log_data, topic_name], retry: true, queue: "logs", jid: SecureRandom.hex(12), created_at: Time.zone.now.to_i, enqueued_at: Time.zone.now.to_i}
  redis_log.lpush(redis_queue_name, redis_format_data.to_json)
end

#kafka_logObject



108
109
110
# File 'lib/common_helper.rb', line 108

def kafka_log
  @kafka_log ||= Kafka.new(kafka_log_end_points, logger: Rails.logger, client_id: "#{current_micro_service_name}-#{Rails.env}")
end

#kafka_log_producerObject



112
113
114
# File 'lib/common_helper.rb', line 112

def kafka_log_producer
  @kafka_producer ||= kafka_log.async_producer
end

#redis_data(key, value) ⇒ Object



23
24
25
# File 'lib/common_helper.rb', line 23

def redis_data(key, value)
  {key: key, value: value}
end

#redis_get(key) ⇒ Object



19
20
21
# File 'lib/common_helper.rb', line 19

def redis_get(key)
  (JSON.parse($redis.get(key)) || {}) rescue {}
end

#redis_processObject



27
28
29
# File 'lib/common_helper.rb', line 27

def redis_process
  redis_set(_batch_events)
end

#redis_push(custom_log_data) ⇒ Object



36
37
38
39
40
# File 'lib/common_helper.rb', line 36

def redis_push(custom_log_data)
  topic_name = "#{current_micro_service_name}-log-#{Rails.env}"
  redis_format_data = {class: "KafkaMicroServicesLogs", args: [custom_log_data, topic_name], retry: true, queue: "logs", jid: SecureRandom.hex(12), created_at: Time.zone.now.to_i, enqueued_at: Time.zone.now.to_i}
  redis_log.lpush(redis_queue_name, redis_format_data.to_json)
end

#redis_queue_nameObject



50
51
52
53
# File 'lib/common_helper.rb', line 50

def redis_queue_name
  return "queue:logs" if Rails.env.production?
  return "test-app:queue:logs"
end

#redis_set(batch_set) ⇒ Object



15
16
17
# File 'lib/common_helper.rb', line 15

def redis_set(batch_set)
  batch_set.each {|key, value| d}
end

#render_error(msg, status) ⇒ Object



11
12
13
# File 'lib/common_helper.rb', line 11

def render_error(msg, status)
  render json: {:error => msg, :status => status}, :status => status
end

#required_organizationObject



6
7
8
9
# File 'lib/common_helper.rb', line 6

def required_organization
  @organization ||= Organization.find_by(namespace: params[:namespace], language: params[:language])
  render_error("Organization or Language not found", 404) if @organization.nil?
end

#user_agent_meta_logObject



55
56
57
58
59
60
61
# File 'lib/common_helper.rb', line 55

def user_agent_meta_log
  ua = DeviceDetector.new(request.user_agent)
  device_info = [:name, :full_version, :user_agent, :os_name, :os_full_version, :device_name, :device_brand, :device_type, :known?, :bot?, :bot_name]
  info_data = {url: request.url, referer: request.referer}  
  ua.methods.each {|c| info_data[c] = ua.__send__(c) if device_info.include?(c) }
  info_data
end

#user_meta_logObject



116
117
118
119
120
121
122
# File 'lib/common_helper.rb', line 116

def user_meta_log
  return {} unless current_ms_user
   = {}
  [:user_id] = current_ms_user.id
  [:email] = current_ms_user.email
  
end