Class: Vtk::Analytics

Inherits:
Object
  • Object
show all
Defined in:
lib/vtk/analytics.rb

Overview

Provides command analytics to VTK team

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, args: nil, hostname: nil) ⇒ Analytics

Returns a new instance of Analytics.



13
14
15
16
17
# File 'lib/vtk/analytics.rb', line 13

def initialize(name:, args: nil, hostname: nil)
  @name = name
  @args = args || ARGV.join('_')
  @hostname = hostname || `hostname -f`.chomp
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



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

def args
  @args
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



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

def hostname
  @hostname
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#emit_pointObject



31
32
33
34
35
36
37
38
# File 'lib/vtk/analytics.rb', line 31

def emit_point
  uri = URI.parse 'https://dev.va.gov/_vfs/vtk-analytics/record'
  Net::HTTP.start uri.host, uri.port, use_ssl: uri.scheme == 'https' do |http|
    request = Net::HTTP::Post.new uri, 'Content-Type' => 'application/json'
    request.body = { series: [point] }.to_json
    http.request request
  end
end

#internet?Boolean

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/vtk/analytics.rb', line 51

def internet?
  true if URI.open 'http://www.google.com/'
rescue SocketError
  false
end

#logObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/vtk/analytics.rb', line 19

def log
  return if ENV['CI'] || ENV['TEST'] || ENV['VTK_DISABLE_ANALYTICS']

  Process.fork do
    exit unless internet?

    emit_point
  rescue StandardError
    false # Silently error
  end
end

#pointObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/vtk/analytics.rb', line 40

def point
  {
    metric: 'vtk.command_executed',
    type: 'count',
    interval: 1,
    tags: ["name:#{name}", "args:#{args}"],
    host: hostname,
    points: [[Time.now.utc.to_i, '1']]
  }
end