analytics 0.1.0 → 0.1.1

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.
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.1'
24
+ gem.add_runtime_dependency 'mysql2', '>= 0.2.6'
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.0
1
+ 0.1.1
data/analytics.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{analytics}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
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"]
12
- s.date = %q{2011-02-20}
12
+ s.date = %q{2011-02-21}
13
13
  s.description = %q{Analytics System}
14
14
  s.email = %q{jcai@fepss.com}
15
15
  s.extra_rdoc_files = [
@@ -48,20 +48,20 @@ Gem::Specification.new do |s|
48
48
  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
49
49
  s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
50
50
  s.add_development_dependency(%q<rcov>, [">= 0"])
51
- s.add_runtime_dependency(%q<mysql2>, ["> 0.1"])
51
+ s.add_runtime_dependency(%q<mysql2>, [">= 0.2.6"])
52
52
  else
53
53
  s.add_dependency(%q<shoulda>, [">= 0"])
54
54
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
55
55
  s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
56
56
  s.add_dependency(%q<rcov>, [">= 0"])
57
- s.add_dependency(%q<mysql2>, ["> 0.1"])
57
+ s.add_dependency(%q<mysql2>, [">= 0.2.6"])
58
58
  end
59
59
  else
60
60
  s.add_dependency(%q<shoulda>, [">= 0"])
61
61
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
62
62
  s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
63
63
  s.add_dependency(%q<rcov>, [">= 0"])
64
- s.add_dependency(%q<mysql2>, ["> 0.1"])
64
+ s.add_dependency(%q<mysql2>, [">= 0.2.6"])
65
65
  end
66
66
  end
67
67
 
data/lib/analytics.rb CHANGED
@@ -1,5 +1,105 @@
1
+ # zxth analytics system
2
+ require 'mysql2'
3
+
1
4
  module Analytics
2
- def self.do
3
- "Hello World"
5
+ #connect to mysql
6
+ DB='analytics'
7
+ CLIENT= Mysql2::Client.new(:host => "localhost", :username => "root",
8
+ :socket => '/tmp/mysql.sock',:encoding => 'utf8',
9
+ :database => DB)
10
+ # gather day visitor data
11
+ def self.gather_day_visitor
12
+ Util.gather_data{|site_id,client|
13
+ gatherer = FetchMainGatherData.new(site_id,client)
14
+ gatherer.gatherDayVisitor
15
+ }
16
+ end
17
+ # gather hour visitor data
18
+ def self.gather_hour_visitor
19
+ Util.gather_data{|site_id,client|
20
+ gatherer = FetchMainGatherData.new(site_id,client)
21
+ gatherer.gatherHourVisitor
22
+ }
23
+ end
24
+ #gather visitor data using dictionary group
25
+ def self.gather_dic_visitor gather_table,column_name
26
+ Util.gather_data{|site_id,client|
27
+ gatherer = FetchMainGatherData.new(site_id,client)
28
+ gatherer.gatherDicDayVisitor gather_table,column_name
29
+ }
30
+ end
31
+ class Util
32
+ def self.gather_data
33
+ #Fetch all sites
34
+ sites = []
35
+ CLIENT.query("select id from sites",:as=> :array).each{|r| sites << r[0]}
36
+ #Gather day visitor data
37
+ sites.each{|site_id|
38
+ yield(site_id,CLIENT)
39
+ }
40
+ end
41
+ # get query time by hour method
42
+ def Util.hour_query_time
43
+ now = Time.new
44
+ last_hour_time = now-(60*60)
45
+ last_hour_start = Time.mktime(last_hour_time.year,last_hour_time.month,last_hour_time.day,last_hour_time.hour,0,0).to_i
46
+ last_hour_end = last_hour_start+(60*60)-1
47
+ return now.to_i*1000,last_hour_start*1000,last_hour_end*1000
48
+ end
49
+ # get query time by hour method
50
+ def Util.day_query_time
51
+ now = Time.new
52
+ last_day_time = now-(60*60*24)
53
+ last_day_start = Time.mktime(last_day_time.year,last_day_time.month,last_day_time.day,0,0,0).to_i
54
+ last_day_end = last_day_start+(60*60*24)-1
55
+ return now.to_i*1000,last_day_start*1000,last_day_end*1000
56
+ end
57
+ end
58
+ #Fetch main visitor data
59
+ class FetchMainGatherData
60
+ def initialize(site_id,client)
61
+ @site_id = site_id
62
+ @client = client
63
+ end
64
+ # gather visitor day data
65
+ def gatherDayVisitor
66
+ now,yesterday_start,yesterday_end = Util.day_query_time
67
+ 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
68
+ ipv = @client.query("select count(distinct ip) from visitors where site_id=#{@site_id} and created_at > #{yesterday_start} and created_at < #{yesterday_end}").first.first
69
+ @client.query("insert into site_day_gather(site_id,pv,ipv,day_time,created_at)
70
+ values( #{@site_id},
71
+ #{pv},
72
+ #{ipv},
73
+ #{yesterday_start},
74
+ #{now})")
75
+ end
76
+ # gather visitor hour data
77
+ def gatherHourVisitor
78
+ now,last_hour_start,last_hour_end = Util.hour_query_time
79
+ 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
80
+ ipv = @client.query("select count(distinct ip) from visitors where site_id=#{@site_id} and created_at >= #{last_hour_start} and created_at <= #{last_hour_end}").first.first
81
+ @client.query("insert into site_hour_gather(site_id,pv,ipv,hour_time,created_at) " +
82
+ "values( #{@site_id},
83
+ #{pv},
84
+ #{ipv},
85
+ #{last_hour_start},
86
+ #{now})")
87
+ end
88
+ # gather dictionary day visitor data
89
+ def gatherDicDayVisitor gather_table,col
90
+
91
+ #get query timestamp
92
+ now,yesterday_start,yesterday_end = Util.day_query_time
93
+
94
+ @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|
95
+
96
+ @client.query("insert into #{gather_table}(site_id,pv,#{col},day_time,created_at)
97
+ values( #{@site_id},
98
+ #{row[0]},
99
+ #{row[1]},
100
+ #{yesterday_start},
101
+ #{now})")
102
+ }
103
+ end
4
104
  end
5
105
  end
@@ -1,7 +1,15 @@
1
1
  require 'helper'
2
2
 
3
3
  class TestAnalytics < Test::Unit::TestCase
4
- should "probably rename this file and start testing for real" do
5
- flunk "hey buddy, you should probably rename this file and start testing for real"
4
+ should "not null" do
5
+ Analytics::Util.day_query_time
6
+ Analytics::Util.hour_query_time
7
+ #connect to mysql
8
+ DB='analytics'
9
+ client = Mysql2::Client.new(:host => "localhost", :username => "root",
10
+ :socket => '/tmp/mysql.sock',:encoding => 'utf8',:database => DB)
11
+ fetcher = Analytics::FetchMainGatherData.new(1,client)
12
+ fetcher.gatherDayVisitor
13
+ fetcher.gatherHourVisitor
6
14
  end
7
15
  end
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: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jun Tsai
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-20 00:00:00 +08:00
18
+ date: 2011-02-21 00:00:00 +08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -82,13 +82,14 @@ 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: 9
87
+ hash: 27
88
88
  segments:
89
89
  - 0
90
- - 1
91
- version: "0.1"
90
+ - 2
91
+ - 6
92
+ version: 0.2.6
92
93
  type: :runtime
93
94
  name: mysql2
94
95
  prerelease: false