-
Notifications
You must be signed in to change notification settings - Fork 20
/
core.rb
87 lines (72 loc) · 2.83 KB
/
core.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# frozen_string_literal: true
module Timescaledb
module ActsAsHypertable
module Core
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def time_column
@time_column ||= hypertable_options[:time_column] || :created_at
end
protected
def define_association_scopes
scope :chunks, -> do
Chunk.where(hypertable_name: table_name)
end
scope :hypertable, -> do
Hypertable.find_by(hypertable_name: table_name)
end
scope :jobs, -> do
Job.where(hypertable_name: table_name)
end
scope :job_stats, -> do
JobStats.where(hypertable_name: table_name)
end
scope :compression_settings, -> do
CompressionSettings.where(hypertable_name: table_name)
end
scope :caggs, -> do
ContinuousAggregates.where(hypertable_name: table_name)
end
end
def define_default_scopes
scope :previous_month, -> do
where(
"DATE(#{time_column}) >= :start_time AND DATE(#{time_column}) <= :end_time",
start_time: Date.today.last_month.in_time_zone.beginning_of_month.to_date,
end_time: Date.today.last_month.in_time_zone.end_of_month.to_date
)
end
scope :previous_week, -> do
where(
"DATE(#{time_column}) >= :start_time AND DATE(#{time_column}) <= :end_time",
start_time: Date.today.last_week.in_time_zone.beginning_of_week.to_date,
end_time: Date.today.last_week.in_time_zone.end_of_week.to_date
)
end
scope :this_month, -> do
where(
"DATE(#{time_column}) >= :start_time AND DATE(#{time_column}) <= :end_time",
start_time: Date.today.in_time_zone.beginning_of_month.to_date,
end_time: Date.today.in_time_zone.end_of_month.to_date
)
end
scope :this_week, -> do
where(
"DATE(#{time_column}) >= :start_time AND DATE(#{time_column}) <= :end_time",
start_time: Date.today.in_time_zone.beginning_of_week.to_date,
end_time: Date.today.in_time_zone.end_of_week.to_date
)
end
scope :yesterday, -> { where("DATE(#{time_column}) = ?", Date.yesterday.in_time_zone.to_date) }
scope :today, -> { where("DATE(#{time_column}) = ?", Date.today.in_time_zone.to_date) }
scope :last_hour, -> { where("#{time_column} between ? and ?", 1.hour.ago.in_time_zone, Time.now.end_of_hour.in_time_zone) }
end
def normalize_hypertable_options
hypertable_options[:time_column] = hypertable_options[:time_column].to_sym
end
end
end
end
end