dateslices 0.0.2 → 0.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.
- checksums.yaml +4 -4
- data/README.md +61 -0
- data/lib/dateslices.rb +3 -3
- data/lib/dateslices/mysql.rb +1 -1
- data/lib/dateslices/scopes.rb +34 -24
- data/lib/dateslices/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3efa5cfa855a50c696f0d04409b2ed0187920eea
|
4
|
+
data.tar.gz: 26c2e49f84cb16c2c3dcd2a2a1f5924e0f134f21
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 854d8b6e033cf34ce2a673fcc2a5eb4b1968bb2869fb77b9a4d3038fd8f31968fe2d7684c56e4a83b10669cb99c53c0a2b0abad20014f7c65529b6b13f4a1ac0
|
7
|
+
data.tar.gz: 0ec27b6e51699b05d77facf95b1dfa280c2bdc6021dba6711450789571d808efda8f81efef6d75c98df0eeac2cd475b7046e0fa55aea5ff2a668edf63502919c
|
data/README.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# Dateslices
|
2
|
+
|
3
|
+
This project rocks and uses MIT-LICENSE.
|
4
|
+
|
5
|
+
This project is based upon [groupdate](https://github.com/ankane/groupdate) and follows a similar API.
|
6
|
+
|
7
|
+
The differences between the groupdate and dateslices are:
|
8
|
+
- dateslices supports sqlite3.
|
9
|
+
- dateslices has much less functionality.
|
10
|
+
- dateslices ignores timezones.
|
11
|
+
- dateslices is rails 4 only.
|
12
|
+
- dateslices uses rspecs for tests.
|
13
|
+
- dateslices doesn't have hour_of_day since it doesn't deal with timezones
|
14
|
+
|
15
|
+
|
16
|
+
The reason that I wrote this is that I use sqlite in development, and wanted to make sure that I could test things locally. I didn't understand the test suite of groupdate, so I ended up rewriting it.
|
17
|
+
|
18
|
+
|
19
|
+
## Example Usage
|
20
|
+
|
21
|
+
```
|
22
|
+
User.where( :created_at > 1.month.ago ).group_by_day
|
23
|
+
```
|
24
|
+
|
25
|
+
## Summing up a column
|
26
|
+
|
27
|
+
```
|
28
|
+
User.group_by_day( :created_at, "sum", "karma" )
|
29
|
+
```
|
30
|
+
|
31
|
+
## Averaging a column
|
32
|
+
|
33
|
+
```
|
34
|
+
Post.group_by_week( :updated_at, "avg", "comment_count")
|
35
|
+
```
|
36
|
+
|
37
|
+
## All find methods
|
38
|
+
|
39
|
+
These methods take three optional arguments:
|
40
|
+
|
41
|
+
1. column to group by, normally "created_at"
|
42
|
+
2. sql function to run, normally "count". Also "sum", "avg"
|
43
|
+
3. column to do the function on, normally "*"
|
44
|
+
|
45
|
+
- group_by_second
|
46
|
+
- group_by_minute
|
47
|
+
- group_by_hour
|
48
|
+
- group_by_day
|
49
|
+
- group_by_week
|
50
|
+
- group_by_day_of_week
|
51
|
+
- group_by_month
|
52
|
+
- group_by_year
|
53
|
+
|
54
|
+
## Configuration
|
55
|
+
|
56
|
+
The output format now defaults to a Chartkick compatible hash of dates and values.
|
57
|
+
If you wish to use the dateslice format, please add `Dateslices.output_format = :dateslice` to an initializer.
|
58
|
+
|
59
|
+
## Tests
|
60
|
+
|
61
|
+
Rspec tests need to be run out of the spec/dummy directory, and you'll need to have a postgres and a mysql database named "dateslice_test" for them to succeed.
|
data/lib/dateslices.rb
CHANGED
@@ -5,9 +5,9 @@ module Dateslices
|
|
5
5
|
FIELDS = [:second, :minute, :hour, :day, :week, :day_of_week, :month, :year ]
|
6
6
|
METHODS = FIELDS.map{|v| :"group_by_#{v}" }
|
7
7
|
|
8
|
-
mattr_accessor :
|
9
|
-
|
10
|
-
self.
|
8
|
+
mattr_accessor :output_format
|
9
|
+
|
10
|
+
self.output_format = :groupdate
|
11
11
|
end
|
12
12
|
|
13
13
|
begin
|
data/lib/dateslices/mysql.rb
CHANGED
data/lib/dateslices/scopes.rb
CHANGED
@@ -3,41 +3,51 @@ module Dateslices
|
|
3
3
|
|
4
4
|
Dateslices::FIELDS.each do |field|
|
5
5
|
define_method :"group_by_#{field}" do |*args|
|
6
|
+
|
6
7
|
args = args.dup
|
7
8
|
|
8
|
-
column = args[0]
|
9
|
-
|
9
|
+
column = args[0].blank? ? 'created_at' : args[0]
|
10
|
+
|
11
|
+
aggregation = args[1].blank? ? 'count' : args[1]
|
12
|
+
|
13
|
+
aggregation_column = args[2].blank? ? '*' : args[2]
|
10
14
|
|
11
|
-
|
12
|
-
aggregation = 'count' if aggregation.blank?
|
15
|
+
sql = ["#{aggregation}(#{aggregation_column}) as count"]
|
13
16
|
|
14
|
-
|
15
|
-
|
17
|
+
time_filter = case connection.adapter_name
|
18
|
+
when 'SQLite'
|
19
|
+
Dateslices::Sqlite.time_filter(column, field)
|
20
|
+
when 'PostgreSQL', 'PostGIS'
|
21
|
+
Dateslices::Postgresql.time_filter(column, field)
|
22
|
+
when 'MySQL', 'Mysql2'
|
23
|
+
Dateslices::Mysql.time_filter(column, field)
|
24
|
+
else
|
25
|
+
throw "Unknown database adaptor #{connection.adapter_name}"
|
26
|
+
end
|
27
|
+
|
28
|
+
sql << "#{time_filter} as date_slice"
|
16
29
|
|
17
|
-
|
30
|
+
slices = select( sql.join(', ')).where.not(column => nil).group('date_slice').order('date_slice')
|
18
31
|
|
19
|
-
|
32
|
+
if Dateslices.output_format == :groupdate
|
20
33
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
34
|
+
slices.collect! do |c|
|
35
|
+
slice = c['date_slice']
|
36
|
+
slice = slice.is_a?(Float) ? slice.to_i.to_s : slice.to_s
|
37
|
+
[slice, c['count']]
|
38
|
+
end
|
39
|
+
|
40
|
+
Hash[slices]
|
28
41
|
else
|
29
|
-
throw "Unknown database adaptor #{connection.adapter_name}"
|
30
|
-
end
|
31
42
|
|
32
|
-
|
43
|
+
slices.collect do |c|
|
44
|
+
slice = c['date_slice']
|
45
|
+
slice = slice.is_a?(Float) ? slice.to_i.to_s : slice.to_s
|
46
|
+
{ date_slice: slice, aggregation.to_sym => c['count'] }
|
47
|
+
end
|
33
48
|
|
34
|
-
select( sql.join(', ')).where.not(column => nil).group('date_slice').order('date_slice').collect do |c|
|
35
|
-
slice = c['date_slice']
|
36
|
-
slice = slice.to_i.to_s if slice.is_a? Float
|
37
|
-
slice = slice.to_s if slice.is_a? Integer
|
38
|
-
slice = slice.to_s
|
39
|
-
{ date_slice: slice, aggregation.to_sym => c["cnt"] }
|
40
49
|
end
|
50
|
+
|
41
51
|
end
|
42
52
|
end
|
43
53
|
|
data/lib/dateslices/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dateslices
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Will Schenk
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-12-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -117,6 +117,7 @@ extensions: []
|
|
117
117
|
extra_rdoc_files: []
|
118
118
|
files:
|
119
119
|
- MIT-LICENSE
|
120
|
+
- README.md
|
120
121
|
- Rakefile
|
121
122
|
- lib/dateslices.rb
|
122
123
|
- lib/dateslices/mysql.rb
|