appstats 0.1.0 → 0.3.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.
@@ -0,0 +1,71 @@
1
+
2
+ module Appstats
3
+ class Query
4
+
5
+ @@default = "1=1"
6
+ attr_accessor :input
7
+
8
+ def initialize(data = {})
9
+ @input = data[:input]
10
+ end
11
+
12
+ def run
13
+ ActiveRecord::Base.connection.select_one(to_sql)["count(*)"].to_i
14
+ end
15
+
16
+ def to_sql
17
+ sql = "select count(*) from appstats_entries"
18
+ return sql if @input.nil?
19
+ current_input = @input
20
+
21
+ m = current_input.match(/^\s*(\#)\s*([^\s]*)\s*(.*)/)
22
+ return sql if m.nil?
23
+ if m[1] == "#"
24
+ sql += " where action = '#{normalize_action_name(m[2])}'"
25
+ end
26
+ current_input = m[3]
27
+
28
+ m_on_server = current_input.match(/^(.*)?\s*on\s*server\s*(.*)$/)
29
+ date_range = m_on_server.nil? ? current_input : m_on_server[1]
30
+ if date_range.size > 0
31
+ range = DateRange.parse(date_range)
32
+ sql += " and #{range.to_sql}" unless range.to_sql == "1=1"
33
+ end
34
+ return sql if m_on_server.nil?
35
+
36
+ host_name = m_on_server[2]
37
+ sql += " and exists (select * from appstats_log_collectors where appstats_entries.appstats_log_collector_id = appstats_log_collectors.id and host = '#{host_name}')"
38
+
39
+ sql
40
+ end
41
+
42
+ def self.host_filter_to_sql(raw_input)
43
+ return @@default if raw_input.nil?
44
+ input = raw_input.strip
45
+ m = raw_input.strip.match(/(^[^\s']*$)/)
46
+ return @@default if m.nil?
47
+ host = m[1]
48
+ return @@default if host == '' or host.nil?
49
+ "EXISTS (select * from appstats_log_collectors where appstats_entries.appstats_log_collector_id = id and host = '#{host}' )"
50
+ end
51
+
52
+ def self.context_filter_to_sql(raw_input)
53
+ return @@default if raw_input.nil?
54
+ m = raw_input.match(/([^']+)=([^']+)/)
55
+ return @@default if m.nil?
56
+ key = m[1].strip
57
+ value = m[2].strip
58
+ return @@default if key == '' or key.nil?
59
+ "EXISTS (select * from appstats_contexts where appstats_entries.id = appstats_contexts.appstats_entry_id and context_key='#{key}' and context_value='#{value}' )"
60
+ end
61
+
62
+ private
63
+
64
+ def normalize_action_name(action_name)
65
+ action = Appstats::Action.where("plural_name = ?",action_name).first
66
+ action.nil? ? action_name : action.name
67
+ end
68
+
69
+
70
+ end
71
+ end
@@ -1,3 +1,3 @@
1
1
  module Appstats
2
- VERSION = "0.1.0"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -32,11 +32,13 @@ require File.join(File.dirname(__FILE__),"..","appstats")
32
32
 
33
33
  Appstats.log(:info,"Started Appstats Log Collector")
34
34
  while($running) do
35
+ ActiveRecord::Base.connection.reconnect!
35
36
  appstats_config["remote_servers"].each do |remote_server|
36
37
  Appstats::LogCollector.find_remote_files(remote_server,remote_server[:path],remote_server[:template])
37
38
  end
38
39
  Appstats::LogCollector.download_remote_files(appstats_config["remote_servers"])
39
40
  Appstats::LogCollector.process_local_files
41
+ ActiveRecord::Base.connection.disconnect!
40
42
  a_day_in_seconds = 60*60*24
41
43
  sleep a_day_in_seconds
42
44
  end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ module Appstats
4
+ describe Action do
5
+
6
+ before(:each) do
7
+ Appstats::Entry.delete_all
8
+ Appstats::Action.delete_all
9
+ @action = Appstats::Action.new
10
+ end
11
+
12
+ describe "#initialize" do
13
+
14
+ it "should set name to nil" do
15
+ @action.name.should == nil
16
+ end
17
+
18
+ it "should set plural_name to nil" do
19
+ @action.plural_name.should == nil
20
+ end
21
+
22
+ it "should set status to nil" do
23
+ @action.status.should == nil
24
+ end
25
+
26
+ it "should set on constructor" do
27
+ action = Appstats::Action.new(:name => 'a', :plural_name => 'b', :status => 'c')
28
+ action.name.should == 'a'
29
+ action.plural_name.should == 'b'
30
+ action.status.should == 'c'
31
+ end
32
+
33
+ end
34
+
35
+ describe "#update_actions" do
36
+
37
+ it "should do nothing if no events" do
38
+ Appstats::Action.update_actions.should == 0
39
+ Appstats::Action.count.should == 0
40
+ end
41
+
42
+ it "should add entry action names" do
43
+ Appstats::Entry.create(:action => 'a')
44
+ Appstats::Action.update_actions.should == 1
45
+ Appstats::Action.count.should == 1
46
+
47
+ action = Appstats::Action.last
48
+ action.name = 'a'
49
+ action.plural_name = 'as'
50
+ action.status = 'derived'
51
+ end
52
+
53
+ end
54
+
55
+
56
+ end
57
+ end
@@ -0,0 +1,286 @@
1
+ require 'spec_helper'
2
+
3
+ module Appstats
4
+ describe DateRange do
5
+
6
+
7
+ before(:each) do
8
+ @time = Time.parse('2010-01-15 10:20:30')
9
+ Time.stub!(:now).and_return(@time)
10
+ end
11
+
12
+ describe "#initialize" do
13
+
14
+ it "should set format to true if not set" do
15
+ DateRange.new.format.should == :inclusive
16
+ DateRange.new(:format => :exclusive).format.should == :exclusive
17
+ end
18
+
19
+ it "should understand from date and to date" do
20
+ date_range = DateRange.new(:from => EntryDate.new(:year => 2009), :to => EntryDate.new(:year => 2010), :format => :inclusive)
21
+ date_range.from.year.should == 2009
22
+ date_range.to.year.should == 2010
23
+ date_range.format.should == :inclusive
24
+ end
25
+
26
+ it "should understand equality" do
27
+ date_range = DateRange.new(:from => EntryDate.new(:year => 2009), :to => EntryDate.new(:year => 2010))
28
+ same_date_range = DateRange.new(:from => EntryDate.new(:year => 2009), :to => EntryDate.new(:year => 2010))
29
+ another_date_range = DateRange.new(:from => EntryDate.new(:year => 2007), :to => EntryDate.new(:year => 2010))
30
+
31
+ date_range.should == date_range
32
+ date_range.should == same_date_range
33
+ date_range.should_not == another_date_range
34
+ end
35
+ end
36
+
37
+ describe "#parse" do
38
+
39
+ it "should understand nil" do
40
+ DateRange.parse(nil).should == DateRange.new
41
+ DateRange.parse("").should == DateRange.new
42
+ end
43
+
44
+ it "should understand between" do
45
+ range = DateRange.parse(" between Mar, 2010 and Jun, 2011 ")
46
+ range.should == DateRange.new(:from => EntryDate.new(:year => 2010, :month => 3), :to => EntryDate.new(:year => 2011, :month => 6), :format => :inclusive )
47
+ end
48
+
49
+ it "should understand in" do
50
+ range = DateRange.parse(" in Mar, 2010 ")
51
+ range.should == DateRange.new(:from => EntryDate.new(:year => 2010, :month => 3), :to => nil, :format => :fixed_point )
52
+ end
53
+
54
+ it "shouldunderstand since" do
55
+ DateRange.parse(" since Mar, 2010 ").should == DateRange.new(:from => EntryDate.new(:year => 2010, :month => 3), :to => nil, :format => :inclusive )
56
+ DateRange.parse(" since 2010-04-15 ").should == DateRange.new(:from => EntryDate.new(:year => 2010, :month => 4, :day => 15), :to => nil, :format => :inclusive )
57
+ end
58
+
59
+ it "should understand on" do
60
+ range = DateRange.parse(" on Mar, 2010 ")
61
+ range.should == DateRange.new(:from => EntryDate.new(:year => 2010, :month => 3), :to => nil, :format => :fixed_point )
62
+ end
63
+
64
+ describe "before and after dates" do
65
+
66
+ it "should understand before" do
67
+ range = DateRange.parse(" before Mar, 2010 ")
68
+ range.should == DateRange.new(:from => nil, :to => EntryDate.new(:year => 2010, :month => 3), :format => :exclusive )
69
+ end
70
+
71
+ it "should understand after" do
72
+ range = DateRange.parse(" after Mar, 2010 ")
73
+ range.should == DateRange.new(:from => EntryDate.new(:year => 2010, :month => 3), :to => nil, :format => :exclusive )
74
+ end
75
+
76
+ end
77
+
78
+
79
+ it "should understand YTD, today, yesterday" do
80
+ DateRange.parse(" YTD ").should == DateRange.new(:from => EntryDate.new(:year => 2010), :to => nil, :format => :fixed_point )
81
+ DateRange.parse(" today ").should == DateRange.new(:from => EntryDate.new(:year => 2010, :month => 1, :day => 15), :to => nil, :format => :fixed_point )
82
+ DateRange.parse(" yesterday ").should == DateRange.new(:from => EntryDate.new(:year => 2010, :month => 1, :day => 14), :to => nil, :format => :fixed_point )
83
+ end
84
+
85
+ describe "this (year|month|week|day)" do
86
+
87
+ it "should understand this year" do
88
+ DateRange.parse(" this year ").should == DateRange.new(:from => EntryDate.new(:year => 2010), :to => nil, :format => :fixed_point )
89
+ end
90
+
91
+ it "should understand this month" do
92
+ DateRange.parse(" this month ").should == DateRange.new(:from => EntryDate.new(:year => 2010, :month => 1), :to => nil, :format => :fixed_point )
93
+ end
94
+
95
+ it "should understand this week" do
96
+ DateRange.parse(" this week ").should == DateRange.new(:from => EntryDate.new(:year => 2010, :month => 1, :day => 11), :to => nil, :format => :inclusive )
97
+ end
98
+
99
+ it "should understand this day" do
100
+ DateRange.parse(" this day ").should == DateRange.new(:from => EntryDate.new(:year => 2010, :month => 1, :day => 15), :to => nil, :format => :fixed_point )
101
+ end
102
+
103
+ end
104
+
105
+ describe "(last|previous) (year|month|week|day)" do
106
+
107
+ it "should understand last year" do
108
+ DateRange.parse(" last year ").should == DateRange.new(:from => EntryDate.new(:year => 2009), :to => nil, :format => :fixed_point )
109
+ end
110
+
111
+ it "should understand last month" do
112
+ DateRange.parse(" last month ").should == DateRange.new(:from => EntryDate.new(:year => 2009, :month => 12), :to => nil, :format => :fixed_point )
113
+ end
114
+
115
+ it "should understand last week" do
116
+ DateRange.parse(" last week ").should == DateRange.new(:from => EntryDate.new(:year => 2010, :month => 1, :day => 4), :to => EntryDate.new(:year => 2010, :month => 1, :day => 10), :format => :inclusive )
117
+ end
118
+
119
+ it "should understand last day" do
120
+ DateRange.parse(" last day ").should == DateRange.new(:from => EntryDate.new(:year => 2010, :month => 1, :day => 14), :to => nil, :format => :fixed_point )
121
+ end
122
+
123
+ it "should understand previous year" do
124
+ DateRange.parse(" previous year ").should == DateRange.new(:from => EntryDate.new(:year => 2009), :to => nil, :format => :fixed_point )
125
+ end
126
+
127
+ it "should understand previous month" do
128
+ DateRange.parse(" previous month ").should == DateRange.new(:from => EntryDate.new(:year => 2009, :month => 12), :to => nil, :format => :fixed_point )
129
+ end
130
+
131
+ it "should understand previous week" do
132
+ DateRange.parse(" previous week ").should == DateRange.new(:from => EntryDate.new(:year => 2010, :month => 1, :day => 4), :to => EntryDate.new(:year => 2010, :month => 1, :day => 10), :format => :inclusive )
133
+ end
134
+
135
+ it "should understand previous day" do
136
+ DateRange.parse(" previous day ").should == DateRange.new(:from => EntryDate.new(:year => 2010, :month => 1, :day => 14), :to => nil, :format => :fixed_point )
137
+ end
138
+
139
+ end
140
+
141
+ describe "last X (year|month|week|day)s" do
142
+
143
+ it "should understand last X years" do
144
+ DateRange.parse(" last 1 year ").should == DateRange.new(:from => EntryDate.new(:year => 2010), :to => nil, :format => :inclusive )
145
+ DateRange.parse(" last 2 years ").should == DateRange.new(:from => EntryDate.new(:year => 2009), :to => nil, :format => :inclusive )
146
+ DateRange.parse(" last 3 years ").should == DateRange.new(:from => EntryDate.new(:year => 2008), :to => nil, :format => :inclusive )
147
+ end
148
+
149
+ it "should understand last X months" do
150
+ DateRange.parse(" last 1 month ").should == DateRange.new(:from => EntryDate.new(:year => 2010, :month => 1), :to => nil, :format => :inclusive )
151
+ DateRange.parse(" last 2 months ").should == DateRange.new(:from => EntryDate.new(:year => 2009, :month => 12), :to => nil, :format => :inclusive )
152
+ DateRange.parse(" last 3 months ").should == DateRange.new(:from => EntryDate.new(:year => 2009, :month => 11), :to => nil, :format => :inclusive )
153
+ end
154
+
155
+ it "should understand last X weeks" do
156
+ DateRange.parse(" last 1 week ").should == DateRange.new(:from => EntryDate.new(:year => 2010, :month => 1, :day => 11), :to => nil, :format => :inclusive )
157
+ DateRange.parse(" last 2 weeks ").should == DateRange.new(:from => EntryDate.new(:year => 2010, :month => 1, :day => 4), :to => nil, :format => :inclusive )
158
+ DateRange.parse(" last 3 weeks ").should == DateRange.new(:from => EntryDate.new(:year => 2009, :month => 12, :day => 28), :to => nil, :format => :inclusive )
159
+ end
160
+
161
+ it "should understand last X days" do
162
+ DateRange.parse(" last 1 day ").should == DateRange.new(:from => EntryDate.new(:year => 2010, :month => 1, :day => 15), :to => nil, :format => :inclusive )
163
+ DateRange.parse(" last 2 days ").should == DateRange.new(:from => EntryDate.new(:year => 2010, :month => 1, :day => 14), :to => nil, :format => :inclusive )
164
+ DateRange.parse(" last 3 days ").should == DateRange.new(:from => EntryDate.new(:year => 2010, :month => 1, :day => 13), :to => nil, :format => :inclusive )
165
+ end
166
+
167
+ end
168
+
169
+ describe "previous X (year|month|week|day)s" do
170
+
171
+ it "should understand previous X years" do
172
+ DateRange.parse(" previous 1 year ").should == DateRange.new(:from => EntryDate.new(:year => 2009), :to => EntryDate.new(:year => 2009), :format => :inclusive )
173
+ DateRange.parse(" previous 2 years ").should == DateRange.new(:from => EntryDate.new(:year => 2008), :to => EntryDate.new(:year => 2009), :format => :inclusive )
174
+ DateRange.parse(" previous 3 years ").should == DateRange.new(:from => EntryDate.new(:year => 2007), :to => EntryDate.new(:year => 2009), :format => :inclusive )
175
+ end
176
+
177
+ it "should understand previous Y months" do
178
+ DateRange.parse(" previous 1 month ").should == DateRange.new(:from => EntryDate.new(:year => 2009, :month => 12), :to => EntryDate.new(:year => 2009, :month => 12), :format => :inclusive )
179
+ DateRange.parse(" previous 2 months ").should == DateRange.new(:from => EntryDate.new(:year => 2009, :month => 11), :to => EntryDate.new(:year => 2009, :month => 12), :format => :inclusive )
180
+ DateRange.parse(" previous 3 months ").should == DateRange.new(:from => EntryDate.new(:year => 2009, :month => 10), :to => EntryDate.new(:year => 2009, :month => 12), :format => :inclusive )
181
+ end
182
+
183
+ it "should understand previous 2 weeks" do
184
+ DateRange.parse(" previous 1 week ").should == DateRange.new(:from => EntryDate.new(:year => 2010, :month => 1, :day => 4), :to => EntryDate.new(:year => 2010, :month => 1, :day => 10), :format => :inclusive )
185
+ DateRange.parse(" previous 2 weeks ").should == DateRange.new(:from => EntryDate.new(:year => 2009, :month => 12, :day => 28), :to => EntryDate.new(:year => 2010, :month => 1, :day => 10), :format => :inclusive )
186
+ DateRange.parse(" previous 3 weeks ").should == DateRange.new(:from => EntryDate.new(:year => 2009, :month => 12, :day => 21), :to => EntryDate.new(:year => 2010, :month => 1, :day => 10), :format => :inclusive )
187
+ end
188
+
189
+ it "should understand previous 2 days" do
190
+ DateRange.parse(" previous 1 day ").should == DateRange.new(:from => EntryDate.new(:year => 2010, :month => 1, :day => 14), :to => EntryDate.new(:year => 2010, :month => 1, :day => 14), :format => :inclusive )
191
+ DateRange.parse(" previous 2 days ").should == DateRange.new(:from => EntryDate.new(:year => 2010, :month => 1, :day => 13), :to => EntryDate.new(:year => 2010, :month => 1, :day => 14), :format => :inclusive )
192
+ DateRange.parse(" previous 3 days ").should == DateRange.new(:from => EntryDate.new(:year => 2010, :month => 1, :day => 12), :to => EntryDate.new(:year => 2010, :month => 1, :day => 14), :format => :inclusive )
193
+ end
194
+
195
+ end
196
+
197
+ end
198
+
199
+
200
+ describe "#from_to_s" do
201
+ it "should handle nil" do
202
+ DateRange.new.from_to_s.should == nil
203
+ end
204
+
205
+ it "should be based on time" do
206
+ DateRange.new(:from => EntryDate.new(:year => 2010)).from_to_s.should == "2010-01-01 00:00:00"
207
+ end
208
+
209
+ it "should be based on end_of time as-is for exclusive" do
210
+ DateRange.new(:from => EntryDate.new(:year => 2009), :format => :exclusive).from_to_s.should == "2009-12-31 23:59:59"
211
+ DateRange.new(:from => EntryDate.new(:year => 2009, :month => 2), :format => :exclusive).from_to_s.should == "2009-02-28 23:59:59"
212
+ DateRange.new(:from => EntryDate.new(:year => 2009, :month => 2, :day => 15), :format => :exclusive).from_to_s.should == "2009-02-15 23:59:59"
213
+ end
214
+
215
+ end
216
+
217
+ describe "#to_to_s" do
218
+ it "should handle nil" do
219
+ DateRange.new.to_to_s.should == nil
220
+ end
221
+
222
+ it "should be based on time as-is for exclusive" do
223
+ DateRange.new(:to => EntryDate.new(:year => 2009), :format => :exclusive).to_to_s.should == "2009-01-01 00:00:00"
224
+ end
225
+
226
+ it "should be based on end_of time as-is for inclusive" do
227
+ DateRange.new(:to => EntryDate.new(:year => 2009), :format => :inclusive).to_to_s.should == "2009-12-31 23:59:59"
228
+ DateRange.new(:to => EntryDate.new(:year => 2009, :month => 2), :format => :inclusive).to_to_s.should == "2009-02-28 23:59:59"
229
+ DateRange.new(:to => EntryDate.new(:year => 2009, :month => 2, :day => 15), :format => :inclusive).to_to_s.should == "2009-02-15 23:59:59"
230
+ end
231
+
232
+
233
+ end
234
+
235
+ describe "#to_sql" do
236
+
237
+ before(:each) do
238
+ @date1 = EntryDate.new(:year => 2010, :month => 10)
239
+ @date2 = EntryDate.new(:year => 2011, :month => 3)
240
+ end
241
+
242
+ it "should support an empty date range" do
243
+ DateRange.new.to_sql.should == "1=1"
244
+ end
245
+
246
+ it "should support from, inclusive" do
247
+ DateRange.new(:from => @date1, :format => :inclusive).to_sql.should == "occurred_at >= '2010-10-01 00:00:00'"
248
+ end
249
+
250
+ it "should support from, exclusive" do
251
+ DateRange.new(:from => @date1, :format => :exclusive).to_sql.should == "occurred_at > '2010-10-31 23:59:59'"
252
+ end
253
+
254
+ it "should support from, fixed_point" do
255
+ DateRange.new(:from => @date1, :format => :fixed_point).to_sql.should == "(year=2010 and month=10)"
256
+ end
257
+
258
+ it "should support to, inclusive" do
259
+ DateRange.new(:to => @date1, :format => :inclusive).to_sql.should == "occurred_at <= '2010-10-31 23:59:59'"
260
+ end
261
+
262
+ it "should support to, exclusive" do
263
+ DateRange.new(:to => @date1, :format => :exclusive).to_sql.should == "occurred_at < '2010-10-01 00:00:00'"
264
+ end
265
+
266
+ it "should not support to, fixed_point" do
267
+ DateRange.new(:to => @date1, :format => :fixed_point).to_sql.should == "1=1"
268
+ end
269
+
270
+ it "should support to and from, inclusive" do
271
+ DateRange.new(:from => @date1, :to => @date2, :format => :inclusive).to_sql.should == "(occurred_at >= '2010-10-01 00:00:00' and occurred_at <= '2011-03-31 23:59:59')"
272
+ end
273
+
274
+ it "should support to and from, exclusive" do
275
+ DateRange.new(:from => @date1, :to => @date2, :format => :exclusive).to_sql.should == "(occurred_at > '2010-10-31 23:59:59' and occurred_at < '2011-03-01 00:00:00')"
276
+ end
277
+
278
+ it "should not support to and from fixed_point" do
279
+ DateRange.new(:to => @date1, :format => :fixed_point).to_sql.should == "1=1"
280
+ end
281
+
282
+ end
283
+
284
+
285
+ end
286
+ end
@@ -0,0 +1,221 @@
1
+ require 'spec_helper'
2
+
3
+ module Appstats
4
+ describe EntryDate do
5
+
6
+ before(:each) do
7
+ @time = Time.parse('2010-01-15 10:20:30') # Friday
8
+ Time.stub!(:now).and_return(@time)
9
+ end
10
+
11
+ describe "#initialize" do
12
+
13
+ it "should understand year, month, day, hour, min, sec" do
14
+ date = EntryDate.new(:year => 2010, :month => 1, :day => 2, :hour => 3, :min => 4, :sec => 5)
15
+ date.year.should == 2010
16
+ date.month.should == 1
17
+ date.day.should == 2
18
+ date.hour.should == 3
19
+ date.min.should == 4
20
+ date.sec.should == 5
21
+ end
22
+
23
+ it "should understand equality" do
24
+ date = EntryDate.new(:year => 2010, :month => 1, :day => 2, :hour => 3, :min => 4, :sec => 5)
25
+ same_date = EntryDate.new(:year => 2010, :month => 1, :day => 2, :hour => 3, :min => 4, :sec => 5)
26
+ another_date = EntryDate.new(:year => 2011, :month => 1, :day => 2, :hour => 3, :min => 4, :sec => 5)
27
+
28
+ date.should == date
29
+ date.should == same_date
30
+ date.should_not == another_date
31
+ end
32
+
33
+
34
+ end
35
+
36
+ describe "#to_time" do
37
+
38
+ it "should handle full dates" do
39
+ EntryDate.new(:year => 2010, :month => 1, :day => 2, :hour => 3, :min => 4, :sec => 5).to_time.should == Time.parse("2010-01-02 03:04:05")
40
+ end
41
+
42
+ it "should handle partial dates" do
43
+ EntryDate.new(:year => 2010, :month => 2).to_time.should == Time.parse("2010-02-01 00:00:00")
44
+ end
45
+
46
+ it "should handle now" do
47
+ EntryDate.new.to_time.should == Time.now
48
+ end
49
+
50
+ it "should handle end_of" do
51
+ EntryDate.new(:year => 2009).to_time(:end).to_s.should == Time.parse("2009-12-31 23:59:59").to_s
52
+ EntryDate.new(:year => 2009, :month => 2).to_time(:end).to_s.should == Time.parse("2009-02-28 23:59:59").to_s
53
+ EntryDate.new(:year => 2009, :month => 2, :day => 15).to_time(:end).to_s.should == Time.parse("2009-02-15 23:59:59").to_s
54
+ end
55
+
56
+
57
+ end
58
+
59
+
60
+ describe "#to_s" do
61
+ it "should handle full dates" do
62
+ EntryDate.new(:year => 2010, :month => 1, :day => 2, :hour => 3, :min => 4, :sec => 5).to_s.should == "2010-01-02 03:04:05"
63
+ end
64
+
65
+ it "should handle partial dates" do
66
+ EntryDate.new(:year => 2010, :month => 1).to_s.should == "2010-01"
67
+ end
68
+ end
69
+
70
+ describe "#end_of_week" do
71
+ it "should return the same 'level' data points" do
72
+ now = EntryDate.new(:year => 2010, :month => 1, :day => 5, :hour => 10) # tuesday
73
+ expected = EntryDate.new(:year => 2010, :month => 1, :day => 10, :hour => 10)
74
+ now.end_of_week.should == expected
75
+ end
76
+ end
77
+
78
+ describe "#parse" do
79
+
80
+ it "should deal with nil" do
81
+ EntryDate.parse(nil).should == EntryDate.new
82
+ EntryDate.parse("").should == EntryDate.new
83
+ end
84
+
85
+ it "should undestand years" do
86
+ EntryDate.parse("2010").should == EntryDate.new(:year => 2010)
87
+ EntryDate.parse("2011").should == EntryDate.new(:year => 2011)
88
+ end
89
+
90
+ it "should understand month, year" do
91
+ EntryDate.parse("February, 2010").should == EntryDate.new(:year => 2010, :month => 2)
92
+ EntryDate.parse("Mar, 2011").should == EntryDate.new(:year => 2011, :month => 3)
93
+ end
94
+
95
+ it "should understand YYYY-mm-dd" do
96
+ EntryDate.parse("2010-04-25").should == EntryDate.new(:year => 2010, :month => 4, :day => 25)
97
+ EntryDate.parse("2011-05-15").should == EntryDate.new(:year => 2011, :month => 5, :day => 15)
98
+ end
99
+
100
+ it "should understand YYYY-mm-dd HH:MM:SS" do
101
+ EntryDate.parse("2010-04-25 10:11:12").should == EntryDate.new(:year => 2010, :month => 4, :day => 25, :hour => 10, :min => 11, :sec => 12)
102
+ end
103
+
104
+ it "should understand YTD" do
105
+ EntryDate.parse("YTD").should == EntryDate.new(:year => 2010)
106
+ EntryDate.parse("ytd").should == EntryDate.new(:year => 2010)
107
+ end
108
+
109
+ it "should understand yesterday" do
110
+ EntryDate.parse("yesterday").should == EntryDate.new(:year => 2010, :month => 1, :day => 14)
111
+ end
112
+
113
+ it "should understand today" do
114
+ EntryDate.parse("today").should == EntryDate.new(:year => 2010, :month => 1, :day => 15)
115
+ end
116
+
117
+ it "should understand last year" do
118
+ EntryDate.parse("last year").should == EntryDate.new(:year => 2009)
119
+ end
120
+
121
+ it "should understand last month" do
122
+ EntryDate.parse("last month").should == EntryDate.new(:year => 2009, :month => 12)
123
+ end
124
+
125
+ it "should understand last week" do
126
+ EntryDate.parse("last week").should == EntryDate.new(:year => 2010, :month => 1, :day => 4)
127
+ end
128
+
129
+ it "should understand last day" do
130
+ EntryDate.parse("last day").should == EntryDate.new(:year => 2010, :month => 1, :day => 14)
131
+ end
132
+
133
+ it "should understand previous year" do
134
+ EntryDate.parse("previous year").should == EntryDate.new(:year => 2009)
135
+ end
136
+
137
+ it "should understand previous month" do
138
+ EntryDate.parse("previous month").should == EntryDate.new(:year => 2009, :month => 12)
139
+ end
140
+
141
+ it "should understand previous week" do
142
+ EntryDate.parse("previous week").should == EntryDate.new(:year => 2010, :month => 1, :day => 4)
143
+ end
144
+
145
+ it "should understand previous day" do
146
+ EntryDate.parse("previous day").should == EntryDate.new(:year => 2010, :month => 1, :day => 14)
147
+ end
148
+
149
+ it "should understand this year" do
150
+ EntryDate.parse("this year").should == EntryDate.new(:year => 2010)
151
+ end
152
+
153
+ it "should understand this month" do
154
+ EntryDate.parse("this month").should == EntryDate.new(:year => 2010, :month => 1)
155
+ end
156
+
157
+ it "should understand this week" do
158
+ EntryDate.parse("this week").should == EntryDate.new(:year => 2010, :month => 1, :day => 11)
159
+ end
160
+
161
+ it "should understand this day" do
162
+ EntryDate.parse("this day").should == EntryDate.new(:year => 2010, :month => 1, :day => 15)
163
+ end
164
+
165
+ it "should understand last X years" do
166
+ EntryDate.parse("last 1 year").should == EntryDate.new(:year => 2010)
167
+ EntryDate.parse("last 2 years").should == EntryDate.new(:year => 2009)
168
+ EntryDate.parse("last 3 years").should == EntryDate.new(:year => 2008)
169
+ end
170
+
171
+ it "should understand last X months" do
172
+ EntryDate.parse("last 1 month").should == EntryDate.new(:year => 2010, :month => 1)
173
+ EntryDate.parse("last 2 months").should == EntryDate.new(:year => 2009, :month => 12)
174
+ EntryDate.parse("last 3 months").should == EntryDate.new(:year => 2009, :month => 11)
175
+ end
176
+
177
+ it "should understand last X weeks" do
178
+ EntryDate.parse("last 1 week").should == EntryDate.new(:year => 2010, :month => 1, :day => 11)
179
+ EntryDate.parse("last 2 weeks").should == EntryDate.new(:year => 2010, :month => 1, :day => 4)
180
+ EntryDate.parse("last 3 weeks").should == EntryDate.new(:year => 2009, :month => 12, :day => 28)
181
+ end
182
+
183
+ it "should understand last X days" do
184
+ EntryDate.parse("last 1 day").should == EntryDate.new(:year => 2010, :month => 1, :day => 15)
185
+ EntryDate.parse("last 2 days").should == EntryDate.new(:year => 2010, :month => 1, :day => 14)
186
+ EntryDate.parse("last 3 days").should == EntryDate.new(:year => 2010, :month => 1, :day => 13)
187
+ end
188
+
189
+ it "should understand previous X years" do
190
+ EntryDate.parse("previous 1 year").should == EntryDate.new(:year => 2009)
191
+ EntryDate.parse("previous 2 years").should == EntryDate.new(:year => 2008)
192
+ EntryDate.parse("previous 3 years").should == EntryDate.new(:year => 2007)
193
+ end
194
+
195
+ it "should understand previous X months" do
196
+ EntryDate.parse("previous 1 month").should == EntryDate.new(:year => 2009, :month => 12)
197
+ EntryDate.parse("previous 2 months").should == EntryDate.new(:year => 2009, :month => 11)
198
+ EntryDate.parse("previous 3 months").should == EntryDate.new(:year => 2009, :month => 10)
199
+ end
200
+
201
+ it "should understand previous X weeks" do
202
+ EntryDate.parse("previous 1 week").should == EntryDate.new(:year => 2010, :month => 1, :day => 4)
203
+ EntryDate.parse("previous 2 weeks").should == EntryDate.new(:year => 2009, :month => 12, :day => 28)
204
+ EntryDate.parse("previous 3 weeks").should == EntryDate.new(:year => 2009, :month => 12, :day => 21)
205
+ end
206
+
207
+ it "should understand previous X days" do
208
+ EntryDate.parse("previous 1 day").should == EntryDate.new(:year => 2010, :month => 1, :day => 14)
209
+ EntryDate.parse("previous 2 days").should == EntryDate.new(:year => 2010, :month => 1, :day => 13)
210
+ EntryDate.parse("previous 3 days").should == EntryDate.new(:year => 2010, :month => 1, :day => 12)
211
+ end
212
+
213
+ it "should accept garbage input" do
214
+ EntryDate.parse("1234asdf1234 1234fds123").should == EntryDate.new
215
+ end
216
+
217
+
218
+ end
219
+
220
+ end
221
+ end