- Ruby
- Python
- Node.js
- Go
- Java
Prerequisites
To follow the steps on this page:- Create a target with the Real-time analytics capability enabled. You need your connection details. This procedure also works for .
- Install Rails.
Connect a Rails app to your service
Every is a 100% database hosted in with extensions such as . You connect to your from a standard Rails app configured for .-
Create a new Rails app configured for
Rails creates and bundles your app, then installs the standard Gems.
-
Install the gem
-
Open
Gemfile, add the following line, then save your changes: -
In Terminal, run the following command:
-
Open
-
Connect your app to your
-
In
<my_app_home>/config/database.ymlupdate the configuration to read securely connect to your by addingurl: <%= ENV['DATABASE_URL'] %>to the default configuration: -
Set the environment variable for
DATABASE_URLto the value ofService URLfrom your connection details -
Create the database:
- : nothing to do. The database is part of your .
-
, create the database for the project:
-
Run migrations:
-
Verify the connection from your app to your :
The result shows the list of extensions in your
Name Version Schema Description pg_buffercache 1.5 public examine the shared buffer cache pg_stat_statements 1.11 public track planning and execution statistics of all SQL statements executed plpgsql 1.0 pg_catalog PL/pgSQL procedural language postgres_fdw 1.1 public foreign-data wrapper for remote servers timescaledb 2.23.0 public Enables scalable inserts and complex queries for time-series data (Community Edition) timescaledb_toolkit 1.22.0 public Library of analytical hyperfunctions, time-series pipelining, and other SQL utilities -
In
Optimize time-series data in hypertables
Hypertables are tables designed to simplify and accelerate data analysis. Anything you can do with regular tables, you can do with hypertables - but much faster and more conveniently.In this section, you use the helpers in the gem to create and manage a hypertable.-
Generate a migration to create the page loads table
This creates the
<my_app_home>/db/migrate/<migration-datetime>_create_page_loads.rbmigration file. -
Add hypertable options
Replace the contents of
<my_app_home>/db/migrate/<migration-datetime>_create_page_loads.rbwith the following:Theidcolumn is not included in the table. This is because requires that anyUNIQUEorPRIMARY KEYindexes on the table include all partitioning columns. In this case, this is the time column. A new Rails model includes aPRIMARY KEYindex for id by default: either remove the column or make sure that the index includes time as part of a “composite key.” For more information, check the Ruby docs around composite primary keys. -
Create a
PageLoadmodel Create a new file called<my_app_home>/app/models/page_load.rband add the following code: -
Run the migration
Insert data your service
The gem provides efficient ways to insert data into hypertables. This section shows you how to ingest test data into your hypertable.-
Create a controller to handle page loads
Create a new file called
<my_app_home>/app/controllers/application_controller.rband add the following code: -
Generate some test data
Use
bin/consoleto join a Rails console session and run the following code to define some random page load access data: -
Insert the generated data into your
- Validate the test data in your
Reference
This section lists the most common tasks you might perform with the gem.Query scopes
The gem provides several convenient scopes for querying your time-series data.-
Built-in time-based scopes:
-
Browser-specific scopes:
-
Query continuous aggregates:
This query fetches the average and standard deviation from the performance stats for the
/productspath over the last day.
TimescaleDB features
The gem provides utility methods to access hypertable and chunk information. Every model that uses theacts_as_hypertable method has access to these methods.Access hypertable and chunk information
-
View chunk or hypertable information:
-
Compress/Decompress chunks:
Access hypertable stats
You collect hypertable stats using methods that provide insights into your hypertable’s structure, size, and compression status:-
Get basic hypertable information:
-
Get detailed size information:
Continuous aggregates
Thecontinuous_aggregates method generates a class for each continuous aggregate.-
Get all the continuous aggregate classes:
-
Manually refresh a continuous aggregate:
-
Create or drop a continuous aggregate:
Create or drop all the continuous aggregates in the proper order to build them hierarchically. See more about how it
works in this blog post.
Next steps
Now that you have integrated the ruby gem into your app:- Learn more about the gem.
- Check out the official docs.
- Follow the LTTB, Open AI long-term storage, and candlesticks tutorials.