get_stats 0.2 → 0.3

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.
@@ -12,129 +12,90 @@ class Stats < ActiveRecord::Base
12
12
  end
13
13
  end
14
14
 
15
- def self.show(field,time_range="week",report_start_date=nil,report_end_date=nil)
16
- end_date = Date.today
17
- case time_range
18
- when 'week'
19
- start_date = end_date - 1.week
20
- when 'month'
21
- start_date = end_date - 1.month
22
- else
23
- start_date = end_date - 1.month
24
- end
25
- if !report_start_date.nil? && !report_end_date.nil?
26
- start_date = report_start_date.to_date
27
- end_date = report_end_date.to_date
28
- end
29
- @stats = Stats.find(:all,:conditions =>["stat_name=? and stat_date >=? and stat_date <=?",field,start_date,end_date])
15
+ def self.process_daily_stats(field, stats, start_date,end_date)
30
16
  @return_stat = []
31
- now = start_date
32
- while (now <= end_date) do
33
- today_stats = @stats.select{|c| c if c.stat_date == now }
17
+ while (start_date <= end_date) do
18
+ today_stats = @stats.select{|c| c if c.stat_date == start_date }
34
19
  if today_stats.blank?
35
- @return_stat << [field,now,0]
20
+ @return_stat << [field,start_date,0]
36
21
  else
37
- @return_stat << [field,today_stats[0].stat_date,today_stats[0].count]
22
+ @return_stat << [field,today_stats[0].stat_date,today_stats[0].count]
38
23
  end
39
- now = now + 1.day
24
+ start_date = start_date + 1
40
25
  end
41
26
  return @return_stat
42
- end
27
+ end
43
28
 
44
- def self.daily_stats(field,time_range="last_week",report_start_date=nil,report_end_date=nil)
29
+ def self.find_date_range(time_range)
45
30
  end_date = Date.today
46
31
  case time_range
47
32
  when 'week'
48
33
  start_date = end_date - 1.week
49
- when 'last_week'
50
- start_date = end_date - 1.week
51
34
  when 'month'
52
35
  start_date = end_date - 1.month
53
- when 'last_month'
54
- start_date = end_date - 1.month
55
36
  else
56
- start_date = end_date - 1.month
37
+ start_date = end_date - 1.week
57
38
  end
39
+ return start_date, end_date
40
+ end
41
+
42
+ def self.show(field,time_range="week",report_start_date=nil,report_end_date=nil)
43
+ start_date,end_date = find_date_range(time_range)
58
44
  if !report_start_date.nil? && !report_end_date.nil?
59
45
  start_date = report_start_date.to_date
60
46
  end_date = report_end_date.to_date
61
47
  end
62
48
  @stats = Stats.find(:all,:conditions =>["stat_name=? and stat_date >=? and stat_date <=?",field,start_date,end_date])
63
- @return_stat = []
64
- now = start_date
65
- while (now <= end_date) do
66
- today_stats = @stats.select{|c| c if c.stat_date == now }
67
- if today_stats.blank?
68
- @return_stat << [field,now,0]
69
- else
70
- @return_stat << [field,today_stats[0].stat_date,today_stats[0].count]
71
- end
72
- now = now + 1.day
73
- end
74
- return @return_stat
49
+ process_daily_stats(field, @stats,start_date,end_date)
75
50
  end
76
51
 
77
- def self.weekly_stats(field,report_start_date=nil,report_end_date=nil)
78
- if report_end_date.nil?
79
- report_end_date = Date.today
80
- end
81
-
82
- if report_start_date.nil? || report_start_date >= report_end_date
83
- report_start_date = report_end_date - 1.month
84
- end
85
- report_start_date = report_start_date - report_start_date.wday + 1
86
- @stats = Stats.find(:all,:conditions =>["stat_name=? and stat_date >=? and stat_date <=?",field,report_start_date,report_end_date])
87
- now = report_start_date
88
- @return_stat = []
89
- week_collection = 0
90
- while (now <= report_end_date) do
91
- today_stats = @stats.select{|c| c if c.stat_date == now }
92
- if !today_stats.blank?
93
- week_collection = week_collection + today_stats[0].count
94
- end
95
- if now.monday? && now != report_start_date
96
- @return_stat << [field,now - 1.week ,week_collection]
97
- week_collection = 0
98
- end
52
+ class << self
53
+ alias :daily_stats :show
54
+ end
99
55
 
