Skip to content

Commit 5317681

Browse files
committed
Add configurable time columns
1 parent 298fbd5 commit 5317681

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,6 @@ SELECT date_trunc('week', created_at), COUNT(*) FROM users GROUP BY 1
323323

324324
3 columns - timestamp, string, numeric - [Example](https://blazer.dokkuapp.com/queries/5-line-chart-format-2)
325325

326-
327326
```sql
328327
SELECT date_trunc('week', created_at), gender, COUNT(*) FROM users GROUP BY 1, 2
329328
```
@@ -950,6 +949,14 @@ Add [sqlite3](https://github.com/sparklemotion/sqlite3-ruby) to your Gemfile and
950949
data_sources:
951950
my_source:
952951
url: sqlite3:path/to/database.sqlite3
952+
# Columns matching these regular expressions will be interpreted as date/time
953+
# if they are strings.
954+
# This is required for sqlite because it doesn't have a special
955+
# date/time type. Without this, the wrong type of chart will be displayed
956+
time_columns:
957+
- '_at$'
958+
- '_timestamp$'
959+
- '_date$'
953960
```
954961

955962
### SQL Server

lib/blazer/data_source.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ def variable_defaults
3535
settings["variable_defaults"] || {}
3636
end
3737

38+
def time_columns
39+
settings["time_columns"]&.map { |column_name_regex| Regexp.new(column_name_regex) } || []
40+
end
41+
3842
def timeout
3943
settings["timeout"]
4044
end

lib/blazer/result.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def column_types
5353
"string"
5454
elsif v.is_a?(Numeric)
5555
"numeric"
56-
elsif v.is_a?(Time) || v.is_a?(Date)
56+
elsif time?(k, v)
5757
"time"
5858
elsif v.nil?
5959
nil
@@ -66,6 +66,15 @@ def column_types
6666
end
6767
end
6868

69+
def time?(column_name, value)
70+
return true if value.is_a?(Time) || value.is_a?(Date)
71+
return false unless value.is_a?(String)
72+
73+
data_source.time_columns.any? do |column_name_regex|
74+
column_name.match?(column_name_regex)
75+
end
76+
end
77+
6978
def chart_type
7079
@chart_type ||= begin
7180
if column_types.compact.size >= 2 && column_types.compact == ["time"] + (column_types.compact.size - 1).times.map { "numeric" }

lib/generators/blazer/templates/config.yml.tt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ data_sources:
3232
smart_columns:
3333
# user_id: "SELECT id, name FROM users WHERE id IN {value}"
3434

35+
# Columns matching these regular expressions will be interpreted as date/time
36+
# if they are strings.
37+
# This is required for sqlite because it doesn't have a special
38+
# date/time type. Without this, the wrong type of chart will be displayed
39+
time_columns:
40+
# - '^created_at$'
41+
# - '^updated_at$'
42+
# - '_at$'
43+
# - '_timestamp$'
44+
# - '_date$'
45+
46+
3547
# create audits
3648
audit: true
3749

0 commit comments

Comments
 (0)