Skip to content

Commit

Permalink
Merge pull request #135 from nightly-labs/events-table
Browse files Browse the repository at this point in the history
Events table
  • Loading branch information
Giems authored Mar 20, 2024
2 parents d6247f1 + 5d451a7 commit 5c5eb29
Show file tree
Hide file tree
Showing 57 changed files with 1,104 additions and 698 deletions.
3 changes: 3 additions & 0 deletions database/bindings/EventType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type EventType = "AppConnect" | "AppDisconnect" | "ClientConnectInit" | "ClientConnectResolve" | "ClientDisconnect" | "SignMessage" | "SignTransaction" | "SignAndSendTransaction" | "ChangeWallet" | "ChangeNetwork";
13 changes: 13 additions & 0 deletions database/migrations/0001_types.sql
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,17 @@ CREATE TYPE geo_location AS (
city TEXT,
lat FLOAT8,
lon FLOAT8
);

CREATE TYPE event_type_enum AS ENUM (
'AppConnect',
'AppDisconnect',
'ClientConnectInit',
'ClientConnectResolve'
'ClientDisconnect',
'SingMessage',
'SignTransaction',
'SignAndSendTransaction',
'ChangeWallet',
'ChangeNetwork'
);
6 changes: 6 additions & 0 deletions database/migrations/0011_events_index.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE events(
event_id SERIAL PRIMARY KEY,
app_id TEXT NOT NULL,
event_type event_type_enum NOT NULL,
creation_timestamp TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
10 changes: 0 additions & 10 deletions database/migrations/0011_requests.sql

This file was deleted.

76 changes: 76 additions & 0 deletions database/migrations/0012_events_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
CREATE TABLE event_app_connect(
event_id BIGINT PRIMARY KEY REFERENCES events(event_id),
session_id TEXT NOT NULL,
device_metadata TEXT NOT NULL,
lang TEXT NOT NULL,
timezone TEXT NOT NULL,
new_session BOOLEAN NOT NULL
);

CREATE TABLE event_app_disconnect(
event_id BIGINT PRIMARY KEY REFERENCES events(event_id),
session_id TEXT NOT NULL
);

CREATE TABLE event_client_connect(
event_id BIGINT PRIMARY KEY REFERENCES events(event_id),
client_id TEXT NOT NULL,
session_id TEXT NOT NULL,
addresses TEXT[],
wallet_name TEXT NOT NULL,
wallet_type TEXT NOT NULL,
session_type session_type_enum NOT NULL,
success BOOLEAN NOT NULL
);

CREATE TABLE event_client_disconnect(
event_id BIGINT PRIMARY KEY REFERENCES events(event_id),
disconnected_session_id TEXT NOT NULL
);

CREATE TABLE event_sign_message(
event_id BIGINT PRIMARY KEY REFERENCES events(event_id),
session_id TEXT NOT NULL,
request_id TEXT NOT NULL,
request_status request_status_enum NOT NULL,
network TEXT NOT NULL
);

CREATE TABLE event_sign_transaction(
event_id BIGINT PRIMARY KEY REFERENCES events(event_id),
session_id TEXT NOT NULL,
request_id TEXT NOT NULL,
request_status request_status_enum NOT NULL,
network TEXT NOT NULL,
tx_hash TEXT
);

CREATE TABLE event_sign_and_send_transaction(
event_id BIGINT PRIMARY KEY REFERENCES events(event_id),
session_id TEXT NOT NULL,
request_id TEXT NOT NULL,
request_status request_status_enum NOT NULL,
network TEXT NOT NULL,
tx_hash TEXT
);

CREATE TABLE event_change_wallet(
event_id BIGINT PRIMARY KEY REFERENCES events(event_id),
session_id TEXT NOT NULL,
request_id TEXT NOT NULL,
request_status request_status_enum NOT NULL,
network TEXT NOT NULL,
wallet_name TEXT NOT NULL,
wallet_type TEXT NOT NULL,
old_wallet_address TEXT NOT NULL,
new_wallet_address TEXT NOT NULL
);

CREATE TABLE event_change_network(
event_id BIGINT PRIMARY KEY REFERENCES events(event_id),
session_id TEXT NOT NULL,
request_id TEXT NOT NULL,
request_status request_status_enum NOT NULL,
old_network TEXT NOT NULL,
new_network TEXT NOT NULL
);
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
SELECT
create_hypertable('connection_events', 'connected_at');

SELECT
create_hypertable('requests', 'creation_timestamp');
-- SELECT
-- create_hypertable('requests', 'creation_timestamp');

SELECT
create_hypertable('sessions', 'session_open_timestamp');
Expand Down
96 changes: 0 additions & 96 deletions database/migrations/0013_requests_stats.sql

This file was deleted.

96 changes: 96 additions & 0 deletions database/migrations/0014_requests_stats.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
-- ----------------- Hourly requests stats per app -----------------
-- --- View
-- CREATE MATERIALIZED VIEW hourly_requests_stats_per_app WITH (timescaledb.continuous) AS
-- SELECT
-- app_id,
-- time_bucket('1 hour' :: interval, creation_timestamp) AS hourly_bucket,
-- COUNT(*) AS hourly_request_count,
-- COUNT(*) FILTER (
-- WHERE
-- request_status = 'Completed'
-- ) :: FLOAT / NULLIF(
-- COUNT(*) FILTER (
-- WHERE
-- request_status IN ('Completed', 'Rejected', 'TimedOut')
-- ),
-- 0
-- ) AS hourly_success_rate
-- FROM
-- requests
-- GROUP BY
-- app_id,
-- hourly_bucket WITH NO DATA;

-- --- Refresh policy
-- SELECT
-- add_continuous_aggregate_policy(
-- 'hourly_requests_stats_per_app',
-- start_offset => INTERVAL '3 h',
-- end_offset => INTERVAL '1 h',
-- schedule_interval => INTERVAL '1 h'
-- );

-- --- Real time aggregation
-- ALTER MATERIALIZED VIEW hourly_requests_stats_per_app
-- set
-- (timescaledb.materialized_only = false);



-- ----------------- Daily requests stats per app -----------------
-- --- View
-- CREATE MATERIALIZED VIEW daily_requests_stats_per_app WITH (timescaledb.continuous) AS
-- SELECT
-- app_id,
-- time_bucket('1 day' :: interval, hourly_bucket) AS daily_bucket,
-- SUM(hourly_request_count) :: BIGINT AS daily_request_count,
-- SUM(hourly_request_count * hourly_success_rate) :: FLOAT / SUM(hourly_request_count) AS daily_success_rate
-- FROM
-- hourly_requests_stats_per_app
-- GROUP BY
-- app_id,
-- daily_bucket WITH NO DATA;

-- --- Refresh policy
-- SELECT
-- add_continuous_aggregate_policy(
-- 'daily_requests_stats_per_app',
-- start_offset => INTERVAL '3 d',
-- end_offset => INTERVAL '1 h',
-- schedule_interval => INTERVAL '12 h'
-- );

-- --- Real time aggregation
-- ALTER MATERIALIZED VIEW daily_requests_stats_per_app
-- set
-- (timescaledb.materialized_only = false);



-- ----------------- Monthly requests per app -----------------
-- --- View
-- CREATE MATERIALIZED VIEW monthly_requests_stats_per_app WITH (timescaledb.continuous) AS
-- SELECT
-- app_id,
-- time_bucket('1 month' :: interval, daily_bucket) AS monthly_bucket,
-- SUM(daily_request_count) :: BIGINT AS monthly_request_count,
-- SUM(daily_request_count * daily_success_rate) :: FLOAT / SUM(daily_request_count) AS monthly_success_rate
-- FROM
-- daily_requests_stats_per_app
-- GROUP BY
-- app_id,
-- monthly_bucket WITH NO DATA;

-- --- Refresh policy
-- SELECT
-- add_continuous_aggregate_policy(
-- 'monthly_requests_stats_per_app',
-- start_offset => INTERVAL '3 month',
-- end_offset => INTERVAL '1 h',
-- schedule_interval => INTERVAL '1 month'
-- );

-- --- Real time aggregation
-- ALTER MATERIALIZED VIEW monthly_requests_stats_per_app
-- set
-- (timescaledb.materialized_only = false);
File renamed without changes.
Loading

0 comments on commit 5c5eb29

Please sign in to comment.