timescaledb 0.2.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/README.md +41 -9
  4. data/bin/console +2 -2
  5. data/bin/tsdb +3 -3
  6. data/docs/command_line.md +178 -0
  7. data/docs/img/lttb_example.png +0 -0
  8. data/docs/img/lttb_sql_vs_ruby.gif +0 -0
  9. data/docs/img/lttb_zoom.gif +0 -0
  10. data/docs/index.md +61 -0
  11. data/docs/migrations.md +69 -0
  12. data/docs/models.md +78 -0
  13. data/docs/toolkit.md +394 -0
  14. data/docs/toolkit_lttb_tutorial.md +557 -0
  15. data/docs/toolkit_lttb_zoom.md +357 -0
  16. data/docs/videos.md +16 -0
  17. data/examples/all_in_one/all_in_one.rb +40 -6
  18. data/examples/all_in_one/benchmark_comparison.rb +108 -0
  19. data/examples/all_in_one/caggs.rb +93 -0
  20. data/examples/all_in_one/query_data.rb +78 -0
  21. data/examples/ranking/config/initializers/timescale.rb +1 -2
  22. data/examples/toolkit-demo/compare_volatility.rb +64 -0
  23. data/examples/toolkit-demo/lttb/README.md +15 -0
  24. data/examples/toolkit-demo/lttb/lttb.rb +92 -0
  25. data/examples/toolkit-demo/lttb/lttb_sinatra.rb +139 -0
  26. data/examples/toolkit-demo/lttb/lttb_test.rb +21 -0
  27. data/examples/toolkit-demo/lttb/views/index.erb +27 -0
  28. data/examples/toolkit-demo/lttb-zoom/README.md +13 -0
  29. data/examples/toolkit-demo/lttb-zoom/lttb_zoomable.rb +90 -0
  30. data/examples/toolkit-demo/lttb-zoom/views/index.erb +33 -0
  31. data/lib/timescaledb/acts_as_time_vector.rb +18 -0
  32. data/lib/timescaledb/dimensions.rb +1 -0
  33. data/lib/timescaledb/hypertable.rb +5 -1
  34. data/lib/timescaledb/migration_helpers.rb +30 -11
  35. data/lib/timescaledb/schema_dumper.rb +4 -1
  36. data/lib/timescaledb/stats_report.rb +1 -1
  37. data/lib/timescaledb/toolkit/helpers.rb +20 -0
  38. data/lib/timescaledb/toolkit/time_vector.rb +66 -0
  39. data/lib/timescaledb/toolkit.rb +3 -0
  40. data/lib/timescaledb/version.rb +1 -1
  41. data/lib/timescaledb.rb +1 -0
  42. data/mkdocs.yml +33 -0
  43. metadata +30 -4
  44. data/examples/all_in_one/Gemfile +0 -11
  45. data/examples/all_in_one/Gemfile.lock +0 -51
@@ -10,13 +10,16 @@ module Timescaledb
10
10
 
11
11
  def table(table_name, stream)
12
12
  super(table_name, stream)
13
- if hypertable = Timescaledb::Hypertable.find_by(hypertable_name: table_name)
13
+ if Timescaledb::Hypertable.table_exists? &&
14
+ (hypertable = Timescaledb::Hypertable.find_by(hypertable_name: table_name))
14
15
  timescale_hypertable(hypertable, stream)
15
16
  timescale_retention_policy(hypertable, stream)
16
17
  end
17
18
  end
18
19
 
19
20
  def views(stream)
21
+ return unless Timescaledb::ContinuousAggregates.table_exists?
22
+
20
23
  timescale_continuous_aggregates(stream) # Define these before any Scenic views that might use them
21
24
  super if defined?(super)
22
25
  end
@@ -19,7 +19,7 @@ module Timescaledb
19
19
  end
20
20
 
21
21
  def compression_resume(scope)
