timescaledb 0.1.0 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
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 cloning the
4
- repository:
3
+ Welcome to the Timescale gem! To experiment with the code, start installing the
4
+ gem:
5
5
 
6
6
  ```bash
7
- git clone https://github.com/jonatas/timescale.git
8
- cd timescale
9
- bundle install
7
+ gem install timescaledb
10
8
  ```
11
9
 
12
- Then you can run `bin/console` for an interactive prompt.
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
- bin/console
19
+ tsdb <uri> --stats
16
20
  ```
17
21
 
18
- You can create a `.env` file locally to run tests locally. Make sure to put your
19
- own credentials there!
22
+ Where the `<uri>` is replaced with params from your connection like:
20
23
 
21
24
  ```bash
22
- PG_URI_TEST="postgres://<user>@localhost:5432/<dbname>"
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
- Alternatively, you can also put some postgres URI directly as a parameter of
29
- `bin/console`. Here is an example from my console:
29
+ Or just check the stats:
30
30
 
31
31
  ```bash
32
- bin/console "postgres://jonatasdp@localhost:5432/timescale_test"
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
- It will allow you to visit any database and have all models mapped as ActiveRecord
39
- with the [HypertableHelpers](lib/timescale/hypertable_helpers.rb).
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
- 1. [Wrapping Functions to Ruby Helpers](https://www.youtube.com/watch?v=hGPsUxLFAYk).
45
- 2. [Extending ActiveRecord with Timescale Helpers](https://www.youtube.com/watch?v=IEyJIHk1Clk).
46
- 3. [Setup Hypertables for Rails testing environment](https://www.youtube.com/watch?v=wM6hVrZe7xA).
47
- 4. [Packing the code to this repository](https://www.youtube.com/watch?v=CMdGAl_XlL4).
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 from HypertableHelpers
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
- #### create_continuous_aggregates
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
- include Timescale::HypertableHelpers
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
- create_continuous_aggregates('ohlc_1m', query, **options)
290
+ create_continuous_aggregate('ohlc_1m', query, **options)
144
291
  ```
145
292
 
146
- ### Hypertable Helpers
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
- You can also use `HypertableHelpers` to get access to some basic scopes for your
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
- self.primary_key = "identifier"
154
-
155
- include Timescale::HypertableHelpers
310
+ acts_as_hypertable
156
311
  end
157
312
  ```
158
313
 
159
- After including the helpers, several methods from timescaledb will be available in the
160
- model.
314
+ By default, ActsAsHypertable assumes a record's _time_column_ is called `created_at`.
161
315
 
162
- ### Chunks
316
+ ### Options
163
317
 
164
- To get chunks from a single hypertable, you can use the `.chunks` directly from
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.chunks
169
- # DEBUG: Timescale::Chunk Load (9.0ms) SELECT "timescaledb_information"."chunks".* FROM "timescaledb_information"."chunks" WHERE "timescaledb_information"."chunks"."hypertable_name" = $1 [["hypertable_name", "events"]]
170
- # => [#<Timescale::Chunk:0x00007f94b0c86008
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
- To get all hypertables you can use `Timescale.hypertables` method.
326
+ ### Chunks
187
327
 
188
- ### Hypertable metadata from model
328
+ To get all the chunks from a model's hypertable, you can use `.chunks`.
189
329
 
190
- To get all details from hypertable, you can access the `.hypertable` from the
191
- model.
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
- You can also use `Timescale.hypertables` to have access of all hypertables
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
- It's also possible to access all data calling `Timescale.compression_settings`.
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
- ### RSpec Hooks
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 is not considering hypertables
244
- metadata.
373
+ issues as the schema dump used for tests does not consider hypertables metadata.
245
374
 
246
- If you add the `Timescale::HypertableHelpers` to your model, you can dynamically
247
- create the hypertable adding this hook to your `spec/rspec_helper.rb` file:
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
- config.before(:suite) do
251
- hypertable_models = ApplicationRecord
252
- .descendants
253
- .select{|clazz| clazz.ancestors.include?( Timescale::HypertableHelpers)}
254
- hypertable_models.each do |clazz|
255
- if clazz.hypertable.exists?
256
- ApplicationRecord.logger.info "skip recreating hypertable for '#{clazz.table_name}'."
257
- next
258
- end
259
- ApplicationRecord.connection.execute <<~SQL
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, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
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 :default => :spec
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
- include Timescale::HypertableHelpers
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
@@ -7,5 +7,7 @@ set -vx
7
7
 
8
8
  bundle install
9
9
 
10
+ bundle install --gemfile Gemfile.scenic
11
+
10
12
  # For running tests it's going to use PG_URI_TEST env variable.
11
13
  # Please make sure you set it properly to a TEST database!"
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
@@ -1,7 +1,7 @@
1
1
 
2
2
  source 'https://rubygems.org'
3
3
 
4
- gem "timescale", path: "../"
4
+ gem "timescaledb", path: "../"
5
5
  gem "pg"
6
6
  gem "activerecord"
7
7
  gem "composite_primary_keys", "~> 6.0"
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- timescale (0.1.0)
4
+ timescaledb (0.1.2)
5
5
  activerecord
6
6
  pg (~> 1.2)
7
7
 
@@ -45,7 +45,7 @@ DEPENDENCIES
45
45
  dotenv (~> 2.7)
46
46
  pg
47
47
  pry
48
- timescale!
48
+ timescaledb!
49
49
 
50
50
  BUNDLED WITH
51
51
  2.1.4
@@ -2,16 +2,13 @@ require 'bundler/setup'
2
2
  require 'timescale'
3
3
  require 'pp'
4
4
  require 'pry'
5
- require 'dotenv'
6
- Dotenv.load!
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