analytics 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -21,7 +21,7 @@ Jeweler::Tasks.new do |gem|
21
21
  gem.authors = ["Jun Tsai"]
22
22
  # Include your dependencies below. Runtime dependencies are required when using your gem,
23
23
  # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
- gem.add_runtime_dependency 'mysql2', '>= 0.2.6'
24
+ gem.add_runtime_dependency 'mysql', '= 2.8.1'
25
25
  # gem.add_runtime_dependency 'jabber4r', '> 0.1'
26
26
  # gem.add_development_dependency 'rspec', '> 1.2.3'
27
27
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.1.5
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{analytics}
8
- s.version = "0.1.4"
8
+ s.version = "0.1.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jun Tsai"]
@@ -26,6 +26,7 @@ Gem::Specification.new do |s|
26
26
  "VERSION",
27
27
  "analytics.gemspec",
28
28
  "lib/analytics.rb",
29
+ "lib/analytics_test.rb",
29
30
  "test/helper.rb",
30
31
  "test/test_analytics.rb"
31
32
  ]
@@ -48,20 +49,20 @@ Gem::Specification.new do |s|
48
49
  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
49
50
  s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
50
51
  s.add_development_dependency(%q<rcov>, [">= 0"])
51
- s.add_runtime_dependency(%q<mysql2>, [">= 0.2.6"])
52
+ s.add_runtime_dependency(%q<mysql>, ["= 2.8.1"])
52
53
  else
53
54
  s.add_dependency(%q<shoulda>, [">= 0"])
54
55
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
55
56
  s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
56
57
  s.add_dependency(%q<rcov>, [">= 0"])
57
- s.add_dependency(%q<mysql2>, [">= 0.2.6"])
58
+ s.add_dependency(%q<mysql>, ["= 2.8.1"])
58
59
  end
59
60
  else
60
61
  s.add_dependency(%q<shoulda>, [">= 0"])
61
62
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
62
63
  s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
63
64
  s.add_dependency(%q<rcov>, [">= 0"])
64
- s.add_dependency(%q<mysql2>, [">= 0.2.6"])
65
+ s.add_dependency(%q<mysql>, ["= 2.8.1"])
65
66
  end
66
67
  end
67
68
 
@@ -1,30 +1,44 @@
1
1
  # zxth analytics system
2
- require 'mysql2'
2
+ require 'mysql'
3
3
 
4
4
  module Analytics
5
5
  #connect to mysql
6
6
  DB='analytics'
7
- CLIENT= Mysql2::Client.new(:host => "localhost", :username => "root",
8
- :socket => '/tmp/mysql.sock',:encoding => 'utf8',
9
- :database => DB)
7
+ @client = nil
8
+ @stmt = nil
9
+ def self.init
10
+ @client=Mysql.new("localhost", "root",nil,DB,nil,'/tmp/mysql.sock')
11
+ @client.autocommit(true)
12
+ @stmt = @client.stmt_init
13
+ end
14
+ def self.close
15
+ @stmt.close
16
+ @client.close
17
+ end
18
+ def self.client
19
+ @client
20
+ end
21
+ def self.stmt
22
+ @stmt
23
+ end
10
24
  # gather day visitor data
11
25
  def self.gather_day_visitor
