Sha256: 1c2903a36a3c63641b1ad0ef402522c2a977817deb8379fc589f7359d5d59505

Contents?: true

Size: 1.29 KB

Versions: 8

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

require 'json'
require 'net/http'
require 'open-uri'
require 'uri'

module Vtk
  # Provides command analytics to VTK team
  class Analytics
    attr_reader :name, :args, :hostname

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

    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

    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

    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

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
vtk-1.0.0 lib/vtk/analytics.rb
vtk-0.9.5 lib/vtk/analytics.rb
vtk-0.9.4 lib/vtk/analytics.rb
vtk-0.9.3 lib/vtk/analytics.rb
vtk-0.9.2 lib/vtk/analytics.rb
vtk-0.9.1 lib/vtk/analytics.rb
vtk-0.9.0 lib/vtk/analytics.rb
vtk-0.8.0 lib/vtk/analytics.rb