microstat 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/microstat.rb +67 -0
- metadata +59 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d2ab16c20cef8f5d02fc9314b42ce2fedbe58074
|
4
|
+
data.tar.gz: ad0b50567cfd624312e8317e8addb85f870a3e2c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 09b750cf90742a1c991d0a476a2a0a1d468f4c789731c23d143c074d186205fafa4711485fb6b9c2139692462d1d4981304143dbc7e520b76dc1fa03f24c9f5b
|
7
|
+
data.tar.gz: c65837abdf2ace03099cc229525b3cae62c776f6564871981a877b663457654da265dfb35099b34b14361933083ad4eea0b14fd7fc7df2153d22575efe2d0d33
|
data/lib/microstat.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require "redis"
|
2
|
+
|
3
|
+
class Microstat
|
4
|
+
def initialize(redis: "redis://localhost:6379", namespace: 'mustat')
|
5
|
+
@redis = Redis.new(url: redis)
|
6
|
+
@key_space = "#{namespace}:events"
|
7
|
+
end
|
8
|
+
|
9
|
+
# Lists the known event types
|
10
|
+
def events
|
11
|
+
@redis.smembers @key_space
|
12
|
+
end
|
13
|
+
|
14
|
+
# Increments a specific event's count, returns the total
|
15
|
+
def track(event_name)
|
16
|
+
# Add the event to the known event space
|
17
|
+
@redis.sadd @key_space, event_name
|
18
|
+
|
19
|
+
# Add today as one of the tracked dates for this event
|
20
|
+
@redis.sadd "#{@key_space}:#{event_name}:dates", timestamp_today
|
21
|
+
|
22
|
+
# Increment event count for the current date
|
23
|
+
@redis.incr "#{@key_space}:#{event_name}:#{timestamp_today}"
|
24
|
+
|
25
|
+
# Increvent total event count, return it
|
26
|
+
@redis.incr "#{@key_space}:#{event_name}:total"
|
27
|
+
end
|
28
|
+
|
29
|
+
# Given an event type, return an array of dates where we have data
|
30
|
+
def dates(event_name)
|
31
|
+
@redis.smembers "#{@key_space}:#{event_name}:dates"
|
32
|
+
end
|
33
|
+
|
34
|
+
# Return today's count for an event
|
35
|
+
def today(event_name)
|
36
|
+
@redis.get "#{@key_space}:#{event_name}:#{timestamp_today}"
|
37
|
+
end
|
38
|
+
|
39
|
+
# Returns a count for an arbitrary date string in the yyyymmdd format
|
40
|
+
def date(event_name, date_string)
|
41
|
+
@redis.get "#{@key_space}:#{event_name}:#{date_string}"
|
42
|
+
end
|
43
|
+
|
44
|
+
# Returns a hash with date:count tuples for a given event
|
45
|
+
def history(event_name)
|
46
|
+
historical_counts = {}
|
47
|
+
|
48
|
+
for date in dates(event_name)
|
49
|
+
historical_counts[date] = date(event_name, date)
|
50
|
+
end
|
51
|
+
|
52
|
+
return historical_counts
|
53
|
+
end
|
54
|
+
|
55
|
+
# Return the total count for an event
|
56
|
+
def total(event_name)
|
57
|
+
@redis.get "#{@key_space}:#{event_name}:total"
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
# Returns the current date timestamp as a string for key matching
|
63
|
+
def timestamp_today
|
64
|
+
# Grab current timestamp and format as YYYYMMDD
|
65
|
+
Time.new.strftime("%Y%m%d")
|
66
|
+
end
|
67
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: microstat
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Fred Oliveira
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-11-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: redis
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.2'
|
27
|
+
description: Microstat is a library to capture events and compile statistics on those
|
28
|
+
events in an application. Like a micro Mixpanel.
|
29
|
+
email: fred@helloform.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- lib/microstat.rb
|
35
|
+
homepage: http://rubygems.org/gems/microstat
|
36
|
+
licenses:
|
37
|
+
- MIT
|
38
|
+
metadata: {}
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 2.6.7
|
56
|
+
signing_key:
|
57
|
+
specification_version: 4
|
58
|
+
summary: A tiny library for event statistics in apps.
|
59
|
+
test_files: []
|