Class: Analytics::FetchMainGatherData

Inherits:
Object
  • Object
show all
Defined in:
lib/analytics.rb

Overview

Fetch main visitor data

Instance Method Summary collapse

Constructor Details

#initialize(site_id) ⇒ FetchMainGatherData

Returns a new instance of FetchMainGatherData.



79
80
81
82
83
# File 'lib/analytics.rb', line 79

def initialize(site_id)
    @site_id = site_id
    @client = Analytics.client
    @stmt = Analytics.stmt
end

Instance Method Details

#gatherDayVisitorObject

gather visitor day data



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/analytics.rb', line 85

def gatherDayVisitor
    now,yesterday_start,yesterday_end = Util.day_query_time
    pv = @client.query("select count(id) from visitors where site_id=#{@site_id} and created_at > #{yesterday_start} and created_at < #{yesterday_end}").fetch_row[0]
    ipv = @client.query("select count(distinct ip_id) from visitors where site_id=#{@site_id} and created_at > #{yesterday_start} and created_at < #{yesterday_end}").fetch_row[0]
    r = @client.query("select count(id) from site_day_gather where site_id=#{@site_id} and day_time=#{yesterday_start}").fetch_row[0].to_i
    if(r == 0)
        @stmt.prepare("insert into site_day_gather(site_id,pv,ipv,day_time,created_at) "+
                        " values(?,?,?,?,?)" )
        @stmt.execute(@site_id,pv,ipv,yesterday_start,now)
    else
        @stmt.prepare("update site_day_gather set pv=?,ipv=?,created_at=? where site_id=? and day_time=?")
        @stmt.execute(pv,ipv,now,@site_id,yesterday_start)
    end
end

#gatherDicDayVisitor(gather_table, col) ⇒ Object

gather dictionary day visitor data



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/analytics.rb', line 116

def gatherDicDayVisitor gather_table,col

    #get query timestamp
    now,yesterday_start,yesterday_end = Util.day_query_time

    @client.query("select count(id),#{col} from visitors where site_id=#{@site_id} and created_at > #{yesterday_start} and created_at < #{yesterday_end} group by #{col}").each{|row|


    r = @client.query("select count(id) from #{gather_table} where site_id=#{@site_id} and day_time=#{yesterday_start} and #{col}=#{row[1]}").fetch_row[0].to_i
    if(r == 0 )
        @stmt.prepare("insert into #{gather_table}(site_id,pv,day_time,created_at,#{col}) "+
                      " values(?,?,?,?,?)" )
        @stmt.execute(@site_id,row[0],yesterday_start,now,row[1])
    else
        @stmt.prepare("update #{gather_table} set pv=?,created_at=? where site_id=? and day_time=? and #{col}=?")
        @stmt.execute(row[0],now,@site_id,yesterday_start,row[1])
    end
    }
end

#gatherHourVisitorObject

gather visitor hour data



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/analytics.rb', line 100

def gatherHourVisitor
    now,last_hour_start,last_hour_end = Util.hour_query_time
    pv = @client.query("select count(id) from visitors where site_id=#{@site_id} and created_at >= #{last_hour_start} and created_at <= #{last_hour_end}").fetch_row[0]
    ipv = @client.query("select count(distinct ip_id) from visitors where site_id=#{@site_id} and created_at >= #{last_hour_start} and created_at <= #{last_hour_end}").fetch_row[0]

    r = @client.query("select count(id) from site_hour_gather where site_id=#{@site_id} and hour_time=#{last_hour_start}").fetch_row[0].to_i
    if(r == 0)
        @stmt.prepare("insert into site_hour_gather(site_id,pv,ipv,hour_time,created_at) "+
                        " values(?,?,?,?,?)" )
        @stmt.execute(@site_id,pv,ipv,last_hour_start,now)
    else
        @stmt.prepare("update site_hour_gather set pv=?,ipv=?,created_at=? where site_id=? and hour_time=?")
        @stmt.execute(pv,ipv,now,@site_id,last_hour_start)
    end
end