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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/common_helper.rb', line 54

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



91
92
93
# File 'lib/common_helper.rb', line 91

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

#custom_params_meta_logObject



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

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

#kafka_logObject



95
96
97
# File 'lib/common_helper.rb', line 95

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



99
100
101
# File 'lib/common_helper.rb', line 99

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("queue:logs", redis_format_data.to_json)
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



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

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



103
104
105
106
107
108
109
# File 'lib/common_helper.rb', line 103

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