22
- sum = -> (method) { (scope.map(&method).inject(:+) || 0).to_formatted_s(:human_size)}
22
+ sum = -> (method) { (scope.map(&method).inject(:+) || 0).to_s(:human_size)}
23
23
  {
24
24
  uncompressed: sum[:before_total_bytes],
25
25
  compressed: sum[:after_total_bytes]
@@ -0,0 +1,20 @@
1
+ require 'active_record/connection_adapters/postgresql_adapter'
2
+
3
+ # Useful methods to run TimescaleDB with Toolkit functions in you Ruby app.
4
+ module Timescaledb
5
+ # Helpers methods to setup queries that uses the toolkit.
6
+ module Toolkit
7
+ module Helpers
8
+
9
+ # Includes toolkit_experimental in the search path to make it easy to have
10
+ # access to all the functions
11
+ def add_toolkit_to_search_path!
12
+ return if schema_search_path.include?("toolkit_experimental")
13
+
14
+ self.schema_search_path = "#{schema_search_path}, toolkit_experimental"
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include(Timescaledb::Toolkit::Helpers)
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Timescaledb
4
+ module Toolkit
5
+ module TimeVector
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+ def value_column
12
+ @value_column ||= time_vector_options[:value_column] || :val
13
+ end
14
+
15
+ def time_column
16
+ respond_to?(:time_column) && super || time_vector_options[:time_column]
17
+ end
18
+ def segment_by_column
19
+ time_vector_options[:segment_by]
20
+ end
21
+
22
+ protected
23
+
24
+ def define_default_scopes
25
+ scope :volatility, -> (segment_by: segment_by_column) do
26
+ select([*segment_by,
27
+ "timevector(#{time_column}, #{value_column}) -> sort() -> delta() -> abs() -> sum() as volatility"
28
+ ].join(", "))
29
+ .group(segment_by)
30
+ end
31
+
32
+ scope :time_weight, -> (segment_by: segment_by_column) do
33
+ select([*segment_by,
34
+ "timevector(#{time_column}, #{value_column}) -> sort() -> delta() -> abs() -> time_weight() as time_weight"
35
+ ].join(", "))
36
+ .group(segment_by)
37
+ end
38
+
39
+ scope :lttb, -> (threshold:, segment_by: segment_by_column, time: time_column, value: value_column) do
40
+ lttb_query = <<~SQL
41
+ WITH x AS ( #{select(*segment_by, time_column, value_column).to_sql})
42
+ SELECT #{"x.#{segment_by}," if segment_by}
43
+ (lttb( x.#{time_column}, x.#{value_column}, #{threshold})
44
+ -> toolkit_experimental.unnest()).*
45
+ FROM x
46
+ #{"GROUP BY device_id" if segment_by}
47
+ SQL
48
+ downsampled = unscoped
49
+ .select(*segment_by, "time as #{time_column}, value as #{value_column}")
50
+ .from("(#{lttb_query}) as x")
51
+ if segment_by
52
+ downsampled.inject({}) do |group,e|
53
+ key = e.send(segment_by)
54
+ (group[key] ||= []) << [e.send(time_column), e.send(value_column)]
55
+ group
56
+ end
57
+ else
58
+ downsampled.map{|e|[ e[time_column],e[value_column]]}
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+
@@ -0,0 +1,3 @@
1
+ require_relative "toolkit/helpers"
2
+ require_relative "acts_as_time_vector"
3
+ require_relative "toolkit/time_vector"
@@ -1,3 +1,3 @@
1
1
  module Timescaledb
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.2'
3
3
  end
data/lib/timescaledb.rb CHANGED
@@ -2,6 +2,7 @@ require 'active_record'
2
2
 
3
3
  require_relative 'timescaledb/acts_as_hypertable'
4
4
  require_relative 'timescaledb/acts_as_hypertable/core'
5
+ require_relative 'timescaledb/toolkit'
5
6
  require_relative 'timescaledb/chunk'
6
7
  require_relative 'timescaledb/compression_settings'
7
8
  require_relative 'timescaledb/continuous_aggregates'
data/mkdocs.yml ADDED
@@ -0,0 +1,33 @@
1
+ site_name: The Timescaledb gem
2
+ repo_url: https://github.com/jonatas/timescaledb
3
+ edit_uri: edit/master/docs/
4
+ extra:
5
+ analytics:
6
+ provider: google
7
+ property: G-9B2BMB0TNQ
8
+ theme:
9
+ name: material
10
+ palette:
11
+ primary: indigo
12
+ accent: pink
13
+ markdown_extensions:
14
+ - admonition
15
+ - codehilite:
16
+ guess_lang: false
17
+ - toc:
18
+ permalink: true
19
+ - pymdownx.highlight:
20
+ anchor_linenums: true
21
+ - pymdownx.inlinehilite
22
+ - pymdownx.snippets
23
+ - pymdownx.superfences
24
+
25
+ nav:
26
+ - Introduction: index.md
27
+ - Migrations: migrations.md
28
+ - Models: models.md
29
+ - Toolkit Integration: toolkit.md
30
+ - Toolkit LTTB Tutorial: toolkit_lttb_tutorial.md
31
+ - Zooming with High Resolution: toolkit_lttb_zoom.md
32
+ - Command Line: command_line.md
33
+ - Videos: videos.md
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timescaledb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jônatas Davi Paganini
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-19 00:00:00.000000000 Z
11
+ date: 2022-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg
@@ -160,9 +160,21 @@ files:
160
160
  - bin/console
161
161
  - bin/setup
162
162
  - bin/tsdb
163
- - examples/all_in_one/Gemfile
164
- - examples/all_in_one/Gemfile.lock
163
+ - docs/command_line.md
164
+ - docs/img/lttb_example.png
165
+ - docs/img/lttb_sql_vs_ruby.gif
166
+ - docs/img/lttb_zoom.gif
167
+ - docs/index.md
168
+ - docs/migrations.md
169
+ - docs/models.md
170
+ - docs/toolkit.md
171
+ - docs/toolkit_lttb_tutorial.md
172
+ - docs/toolkit_lttb_zoom.md
173
+ - docs/videos.md
165
174
  - examples/all_in_one/all_in_one.rb
175
+ - examples/all_in_one/benchmark_comparison.rb
176
+ - examples/all_in_one/caggs.rb
177
+ - examples/all_in_one/query_data.rb
166
178
  - examples/ranking/.gitattributes
167
179
  - examples/ranking/.gitignore
168
180
  - examples/ranking/.ruby-version
@@ -212,9 +224,19 @@ files:
212
224
  - examples/ranking/tmp/pids/.keep
213
225
  - examples/ranking/tmp/storage/.keep
214
226
  - examples/ranking/vendor/.keep
227
+ - examples/toolkit-demo/compare_volatility.rb
228
+ - examples/toolkit-demo/lttb-zoom/README.md
229
+ - examples/toolkit-demo/lttb-zoom/lttb_zoomable.rb
230
+ - examples/toolkit-demo/lttb-zoom/views/index.erb
231
+ - examples/toolkit-demo/lttb/README.md
232
+ - examples/toolkit-demo/lttb/lttb.rb
233
+ - examples/toolkit-demo/lttb/lttb_sinatra.rb
234
+ - examples/toolkit-demo/lttb/lttb_test.rb
235
+ - examples/toolkit-demo/lttb/views/index.erb
215
236
  - lib/timescaledb.rb
216
237
  - lib/timescaledb/acts_as_hypertable.rb
217
238
  - lib/timescaledb/acts_as_hypertable/core.rb
239
+ - lib/timescaledb/acts_as_time_vector.rb
218
240
  - lib/timescaledb/chunk.rb
219
241
  - lib/timescaledb/compression_settings.rb
220
242
  - lib/timescaledb/continuous_aggregates.rb
@@ -227,7 +249,11 @@ files:
227
249
  - lib/timescaledb/scenic/extension.rb
228
250
  - lib/timescaledb/schema_dumper.rb
229
251
  - lib/timescaledb/stats_report.rb
252
+ - lib/timescaledb/toolkit.rb
253
+ - lib/timescaledb/toolkit/helpers.rb
254
+ - lib/timescaledb/toolkit/time_vector.rb
230
255
  - lib/timescaledb/version.rb
256
+ - mkdocs.yml
231
257
  - timescaledb.gemspec
232
258
  homepage: https://github.com/jonatas/timescaledb
233
259
  licenses:
@@ -1,11 +0,0 @@
1
-
2
- source 'https://rubygems.org'
3
-
4
- gem "timescaledb", path: "../"
5
- gem "pg"
6
- gem "activerecord"
7
- gem "composite_primary_keys", "~> 6.0"
8
- gem 'pry'
9
-
10
-
11
- gem "dotenv", "~> 2.7"
@@ -1,51 +0,0 @@
1
- PATH
2
- remote: ..
3
- specs:
4
- timescaledb (0.1.2)
5
- activerecord
6
- pg (~> 1.2)
7
-
8
- GEM
9
- remote: https://rubygems.org/
10
- specs:
11
- activemodel (6.1.4.1)
12
- activesupport (= 6.1.4.1)
13
- activerecord (6.1.4.1)
14
- activemodel (= 6.1.4.1)
15
- activesupport (= 6.1.4.1)
16
- activesupport (6.1.4.1)
17
- concurrent-ruby (~> 1.0, >= 1.0.2)
18
- i18n (>= 1.6, < 2)
19
- minitest (>= 5.1)
20
- tzinfo (~> 2.0)
21
- zeitwerk (~> 2.3)
22
- coderay (1.1.3)
23
- composite_primary_keys (6.0.5)
24
- activerecord (>= 4.0.0)
25
- concurrent-ruby (1.1.9)
26
- dotenv (2.7.6)
27
- i18n (1.8.10)
28
- concurrent-ruby (~> 1.0)
29
- method_source (1.0.0)
30
- minitest (5.14.4)
31
- pg (1.2.3)
32
- pry (0.14.0)
33
- coderay (~> 1.1)
34
- method_source (~> 1.0)
35
- tzinfo (2.0.4)
36
- concurrent-ruby (~> 1.0)
37
- zeitwerk (2.4.2)
38
-
39
- PLATFORMS
40
- ruby
41
-
42
- DEPENDENCIES
43
- activerecord
44
- composite_primary_keys (~> 6.0)
45
- dotenv (~> 2.7)
46
- pg
47
- pry
48
- timescaledb!
49
-
50
- BUNDLED WITH
51
- 2.1.4