module Statistics

Constants

SAVE_DAYS
TYPE_DAY
TYPE_MONTH
VERSION

Public Class Methods

clear_historic_data(the_time) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/sinatra-statistics.rb, line 37
def self.set_filter_pass_list(pass_list)
    $pass_list = pass_list
end