12
- Util.gather_data{|site_id,client|
13
- gatherer = FetchMainGatherData.new(site_id,client)
26
+ Util.gather_data{|site_id|
27
+ gatherer = FetchMainGatherData.new(site_id)
14
28
  gatherer.gatherDayVisitor
15
29
  }
16
30
  end
17
31
  # gather hour visitor data
18
32
  def self.gather_hour_visitor
19
- Util.gather_data{|site_id,client|
20
- gatherer = FetchMainGatherData.new(site_id,client)
33
+ Util.gather_data{|site_id|
34
+ gatherer = FetchMainGatherData.new(site_id)
21
35
  gatherer.gatherHourVisitor
22
36
  }
23
37
  end
24
38
  #gather visitor data using dictionary group
25
39
  def self.gather_dic_visitor gather_table,column_name
26
- Util.gather_data{|site_id,client|
27
- gatherer = FetchMainGatherData.new(site_id,client)
40
+ Util.gather_data{|site_id|
41
+ gatherer = FetchMainGatherData.new(site_id)
28
42
  gatherer.gatherDicDayVisitor gather_table,column_name
29
43
  }
30
44
  end
@@ -32,11 +46,11 @@ module Analytics
32
46
  def self.gather_data
33
47
  #Fetch all sites
34
48
  sites = []
35
- CLIENT.query("select id from sites",:as=> :array).each{|r| sites << r[0]}
49
+ Analytics.client.query("select id from sites").each{|r| sites << r[0]}
36
50
  #Gather day visitor data
37
51
  sites.each{|site_id|
38
52
  begin
39
- yield(site_id,CLIENT)
53
+ yield(site_id)
40
54
  rescue => err
41
55
  puts err
42
56
  # TODO 自动发送错误信息
@@ -62,33 +76,41 @@ module Analytics
62
76
  end
63
77
  #Fetch main visitor data
64
78
  class FetchMainGatherData
65
- def initialize(site_id,client)
79
+ def initialize(site_id)
66
80
  @site_id = site_id
67
- @client = client
81
+ @client = Analytics.client
82
+ @stmt = Analytics.stmt
68
83
  end
69
84
  # gather visitor day data
70
85
  def gatherDayVisitor
71
86
  now,yesterday_start,yesterday_end = Util.day_query_time
72
- pv = @client.query("select count(id) from visitors where site_id=#{@site_id} and created_at > #{yesterday_start} and created_at < #{yesterday_end}",:as=>:array).first.first
73
- 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}").first.first
74
- @client.query("insert into site_day_gather(site_id,pv,ipv,day_time,created_at)
75
- values( #{@site_id},
76
- #{pv},
77
- #{ipv},
78
- #{yesterday_start},
79
- #{now})")
87
+ 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]
88
+ 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]
89
+ r = @client.query("select count(id) from site_day_gather where site_id=#{@site_id} and day_time=#{yesterday_start}").fetch_row[0]
90
+ if(r == 0)
91
+ @stmt.prepare("insert into site_day_gather(site_id,pv,ipv,day_time,created_at) "+
92
+ " values(?,?,?,?,?)" )
93
+ @stmt.execute(@site_id,pv,ipv,yesterday_start,now)
94
+ else
95
+ @stmt.prepare("update site_day_gather set pv=?,ipv=?,created_at=? where site_id=? and day_time=?")
96
+ @stmt.execute(pv,ipv,now,@site_id,yesterday_start)
97
+ end
80
98
  end
81
99
  # gather visitor hour data
82
100
  def gatherHourVisitor
83
101
  now,last_hour_start,last_hour_end = Util.hour_query_time
84
- 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}",:as=>:array).first.first
85
- 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}").first.first
86
- @client.query("insert into site_hour_gather(site_id,pv,ipv,hour_time,created_at) " +
87
- "values( #{@site_id},
88
- #{pv},
89
- #{ipv},
90
- #{last_hour_start},
91
- #{now})")
102
+ 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]
103
+ 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]
104
+
105
+ r = @client.query("select count(id) from site_hour_gather where site_id=#{@site_id} and hour_time=#{last_hour_start}").fetch_row[0]
106
+ if(r == 0)
107
+ @stmt.prepare("insert into site_hour_gather(site_id,pv,ipv,hour_time,created_at) "+
108
+ " values(?,?,?,?,?)" )
109
+ @stmt.execute(@site_id,pv,ipv,last_hour_start,now)
110
+ else
111
+ @stmt.prepare("update site_hour_gather set pv=?,ipv=?,created_at=? where site_id=? and hour_time=?")
112
+ @stmt.execute(pv,ipv,now,@site_id,last_hour_start)
113
+ end
92
114
  end
93
115
  # gather dictionary day visitor data
94
116
  def gatherDicDayVisitor gather_table,col
@@ -96,14 +118,18 @@ module Analytics
96
118
  #get query timestamp
97
119
  now,yesterday_start,yesterday_end = Util.day_query_time
98
120
 
99
- @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}",:as=>:array).each{|row|
121
+ @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|
122
+
100
123
 
101
- @client.query("insert into #{gather_table}(site_id,pv,#{col},day_time,created_at)
102
- values( #{@site_id},
103
- #{row[0]},
104
- #{row[1]},
105
- #{yesterday_start},
106
- #{now})")
124
+ r = @client.query("select count(id) from #{gather_table} where site_id=#{@site_id} and day_time=#{yesterday_start}").fetch_row[0]
125
+ if(r == 0 )
126
+ @stmt.prepare("insert into #{gather_table}(site_id,pv,day_time,created_at) "+
127
+ " values(,?,?,?,?)" )
128
+ @stmt.execute(@site_id,pv,yesterday_start,now)
129
+ else
130
+ @stmt.prepare("update #{gather_table} set pv=?,created_at=? where site_id=? and day_time=?")
131
+ @stmt.execute(pv,now,@site_id,yesterday_start)
132
+ end
107
133
  }
108
134
  end
109
135
  end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'analytics'
3
+ Analytics.init
4
+ 0..100.times {|i|
5
+ Analytics.gather_hour_visitor
6
+ Analytics.gather_day_visitor
7
+ Analytics.gather_dic_visitor "site_day_browser_gather","browser_id"
8
+ }
9
+ Analytics.close
@@ -1,4 +1,4 @@
1
- require 'helper'
1
+ require 'test/unit'
2
2
 
3
3
  class TestAnalytics < Test::Unit::TestCase
4
4
  should "not null" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: analytics
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 4
10
- version: 0.1.4
9
+ - 5
10
+ version: 0.1.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jun Tsai
@@ -82,16 +82,16 @@ dependencies:
82
82
  requirement: &id005 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
- - - ">="
85
+ - - "="
86
86
  - !ruby/object:Gem::Version
87
- hash: 27
87
+ hash: 45
88
88
  segments:
89
- - 0
90
89
  - 2
91
- - 6
92
- version: 0.2.6
90
+ - 8
91
+ - 1
92
+ version: 2.8.1
93
93
  type: :runtime
94
- name: mysql2
94
+ name: mysql
95
95
  prerelease: false
96
96
  version_requirements: *id005
97
97
  description: Analytics System
@@ -113,6 +113,7 @@ files:
113
113
  - VERSION
114
114
  - analytics.gemspec
115
115
  - lib/analytics.rb
116
+ - lib/analytics_test.rb
116
117
  - test/helper.rb
117
118
  - test/test_analytics.rb
118
119
  has_rdoc: true