Class: CloudRailSi::ServiceCode::Statistics

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudrail_si/statistics/Statistics.rb

Constant Summary collapse

@@CR_VERSION =
@get_cr_ver
@@SERVER_URL =
'https://developers.cloudrail.com/api/entries'
@@next =
1
@@count =
0
@@data =
{}
@@entry_id =
nil

Class Method Summary collapse

Class Method Details

.add_call(service, method) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cloudrail_si/statistics/Statistics.rb', line 18

def add_call(service, method)
    message = 'A valid CloudRail license key is required. You can get one for free at https://developers.cloudrail.com'
    keyMatch = /^[a-f\d]{24}$/i
    r = Regexp.new(keyMatch).freeze
    raise message if (!Settings.license_key || !Settings.license_key.match(r))

    calls = get_method_calls(service, method)
    calls['count'] += 1

    @@count += 1
    if (@@count === @@next)
        @@next *= 2
    end
    send_statistics()
end

.add_error(service, method) ⇒ Object



34
35
36
37
# File 'lib/cloudrail_si/statistics/Statistics.rb', line 34

def add_error(service, method)
    calls = get_method_calls(service, method)
    calls['error'] += 1
end

.get_cr_verObject



119
120
121
# File 'lib/cloudrail_si/statistics/Statistics.rb', line 119

def get_cr_ver
    Cloudrail::VERSION
end

.get_method_calls(service, method) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cloudrail_si/statistics/Statistics.rb', line 86

def get_method_calls(service, method)
    @@data[service] = @@data[service] || {}
    calls_to_service = @@data[service]
    if (!calls_to_service[method])
        calls_to_service[method] = {
            'count' => 0,
            'error' => 0
        }
    end

    calls_to_service[method]
end

.hash_string(str) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/cloudrail_si/statistics/Statistics.rb', line 99

def hash_string(str)
   service_code = {
        "hashString" => [
            ["hash.md5", "$L0", "$P1"],
            ["size", "$L1", "$L0"],
            ["set", "$L2", 0],
            ["set", "$P0", ""],
            ["get", "$L3", "$L0", "$L2"],
            ["string.format", "$L4", "%02X", "$L3"],
            ["string.concat", "$P0", "$P0", "$L4"],
            ["math.add", "$L2", "$L2", 1],
            ["if>=than", "$L2", "$L1", -5]
        ]
    }

    interpreter = Interpreter.new(Sandbox.new(service_code, [], {}))
    interpreter.call_function_sync("hashString", nil, str)
    interpreter.get_parameter(0)
end

.send_statisticsObject



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
82
83
84
# File 'lib/cloudrail_si/statistics/Statistics.rb', line 39

def send_statistics
    begin
        return if (@@count == 0)

        body = {
            data: @@data
        }

        if (@@entry_id)
            body.id = @@entry_id
        else
            mac = InitSelfTest.get_mac
            app = InitSelfTest.get_name_version
            client = {
                'os' => InitSelfTest.get_os
            }

            app_hash = hash_string([app['name'], app['version']].to_json)
            client_hash = hash_string([mac, client['os']].to_json)

            body['app'] = app
            body['client'] = client
            body['appKey'] = Settings.license_key unless (Settings.license_key.nil?)
            body['libraryVersion'] = @@CR_VERSION
            body['appHash'] = app_hash
            body['clientHash'] = client_hash
            body['platform'] = "Ruby"

            res = Helper.make_request(
                @@SERVER_URL,
                { "Content-Type" => "application/json" },
                Helper.streamify_string(body.to_json, nil),
                "POST")
            raise StandardError.new("Unexpected response when sending stats: #{res.code}") if (res.code != '200')
            res_str = Helper.dump_stream(res.body)
            if !@@entry_id
                obj = JSON.parse(res_str)
                @@entry_id = obj.id
            end
            @@data = {}
            @@count = 0
        end
    rescue => ex
        Helper.log(ex)
    end
end