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/Gemfile.lock
DELETED
@@ -1,75 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
timescaledb (0.2.6)
|
5
|
-
activerecord
|
6
|
-
activesupport
|
7
|
-
pg (~> 1.2)
|
8
|
-
|
9
|
-
GEM
|
10
|
-
remote: https://rubygems.org/
|
11
|
-
specs:
|
12
|
-
activemodel (6.1.4.1)
|
13
|
-
activesupport (= 6.1.4.1)
|
14
|
-
activerecord (6.1.4.1)
|
15
|
-
activemodel (= 6.1.4.1)
|
16
|
-
activesupport (= 6.1.4.1)
|
17
|
-
activesupport (6.1.4.1)
|
18
|
-
concurrent-ruby (~> 1.0, >= 1.0.2)
|
19
|
-
i18n (>= 1.6, < 2)
|
20
|
-
minitest (>= 5.1)
|
21
|
-
tzinfo (~> 2.0)
|
22
|
-
zeitwerk (~> 2.3)
|
23
|
-
coderay (1.1.3)
|
24
|
-
concurrent-ruby (1.1.9)
|
25
|
-
database_cleaner-active_record (2.0.1)
|
26
|
-
activerecord (>= 5.a)
|
27
|
-
database_cleaner-core (~> 2.0.0)
|
28
|
-
database_cleaner-core (2.0.1)
|
29
|
-
diff-lcs (1.4.4)
|
30
|
-
dotenv (2.7.6)
|
31
|
-
gemika (0.6.1)
|
32
|
-
i18n (1.8.10)
|
33
|
-
concurrent-ruby (~> 1.0)
|
34
|
-
method_source (1.0.0)
|
35
|
-
minitest (5.14.4)
|
36
|
-
pg (1.4.5)
|
37
|
-
pry (0.14.1)
|
38
|
-
coderay (~> 1.1)
|
39
|
-
method_source (~> 1.0)
|
40
|
-
rake (12.3.3)
|
41
|
-
rspec (3.10.0)
|
42
|
-
rspec-core (~> 3.10.0)
|
43
|
-
rspec-expectations (~> 3.10.0)
|
44
|
-
rspec-mocks (~> 3.10.0)
|
45
|
-
rspec-core (3.10.1)
|
46
|
-
rspec-support (~> 3.10.0)
|
47
|
-
rspec-expectations (3.10.1)
|
48
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
49
|
-
rspec-support (~> 3.10.0)
|
50
|
-
rspec-its (1.3.0)
|
51
|
-
rspec-core (>= 3.0.0)
|
52
|
-
rspec-expectations (>= 3.0.0)
|
53
|
-
rspec-mocks (3.10.2)
|
54
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
55
|
-
rspec-support (~> 3.10.0)
|
56
|
-
rspec-support (3.10.2)
|
57
|
-
tzinfo (2.0.4)
|
58
|
-
concurrent-ruby (~> 1.0)
|
59
|
-
zeitwerk (2.4.2)
|
60
|
-
|
61
|
-
PLATFORMS
|
62
|
-
ruby
|
63
|
-
|
64
|
-
DEPENDENCIES
|
65
|
-
database_cleaner-active_record
|
66
|
-
dotenv
|
67
|
-
gemika
|
68
|
-
pry
|
69
|
-
rake (~> 12.0)
|
70
|
-
rspec (~> 3.0)
|
71
|
-
rspec-its
|
72
|
-
timescaledb!
|
73
|
-
|
74
|
-
BUNDLED WITH
|
75
|
-
2.2.31
|
data/Gemfile.scenic
DELETED
data/Gemfile.scenic.lock
DELETED
@@ -1,119 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
timescaledb (0.2.6)
|
5
|
-
activerecord
|
6
|
-
activesupport
|
7
|
-
pg (~> 1.2)
|
8
|
-
|
9
|
-
GEM
|
10
|
-
remote: https://rubygems.org/
|
11
|
-
specs:
|
12
|
-
actionpack (6.1.4.1)
|
13
|
-
actionview (= 6.1.4.1)
|
14
|
-
activesupport (= 6.1.4.1)
|
15
|
-
rack (~> 2.0, >= 2.0.9)
|
16
|
-
rack-test (>= 0.6.3)
|
17
|
-
rails-dom-testing (~> 2.0)
|
18
|
-
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
19
|
-
actionview (6.1.4.1)
|
20
|
-
activesupport (= 6.1.4.1)
|
21
|
-
builder (~> 3.1)
|
22
|
-
erubi (~> 1.4)
|
23
|
-
rails-dom-testing (~> 2.0)
|
24
|
-
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
25
|
-
activemodel (6.1.4.1)
|
26
|
-
activesupport (= 6.1.4.1)
|
27
|
-
activerecord (6.1.4.1)
|
28
|
-
activemodel (= 6.1.4.1)
|
29
|
-
activesupport (= 6.1.4.1)
|
30
|
-
activesupport (6.1.4.1)
|
31
|
-
concurrent-ruby (~> 1.0, >= 1.0.2)
|
32
|
-
i18n (>= 1.6, < 2)
|
33
|
-
minitest (>= 5.1)
|
34
|
-
tzinfo (~> 2.0)
|
35
|
-
zeitwerk (~> 2.3)
|
36
|
-
builder (3.2.4)
|
37
|
-
coderay (1.1.3)
|
38
|
-
concurrent-ruby (1.1.9)
|
39
|
-
crass (1.0.6)
|
40
|
-
database_cleaner-active_record (2.0.1)
|
41
|
-
activerecord (>= 5.a)
|
42
|
-
database_cleaner-core (~> 2.0.0)
|
43
|
-
database_cleaner-core (2.0.1)
|
44
|
-
diff-lcs (1.4.4)
|
45
|
-
dotenv (2.7.6)
|
46
|
-
erubi (1.10.0)
|
47
|
-
gemika (0.6.1)
|
48
|
-
i18n (1.8.11)
|
49
|
-
concurrent-ruby (~> 1.0)
|
50
|
-
loofah (2.12.0)
|
51
|
-
crass (~> 1.0.2)
|
52
|
-
nokogiri (>= 1.5.9)
|
53
|
-
method_source (1.0.0)
|
54
|
-
mini_portile2 (2.6.1)
|
55
|
-
minitest (5.14.4)
|
56
|
-
nokogiri (1.12.5)
|
57
|
-
mini_portile2 (~> 2.6.1)
|
58
|
-
racc (~> 1.4)
|
59
|
-
pg (1.4.5)
|
60
|
-
pry (0.14.1)
|
61
|
-
coderay (~> 1.1)
|
62
|
-
method_source (~> 1.0)
|
63
|
-
racc (1.6.0)
|
64
|
-
rack (2.2.3)
|
65
|
-
rack-test (1.1.0)
|
66
|
-
rack (>= 1.0, < 3)
|
67
|
-
rails-dom-testing (2.0.3)
|
68
|
-
activesupport (>= 4.2.0)
|
69
|
-
nokogiri (>= 1.6)
|
70
|
-
rails-html-sanitizer (1.4.2)
|
71
|
-
loofah (~> 2.3)
|
72
|
-
railties (6.1.4.1)
|
73
|
-
actionpack (= 6.1.4.1)
|
74
|
-
activesupport (= 6.1.4.1)
|
75
|
-
method_source
|
76
|
-
rake (>= 0.13)
|
77
|
-
thor (~> 1.0)
|
78
|
-
rake (12.3.3)
|
79
|
-
rspec (3.10.0)
|
80
|
-
rspec-core (~> 3.10.0)
|
81
|
-
rspec-expectations (~> 3.10.0)
|
82
|
-
rspec-mocks (~> 3.10.0)
|
83
|
-
rspec-core (3.10.1)
|
84
|
-
rspec-support (~> 3.10.0)
|
85
|
-
rspec-expectations (3.10.1)
|
86
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
87
|
-
rspec-support (~> 3.10.0)
|
88
|
-
rspec-its (1.3.0)
|
89
|
-
rspec-core (>= 3.0.0)
|
90
|
-
rspec-expectations (>= 3.0.0)
|
91
|
-
rspec-mocks (3.10.2)
|
92
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
93
|
-
rspec-support (~> 3.10.0)
|
94
|
-
rspec-support (3.10.3)
|
95
|
-
scenic (1.5.4)
|
96
|
-
activerecord (>= 4.0.0)
|
97
|
-
railties (>= 4.0.0)
|
98
|
-
thor (1.1.0)
|
99
|
-
tzinfo (2.0.4)
|
100
|
-
concurrent-ruby (~> 1.0)
|
101
|
-
zeitwerk (2.5.1)
|
102
|
-
|
103
|
-
PLATFORMS
|
104
|
-
ruby
|
105
|
-
x86_64-darwin-20
|
106
|
-
|
107
|
-
DEPENDENCIES
|
108
|
-
database_cleaner-active_record
|
109
|
-
dotenv
|
110
|
-
gemika
|
111
|
-
pry
|
112
|
-
rake (~> 12.0)
|
113
|
-
rspec (~> 3.0)
|
114
|
-
rspec-its
|
115
|
-
scenic
|
116
|
-
timescaledb!
|
117
|
-
|
118
|
-
BUNDLED WITH
|
119
|
-
2.2.33
|
data/README.md
DELETED
@@ -1,490 +0,0 @@
|
|
1
|
-
# TimescaleDB
|
2
|
-
|
3
|
-
Welcome to the TimescaleDB gem! To experiment with the code, start installing the
|
4
|
-
gem:
|
5
|
-
|
6
|
-
```bash
|
7
|
-
gem install timescaledb
|
8
|
-
```
|
9
|
-
|
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:
|
17
|
-
|
18
|
-
```bash
|
19
|
-
tsdb <uri> --stats
|
20
|
-
```
|
21
|
-
|
22
|
-
Where the `<uri>` is replaced with params from your connection like:
|
23
|
-
|
24
|
-
```bash
|
25
|
-
tsdb postgres://<user>@localhost:5432/<dbname> --stats
|
26
|
-
```
|
27
|
-
|
28
|
-
|
29
|
-
Or just check the stats:
|
30
|
-
|
31
|
-
```bash
|
32
|
-
tsdb "postgres://<user>@localhost:5432/timescaledb_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}]}
|
45
|
-
```
|
46
|
-
|
47
|
-
To start a interactive ruby/[pry](https://github.com/pry/pry) console use `--console`:
|
48
|
-
The console will dynamically create models for all hypertables that it finds
|
49
|
-
in the database.
|
50
|
-
|
51
|
-
Let's consider the [caggs.sql](https://gist.github.com/jonatas/95573ad8744994094ec9f284150004f9#file-caggs-sql)
|
52
|
-
as the example of database.
|
53
|
-
|
54
|
-
|
55
|
-
```bash
|
56
|
-
psql postgres://<user>@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://<user>@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://<user>@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
|
-
=> Timescaledb::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
|
-
=> #<Timescaledb::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
|
-
=> [#<Timescaledb::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
|
-
=> Timescaledb::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
|
-
=> #<Timescaledb::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
|
-
```
|
194
|
-
|
195
|
-
## Installation
|
196
|
-
|
197
|
-
Add this line to your application's Gemfile:
|
198
|
-
|
199
|
-
```ruby
|
200
|
-
gem 'timescaledb'
|
201
|
-
```
|
202
|
-
|
203
|
-
And then execute:
|
204
|
-
|
205
|
-
$ bundle install
|
206
|
-
|
207
|
-
Or install it yourself as:
|
208
|
-
|
209
|
-
$ gem install timescaledb
|
210
|
-
|
211
|
-
|
212
|
-
## Usage
|
213
|
-
|
214
|
-
Check the [examples/ranking](examples/ranking) to get a Rails complete example.
|
215
|
-
|
216
|
-
You can check the [all_in_one.rb](examples/all_in_one/all_in_one.rb) example that will:
|
217
|
-
|
218
|
-
1. Create hypertable with compression settings
|
219
|
-
2. Insert data
|
220
|
-
3. Run some queries
|
221
|
-
4. Check chunk size per model
|
222
|
-
5. Compress a chunk
|
223
|
-
6. Check chunk status
|
224
|
-
7. Decompress a chunk
|
225
|
-
|
226
|
-
### Toolkit
|
227
|
-
|
228
|
-
Toolkit contains a lot of extra features to analyse data more deeply directly in
|
229
|
-
the SQL. There are a few examples in the [examples/toolkit-demo](examples/toolkit-demo)
|
230
|
-
folder that can let you benchmark and see the differences between implementing
|
231
|
-
the algorithm directly in Ruby or directly in SQL using the [Timescaledb
|
232
|
-
Toolkit](https://github.com/timescale/timescaledb-toolkit) extension.
|
233
|
-
|
234
|
-
For now you can benchmark and compare:
|
235
|
-
|
236
|
-
1. [volatility](examples/toolkit-demo/compare_volatility.rb) algorithm.
|
237
|
-
2. [lttb](examples/toolkit-demo/lttb/lttb_sinatra.rb) algorithm.
|
238
|
-
|
239
|
-
### Testing
|
240
|
-
|
241
|
-
If you need some inspiration for how are you going to test your hypertables,
|
242
|
-
please check the [spec/spec_helper.rb](spec/spec_helper.rb) for inspiration.
|
243
|
-
|
244
|
-
### Migrations
|
245
|
-
|
246
|
-
Create table is now with the `hypertable` keyword allowing to pass a few options
|
247
|
-
to the function call while also using `create_table` method:
|
248
|
-
|
249
|
-
#### create_table with `:hypertable`
|
250
|
-
|
251
|
-
```ruby
|
252
|
-
hypertable_options = {
|
253
|
-
time_column: 'created_at',
|
254
|
-
chunk_time_interval: '1 min',
|
255
|
-
compress_segmentby: 'identifier',
|
256
|
-
compression_interval: '7 days'
|
257
|
-
}
|
258
|
-
|
259
|
-
create_table(:events, id: false, hypertable: hypertable_options) do |t|
|
260
|
-
t.string :identifier, null: false
|
261
|
-
t.jsonb :payload
|
262
|
-
t.timestamps
|
263
|
-
end
|
264
|
-
```
|
265
|
-
|
266
|
-
#### create_continuous_aggregate
|
267
|
-
|
268
|
-
This example shows a ticks table grouping ticks as OHLCV histograms for every
|
269
|
-
minute.
|
270
|
-
|
271
|
-
```ruby
|
272
|
-
hypertable_options = {
|
273
|
-
time_column: 'created_at',
|
274
|
-
chunk_time_interval: '1 min',
|
275
|
-
compress_segmentby: 'symbol',
|
276
|
-
compress_orderby: 'created_at',
|
277
|
-
compression_interval: '7 days'
|
278
|
-
}
|
279
|
-
create_table :ticks, hypertable: hypertable_options, id: false do |t|
|
280
|
-
t.string :symbol
|
281
|
-
t.decimal :price
|
282
|
-
t.integer :volume
|
283
|
-
t.timestamps
|
284
|
-
end
|
285
|
-
Tick = Class.new(ActiveRecord::Base) do
|
286
|
-
self.table_name = 'ticks'
|
287
|
-
self.primary_key = 'symbol'
|
288
|
-
acts_as_hypertable
|
289
|
-
end
|
290
|
-
|
291
|
-
query = Tick.select(<<~QUERY)
|
292
|
-
time_bucket('1m', created_at) as time,
|
293
|
-
symbol,
|
294
|
-
FIRST(price, created_at) as open,
|
295
|
-
MAX(price) as high,
|
296
|
-
MIN(price) as low,
|
297
|
-
LAST(price, created_at) as close,
|
298
|
-
SUM(volume) as volume").group("1,2")
|
299
|
-
QUERY
|
300
|
-
|
301
|
-
options = {
|
302
|
-
with_data: false,
|
303
|
-
refresh_policies: {
|
304
|
-
start_offset: "INTERVAL '1 month'",
|
305
|
-
end_offset: "INTERVAL '1 minute'",
|
306
|
-
schedule_interval: "INTERVAL '1 minute'"
|
307
|
-
}
|
308
|
-
}
|
309
|
-
|
310
|
-
create_continuous_aggregate('ohlc_1m', query, **options)
|
311
|
-
```
|
312
|
-
|
313
|
-
#### Scenic integration
|
314
|
-
|
315
|
-
The [Scenic](https://github.com/scenic-views/scenic) gem is an easy way to
|
316
|
-
manage database view definitions for a Rails application. TimescaleDB's
|
317
|
-
continuous aggregates are more complex than regular PostgreSQL views, and
|
318
|
-
the schema dumper included with Scenic can't dump a complete definition.
|
319
|
-
|
320
|
-
This gem automatically configures Scenic to use a `Timescaledb::Scenic::Adapter`
|
321
|
-
which will correctly handle schema dumping.
|
322
|
-
|
323
|
-
### Enable ActsAsHypertable
|
324
|
-
|
325
|
-
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.
|
326
|
-
model:
|
327
|
-
|
328
|
-
```ruby
|
329
|
-
class Event < ActiveRecord::Base
|
330
|
-
acts_as_hypertable
|
331
|
-
end
|
332
|
-
```
|
333
|
-
|
334
|
-
By default, ActsAsHypertable assumes a record's _time_column_ is called `created_at`.
|
335
|
-
|
336
|
-
### Options
|
337
|
-
|
338
|
-
If you are using a different time_column name, you can specify it as follows when invoking the `acts_as_hypertable` macro:
|
339
|
-
|
340
|
-
```ruby
|
341
|
-
class Event < ActiveRecord::Base
|
342
|
-
acts_as_hypertable time_column: :timestamp
|
343
|
-
end
|
344
|
-
```
|
345
|
-
|
346
|
-
### Chunks
|
347
|
-
|
348
|
-
To get all the chunks from a model's hypertable, you can use `.chunks`.
|
349
|
-
|
350
|
-
```ruby
|
351
|
-
Event.chunks # => [#<Timescaledb::Chunk>, ...]
|
352
|
-
```
|
353
|
-
|
354
|
-
### Hypertable metadata
|
355
|
-
|
356
|
-
To get the models' hypertable metadata, you can use `.hypertable`.
|
357
|
-
|
358
|
-
```ruby
|
359
|
-
Event.hypertable # => #<Timescaledb::Hypertable>
|
360
|
-
```
|
361
|
-
|
362
|
-
To get hypertable metadata for all hypertables: `Timescaledb.hypertables`.
|
363
|
-
|
364
|
-
### Compression Settings
|
365
|
-
|
366
|
-
Compression settings are accessible through the hypertable.
|
367
|
-
|
368
|
-
```ruby
|
369
|
-
Event.hypertable.compression_settings # => [#<Timescaledb::CompressionSettings>, ...]
|
370
|
-
```
|
371
|
-
|
372
|
-
To get compression settings for all hypertables: `Timescaledb.compression_settings`.
|
373
|
-
|
374
|
-
### Scopes
|
375
|
-
|
376
|
-
The `acts_as_hypertable` macro can be very useful to generate some extra scopes
|
377
|
-
for you. Example of a weather condition:
|
378
|
-
|
379
|
-
```ruby
|
380
|
-
class Condition < ActiveRecord::Base
|
381
|
-
acts_as_hypertable time_column: "time"
|
382
|
-
end
|
383
|
-
```
|
384
|
-
|
385
|
-
Through the [ActsAsHypertable](./lib/timescaledb/acts_as_hypertable) on the model,
|
386
|
-
a few scopes are created based on the `time_column` argument:
|
387
|
-
|
388
|
-
| Scope name | What they return |
|
389
|
-
|------------------------|---------------------------------------|
|
390
|
-
| `Model.previous_month` | Records created in the previous month |
|
391
|
-
| `Model.previous_week` | Records created in the previous week |
|
392
|
-
| `Model.this_month` | Records created this month |
|
393
|
-
| `Model.this_week` | Records created this week |
|
394
|
-
| `Model.yesterday` | Records created yesterday |
|
395
|
-
| `Model.today` | Records created today |
|
396
|
-
| `Model.last_hour` | Records created in the last hour |
|
397
|
-
|
398
|
-
All time-related scopes respect your application's timezone.
|
399
|
-
|
400
|
-
When you enable ActsAsTimeVector on your model, we include a couple default scopes. They are:
|
401
|
-
|
402
|
-
```ruby
|
403
|
-
class Condition < ActiveRecord::Base
|
404
|
-
acts_as_time_vector time_column: "time",
|
405
|
-
value_column: "temperature",
|
406
|
-
segment_by: "device_id"
|
407
|
-
end
|
408
|
-
```
|
409
|
-
|
410
|
-
## RSpec Hooks
|
411
|
-
|
412
|
-
In case you want to use TimescaleDB on a Rails environment, you may have some
|
413
|
-
issues as the schema dump used for tests does not consider hypertables metadata.
|
414
|
-
|
415
|
-
As a work around, you can dynamically create the hypertables yourself for
|
416
|
-
testing environments using the following hook which you can
|
417
|
-
define in `spec/rspec_helper.rb`:
|
418
|
-
|
419
|
-
```ruby
|
420
|
-
config.before(:suite) do
|
421
|
-
hypertable_models = ActiveRecord::Base.descendants.select(&:acts_as_hypertable?)
|
422
|
-
|
423
|
-
hypertable_models.each do |klass|
|
424
|
-
table_name = klass.table_name
|
425
|
-
time_column = klass.hypertable_options[:time_column]
|
426
|
-
|
427
|
-
if klass.try(:hypertable).present?
|
428
|
-
ApplicationRecord.logger.info "hypertable already created for '#{table_name}', skipping."
|
429
|
-
next
|
430
|
-
end
|
431
|
-
|
432
|
-
ApplicationRecord.connection.execute <<~SQL
|
433
|
-
SELECT create_hypertable('#{table_name}', '#{time_column.to_s}')
|
434
|
-
SQL
|
435
|
-
end
|
436
|
-
end
|
437
|
-
```
|
438
|
-
|
439
|
-
## Development
|
440
|
-
|
441
|
-
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.
|
442
|
-
|
443
|
-
You can also run `tsdb` for an interactive prompt that will allow you to experiment.
|
444
|
-
|
445
|
-
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).
|
446
|
-
|
447
|
-
You can create a `.env` file locally to run tests locally. Make sure to put your
|
448
|
-
own credentials there!
|
449
|
-
|
450
|
-
```bash
|
451
|
-
PG_URI_TEST="postgres://<user>@localhost:5432/<dbname>"
|
452
|
-
```
|
453
|
-
|
454
|
-
You can put some postgres URI directly as a parameter of
|
455
|
-
`tsdb`. Here is an example from the console:
|
456
|
-
|
457
|
-
```bash
|
458
|
-
tsdb "postgres://<user>@localhost:5432/timescaledb_test"
|
459
|
-
```
|
460
|
-
|
461
|
-
## More resources
|
462
|
-
|
463
|
-
This library was started on [twitch.tv/timescaledb](https://twitch.tv/timescaledb).
|
464
|
-
You can watch all episodes here:
|
465
|
-
|
466
|
-
1. [Wrapping Functions to Ruby Helpers](https://www.youtube.com/watch?v=hGPsUxLFAYk).
|
467
|
-
2. [Extending ActiveRecord with Timescale Helpers](https://www.youtube.com/watch?v=IEyJIHk1Clk).
|
468
|
-
3. [Setup Hypertables for Rails testing environment](https://www.youtube.com/watch?v=wM6hVrZe7xA).
|
469
|
-
4. [Packing the code to this repository](https://www.youtube.com/watch?v=CMdGAl_XlL4).
|
470
|
-
4. [the code to this repository](https://www.youtube.com/watch?v=CMdGAl_XlL4).
|
471
|
-
5. [Working with Timescale continuous aggregates](https://youtu.be/co4HnBkHzVw).
|
472
|
-
6. [Creating the command-line application in Ruby to explore the Timescale API](https://www.youtube.com/watch?v=I3vM_q2m7T0).
|
473
|
-
|
474
|
-
### TODO
|
475
|
-
|
476
|
-
Here is a list of functions that would be great to have:
|
477
|
-
|
478
|
-
- [ ] Add data nodes support
|
479
|
-
|
480
|
-
## Contributing
|
481
|
-
|
482
|
-
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).
|
483
|
-
|
484
|
-
## License
|
485
|
-
|
486
|
-
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
487
|
-
|
488
|
-
## Code of Conduct
|
489
|
-
|
490
|
-
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/Rakefile
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
require "bundler/gem_tasks"
|
2
|
-
require "rspec/core/rake_task"
|
3
|
-
|
4
|
-
begin
|
5
|
-
require "gemika/tasks"
|
6
|
-
rescue LoadError
|
7
|
-
puts "Run `gem install gemika` for additional tasks"
|
8
|
-
end
|
9
|
-
|
10
|
-
RSpec::Core::RakeTask.new(:spec)
|
11
|
-
|
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
|