Skip to content

Instantly share code, notes, and snippets.

View jonatas's full-sized avatar
🏠
Working from home

Jônatas Davi Paganini jonatas

🏠
Working from home
View GitHub Profile
@jonatas
jonatas / semantic_tracking.sql
Last active August 21, 2024 19:22
Semantic tracking with PGAI
CREATE EXTENSION IF NOT EXISTS ai CASCADE;
DROP TABLE IF EXISTS vecs CASCADE;
DROP TABLE IF EXISTS interests CASCADE;
DROP TABLE IF EXISTS notifications CASCADE;
-- Create or replace the function to embed content
CREATE OR REPLACE FUNCTION embed(content TEXT) RETURNS VECTOR AS $$
DECLARE
vectorized_content VECTOR;
BEGIN
# Search time_bucket calls
Fast.shortcut :time_bucket, '^^(func_call (funcname (string (sval "time_bucket")'
if ENV['PG_URI']
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'timescaledb', path: '../../code/timescale/timescale-gem'
gem 'pry'
@jonatas
jonatas / poc_downloads.rb
Last active June 21, 2024 17:36
POC Make RubyGems track downloads with TimescaleDB
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'timescaledb'
gem 'bulk_insert'
gem 'pry'
end
require 'timescaledb'

ADA.rb

Sonic PI

&

Timescale

Jônatas Davi Paganini

@jonatasdp

@jonatasdp

DROP TABLE "ticks" CASCADE;
CREATE TABLE "ticks" ("time" timestamp with time zone not null, "symbol" text, "price" decimal, "volume" float);
SELECT create_hypertable('ticks', 'time', chunk_time_interval => INTERVAL '1 week');
ALTER TABLE ticks SET (
timescaledb.compress,
timescaledb.compress_orderby = 'time',
timescaledb.compress_segmentby = 'symbol'
@jonatas
jonatas / corr.sql
Last active October 26, 2022 18:33
Correlation matrix done with the toolkit
SELECT * FROM crosstab($$
WITH city_names AS (
SELECT DISTINCT city_name as name
FROM weather_metrics order by 1
),
pairs as (
SELECT a.name as first, b.name as second
FROM city_names a
JOIN city_names b ON true --# a.name != b.name
),
@jonatas
jonatas / ada-meetup-walkthrough-timescale-and-ruby.md
Last active October 21, 2022 17:05
ADA Meetup - Processing Data with TimescaleDB and Ruby
require 'bundler/inline'
gemfile(true) do
gem 'timescaledb'
gem 'pry'
end
require 'pp'
# ruby caggs.rb postgres://user:pass@host:port/db_name
ActiveRecord::Base.establish_connection( ARGV.last)
@jonatas
jonatas / poc_100k_hypertables.sql
Last active July 21, 2022 20:47
This is an attempt to create several hypertables and test the setup of millions of chunks
-- First set the numbers of hypertables you'd like to test
\set hypertables_count 100
-- Hypertable configuration with the chunk time interval for every hypertable
\set chunk_time_interval '''1 hour'''
-- How much data you'd like to append for every append_data call
\set append_interval '''1 day'''
-- How many devices would you like to simulate in parallel
\set number_of_devices '''1'''
-- When the data starts
\set start_date '''2000-01-01'''
@jonatas
jonatas / massive_inserts.sql
Last active July 7, 2022 20:19
Limit 10k rows per device in a hypertable
select delete_job(job_id) from timescaledb_information.jobs where job_id >=1000;
drop table conditions cascade;
CREATE TABLE conditions (
time TIMESTAMPTZ NOT NULL,
device INTEGER NOT NULL,
temperature FLOAT NOT NULL
);
SELECT * FROM create_hypertable('conditions', 'time');
INSERT INTO conditions