-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from nightly-labs/queries-improvements
Replace Postgress with Timescale
- Loading branch information
Showing
20 changed files
with
349 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,9 @@ | ||
CREATE TABLE requests( | ||
request_id TEXT NOT NULL UNIQUE, | ||
request_type TEXT NOT NULL, | ||
request_id TEXT NOT NULL, | ||
session_id TEXT NOT NULL, | ||
app_id TEXT NOT NULL, | ||
request_type TEXT NOT NULL, | ||
request_status request_status_enum NOT NULL, | ||
network TEXT NOT NULL, | ||
creation_timestamp BIGINT NOT NULL | ||
creation_timestamp TIMESTAMPTZ NOT NULL | ||
); | ||
|
||
CREATE UNIQUE INDEX requests_request_id ON requests(request_id); | ||
|
||
ALTER TABLE requests | ||
ADD CONSTRAINT fk_requests_sessions | ||
FOREIGN KEY (session_id) | ||
REFERENCES sessions (session_id) | ||
ON DELETE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
SELECT create_hypertable('sessions', 'session_open_timestamp'); | ||
SELECT create_hypertable('requests', 'creation_timestamp'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
----------------- Hourly requests per app ----------------- | ||
CREATE MATERIALIZED VIEW hourly_requests_per_app | ||
WITH (timescaledb.continuous) | ||
AS SELECT | ||
app_id, | ||
time_bucket('1 h'::interval, creation_timestamp) as hourly_bucket, | ||
COUNT(*) AS hourly_request_count | ||
FROM requests | ||
GROUP BY app_id, hourly_bucket | ||
WITH NO DATA; | ||
|
||
SELECT add_continuous_aggregate_policy('hourly_requests_per_app', | ||
start_offset => INTERVAL '3 h', | ||
end_offset => INTERVAL '1 h', | ||
schedule_interval => INTERVAL '1 h'); | ||
|
||
----------------- Daily requests per app ----------------- | ||
CREATE MATERIALIZED VIEW daily_requests_per_app | ||
WITH (timescaledb.continuous) AS | ||
SELECT | ||
app_id, | ||
time_bucket('1 d'::interval, hourly_bucket) AS daily_bucket, | ||
SUM(hourly_request_count)::BIGINT AS daily_request_count | ||
FROM hourly_requests_per_app | ||
GROUP BY app_id, daily_bucket | ||
WITH NO DATA; | ||
|
||
SELECT add_continuous_aggregate_policy('daily_requests_per_app', | ||
start_offset => INTERVAL '3 d', | ||
end_offset => INTERVAL '1 h', | ||
schedule_interval => INTERVAL '12 h'); | ||
|
||
----------------- Monthly requests per app ----------------- | ||
CREATE MATERIALIZED VIEW monthly_requests_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 | ||
FROM daily_requests_per_app | ||
GROUP BY app_id, monthly_bucket | ||
WITH NO DATA; | ||
|
||
SELECT add_continuous_aggregate_policy('monthly_requests_per_app', | ||
start_offset => INTERVAL '3 month', | ||
end_offset => INTERVAL '1 h', | ||
schedule_interval => INTERVAL '1 month'); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
use sqlx::types::chrono::{DateTime, Utc}; | ||
|
||
#[derive(Clone, Debug, Eq, PartialEq)] | ||
pub struct ClientData { | ||
pub client_id: Option<String>, | ||
pub device: Option<String>, | ||
pub metadata: Option<String>, | ||
pub notification_endpoint: Option<String>, | ||
pub connected_at: u64, // Timestamp of when the client connected to the session | ||
pub connected_at: DateTime<Utc>, // Timestamp of when the client connected to the session | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
pub const LAST_24_HOURS: &str = "EXTRACT(EPOCH FROM NOW() - INTERVAL '1 day')::BIGINT * 1000"; | ||
pub const LAST_7_DAYS: &str = "EXTRACT(EPOCH FROM NOW() - INTERVAL '7 days')::BIGINT * 1000"; | ||
pub const LAST_30_DAYS: &str = "EXTRACT(EPOCH FROM NOW() - INTERVAL '30 days')::BIGINT * 1000"; | ||
pub const DAY_IN_SECONDS: u64 = 60 * 60 * 24; // 86400 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use sqlx::types::chrono::{DateTime, Utc}; | ||
|
||
#[derive(Debug, sqlx::FromRow)] | ||
pub struct AggregatedRequestCount { | ||
pub app_id: String, | ||
pub bucket: DateTime<Utc>, | ||
pub request_count: i64, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
pub mod client_data; | ||
pub mod consts; | ||
pub mod filter_requests; | ||
pub mod request_status; | ||
pub mod subscription; | ||
pub mod time_filters; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use super::consts::DAY_IN_SECONDS; | ||
use sqlx::types::chrono::{NaiveDate, Utc}; | ||
use std::time::Duration; | ||
|
||
pub enum TimeFilter { | ||
Last24Hours, | ||
Last7Days, | ||
Last30Days, | ||
} | ||
|
||
impl TimeFilter { | ||
pub fn to_date(&self) -> NaiveDate { | ||
let duration = match self { | ||
TimeFilter::Last24Hours => Duration::from_secs(DAY_IN_SECONDS), | ||
TimeFilter::Last7Days => Duration::from_secs(7 * DAY_IN_SECONDS), | ||
TimeFilter::Last30Days => Duration::from_secs(30 * DAY_IN_SECONDS), | ||
}; | ||
// Subtract the duration from the current time and convert to NaiveDate | ||
(Utc::now() - duration).date_naive() | ||
} | ||
|
||
pub fn bucket_size(&self) -> &'static str { | ||
match self { | ||
TimeFilter::Last24Hours => "1 hour", | ||
TimeFilter::Last7Days => "1 day", | ||
TimeFilter::Last30Days => "1 day", | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.