-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcounter.rb
63 lines (51 loc) · 1.45 KB
/
counter.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
require 'dotenv'
require 'keen'
require "csv"
# given a keen project, count all the number of events in each event collection, output results to console and csv
# specify your project id, read key, and master key in .env
# KEEN_PROJECT_ID=x
# KEEN_READ_KEY=y
# KEEN_MASTER_KEY=Z
Dotenv.load
# allow timeframe to be specified via the command line
# usage: ruby collection_counts.rb previous_7_days
if !ARGV[0].nil?
timeframe = ARGV[0]
else
timeframe = 'previous_30_days'
end
# iterates over collections and counts the total number of events in each
total_events = 0
collections = []
Keen.event_collections.each {|collection|
collection = collection["name"]
# count
results = Keen.count(collection,
:timeframe => timeframe,
)
# print the results in the console
puts collection + " " + results.to_s
# sum up the to find total events in project
total_events += results
collections.push([collection, results])
}
collections.sort! {|x,y|
if x[1] > y[1]
0
elsif x[1] < y[1]
-1
else
1
end
}
# Create a CSV file to output the results
# You need to delete the file if one already exists
CSV.open("event_counts_"+ENV["KEEN_PROJECT_ID"]+"_"+timeframe+".csv", "wb") do |csv|
csv << ["project id", "collection", "count"]
end
collections.each do |data|
CSV.open("event_counts_"+ENV["KEEN_PROJECT_ID"]+"_"+timeframe+".csv", "a+") do |csv|
csv << [ENV["KEEN_PROJECT_ID"], data[0], data[1]]
end
end
puts "total events,#{total_events}"