-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstedd_telemetry.rb
81 lines (64 loc) · 1.7 KB
/
instedd_telemetry.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
Dir[File.expand_path("../instedd_telemetry/**/*.rb", __FILE__)].each do |file|
require file
end
module InsteddTelemetry
extend Tracking
def self.setup(&block)
case block.arity
when 0
configuration.instance_eval(&block)
when 1
block.call(configuration)
end
end
def self.configuration
@configuration ||= Configuration.new
end
def self.instance_id
@instance_id ||= load_instance_id
end
def self.upload_enabled
not Setting.get_bool(:disable_upload)
end
def self.api
@api ||= Api.new(configuration.server_url)
end
def self.application
configuration.application
end
def self.ensure_periods_exists
InsteddTelemetry::Period.ensure_periods_exist
end
def self.current_period
if current_period_cached
@current_period
else
@current_period = InsteddTelemetry::Period.current
end
end
def self.update_installation
unless Setting.get_bool(:installation_info_synced)
params = {
application: InsteddTelemetry.application,
admin_email: InsteddTelemetry::Setting.get(:admin_email),
opt_out: InsteddTelemetry::Setting.get(:disable_upload)
}
params = params.select { |k,v| !v.nil? }
begin
InsteddTelemetry.api.update_installation(params)
Setting.set(:installation_info_synced, true)
rescue
end
end
end
private
def self.current_period_cached
!Rails.env.test? && @current_period && DateTime.now < @current_period.end
end
def self.load_instance_id
id_setting = InsteddTelemetry::Setting.find_or_create_by(key: :installation_id) do |guid_setting|
guid_setting.value = SecureRandom.uuid
end
id_setting.value
end
end