-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracking.rb
59 lines (50 loc) · 1.38 KB
/
tracking.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
require 'grape'
$data = {}
$stats = nil
module Tracking
class API < Grape::API
version 'v1', using: :header, vendor: 'manyfold'
format :json
default_format :json
helpers do
def store(id, version)
$data[id] = version.merge(time: DateTime.now)
$stats = nil
end
def stats
$stats ||= begin
recent = $data.select{ |k,v| v[:time] >= (Date.today - 7) }
{
instances: recent.size,
versions: recent.group_by{ |k,v| v["app"] }.map{|k,v| [k, v.length]}.to_h,
images: recent.group_by{ |k,v| v["image"] }.map{|k,v| [k, v.length]}.to_h,
architectures: recent.group_by{ |k,v| v["arch"] }.map{|k,v| [k, v.length]}.to_h
}
end
end
end
desc "Store tracking data for a single ID"
params do
requires :id, type: String
requires :version, type: Hash do
requires :app, type: String
requires :sha, type: String
end
end
post do
store(params["id"], params["version"])
end
desc "Send browser visitors to docs"
get do
redirect 'https://manyfold.app/sysadmin/tracking.html'
end
desc "Get aggregated usage stats"
get :stats do
stats
end
desc "Get a nice badge"
get :badge do
redirect "https://img.shields.io/badge/instances-#{stats[:instances]}-blue"
end
end
end