chartable 0.0.0.1 → 0.0.0.2
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/lib/chartable/queries/daily.rb +13 -1
- data/lib/chartable/queries/monthly.rb +11 -1
- data/lib/chartable/queries/quarterly.rb +18 -1
- data/lib/chartable/queries/weekly.rb +8 -2
- data/lib/chartable/queries/yearly.rb +7 -1
- data/lib/chartable/range_query.rb +7 -6
- data/lib/chartable/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7366c67606157ae0f28e3cda5bd591858a662fb4d1ac27dca22d3e26e1155a3b
|
4
|
+
data.tar.gz: 63db758f26ae8057f200727a2fb735e8cb675a28f01d751bd31a8dcd3bc16aba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8ccdf6bf85dd948d187caa1cd4d4455bd30c7c2c1831e872896802ef909ca1d572ff3db9e212781d2335e1b2a2d4d9e29f2b847bf08f342a0069292bfa09496e
|
7
|
+
data.tar.gz: 0fba6005933df7394b20ea0c69c3a27611acb00e863f793a3910c5b8eaea24496e0896eae700080b99f8e9ac7fcd376958d094ef6d8fc0476830c2aa41026358
|
@@ -6,7 +6,19 @@ module Chartable
|
|
6
6
|
#
|
7
7
|
# @return [Hash]
|
8
8
|
def self.call(scope, on:)
|
9
|
-
|
9
|
+
if ActiveRecord::Base.connection.class.to_s.match(/sqlite/i)
|
10
|
+
scope.group("strftime('%m %d, %Y', #{on})").size.transform_keys do |key|
|
11
|
+
data = key.split(" ")
|
12
|
+
month_number = data.shift
|
13
|
+
month_name = Date::MONTHNAMES[month_number.to_i]
|
14
|
+
data.unshift(month_name)
|
15
|
+
data.join(" ")
|
16
|
+
end
|
17
|
+
elsif ActiveRecord::Base.connection.class.to_s.match(/postgresql/i)
|
18
|
+
scope.group("to_char(#{on},'FMMonth DD, YYYY')").size
|
19
|
+
else
|
20
|
+
scope.group("DATE_FORMAT(#{on}, '%M %d, %Y')").size
|
21
|
+
end
|
10
22
|
end
|
11
23
|
end
|
12
24
|
end
|
@@ -3,10 +3,20 @@ module Chartable
|
|
3
3
|
class Monthly
|
4
4
|
# It returns analytics data for the monthly period.
|
5
5
|
# Example output: `{"November 2018" => 1, "October 2018" => 1}`
|
6
|
+
# SQLite does not support DATE_FORMAT function so a little hack is needed
|
6
7
|
#
|
7
8
|
# @return [Hash]
|
8
9
|
def self.call(scope, on:)
|
9
|
-
|
10
|
+
if ActiveRecord::Base.connection.class.to_s.match(/sqlite/i)
|
11
|
+
scope.group("strftime('%m %Y', #{on})").size.transform_keys do |key|
|
12
|
+
month_number = key.match(/^[0-9]{2}(?= )/).to_s
|
13
|
+
key.gsub(month_number, Date::MONTHNAMES[month_number.to_i])
|
14
|
+
end
|
15
|
+
elsif ActiveRecord::Base.connection.class.to_s.match(/postgresql/i)
|
16
|
+
scope.group("to_char(#{on},'FMMonth YYYY')").size
|
17
|
+
else
|
18
|
+
scope.group("DATE_FORMAT(#{on},'%M %Y')").size
|
19
|
+
end
|
10
20
|
end
|
11
21
|
end
|
12
22
|
end
|
@@ -6,7 +6,24 @@ module Chartable
|
|
6
6
|
#
|
7
7
|
# @return [Hash]
|
8
8
|
def self.call(scope, on:)
|
9
|
-
|
9
|
+
if ActiveRecord::Base.connection.class.to_s.match(/sqlite/i)
|
10
|
+
scope.group("strftime('%m %Y', #{on})").size.transform_keys do |key|
|
11
|
+
month_number = key.match(/^[0-9]{2}(?= )/).to_s
|
12
|
+
quarter = case month_number.to_i
|
13
|
+
when 1..3 then 1
|
14
|
+
when 4..6 then 2
|
15
|
+
when 7..9 then 3
|
16
|
+
else
|
17
|
+
4
|
18
|
+
end
|
19
|
+
|
20
|
+
key.gsub(month_number, "Q#{quarter}")
|
21
|
+
end
|
22
|
+
elsif ActiveRecord::Base.connection.class.to_s.match(/postgresql/i)
|
23
|
+
scope.group("concat('Q', to_char(#{on},'Q YYYY'))").size
|
24
|
+
else
|
25
|
+
scope.group("CONCAT('Q', QUARTER(#{on}), DATE_FORMAT(#{on},' %Y'))").size
|
26
|
+
end
|
10
27
|
end
|
11
28
|
end
|
12
29
|
end
|
@@ -2,11 +2,17 @@ module Chartable
|
|
2
2
|
module Queries
|
3
3
|
class Weekly
|
4
4
|
# It returns analytics data for the weekly period.
|
5
|
-
# Example output: `{"01/28/
|
5
|
+
# Example output: `{"01/28/2018 - 02/03/2018" => 1, "02/11/2018 - 02/17/2018" => 1}`
|
6
6
|
#
|
7
7
|
# @return [Hash]
|
8
8
|
def self.call(scope, on:)
|
9
|
-
|
9
|
+
if ActiveRecord::Base.connection.class.to_s.match(/sqlite/i)
|
10
|
+
scope.group("strftime('%m/%d/%Y', date(#{on}, 'weekday 0', '-7 days')) || ' - ' || strftime('%m/%d/%Y', date(#{on}, 'weekday 0', '-1 days'))").size
|
11
|
+
elsif ActiveRecord::Base.connection.class.to_s.match(/postgresql/i)
|
12
|
+
scope.group("concat(to_char(date_trunc('week', #{on}) + '-1 day', 'MM/DD/YYYY'), ' - ', to_char(date_trunc('week', #{on}) + '5 days', 'MM/DD/YYYY'))").size
|
13
|
+
else
|
14
|
+
scope.group("CONCAT(DATE_FORMAT(DATE(DATE_ADD(#{on}, INTERVAL(1-DAYOFWEEK(#{on})) DAY)), '%m/%d/%Y'), ' - ', DATE_FORMAT(DATE(DATE_ADD(#{on}, INTERVAL(7-DAYOFWEEK(#{on})) DAY)),'%m/%d/%Y'))").size
|
15
|
+
end
|
10
16
|
end
|
11
17
|
end
|
12
18
|
end
|
@@ -6,7 +6,13 @@ module Chartable
|
|
6
6
|
#
|
7
7
|
# @return [Hash]
|
8
8
|
def self.call(scope, on:)
|
9
|
-
|
9
|
+
if ActiveRecord::Base.connection.class.to_s.match(/sqlite/i)
|
10
|
+
scope.group("cast(strftime('%Y', #{on}) as decimal)").size
|
11
|
+
elsif ActiveRecord::Base.connection.class.to_s.match(/postgresql/i)
|
12
|
+
scope.group("cast(to_char(#{on},'YYYY') as integer)").size
|
13
|
+
else
|
14
|
+
scope.group("YEAR(#{on})").size
|
15
|
+
end
|
10
16
|
end
|
11
17
|
end
|
12
18
|
end
|
@@ -13,12 +13,13 @@ module Chartable
|
|
13
13
|
|
14
14
|
return scope if from_date.nil? && to_date.nil?
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
16
|
+
case
|
17
|
+
when from_date.nil?
|
18
|
+
scope.where("DATE(#{on}) <= ?", to_date.to_date)
|
19
|
+
when to_date.nil?
|
20
|
+
scope.where("DATE(#{on}) >= ?", from_date.to_date)
|
21
|
+
else
|
22
|
+
scope.where("DATE(#{on}) >= ? AND DATE(#{on}) <= ?", from_date.to_date, to_date.to_date)
|
22
23
|
end
|
23
24
|
end
|
24
25
|
end
|
data/lib/chartable/version.rb
CHANGED