100
- if now == report_end_date && !now.monday?
101
- @return_stat << [field,now - now.wday + 1,week_collection]
102
- end
103
- now = now + 1.day
104
- end
105
- return @return_stat
56
+ ["monthly","weekly"].each do |name|
57
+ class_eval %{ def self.#{name}_stats(field,report_start_date=nil,report_end_date=nil)
58
+ start_date,end_date = process_dates("#{name}",report_start_date,report_end_date)
59
+ process_weekly_and_monthly_stats("#{name}",field,start_date,end_date)
60
+ end
61
+ }
106
62
  end
107
-
108
- def self.monthly_stats(field,report_start_date=nil,report_end_date=nil)
109
- if report_end_date.nil?
110
- report_end_date = Date.today
111
- end
112
-
113
- if report_start_date.nil? || report_start_date >= report_end_date
114
- report_start_date = report_end_date - 4.months
115
- end
116
- report_start_date = report_start_date - report_start_date.mday + 1
117
- @stats = Stats.find(:all,:conditions =>["stat_name=? and stat_date >=? and stat_date <=?",field,report_start_date,report_end_date])
118
- now = report_start_date
63
+
64
+
65
+ def self.process_weekly_and_monthly_stats(method_type,field,start_date,end_date)
66
+ @stats = Stats.find(:all,:conditions =>["stat_name=? and stat_date >=? and stat_date <=?",field,start_date,end_date])
119
67
  @return_stat = []
120
- month_collection = 0
121
- while (now <= report_end_date) do
122
- today_stats = @stats.select{|c| c if c.stat_date == now }
123
- if !today_stats.blank?
124
- month_collection = month_collection + today_stats[0].count
68
+ stat_collection = 0
69
+ original_start_date = start_date
70
+ while (start_date <= end_date) do
71
+ today_stats = @stats.select{|c| c if c.stat_date == start_date }
72
+ stat_collection = stat_collection + today_stats[0].count if !today_stats.blank?
73
+ conitions_day = (method_type == "weekly" ? start_date.monday? : (start_date.mday == 1) )
74
+ if conitions_day && start_date != original_start_date
75
+ @return_stat << [field,(method_type == "weekly" ? start_date - 1.week : start_date - 1.month ),stat_collection]
76
+ stat_collection = 0
125
77
  end
126
- if now.mday == 1 && now != report_start_date
127
- @return_stat << [field,now - 1.month ,month_collection]
128
- month_collection = 0
129
- end
130
- if now + 1.day == report_end_date && (now + 1.day).mday != 1
131
- @return_stat << [field,now ,month_collection]
78
+ if start_date + 1.day == end_date && !conitions_day
79
+ @return_stat << [field,start_date,stat_collection]
132
80
  end
133
- now = now + 1.day
81
+ start_date = start_date + 1.day
134
82
  end
135
83
  return @return_stat
136
- end
137
-
84
+ end
85
+
86
+ def self.process_dates(method_type,start_date,end_date)
87
+ end_date = Date.today if end_date.nil?
88
+ if start_date.nil? || start_date >= end_date
89
+ start_date = end_date - 4.months if method_type == "monthly"
90
+ start_date = end_date - 1.month if method_type == "weekly"
91
+ end
92
+ if method_type == "weekly"
93
+ start_date = start_date - start_date.wday + 1
94
+ else
95
+ start_date = start_date - start_date.mday + 1
96
+ end
97
+ return start_date, end_date
98
+ end
138
99
 
139
100
  def self.show_all_stat_names
140
101
  @stats = Stats.find(:all,:group => 'stat_name')
@@ -1,3 +1,3 @@
1
1
  module GetStats
2
- VERSION = "0.2"
2
+ VERSION = "0.3"
3
3
  end
@@ -1,9 +0,0 @@
1
- ActiveRecord::Schema.define(:version => 0) do
2
- create_table :stats, :force => true do |t|
3
- t.string :stat_name
4
- t.date :stat_date
5
- t.integer :count
6
- t.timestamps
7
- end
8
- add_index :stats, [:stat_date, :stat_name], :unique => true
9
- end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: get_stats
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: '0.3'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-27 00:00:00.000000000 Z
12
+ date: 2013-03-01 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Dead simply statistics for Rails.
15
15
  email: