timescaledb 0.2.6 → 0.2.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/timescaledb/acts_as_hypertable/core.rb +1 -1
- data/lib/timescaledb/database/quoting.rb +12 -0
- data/lib/timescaledb/database/schema_statements.rb +168 -0
- data/lib/timescaledb/database/types.rb +17 -0
- data/lib/timescaledb/database.rb +11 -0
- data/lib/timescaledb/toolkit/time_vector.rb +41 -4
- data/lib/timescaledb/version.rb +1 -1
- metadata +6 -95
- data/.github/workflows/ci.yml +0 -72
- data/.gitignore +0 -12
- data/.rspec +0 -3
- data/.ruby-version +0 -1
- data/.tool-versions +0 -1
- data/.travis.yml +0 -9
- data/CODE_OF_CONDUCT.md +0 -74
- data/Fastfile +0 -17
- data/Gemfile +0 -8
- data/Gemfile.lock +0 -75
- data/Gemfile.scenic +0 -7
- data/Gemfile.scenic.lock +0 -119
- data/README.md +0 -490
- data/Rakefile +0 -21
- data/bin/console +0 -28
- data/bin/setup +0 -13
- data/docs/command_line.md +0 -178
- data/docs/img/lttb_example.png +0 -0
- data/docs/img/lttb_sql_vs_ruby.gif +0 -0
- data/docs/img/lttb_zoom.gif +0 -0
- data/docs/index.md +0 -72
- data/docs/migrations.md +0 -76
- data/docs/models.md +0 -78
- data/docs/toolkit.md +0 -507
- data/docs/toolkit_lttb_tutorial.md +0 -557
- data/docs/toolkit_lttb_zoom.md +0 -357
- data/docs/toolkit_ohlc.md +0 -315
- data/docs/videos.md +0 -16
- data/examples/all_in_one/all_in_one.rb +0 -94
- data/examples/all_in_one/benchmark_comparison.rb +0 -108
- data/examples/all_in_one/caggs.rb +0 -93
- data/examples/all_in_one/query_data.rb +0 -78
- data/examples/ranking/.gitattributes +0 -7
- data/examples/ranking/.gitignore +0 -29
- data/examples/ranking/.ruby-version +0 -1
- data/examples/ranking/Gemfile +0 -33
- data/examples/ranking/Gemfile.lock +0 -189
- data/examples/ranking/README.md +0 -166
- data/examples/ranking/Rakefile +0 -6
- data/examples/ranking/app/controllers/application_controller.rb +0 -2
- data/examples/ranking/app/controllers/concerns/.keep +0 -0
- data/examples/ranking/app/jobs/application_job.rb +0 -7
- data/examples/ranking/app/models/application_record.rb +0 -3
- data/examples/ranking/app/models/concerns/.keep +0 -0
- data/examples/ranking/app/models/game.rb +0 -2
- data/examples/ranking/app/models/play.rb +0 -7
- data/examples/ranking/bin/bundle +0 -114
- data/examples/ranking/bin/rails +0 -4
- data/examples/ranking/bin/rake +0 -4
- data/examples/ranking/bin/setup +0 -33
- data/examples/ranking/config/application.rb +0 -39
- data/examples/ranking/config/boot.rb +0 -4
- data/examples/ranking/config/credentials.yml.enc +0 -1
- data/examples/ranking/config/database.yml +0 -86
- data/examples/ranking/config/environment.rb +0 -5
- data/examples/ranking/config/environments/development.rb +0 -60
- data/examples/ranking/config/environments/production.rb +0 -75
- data/examples/ranking/config/environments/test.rb +0 -53
- data/examples/ranking/config/initializers/cors.rb +0 -16
- data/examples/ranking/config/initializers/filter_parameter_logging.rb +0 -8
- data/examples/ranking/config/initializers/inflections.rb +0 -16
- data/examples/ranking/config/initializers/timescale.rb +0 -2
- data/examples/ranking/config/locales/en.yml +0 -33
- data/examples/ranking/config/puma.rb +0 -43
- data/examples/ranking/config/routes.rb +0 -6
- data/examples/ranking/config/storage.yml +0 -34
- data/examples/ranking/config.ru +0 -6
- data/examples/ranking/db/migrate/20220209120747_create_games.rb +0 -10
- data/examples/ranking/db/migrate/20220209120910_create_plays.rb +0 -19
- data/examples/ranking/db/migrate/20220209143347_create_score_per_hours.rb +0 -5
- data/examples/ranking/db/schema.rb +0 -47
- data/examples/ranking/db/seeds.rb +0 -7
- data/examples/ranking/db/views/score_per_hours_v01.sql +0 -7
- data/examples/ranking/lib/tasks/.keep +0 -0
- data/examples/ranking/log/.keep +0 -0
- data/examples/ranking/public/robots.txt +0 -1
- data/examples/ranking/storage/.keep +0 -0
- data/examples/ranking/tmp/.keep +0 -0
- data/examples/ranking/tmp/pids/.keep +0 -0
- data/examples/ranking/tmp/storage/.keep +0 -0
- data/examples/ranking/vendor/.keep +0 -0
- data/examples/toolkit-demo/compare_volatility.rb +0 -104
- data/examples/toolkit-demo/lttb/README.md +0 -15
- data/examples/toolkit-demo/lttb/lttb.rb +0 -92
- data/examples/toolkit-demo/lttb/lttb_sinatra.rb +0 -139
- data/examples/toolkit-demo/lttb/lttb_test.rb +0 -21
- data/examples/toolkit-demo/lttb/views/index.erb +0 -27
- data/examples/toolkit-demo/lttb-zoom/README.md +0 -13
- data/examples/toolkit-demo/lttb-zoom/lttb_zoomable.rb +0 -90
- data/examples/toolkit-demo/lttb-zoom/views/index.erb +0 -33
- data/examples/toolkit-demo/ohlc.rb +0 -175
- data/mkdocs.yml +0 -34
- data/timescaledb.gemspec +0 -40
data/bin/console
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require "bundler/setup"
|
4
|
-
require "timescaledb"
|
5
|
-
|
6
|
-
def uri_from_test
|
7
|
-
require 'dotenv'
|
8
|
-
Dotenv.load!
|
9
|
-
ENV['PG_URI_TEST']
|
10
|
-
end
|
11
|
-
|
12
|
-
ActiveRecord::Base.establish_connection(ARGV[0] || uri_from_test)
|
13
|
-
|
14
|
-
Timescaledb::Hypertable.find_each do |hypertable|
|
15
|
-
class_name = hypertable.hypertable_name.singularize.camelize
|
16
|
-
|
17
|
-
model = Class.new(ActiveRecord::Base) do
|
18
|
-
self.table_name = hypertable.hypertable_name
|
19
|
-
self.primary_key = self.column_names.first
|
20
|
-
|
21
|
-
acts_as_hypertable
|
22
|
-
end
|
23
|
-
|
24
|
-
Timescaledb.const_set(class_name, model)
|
25
|
-
end
|
26
|
-
|
27
|
-
require "pry"
|
28
|
-
Pry.start(Timescaledb)
|
data/bin/setup
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
#!/usr/bin/env bash
|
2
|
-
set -euo pipefail
|
3
|
-
IFS=$'\n\t'
|
4
|
-
|
5
|
-
|
6
|
-
set -vx
|
7
|
-
|
8
|
-
bundle install
|
9
|
-
|
10
|
-
bundle install --gemfile Gemfile.scenic
|
11
|
-
|
12
|
-
# For running tests it's going to use PG_URI_TEST env variable.
|
13
|
-
# Please make sure you set it properly to a TEST database!"
|
data/docs/command_line.md
DELETED
@@ -1,178 +0,0 @@
|
|
1
|
-
# Command line application
|
2
|
-
|
3
|
-
When you install the gem locally, a new command line application named `tsdb` will be available on your command line.
|
4
|
-
|
5
|
-
## The `tsdb` CLI
|
6
|
-
|
7
|
-
It accepts a Postgresql URI and some extra flags that can help you to get more info from your TimescaleDB server:
|
8
|
-
|
9
|
-
```bash
|
10
|
-
tsdb <uri> --stats
|
11
|
-
```
|
12
|
-
|
13
|
-
Where the `<uri>` is replaced with params from your connection like:
|
14
|
-
|
15
|
-
```bash
|
16
|
-
tsdb postgres://<user>@localhost:5432/<dbname> --stats
|
17
|
-
```
|
18
|
-
|
19
|
-
Or merely check the stats:
|
20
|
-
|
21
|
-
```bash
|
22
|
-
tsdb "postgres://<user>@localhost:5432/timescaledb_test" --stats
|
23
|
-
```
|
24
|
-
|
25
|
-
Here is a sample output from a database example with almost no data:
|
26
|
-
|
27
|
-
```ruby
|
28
|
-
{:hypertables=>
|
29
|
-
{:count=>3,
|
30
|
-
:uncompressed=>2,
|
31
|
-
:chunks=>{:total=>1, :compressed=>0, :uncompressed=>1},
|
32
|
-
:size=>{:befoe_compressing=>"80 KB", :after_compressing=>"0 Bytes"}},
|
33
|
-
:continuous_aggregates=>{:count=>1},
|
34
|
-
:jobs_stats=>[{:success=>nil, :runs=>nil, :failures=>nil}]}
|
35
|
-
```
|
36
|
-
|
37
|
-
To start a interactive ruby/[pry](https://github.com/pry/pry) console use `--console`:
|
38
|
-
The console will dynamically create models for all hypertables that it finds
|
39
|
-
in the database.
|
40
|
-
|
41
|
-
Let's consider the [caggs.sql](https://gist.github.com/jonatas/95573ad8744994094ec9f284150004f9#file-caggs-sql) as the example of a database.
|
42
|
-
|
43
|
-
|
44
|
-
```bash
|
45
|
-
psql postgres://<user>@localhost:5432/playground -f caggs.sql
|
46
|
-
```
|
47
|
-
|
48
|
-
Then use `tsdb` in the command line with the same URI and `--stats`:
|
49
|
-
|
50
|
-
```ruby
|
51
|
-
tsdb postgres://<user>@localhost:5432/playground --stats
|
52
|
-
{:hypertables=>
|
53
|
-
{:count=>1,
|
54
|
-
:uncompressed=>1,
|
55
|
-
:approximate_row_count=>{"ticks"=>352},
|
56
|
-
:chunks=>{:total=>1, :compressed=>0, :uncompressed=>1},
|
57
|
-
:size=>{:uncompressed=>"88 KB", :compressed=>"0 Bytes"}},
|
58
|
-
:continuous_aggregates=>{:total=>1},
|
59
|
-
:jobs_stats=>[{:success=>nil, :runs=>nil, :failures=>nil}]}
|
60
|
-
```
|
61
|
-
|
62
|
-
To have some interactive playground with the actual database using ruby, just
|
63
|
-
try the same command before changing from `--stats` to `--console`:
|
64
|
-
|
65
|
-
### tsdb --console
|
66
|
-
|
67
|
-
We are using the same database from the previous example for this context which contains a hypertable named `ticks` and a view called `ohlc_1m`.
|
68
|
-
|
69
|
-
|
70
|
-
```ruby
|
71
|
-
tsdb postgres://<user>@localhost:5432/playground --console
|
72
|
-
pry(Timescale)>
|
73
|
-
```
|
74
|
-
|
75
|
-
The `tsdb` CLI will automatically create ActiveRecord models for hypertables and the continuous aggregates views.
|
76
|
-
|
77
|
-
```ruby
|
78
|
-
Tick
|
79
|
-
=> Timescaledb::Tick(time: datetime, symbol: string, price: decimal, volume: integer)
|
80
|
-
```
|
81
|
-
|
82
|
-
Note that it's only created for this session and will never cache in the
|
83
|
-
library or any other place.
|
84
|
-
|
85
|
-
In this case, the `Tick` model comes from the `ticks` hypertable found in the database.
|
86
|
-
It contains several methods inherited from the `acts_as_hypertable` macro.
|
87
|
-
|
88
|
-
Let's start with the `.hypertable` method.
|
89
|
-
|
90
|
-
```ruby
|
91
|
-
Tick.hypertable
|
92
|
-
=> #<Timescaledb::Hypertable:0x00007fe99c258900
|
93
|
-
hypertable_schema: "public",
|
94
|
-
hypertable_name: "ticks",
|
95
|
-
owner: "jonatasdp",
|
96
|
-
num_dimensions: 1,
|
97
|
-
num_chunks: 1,
|
98
|
-
compression_enabled: false,
|
99
|
-
is_distributed: false,
|
100
|
-
replication_factor: nil,
|
101
|
-
data_nodes: nil,
|
102
|
-
tablespaces: nil>
|
103
|
-
```
|
104
|
-
|
105
|
-
The core of the hypertables is the fragmentation of the data into chunks, the child tables that distribute the data. You can check all chunks directly from the hypertable relation.
|
106
|
-
|
107
|
-
```ruby
|
108
|
-
Tick.hypertable.chunks
|
109
|
-
unknown OID 2206: failed to recognize type of 'primary_dimension_type'. It will cast as a String.
|
110
|
-
=> [#<Timescaledb::Chunk:0x00007fe99c31b068
|
111
|
-
hypertable_schema: "public",
|
112
|
-
hypertable_name: "ticks",
|
113
|
-
chunk_schema: "_timescaledb_internal",
|
114
|
-
chunk_name: "_hyper_33_17_chunk",
|
115
|
-
primary_dimension: "time",
|
116
|
-
primary_dimension_type: "timestamp without time zone",
|
117
|
-
range_start: 1999-12-30 00:00:00 +0000,
|
118
|
-
range_end: 2000-01-06 00:00:00 +0000,
|
119
|
-
range_start_integer: nil,
|
120
|
-
range_end_integer: nil,
|
121
|
-
is_compressed: false,
|
122
|
-
chunk_tablespace: nil,
|
123
|
-
data_nodes: nil>]
|
124
|
-
```
|
125
|
-
|
126
|
-
> Chunks are created by partitioning the hypertable data into one
|
127
|
-
> (or potentially multiple) dimensions. All hypertables are partitions by the
|
128
|
-
> values belonging to a time column, which may be in timestamp, date, or
|
129
|
-
> various integer forms. If the time partitioning interval is one day,
|
130
|
-
> for example, then rows with timestamps that belong to the same day are co-located
|
131
|
-
> within the same chunk, while rows belonging to different days belong to different chunks.
|
132
|
-
> Learn more [here](https://docs.timescale.com/timescaledb/latest/overview/core-concepts/hypertables-and-chunks/).
|
133
|
-
|
134
|
-
Another core concept of TimescaleDB is compression. With data partitioned, it
|
135
|
-
becomes very convenient to compress and decompress chunks independently.
|
136
|
-
|
137
|
-
```ruby
|
138
|
-
Tick.hypertable.chunks.first.compress!
|
139
|
-
ActiveRecord::StatementInvalid: PG::FeatureNotSupported: ERROR: compression not enabled on "ticks"
|
140
|
-
DETAIL: It is not possible to compress chunks on a hypertable that does not have compression enabled.
|
141
|
-
HINT: Enable compression using ALTER TABLE with the timescaledb.compress option.
|
142
|
-
```
|
143
|
-
|
144
|
-
As compression is not enabled, let's do it by executing plain SQL directly from the actual context. To borrow a connection, let's use the Tick object.
|
145
|
-
|
146
|
-
```ruby
|
147
|
-
Tick.connection.execute("ALTER TABLE ticks SET (timescaledb.compress)") # => PG_OK
|
148
|
-
```
|
149
|
-
|
150
|
-
And now, it's possible to compress and decompress:
|
151
|
-
|
152
|
-
```ruby
|
153
|
-
Tick.hypertable.chunks.first.compress!
|
154
|
-
Tick.hypertable.chunks.first.decompress!
|
155
|
-
```
|
156
|
-
Learn more about TimescaleDB compression [here](https://docs.timescale.com/timescaledb/latest/overview/core-concepts/compression/).
|
157
|
-
|
158
|
-
The `ohlc_1m` view is also available as an ActiveRecord:
|
159
|
-
|
160
|
-
```ruby
|
161
|
-
Ohlc1m
|
162
|
-
=> Timescaledb::Ohlc1m(bucket: datetime, symbol: string, open: decimal, high: decimal, low: decimal, close: decimal, volume: integer)
|
163
|
-
```
|
164
|
-
|
165
|
-
And you can run any query as you do with regular active record queries.
|
166
|
-
|
167
|
-
```ruby
|
168
|
-
Ohlc1m.order(bucket: :desc).last
|
169
|
-
=> #<Timescaledb::Ohlc1m:0x00007fe99c2c38e0
|
170
|
-
bucket: 2000-01-01 00:00:00 UTC,
|
171
|
-
symbol: "SYMBOL",
|
172
|
-
open: 0.13e2,
|
173
|
-
high: 0.3e2,
|
174
|
-
low: 0.1e1,
|
175
|
-
close: 0.1e2,
|
176
|
-
volume: 27600>
|
177
|
-
```
|
178
|
-
|
data/docs/img/lttb_example.png
DELETED
Binary file
|
Binary file
|
data/docs/img/lttb_zoom.gif
DELETED
Binary file
|
data/docs/index.md
DELETED
@@ -1,72 +0,0 @@
|
|
1
|
-
# The TimescaleDB Ruby Gem
|
2
|
-
|
3
|
-
Welcome to the TimescaleDB gem! To experiment with the code, start installing the
|
4
|
-
gem:
|
5
|
-
|
6
|
-
## Installing
|
7
|
-
|
8
|
-
You can install the gem locally:
|
9
|
-
|
10
|
-
```bash
|
11
|
-
gem install timescaledb
|
12
|
-
```
|
13
|
-
|
14
|
-
Or require it directly in the Gemfile of your project:
|
15
|
-
|
16
|
-
```ruby
|
17
|
-
gem "timescaledb"
|
18
|
-
```
|
19
|
-
|
20
|
-
## Features
|
21
|
-
|
22
|
-
* The model can use the [acts_as_hypertable](https://github.com/jonatas/timescaledb/tree/master/lib/timescaledb/acts_as_hypertable.rb) macro. Check more on [models](models) documentation.
|
23
|
-
* The ActiveRecord [migrations](migrations) can use the [create_table](https://github.com/jonatas/timescaledb/tree/master/lib/timescaledb/migration_helpers.rb) supporting the `hypertable` keyword. It's also enabling you to add retention and continuous aggregates policies
|
24
|
-
* A standalone `create_hypertable` macro is also allowed in the migrations.
|
25
|
-
* Testing also becomes easier as the [schema dumper](https://github.com/jonatas/timescaledb/tree/master/lib/timescaledb/schema_dumper.rb) will automatically introduce the hypertables to all environments.
|
26
|
-
* It also contains a [scenic extension](https://github.com/jonatas/timescaledb/tree/master/lib/timescaledb/scenic/extension.rb) to work with [scenic views](https://github.com/scenic-views/scenic) as it's a wide adoption in the community.
|
27
|
-
* The gem is also packed with a [command line utility](command_line) that makes it easier to navigate in your database with Pry and all your hypertables available in a Ruby style.
|
28
|
-
|
29
|
-
## Examples
|
30
|
-
|
31
|
-
The [all_in_one](https://github.com/jonatas/timescaledb/tree/master/examples/all_in_one/all_in_one.rb) example shows:
|
32
|
-
|
33
|
-
1. Create a hypertable with compression settings
|
34
|
-
2. Insert data
|
35
|
-
3. Run some queries
|
36
|
-
4. Check chunk size per model
|
37
|
-
5. Compress a chunk
|
38
|
-
6. Check chunk status
|
39
|
-
7. Decompress a chunk
|
40
|
-
|
41
|
-
The [ranking](https://github.com/jonatas/timescaledb/tree/master/examples/ranking) example shows how to configure a Rails app and navigate all the features available.
|
42
|
-
|
43
|
-
|
44
|
-
## Toolkit examples
|
45
|
-
|
46
|
-
There are also examples in the [toolkit-demo](https://github.com/jonatas/timescaledb/tree/master/examples/toolkit-demo) folder that can help you to
|
47
|
-
understand how to properly use the toolkit functions.
|
48
|
-
|
49
|
-
* [ohlc](https://github.com/jonatas/timescaledb/tree/master/examples/toolkit-demo/ohlc.rb) is a funtion that groups data by Open, High, Low, Close and make histogram availables to group the data, very useful for financial analysis.
|
50
|
-
* While building the [LTTB tutorial]( https://jonatas.github.io/timescaledb/toolkit_lttb_tutorial/) I created the [lttb](https://github.com/jonatas/timescaledb/tree/master/examples/toolkit-demo/lttb) is a simple charting using the Largest Triangle Three Buckets and there. A [zoomable](https://github.com/jonatas/timescaledb/tree/master/examples/toolkit-demo/lttb-zoom) version which allows to navigate in the data and zoom it keeping the same data resolution is also available.
|
51
|
-
* A small example showing how to process [volatility](https://github.com/jonatas/timescaledb/blob/master/examples/toolkit-demo/compare_volatility.rb) is also good to get familiar with the pipeline functions. A benchmark implementing the same in Ruby is also available to check how it compares to the SQL implementation.
|
52
|
-
|
53
|
-
|
54
|
-
## Extra resources
|
55
|
-
|
56
|
-
If you need extra help, please join the fantastic [timescale community](https://www.timescale.com/community)
|
57
|
-
or ask your question on [StackOverflow](https://stackoverflow.com/questions/tagged/timescaledb) using the `#timescaledb` tag.
|
58
|
-
|
59
|
-
If you want to go deeper in the library, the [videos](videos) links to all
|
60
|
-
live-coding sessions showed how [@jonatasdp](https://twitter.com/jonatasdp) built the gem.
|
61
|
-
|
62
|
-
## Contributing
|
63
|
-
|
64
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/jonatas/timescaledb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/jonatas/timescaledb/blob/master/CODE_OF_CONDUCT.md).
|
65
|
-
|
66
|
-
## License
|
67
|
-
|
68
|
-
The gem is available as open source under the [MIT License](https://opensource.org/licenses/MIT).
|
69
|
-
|
70
|
-
## Code of Conduct
|
71
|
-
|
72
|
-
Everyone interacting in the Timescale project's codebases, issue trackers, chat rooms, and mailing lists is expected to follow the [code of conduct](https://github.com/jonatas/timescaledb/blob/master/CODE_OF_CONDUCT.md).
|
data/docs/migrations.md
DELETED
@@ -1,76 +0,0 @@
|
|
1
|
-
# ActiveRecord migrations helpers for Timescale
|
2
|
-
|
3
|
-
Create table is now with the `hypertable` keyword allowing to pass a few options
|
4
|
-
to the function call while also using the `create_table` method:
|
5
|
-
|
6
|
-
## create_table with the `:hypertable` option
|
7
|
-
|
8
|
-
```ruby
|
9
|
-
hypertable_options = {
|
10
|
-
time_column: 'created_at',
|
11
|
-
chunk_time_interval: '1 min',
|
12
|
-
compress_segmentby: 'identifier',
|
13
|
-
compression_interval: '7 days'
|
14
|
-
}
|
15
|
-
|
16
|
-
create_table(:events, id: false, hypertable: hypertable_options) do |t|
|
17
|
-
t.string :identifier, null: false
|
18
|
-
t.jsonb :payload
|
19
|
-
t.timestamps
|
20
|
-
end
|
21
|
-
```
|
22
|
-
|
23
|
-
## The `create_continuous_aggregate` helper
|
24
|
-
|
25
|
-
This example shows a ticks table grouping ticks as OHLCV histograms for every
|
26
|
-
minute.
|
27
|
-
|
28
|
-
```ruby
|
29
|
-
hypertable_options = {
|
30
|
-
time_column: 'created_at',
|
31
|
-
chunk_time_interval: '1 min',
|
32
|
-
compress_segmentby: 'symbol',
|
33
|
-
compress_orderby: 'created_at',
|
34
|
-
compression_interval: '7 days'
|
35
|
-
}
|
36
|
-
create_table :ticks, hypertable: hypertable_options, id: false do |t|
|
37
|
-
t.string :symbol
|
38
|
-
t.decimal :price
|
39
|
-
t.integer :volume
|
40
|
-
t.timestamps
|
41
|
-
end
|
42
|
-
Tick = Class.new(ActiveRecord::Base) do
|
43
|
-
self.table_name = 'ticks'
|
44
|
-
self.primary_key = 'symbol'
|
45
|
-
acts_as_hypertable
|
46
|
-
end
|
47
|
-
|
48
|
-
query = Tick.select(<<~QUERY)
|
49
|
-
time_bucket('1m', created_at) as time,
|
50
|
-
symbol,
|
51
|
-
FIRST(price, created_at) as open,
|
52
|
-
MAX(price) as high,
|
53
|
-
MIN(price) as low,
|
54
|
-
LAST(price, created_at) as close,
|
55
|
-
SUM(volume) as volume").group("1,2")
|
56
|
-
QUERY
|
57
|
-
|
58
|
-
options = {
|
59
|
-
with_data: false,
|
60
|
-
refresh_policies: {
|
61
|
-
start_offset: "INTERVAL '1 month'",
|
62
|
-
end_offset: "INTERVAL '1 minute'",
|
63
|
-
schedule_interval: "INTERVAL '1 minute'"
|
64
|
-
}
|
65
|
-
}
|
66
|
-
|
67
|
-
create_continuous_aggregate('ohlc_1m', query, **options)
|
68
|
-
```
|
69
|
-
|
70
|
-
If you need more details, please check this [blog post][1].
|
71
|
-
|
72
|
-
If you're interested in candlesticks and need to get the OHLC values, take a look
|
73
|
-
at the [toolkit ohlc](/toolkit_ohlc) function that do the same but through a
|
74
|
-
function that can be reusing candlesticks from smaller timeframes.
|
75
|
-
|
76
|
-
[1]: https://ideia.me/timescale-continuous-aggregates-with-ruby
|
data/docs/models.md
DELETED
@@ -1,78 +0,0 @@
|
|
1
|
-
# Models
|
2
|
-
|
3
|
-
The ActiveRecord is the default ORM in the Ruby community. We have introduced a macro that helps you to inject the behavior as other libraries do in the Rails ecosystem.
|
4
|
-
|
5
|
-
## The `acts_as_hypertable` macro
|
6
|
-
|
7
|
-
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.
|
8
|
-
model:
|
9
|
-
|
10
|
-
```ruby
|
11
|
-
class Event < ActiveRecord::Base
|
12
|
-
acts_as_hypertable
|
13
|
-
end
|
14
|
-
```
|
15
|
-
|
16
|
-
By default, ActsAsHypertable assumes a record's _time_column_ is called `created_at`.
|
17
|
-
|
18
|
-
### Options
|
19
|
-
|
20
|
-
If you are using a different time_column name, you can specify it as follows when invoking the `acts_as_hypertable` macro:
|
21
|
-
|
22
|
-
```ruby
|
23
|
-
class Event < ActiveRecord::Base
|
24
|
-
acts_as_hypertable time_column :timestamp
|
25
|
-
end
|
26
|
-
```
|
27
|
-
|
28
|
-
### Chunks
|
29
|
-
|
30
|
-
To get all the chunks from a model's hypertable, you can use `.chunks`.
|
31
|
-
|
32
|
-
```ruby
|
33
|
-
Event.chunks # => [#<Timescaledb::Chunk>, ...]
|
34
|
-
```
|
35
|
-
|
36
|
-
### Hypertable metadata
|
37
|
-
|
38
|
-
To get the models' hypertable metadata, you can use `.hypertable`.
|
39
|
-
|
40
|
-
```ruby
|
41
|
-
Event.hypertable # => #<Timescaledb::Hypertable>
|
42
|
-
```
|
43
|
-
|
44
|
-
To get hypertable metadata for all hypertables: `Timescaledb.hypertables`.
|
45
|
-
|
46
|
-
### Compression Settings
|
47
|
-
|
48
|
-
Compression settings are accessible through the hypertable.
|
49
|
-
|
50
|
-
```ruby
|
51
|
-
Event.hypertable.compression_settings # => [#<Timescaledb::CompressionSettings>, ...]
|
52
|
-
```
|
53
|
-
|
54
|
-
To get compression settings for all hypertables: `Timescaledb.compression_settings`.
|
55
|
-
|
56
|
-
### Scopes
|
57
|
-
|
58
|
-
When you enable ActsAsHypertable on your model, we include a few default scopes. They are:
|
59
|
-
|
60
|
-
| Scope name | What they return |
|
61
|
-
|------------------------|---------------------------------------|
|
62
|
-
| `Model.previous_month` | Records created in the previous month |
|
63
|
-
| `Model.previous_week` | Records created in the previous week |
|
64
|
-
| `Model.this_month` | Records created this month |
|
65
|
-
| `Model.this_week` | Records created this week |
|
66
|
-
| `Model.yesterday` | Records created yesterday |
|
67
|
-
| `Model.today` | Records created today |
|
68
|
-
| `Model.last_hour` | Records created in the last hour |
|
69
|
-
|
70
|
-
All time-related scopes respect your application's timezone.
|
71
|
-
|
72
|
-
|
73
|
-
## Scenic integration
|
74
|
-
|
75
|
-
The [Scenic](https://github.com/scenic-views/scenic) gem is easy to
|
76
|
-
manage database view definitions for a Rails application. Unfortunately, TimescaleDB's continuous aggregates are more complex than regular PostgreSQL views, and the schema dumper included with Scenic can't dump a complete definition.
|
77
|
-
|
78
|
-
This gem automatically configures Scenic to use a `Timescaledb::Scenic::Adapter.` which will correctly handle schema dumping.
|