timescaledb 0.1.0 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -0
- data/.tool-versions +1 -0
- data/.travis.yml +3 -0
- data/Gemfile +3 -5
- data/Gemfile.lock +11 -3
- data/Gemfile.scenic +7 -0
- data/Gemfile.scenic.lock +121 -0
- data/README.md +273 -112
- data/Rakefile +16 -1
- data/bin/console +4 -1
- data/bin/setup +2 -0
- data/bin/tsdb +48 -0
- data/examples/Gemfile +1 -1
- data/examples/Gemfile.lock +2 -2
- data/examples/all_in_one.rb +3 -6
- data/lib/timescale/acts_as_hypertable/core.rb +87 -0
- data/lib/timescale/acts_as_hypertable.rb +62 -0
- data/lib/timescale/chunk.rb +8 -0
- data/lib/timescale/compression_settings.rb +2 -1
- data/lib/timescale/continuous_aggregates.rb +11 -1
- data/lib/timescale/dimensions.rb +6 -0
- data/lib/timescale/hypertable.rb +25 -5
- data/lib/timescale/job.rb +2 -5
- data/lib/timescale/job_stats.rb +8 -3
- data/lib/timescale/migration_helpers.rb +33 -3
- data/lib/timescale/scenic/adapter.rb +55 -0
- data/lib/timescale/schema_dumper.rb +88 -0
- data/lib/timescale/stats_report.rb +35 -0
- data/lib/timescale/version.rb +1 -1
- data/lib/timescale.rb +29 -4
- data/timescale.gemspec +13 -5
- metadata +116 -7
- data/lib/timescale/hypertable_helpers.rb +0 -39
data/README.md
CHANGED
@@ -1,50 +1,196 @@
|
|
1
1
|
# Timescale
|
2
2
|
|
3
|
-
Welcome to the Timescale gem! To experiment with the code, start
|
4
|
-
|
3
|
+
Welcome to the Timescale gem! To experiment with the code, start installing the
|
4
|
+
gem:
|
5
5
|
|
6
6
|
```bash
|
7
|
-
|
8
|
-
cd timescale
|
9
|
-
bundle install
|
7
|
+
gem install timescaledb
|
10
8
|
```
|
11
9
|
|
12
|
-
|
10
|
+
## The `tsdb` CLI
|
11
|
+
|
12
|
+
When you install the gem locally, a new command line application named `tsdb`
|
13
|
+
will be linked in your command line.
|
14
|
+
|
15
|
+
It accepts a Postgresql URI and some extra flags that can help you to get more
|
16
|
+
info from your TimescaleDB server:
|
13
17
|
|
14
18
|
```bash
|
15
|
-
|
19
|
+
tsdb <uri> --stats
|
16
20
|
```
|
17
21
|
|
18
|
-
|
19
|
-
own credentials there!
|
22
|
+
Where the `<uri>` is replaced with params from your connection like:
|
20
23
|
|
21
24
|
```bash
|
22
|
-
|
25
|
+
tsdb postgres://<user>@localhost:5432/<dbname> --stats
|
23
26
|
```
|
24
27
|
|
25
|
-
You can also use `bin/console` without any parameters and it will use the
|
26
|
-
`PG_URI_TEST` from your `.env` file.
|
27
28
|
|
28
|
-
|
29
|
-
`bin/console`. Here is an example from my console:
|
29
|
+
Or just check the stats:
|
30
30
|
|
31
31
|
```bash
|
32
|
-
|
32
|
+
tsdb "postgres://jonatasdp@localhost:5432/timescale_test" --stats
|
33
|
+
```
|
34
|
+
|
35
|
+
These is a sample output from database example with almost no data:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
{:hypertables=>
|
39
|
+
{:count=>3,
|
40
|
+
:uncompressed=>2,
|
41
|
+
:chunks=>{:total=>1, :compressed=>0, :uncompressed=>1},
|
42
|
+
:size=>{:before_compressing=>"80 KB", :after_compressing=>"0 Bytes"}},
|
43
|
+
:continuous_aggregates=>{:count=>1},
|
44
|
+
:jobs_stats=>[{:success=>nil, :runs=>nil, :failures=>nil}]}
|
33
45
|
```
|
34
46
|
|
47
|
+
To start a interactive ruby/[pry](https://github.com/pry/pry) console use `--console`:
|
35
48
|
The console will dynamically create models for all hypertables that it finds
|
36
49
|
in the database.
|
37
50
|
|
38
|
-
|
39
|
-
|
51
|
+
Let's consider the [caggs.sql](https://gist.github.com/jonatas/95573ad8744994094ec9f284150004f9#file-caggs-sql)
|
52
|
+
as the example of database.
|
40
53
|
|
41
|
-
This library was started on [twitch.tv/timescaledb](https://twitch.tv/timescaledb).
|
42
|
-
You can watch all episodes here:
|
43
54
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
55
|
+
```bash
|
56
|
+
psql postgres://jonatasdp@localhost:5432/playground -f caggs.sql
|
57
|
+
```
|
58
|
+
|
59
|
+
Then use `tsdb` in the command line with the same URI and `--stats`:
|
60
|
+
|
61
|
+
```bash
|
62
|
+
tsdb postgres://jonatasdp@localhost:5432/playground --stats
|
63
|
+
{:hypertables=>
|
64
|
+
{:count=>1,
|
65
|
+
:uncompressed=>1,
|
66
|
+
:approximate_row_count=>{"ticks"=>352},
|
67
|
+
:chunks=>{:total=>1, :compressed=>0, :uncompressed=>1},
|
68
|
+
:size=>{:uncompressed=>"88 KB", :compressed=>"0 Bytes"}},
|
69
|
+
:continuous_aggregates=>{:total=>1},
|
70
|
+
:jobs_stats=>[{:success=>nil, :runs=>nil, :failures=>nil}]}
|
71
|
+
```
|
72
|
+
|
73
|
+
To have some interactive playground with the actual database using ruby, just
|
74
|
+
try the same command before changing from `--stats` to `--console`:
|
75
|
+
|
76
|
+
### tsdb --console
|
77
|
+
|
78
|
+
The same database from previous example, is used so
|
79
|
+
the context has a hypertable named `ticks` and a view named `ohlc_1m`.
|
80
|
+
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
tsdb postgres://jonatasdp@localhost:5432/playground --console
|
84
|
+
pry(Timescale)>
|
85
|
+
```
|
86
|
+
|
87
|
+
The `tsdb` CLI will automatically create ActiveRecord models for hypertables and
|
88
|
+
continuous aggregates views.
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
Tick
|
92
|
+
=> Timescale::Tick(time: datetime, symbol: string, price: decimal, volume: integer)
|
93
|
+
```
|
94
|
+
|
95
|
+
Note that it's only created for this session and will never be cached in the
|
96
|
+
library or any other place.
|
97
|
+
|
98
|
+
In this case, `Tick` model comes from `ticks` hypertable that was found in the database.
|
99
|
+
It contains several extra methods inherited from `acts_as_hypertable` macro.
|
100
|
+
|
101
|
+
Let's start with the `.hypertable` method.
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
Tick.hypertable
|
105
|
+
=> #<Timescale::Hypertable:0x00007fe99c258900
|
106
|
+
hypertable_schema: "public",
|
107
|
+
hypertable_name: "ticks",
|
108
|
+
owner: "jonatasdp",
|
109
|
+
num_dimensions: 1,
|
110
|
+
num_chunks: 1,
|
111
|
+
compression_enabled: false,
|
112
|
+
is_distributed: false,
|
113
|
+
replication_factor: nil,
|
114
|
+
data_nodes: nil,
|
115
|
+
tablespaces: nil>
|
116
|
+
```
|
117
|
+
|
118
|
+
The core of the hypertables are the fragmentation of the data into chunks that
|
119
|
+
are the child tables that distribute the data. You can check all chunks directly
|
120
|
+
from the hypertable relation.
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
Tick.hypertable.chunks
|
124
|
+
unknown OID 2206: failed to recognize type of 'primary_dimension_type'. It will be treated as String.
|
125
|
+
=> [#<Timescale::Chunk:0x00007fe99c31b068
|
126
|
+
hypertable_schema: "public",
|
127
|
+
hypertable_name: "ticks",
|
128
|
+
chunk_schema: "_timescaledb_internal",
|
129
|
+
chunk_name: "_hyper_33_17_chunk",
|
130
|
+
primary_dimension: "time",
|
131
|
+
primary_dimension_type: "timestamp without time zone",
|
132
|
+
range_start: 1999-12-30 00:00:00 +0000,
|
133
|
+
range_end: 2000-01-06 00:00:00 +0000,
|
134
|
+
range_start_integer: nil,
|
135
|
+
range_end_integer: nil,
|
136
|
+
is_compressed: false,
|
137
|
+
chunk_tablespace: nil,
|
138
|
+
data_nodes: nil>]
|
139
|
+
```
|
140
|
+
|
141
|
+
> Chunks are created by partitioning a hypertable's data into one
|
142
|
+
> (or potentially multiple) dimensions. All hypertables are partitioned by the
|
143
|
+
> values belonging to a time column, which may be in timestamp, date, or
|
144
|
+
> various integer forms. If the time partitioning interval is one day,
|
145
|
+
> for example, then rows with timestamps that belong to the same day are co-located
|
146
|
+
> within the same chunk, while rows belonging to different days belong to different chunks.
|
147
|
+
> Learn more [here](https://docs.timescale.com/timescaledb/latest/overview/core-concepts/hypertables-and-chunks/).
|
148
|
+
|
149
|
+
Another core concept of TimescaleDB is compression. With data partitioned, it
|
150
|
+
becomes very convenient to compress and decompress chunks independently.
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
Tick.hypertable.chunks.first.compress!
|
154
|
+
ActiveRecord::StatementInvalid: PG::FeatureNotSupported: ERROR: compression not enabled on "ticks"
|
155
|
+
DETAIL: It is not possible to compress chunks on a hypertable that does not have compression enabled.
|
156
|
+
HINT: Enable compression using ALTER TABLE with the timescaledb.compress option.
|
157
|
+
```
|
158
|
+
|
159
|
+
As compression is not enabled, let's do it executing a plain SQL directly from
|
160
|
+
the actual context. To borrow a connection, let's use the Tick object.
|
161
|
+
|
162
|
+
```ruby
|
163
|
+
Tick.connection.execute("ALTER TABLE ticks SET (timescaledb.compress)") # => PG_OK
|
164
|
+
```
|
165
|
+
|
166
|
+
And now, it's possible to compress and decompress:
|
167
|
+
|
168
|
+
```ruby
|
169
|
+
Tick.hypertable.chunks.first.compress!
|
170
|
+
Tick.hypertable.chunks.first.decompress!
|
171
|
+
```
|
172
|
+
Learn more about TimescaleDB compression [here](https://docs.timescale.com/timescaledb/latest/overview/core-concepts/compression/).
|
173
|
+
|
174
|
+
The `ohlc_1m` view is also available as an ActiveRecord:
|
175
|
+
|
176
|
+
```ruby
|
177
|
+
Ohlc1m
|
178
|
+
=> Timescale::Ohlc1m(bucket: datetime, symbol: string, open: decimal, high: decimal, low: decimal, close: decimal, volume: integer)
|
179
|
+
```
|
180
|
+
|
181
|
+
And you can run any query as you do with regular active record queries.
|
182
|
+
|
183
|
+
```ruby
|
184
|
+
Ohlc1m.order(bucket: :desc).last
|
185
|
+
=> #<Timescale::Ohlc1m:0x00007fe99c2c38e0
|
186
|
+
bucket: 2000-01-01 00:00:00 UTC,
|
187
|
+
symbol: "SYMBOL",
|
188
|
+
open: 0.13e2,
|
189
|
+
high: 0.3e2,
|
190
|
+
low: 0.1e1,
|
191
|
+
close: 0.1e2,
|
192
|
+
volume: 27600>
|
193
|
+
```
|
48
194
|
|
49
195
|
## Installation
|
50
196
|
|
@@ -62,13 +208,14 @@ Or install it yourself as:
|
|
62
208
|
|
63
209
|
$ gem install timescaledb
|
64
210
|
|
211
|
+
|
65
212
|
## Usage
|
66
213
|
|
67
|
-
You can check the [all_in_one.rb](examples/all_in_one.rb) that will:
|
214
|
+
You can check the [all_in_one.rb](examples/all_in_one.rb) example that will:
|
68
215
|
|
69
216
|
1. Create hypertable with compression settings
|
70
217
|
2. Insert data
|
71
|
-
3. Run some queries
|
218
|
+
3. Run some queries
|
72
219
|
4. Check chunk size per model
|
73
220
|
5. Compress a chunk
|
74
221
|
6. Check chunk status
|
@@ -96,7 +243,7 @@ create_table(:events, id: false, hypertable: hypertable_options) do |t|
|
|
96
243
|
end
|
97
244
|
```
|
98
245
|
|
99
|
-
####
|
246
|
+
#### create_continuous_aggregate
|
100
247
|
|
101
248
|
This example shows a ticks table grouping ticks as OHLCV histograms for every
|
102
249
|
minute.
|
@@ -118,7 +265,7 @@ end
|
|
118
265
|
Tick = Class.new(ActiveRecord::Base) do
|
119
266
|
self.table_name = 'ticks'
|
120
267
|
self.primary_key = 'symbol'
|
121
|
-
|
268
|
+
acts_as_hypertable
|
122
269
|
end
|
123
270
|
|
124
271
|
query = Tick.select(<<~QUERY)
|
@@ -140,142 +287,156 @@ options = {
|
|
140
287
|
}
|
141
288
|
}
|
142
289
|
|
143
|
-
|
290
|
+
create_continuous_aggregate('ohlc_1m', query, **options)
|
144
291
|
```
|
145
292
|
|
146
|
-
|
293
|
+
#### Scenic integration
|
294
|
+
|
295
|
+
The [Scenic](https://github.com/scenic-views/scenic) gem is an easy way to
|
296
|
+
manage database view definitions for a Rails application. TimescaleDB's
|
297
|
+
continuous aggregates are more complex than regular PostgreSQL views, and
|
298
|
+
the schema dumper included with Scenic can't dump a complete definition.
|
299
|
+
|
300
|
+
This gem automatically configures Scenic to use a `Timescale::Scenic::Adapter`
|
301
|
+
which will correctly handle schema dumping.
|
147
302
|
|
148
|
-
|
303
|
+
### Enable ActsAsHypertable
|
304
|
+
|
305
|
+
You can declare a Rails model as a Hypertable by invoking the `acts_as_hypertable` macro. This macro extends your existing model with timescaledb-related functionality.
|
149
306
|
model:
|
150
307
|
|
151
308
|
```ruby
|
152
309
|
class Event < ActiveRecord::Base
|
153
|
-
|
154
|
-
|
155
|
-
include Timescale::HypertableHelpers
|
310
|
+
acts_as_hypertable
|
156
311
|
end
|
157
312
|
```
|
158
313
|
|
159
|
-
|
160
|
-
model.
|
314
|
+
By default, ActsAsHypertable assumes a record's _time_column_ is called `created_at`.
|
161
315
|
|
162
|
-
###
|
316
|
+
### Options
|
163
317
|
|
164
|
-
|
165
|
-
the model name.
|
318
|
+
If you are using a different time_column name, you can specify it as follows when invoking the `acts_as_hypertable` macro:
|
166
319
|
|
167
320
|
```ruby
|
168
|
-
Event
|
169
|
-
|
170
|
-
|
171
|
-
# hypertable_schema: "public",
|
172
|
-
# hypertable_name: "events",
|
173
|
-
# chunk_schema: "_timescaledb_internal",
|
174
|
-
# chunk_name: "_hyper_180_74_chunk",
|
175
|
-
# primary_dimension: "created_at",
|
176
|
-
# primary_dimension_type: "timestamp without time zone",
|
177
|
-
# range_start: 2021-09-22 21:28:00 +0000,
|
178
|
-
# range_end: 2021-09-22 21:29:00 +0000,
|
179
|
-
# range_start_integer: nil,
|
180
|
-
# range_end_integer: nil,
|
181
|
-
# is_compressed: false,
|
182
|
-
# chunk_tablespace: nil,
|
183
|
-
# data_nodes: nil>
|
321
|
+
class Event < ActiveRecord::Base
|
322
|
+
acts_as_hypertable time_column: :timestamp
|
323
|
+
end
|
184
324
|
```
|
185
325
|
|
186
|
-
|
326
|
+
### Chunks
|
187
327
|
|
188
|
-
|
328
|
+
To get all the chunks from a model's hypertable, you can use `.chunks`.
|
189
329
|
|
190
|
-
|
191
|
-
|
330
|
+
```ruby
|
331
|
+
Event.chunks # => [#<Timescale::Chunk>, ...]
|
332
|
+
```
|
333
|
+
|
334
|
+
### Hypertable metadata
|
335
|
+
|
336
|
+
To get the models' hypertable metadata, you can use `.hypertable`.
|
192
337
|
|
193
338
|
```ruby
|
194
|
-
Event.hypertable
|
195
|
-
# Timescale::Hypertable Load (4.8ms) SELECT "timescaledb_information"."hypertables".* FROM "timescaledb_information"."hypertables" WHERE "timescaledb_information"."hypertables"."hypertable_name" = $1 LIMIT $2 [["hypertable_name", "events"], ["LIMIT", 1]]
|
196
|
-
# => #<Timescale::Hypertable:0x00007f94c3151cd8
|
197
|
-
# hypertable_schema: "public",
|
198
|
-
# hypertable_name: "events",
|
199
|
-
# owner: "jonatasdp",
|
200
|
-
# num_dimensions: 1,
|
201
|
-
# num_chunks: 1,
|
202
|
-
# compression_enabled: true,
|
203
|
-
# is_distributed: false,
|
204
|
-
# replication_factor: nil,
|
205
|
-
# data_nodes: nil,
|
206
|
-
# tablespaces: nil>
|
339
|
+
Event.hypertable # => #<Timescale::Hypertable>
|
207
340
|
```
|
208
341
|
|
209
|
-
|
210
|
-
metadata.
|
342
|
+
To get hypertable metadata for all hypertables: `Timescale.hypertables`.
|
211
343
|
|
212
344
|
### Compression Settings
|
213
345
|
|
214
346
|
Compression settings are accessible through the hypertable.
|
215
347
|
|
216
348
|
```ruby
|
217
|
-
Event.hypertable.compression_settings
|
218
|
-
# Timescale::Hypertable Load (1.2ms) SELECT "timescaledb_information"."hypertables".* FROM "timescaledb_information"."hypertables" WHERE "timescaledb_information"."hypertables"."hypertable_name" = $1 LIMIT $2 [["hypertable_name", "events"], ["LIMIT", 1]]
|
219
|
-
# Timescale::CompressionSettings Load (1.2ms) SELECT "timescaledb_information"."compression_settings".* FROM "timescaledb_information"."compression_settings" WHERE "timescaledb_information"."compression_settings"."hypertable_name" = $1 [["hypertable_name", "events"]]
|
220
|
-
# => [#<Timescale::CompressionSettings:0x00007f94b0bf7010
|
221
|
-
# hypertable_schema: "public",
|
222
|
-
# hypertable_name: "events",
|
223
|
-
# attname: "identifier",
|
224
|
-
# segmentby_column_index: 1,
|
225
|
-
# orderby_column_index: nil,
|
226
|
-
# orderby_asc: nil,
|
227
|
-
# orderby_nullsfirst: nil>,
|
228
|
-
# #<Timescale::CompressionSettings:0x00007f94b0c3e460
|
229
|
-
# hypertable_schema: "public",
|
230
|
-
# hypertable_name: "events",
|
231
|
-
# attname: "created_at",
|
232
|
-
# segmentby_column_index: nil,
|
233
|
-
# orderby_column_index: 1,
|
234
|
-
# orderby_asc: true,
|
235
|
-
# orderby_nullsfirst: false>]
|
349
|
+
Event.hypertable.compression_settings # => [#<Timescale::CompressionSettings>, ...]
|
236
350
|
```
|
237
351
|
|
238
|
-
|
352
|
+
To get compression settings for all hypertables: `Timescale.compression_settings`.
|
353
|
+
|
354
|
+
### Scopes
|
355
|
+
|
356
|
+
When you enable ActsAsHypertable on your model, we include a couple default scopes. They are:
|
357
|
+
|
358
|
+
| Scope name | What they return |
|
359
|
+
|------------------------|---------------------------------------|
|
360
|
+
| `Model.previous_month` | Records created in the previous month |
|
361
|
+
| `Model.previous_week` | Records created in the previous week |
|
362
|
+
| `Model.this_month` | Records created this month |
|
363
|
+
| `Model.this_week` | Records created this week |
|
364
|
+
| `Model.yesterday` | Records created yesterday |
|
365
|
+
| `Model.today` | Records created today |
|
366
|
+
| `Model.last_hour` | Records created in the last hour |
|
239
367
|
|
240
|
-
|
368
|
+
All time-related scopes respect your application's timezone.
|
369
|
+
|
370
|
+
## RSpec Hooks
|
241
371
|
|
242
372
|
In case you want to use TimescaleDB on a Rails environment, you may have some
|
243
|
-
issues as the schema dump used for tests
|
244
|
-
metadata.
|
373
|
+
issues as the schema dump used for tests does not consider hypertables metadata.
|
245
374
|
|
246
|
-
|
247
|
-
|
375
|
+
As a work around, you can dynamically create the hypertables yourself for
|
376
|
+
testing environments using the following hook which you can
|
377
|
+
define in `spec/rspec_helper.rb`:
|
248
378
|
|
249
379
|
```ruby
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
SELECT create_hypertable('#{clazz.table_name}', 'created_at')
|
261
|
-
SQL
|
380
|
+
config.before(:suite) do
|
381
|
+
hypertable_models = ActiveRecord::Base.descendants.select(&:acts_as_hypertable?)
|
382
|
+
|
383
|
+
hypertable_models.each do |klass|
|
384
|
+
table_name = klass.table_name
|
385
|
+
time_column = klass.hypertable_options[:time_column]
|
386
|
+
|
387
|
+
if klass.try(:hypertable).present?
|
388
|
+
ApplicationRecord.logger.info "hypertable already created for '#{table_name}', skipping."
|
389
|
+
next
|
262
390
|
end
|
391
|
+
|
392
|
+
ApplicationRecord.connection.execute <<~SQL
|
393
|
+
SELECT create_hypertable('#{table_name}', '#{time_column.to_s}')
|
394
|
+
SQL
|
263
395
|
end
|
396
|
+
end
|
264
397
|
```
|
265
398
|
|
266
399
|
## Development
|
267
400
|
|
268
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then,
|
401
|
+
After checking out the repo, run `bin/setup` to install the development dependencies. Then, `bundle exec rake test:setup` to setup the test database and tables. Finally, run `bundle exec rake` to run the tests.
|
402
|
+
|
403
|
+
You can also run `tsdb` for an interactive prompt that will allow you to experiment.
|
269
404
|
|
270
405
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
271
406
|
|
407
|
+
You can create a `.env` file locally to run tests locally. Make sure to put your
|
408
|
+
own credentials there!
|
409
|
+
|
410
|
+
```bash
|
411
|
+
PG_URI_TEST="postgres://<user>@localhost:5432/<dbname>"
|
412
|
+
```
|
413
|
+
|
414
|
+
You can put some postgres URI directly as a parameter of
|
415
|
+
`tsdb`. Here is an example from the console:
|
416
|
+
|
417
|
+
```bash
|
418
|
+
tsdb "postgres://jonatasdp@localhost:5432/timescale_test"
|
419
|
+
```
|
420
|
+
|
421
|
+
## More resources
|
422
|
+
|
423
|
+
This library was started on [twitch.tv/timescaledb](https://twitch.tv/timescaledb).
|
424
|
+
You can watch all episodes here:
|
425
|
+
|
426
|
+
1. [Wrapping Functions to Ruby Helpers](https://www.youtube.com/watch?v=hGPsUxLFAYk).
|
427
|
+
2. [Extending ActiveRecord with Timescale Helpers](https://www.youtube.com/watch?v=IEyJIHk1Clk).
|
428
|
+
3. [Setup Hypertables for Rails testing environment](https://www.youtube.com/watch?v=wM6hVrZe7xA).
|
429
|
+
4. [Packing the code to this repository](https://www.youtube.com/watch?v=CMdGAl_XlL4).
|
430
|
+
4. [the code to this repository](https://www.youtube.com/watch?v=CMdGAl_XlL4).
|
431
|
+
5. [Working with Timescale continuous aggregates](https://youtu.be/co4HnBkHzVw).
|
432
|
+
6. [Creating the command-line application in Ruby to explore the Timescale API](https://www.youtube.com/watch?v=I3vM_q2m7T0).
|
433
|
+
|
272
434
|
### TODO
|
273
435
|
|
274
436
|
Here is a list of functions that would be great to have:
|
275
437
|
|
276
438
|
- [ ] Dump and Restore Timescale metadata - Like db/schema.rb but for Timescale configuration.
|
277
439
|
- [ ] Add data nodes support
|
278
|
-
- [ ] Implement the `timescale` CLI to explore the full API.
|
279
440
|
|
280
441
|
## Contributing
|
281
442
|
|
data/Rakefile
CHANGED
@@ -1,6 +1,21 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
2
|
require "rspec/core/rake_task"
|
3
3
|
|
4
|
+
begin
|
5
|
+
require "gemika/tasks"
|
6
|
+
rescue LoadError
|
7
|
+
puts "Run `gem install gemika` for additional tasks"
|
8
|
+
end
|
9
|
+
|
4
10
|
RSpec::Core::RakeTask.new(:spec)
|
5
11
|
|
6
|
-
task :
|
12
|
+
task default: "matrix:spec"
|
13
|
+
|
14
|
+
namespace :test do
|
15
|
+
task :setup do
|
16
|
+
require_relative "spec/spec_helper"
|
17
|
+
|
18
|
+
teardown_tables
|
19
|
+
setup_tables
|
20
|
+
end
|
21
|
+
end
|
data/bin/console
CHANGED
@@ -13,11 +13,14 @@ ActiveRecord::Base.establish_connection(ARGV[0] || uri_from_test)
|
|
13
13
|
|
14
14
|
Timescale::Hypertable.find_each do |hypertable|
|
15
15
|
class_name = hypertable.hypertable_name.singularize.camelize
|
16
|
+
|
16
17
|
model = Class.new(ActiveRecord::Base) do
|
17
18
|
self.table_name = hypertable.hypertable_name
|
18
19
|
self.primary_key = self.column_names.first
|
19
|
-
|
20
|
+
|
21
|
+
acts_as_hypertable
|
20
22
|
end
|
23
|
+
|
21
24
|
Timescale.const_set(class_name, model)
|
22
25
|
end
|
23
26
|
|
data/bin/setup
CHANGED
data/bin/tsdb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require "bundler/setup"
|
3
|
+
require "timescale"
|
4
|
+
require "pry"
|
5
|
+
|
6
|
+
ActiveRecord::Base.establish_connection(ARGV[0])
|
7
|
+
|
8
|
+
Timescale::Hypertable.find_each do |hypertable|
|
9
|
+
class_name = hypertable.hypertable_name.singularize.camelize
|
10
|
+
model = Class.new(ActiveRecord::Base) do
|
11
|
+
self.table_name = hypertable.hypertable_name
|
12
|
+
acts_as_hypertable
|
13
|
+
end
|
14
|
+
Timescale.const_set(class_name, model)
|
15
|
+
end
|
16
|
+
|
17
|
+
Timescale::ContinuousAggregates.find_each do |cagg|
|
18
|
+
class_name = cagg.view_name.singularize.camelize
|
19
|
+
model = Class.new(ActiveRecord::Base) do
|
20
|
+
self.table_name = cagg.view_name
|
21
|
+
acts_as_hypertable
|
22
|
+
end
|
23
|
+
Timescale.const_set(class_name, model)
|
24
|
+
end
|
25
|
+
|
26
|
+
def show(obj)
|
27
|
+
Pry::ColorPrinter.pp(obj)
|
28
|
+
end
|
29
|
+
|
30
|
+
if ARGV.index("--stats")
|
31
|
+
scope = Timescale::Hypertable.all
|
32
|
+
|
33
|
+
if (only = ARGV.index("--only"))
|
34
|
+
only_hypertables = ARGV[only+1].split(",")
|
35
|
+
scope = scope.where({hypertable_name: only_hypertables})
|
36
|
+
end
|
37
|
+
|
38
|
+
if (except = ARGV.index("--except"))
|
39
|
+
except_hypertables = ARGV[except+1].split(",")
|
40
|
+
scope = scope.where.not(hypertable_name: except_hypertables)
|
41
|
+
end
|
42
|
+
|
43
|
+
show(Timescale.stats(scope))
|
44
|
+
end
|
45
|
+
|
46
|
+
if ARGV.index("--console")
|
47
|
+
Pry.start(Timescale)
|
48
|
+
end
|
data/examples/Gemfile
CHANGED
data/examples/Gemfile.lock
CHANGED
data/examples/all_in_one.rb
CHANGED
@@ -2,16 +2,13 @@ require 'bundler/setup'
|
|
2
2
|
require 'timescale'
|
3
3
|
require 'pp'
|
4
4
|
require 'pry'
|
5
|
-
|
6
|
-
|
7
|
-
# set PG_URI=postgres://user:pass@host:port/db_name
|
8
|
-
ActiveRecord::Base.establish_connection(ENV['PG_URI_TEST'])
|
5
|
+
# ruby all_in_one.rb postgres://user:pass@host:port/db_name
|
6
|
+
ActiveRecord::Base.establish_connection( ARGV.last)
|
9
7
|
|
10
8
|
# Simple example
|
11
9
|
class Event < ActiveRecord::Base
|
12
10
|
self.primary_key = "identifier"
|
13
|
-
|
14
|
-
include Timescale::HypertableHelpers
|
11
|
+
acts_as_hypertable
|
15
12
|
end
|
16
13
|
|
17
14
|
# Setup Hypertable as in a migration
|