sinatra-statistics 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c27c3dd6f004e2ea0a9f4a82ba659b57ebdd1877
4
+ data.tar.gz: 534e1ab384f02014259a1a5d1c37f4c08451497f
5
+ SHA512:
6
+ metadata.gz: 49c88ec6cc9d671aba71fa87dbd94f402139b73bd5b6753e2067c9e8fb6d71c54dfe53a02531d5958b122b43a08fe9c99d35c501a619c080d9f5878ae3d00304
7
+ data.tar.gz: 10da4320263eb9b2daa82bde58a1fa04b8974b0aa5125a96e13162df331225b9eed21b46daf35c77ff052f447cc327b0b53f2a6adb558f54b3f7dfe7177bcdfc
data/lib/json_db.rb ADDED
@@ -0,0 +1,63 @@
1
+ require "json"
2
+
3
+ class JsonDB
4
+ def initialize(file_path)
5
+ @data_file_path = file_path
6
+ if not File.exists?(file_path)
7
+ @data_hash_arr = []
8
+ save()
9
+ puts("create empty list file #{file_path}")
10
+ end
11
+ @data_hash_arr = JsonDB.load(@data_file_path)
12
+ end
13
+ def self.load(file_path)
14
+ File.open(file_path) do |the_file|
15
+ JSON.parse(the_file.read())["data"]
16
+ end
17
+ end
18
+ def reload()
19
+ @data_hash_arr = JsonDB.load(@data_file_path)
20
+ end
21
+ def get_data()
22
+ @data_hash_arr
23
+ end
24
+ def set_data(data_hash_arr)
25
+ @data_hash_arr = data_hash_arr
26
+ end
27
+ def save()
28
+ the_data = {"data"=>@data_hash_arr}
29
+ File.open(@data_file_path,"w") do |the_file|
30
+ the_file.write(JSON.pretty_generate(the_data))
31
+ end
32
+ end
33
+ end
34
+
35
+
36
+ if __FILE__ == $0
37
+ require 'minitest/autorun'
38
+ require 'minitest/spec'
39
+ require 'testhelper'
40
+
41
+ cur_dir = File.dirname(__FILE__)
42
+ data_file_path = File.join(cur_dir,"../history_db.json")
43
+ the_db = JsonDB.new(data_file_path)
44
+ describe "JsonDB" do
45
+ it "load" do
46
+ the_db.get_data.ppt()
47
+ # require 'pry';binding.pry
48
+ end
49
+ # it "add" do
50
+ # the_db.add({"comment"=>"test"})
51
+ # the_db.save()
52
+ # # require 'pry';binding.pry
53
+ # end
54
+ # it "delete" do
55
+ # the_db.delete_and_save(5)
56
+ # end
57
+ # it "edit" do
58
+ # the_db.edit_and_save({"comment"=>"mmm", "id"=>5})
59
+ # the_db.get_data.ppt()
60
+ # end
61
+
62
+ end
63
+ end
@@ -0,0 +1,80 @@
1
+ require_relative 'json_db'
2
+
3
+
4
+ class KeyValueDB < JsonDB
5
+ def initialize(file_path, max_count=999999)
6
+ super(file_path)
7
+ @max_count = max_count
8
+ end
9
+ def set_max_count(max_count)
10
+ @max_count = max_count
11
+ end
12
+ def add(the_data)
13
+ if @data_hash_arr.size >= @max_count
14
+ @data_hash_arr.shift
15
+ end
16
+ new_record = the_data
17
+ @data_hash_arr << new_record
18
+ new_record
19
+ end
20
+ def add_or_update_by_key(the_data, the_key)
21
+ found_index = -1
22
+ @data_hash_arr.each_with_index do |cur_data, i|
23
+ if cur_data[the_key]== the_data[the_key]
24
+ found_index = i
25
+ break
26
+ end
27
+ end
28
+ if found_index > -1
29
+ @data_hash_arr[found_index] = the_data
30
+ else
31
+ add(the_data)
32
+ end
33
+ the_data
34
+ end
35
+ def find(the_key, the_value)
36
+ @data_hash_arr.each do |cur_data|
37
+ if cur_data[the_key] == the_value
38
+ return cur_data
39
+ end
40
+ end
41
+ return nil
42
+ end
43
+ def find_all(the_key, the_value)
44
+ @data_hash_arr.select do |cur_data|
45
+ cur_data[the_key] == the_value
46
+ end
47
+ end
48
+ end
49
+
50
+ if __FILE__ == $0
51
+ require 'minitest/autorun'
52
+ require 'minitest/spec'
53
+ require 'testhelper'
54
+
55
+
56
+ cur_dir = File.dirname(__FILE__)
57
+ data_file_path = File.join(cur_dir,"../data/confirmation_db.json")
58
+ cc_db = KeyValueDB.new(data_file_path)
59
+
60
+ describe "KeyValueDB" do
61
+ it "find" do
62
+ cc_db.find("survey_id","127.0.0.1-20170901120433934271").pt()
63
+ end
64
+ end
65
+ # describe "Hash" do
66
+ # it "get_by_keys" do
67
+ # a = {"id"=>123,
68
+ # "config" => {
69
+ # "worker_id"=>345,
70
+ # "other"=>111
71
+ # }
72
+
73
+ # }
74
+ # a.get_by_keys(["config", "worker_id"]).pt()
75
+ # a.get_by_keys(["config", "mmm"]).pt()
76
+
77
+ # end
78
+ # end
79
+
80
+ end
data/lib/statistics.rb ADDED
@@ -0,0 +1,106 @@
1
+ require 'Date'
2
+ require_relative 'key_value_db'
3
+
4
+ module Statistics
5
+ $statistics_db = nil
6
+ $pass_list = []
7
+ VERSION = "0.0.1"
8
+ SAVE_DAYS = 7
9
+ def self.set_db_file(db_file_path)
10
+ $statistics_db = KeyValueDB.new(db_file_path, 999999)
11
+ $today = $statistics_db.find("id",get_day_id())
12
+ if $today == nil
13
+ $today = {
14
+ "id" => get_day_id(),
15
+ "type" => "day",
16
+ "version" => VERSION,
17
+ "count" => 0,
18
+ "ip" => {},
19
+ "updated_time" => get_time_str()
20
+ }
21
+ end
22
+ $cur_month = $statistics_db.find("id",get_month_id())
23
+ if $cur_month == nil
24
+ $cur_month = {
25
+ "id" => get_month_id(),
26
+ "type" => "month",
27
+ "version" => VERSION,
28
+ "count" => 0,
29
+ "ip" => {},
30
+ "updated_time" => get_time_str()
31
+ }
32
+ end
33
+ end
34
+ def self.set_filter_pass_list(pass_list)
35
+ $pass_list = pass_list
36
+ end
37
+
38
+ def self.get_day_id(the_time=nil)
39
+ if the_time == nil
40
+ the_time = Time.now()
41
+ end
42
+ the_time.strftime('%Y%m%d')
43
+ end
44
+ def self.get_month_id(the_time=nil)
45
+ if the_time == nil
46
+ the_time = Time.now()
47
+ end
48
+ the_time.strftime('%Y%m')
49
+ end
50
+ def self.get_time_str(the_time=nil)
51
+ if the_time == nil
52
+ the_time = Time.now()
53
+ end
54
+ the_time.strftime('%Y-%m-%d %H:%M:%S.%6N')
55
+ end
56
+ def self.clear_historic_data(the_time)
57
+ pre_day_time = the_time - SAVE_DAYS * 86400
58
+ pre_day_id = get_day_id(pre_day_time)
59
+ all_data = $statistics_db.get_data()
60
+ all_data.reject! do |record|
61
+ record["type"] == "day" and record["id"] < pre_day_id
62
+ end
63
+ $statistics_db.set_data(all_data)
64
+ end
65
+
66
+ def self.execute(request)
67
+ puts request.path_info
68
+ if not request.path_info.start_with?("/statistics") and
69
+ not $pass_list.include?(request.path_info)
70
+ cur_time = Time.now()
71
+
72
+ $today["count"] += 1
73
+ $today["updated_time"] = get_time_str(cur_time)
74
+ if $today["ip"].include?(request.ip)
75
+ $today["ip"][request.ip] += 1
76
+ else
77
+ $today["ip"][request.ip] = 1
78
+ end
79
+
80
+ $cur_month["count"] += 1
81
+ if $cur_month["ip"].include?(request.ip)
82
+ $cur_month["ip"][request.ip] += 1
83
+ else
84
+ $cur_month["ip"][request.ip] = 1
85
+ end
86
+ $cur_month["updated_time"] = $today["updated_time"]
87
+
88
+ $statistics_db.add_or_update_by_key($today, "id")
89
+ $statistics_db.add_or_update_by_key($cur_month, "id")
90
+ clear_historic_data(cur_time)
91
+ $statistics_db.save()
92
+ end
93
+ end
94
+ end
95
+ get "/extension/statistics/:month_count" do |month_count|
96
+ month_count = month_count.to_i
97
+ cur_month = Date.today
98
+ (1).upto(month_count).each {|x| cur_month=cur_month.prev_month}
99
+ prev_month_str = cur_month.to_s.gsub("-","")[0..5]
100
+ all_data = $statistics_db.get_data()
101
+ new_data = all_data.select do |record|
102
+ # [record["id"], prev_month_str, (record["id"] >= prev_month_str)].ppt
103
+ record["id"] >= prev_month_str
104
+ end
105
+ new_data.to_json
106
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-statistics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Colin Ji
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-10-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: a statistics extension for sinatra.
14
+ email: jichen3000@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/json_db.rb
20
+ - lib/key_value_db.rb
21
+ - lib/statistics.rb
22
+ homepage:
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 2.0.0
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.5.1
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: a statistics extension for sinatra.
46
+ test_files: []