Module: Statistics
- Defined in:
- lib/sinatra-statistics.rb
Constant Summary collapse
- TYPE_DAY =
"day"- TYPE_MONTH =
"month"- VERSION =
"1.0.0"- SAVE_DAYS =
32
Class Method Summary collapse
- .clear_historic_data(the_time) ⇒ Object
- .execute(request) ⇒ Object
- .get_day_id(the_time = nil) ⇒ Object
- .get_month_id(the_time = nil) ⇒ Object
- .get_time_str(the_time = nil) ⇒ Object
- .get_today_and_this_month(the_time) ⇒ Object
- .new_this_month_data(cur_time) ⇒ Object
- .new_today_data(cur_time) ⇒ Object
- .set_db_file(db_file_path) ⇒ Object
- .set_filter_pass_list(pass_list) ⇒ Object
Class Method Details
.clear_historic_data(the_time) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/sinatra-statistics.rb', line 59 def self.clear_historic_data(the_time) pre_day_time = the_time - SAVE_DAYS * 86400 pre_day_id = get_day_id(pre_day_time) all_data = $statistics_db.get_data() all_data.reject! do |record| record["type"] == TYPE_DAY and record["id"] < pre_day_id end $statistics_db.set_data(all_data) end |
.execute(request) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/sinatra-statistics.rb', line 88 def self.execute(request) puts request.path_info if not request.path_info.start_with?("/statistics") and not $pass_list.include?(request.path_info) cur_time = Time.now() today, this_month = get_today_and_this_month(cur_time) today["count"] += 1 today["updated_time"] = get_time_str(cur_time) if today["ip"].include?(request.ip) today["ip"][request.ip] += 1 else today["ip"][request.ip] = 1 end this_month["count"] += 1 if this_month["ip"].include?(request.ip) this_month["ip"][request.ip] += 1 else this_month["ip"][request.ip] = 1 end this_month["updated_time"] = today["updated_time"] $statistics_db.add_or_update_by_key(today, "id") $statistics_db.add_or_update_by_key(this_month, "id") clear_historic_data(cur_time) $statistics_db.save() end end |
.get_day_id(the_time = nil) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/sinatra-statistics.rb', line 41 def self.get_day_id(the_time=nil) if the_time == nil the_time = Time.now() end the_time.strftime('%Y%m%d') end |
.get_month_id(the_time = nil) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/sinatra-statistics.rb', line 47 def self.get_month_id(the_time=nil) if the_time == nil the_time = Time.now() end the_time.strftime('%Y%m') end |
.get_time_str(the_time = nil) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/sinatra-statistics.rb', line 53 def self.get_time_str(the_time=nil) if the_time == nil the_time = Time.now() end the_time.strftime('%Y-%m-%d %H:%M:%S.%6N') end |
.get_today_and_this_month(the_time) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/sinatra-statistics.rb', line 68 def self.get_today_and_this_month(the_time) if $today == nil and $this_month == nil $today = $statistics_db.find("id",get_day_id(the_time)) if $today == nil $today = new_today_data(the_time) end $this_month = $statistics_db.find("id",get_month_id(the_time)) if $this_month == nil $this_month = new_this_month_data(the_time) end end if $today["id"] != get_day_id(the_time) $today = new_today_data(the_time) end if $this_month["id"] != get_month_id(the_time) $this_month = new_this_month_data(the_time) end [$today, $this_month] end |
.new_this_month_data(cur_time) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/sinatra-statistics.rb', line 27 def self.new_this_month_data(cur_time) this_month = { "id" => get_month_id(cur_time), "type" => TYPE_MONTH, "version" => VERSION, "count" => 0, "ip" => {}, "updated_time" => get_time_str(cur_time) } end |
.new_today_data(cur_time) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/sinatra-statistics.rb', line 17 def self.new_today_data(cur_time) today = { "id" => get_day_id(cur_time), "type" => TYPE_DAY, "version" => VERSION, "count" => 0, "ip" => {}, "updated_time" => get_time_str(cur_time) } end |
.set_db_file(db_file_path) ⇒ Object
14 15 16 |
# File 'lib/sinatra-statistics.rb', line 14 def self.set_db_file(db_file_path) $statistics_db = KeyValueDB.new(db_file_path, 999999) end |
.set_filter_pass_list(pass_list) ⇒ Object
37 38 39 |
# File 'lib/sinatra-statistics.rb', line 37 def self.set_filter_pass_list(pass_list) $pass_list = pass_list end |