websocket done, api needs rebuild
This commit is contained in:
parent
5c3a9a1888
commit
50c4ac387a
13 changed files with 192 additions and 195 deletions
|
@ -2,20 +2,11 @@ use async_once::AsyncOnce;
|
|||
use dotenvy::dotenv;
|
||||
use lazy_static::lazy_static;
|
||||
use migrations::cmd_migrate;
|
||||
use once_cell::sync::Lazy;
|
||||
use redis::Commands;
|
||||
use sqlx::query;
|
||||
use std::env::args;
|
||||
use std::ops::Sub;
|
||||
use std::time::{Duration, SystemTime};
|
||||
|
||||
mod migrations;
|
||||
|
||||
pub static REDIS_CLIENT: Lazy<redis::Client> = Lazy::new(|| {
|
||||
redis::Client::open(std::env::var("REDIS_ADDR").unwrap_or("redis://localhost:6379".to_string()))
|
||||
.unwrap()
|
||||
});
|
||||
|
||||
lazy_static! {
|
||||
pub static ref PG: AsyncOnce<sqlx::PgPool> = AsyncOnce::new(async {
|
||||
let db_url = std::env::var("DATABASE_URL")
|
||||
|
@ -24,36 +15,37 @@ lazy_static! {
|
|||
});
|
||||
}
|
||||
|
||||
fn cmd_prune() {
|
||||
async fn cmd_prune() {
|
||||
println!("Pruning old data...");
|
||||
let mut con = REDIS_CLIENT.get_connection().unwrap();
|
||||
let pool = PG.get().await;
|
||||
|
||||
let prune_after = SystemTime::now()
|
||||
.sub(Duration::from_secs(60 * 15))
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
let rows = query("DELETE FROM players WHERE time < NOW() - INTERVAL '15 minutes';")
|
||||
.execute(pool)
|
||||
.await
|
||||
.unwrap()
|
||||
.as_secs();
|
||||
.rows_affected();
|
||||
println!("Deleted {} rows of old player data", rows);
|
||||
|
||||
let keys: Vec<String> = con.keys("wp:*").unwrap();
|
||||
for key in keys {
|
||||
println!("-> Pruning world pop {}", key);
|
||||
let removed_items: u64 = con.zrembyscore(key, 0, prune_after).unwrap();
|
||||
println!("==> Removed {} items", removed_items);
|
||||
}
|
||||
let rows = query("DELETE FROM classes WHERE time < NOW() - INTERVAL '15 minutes';")
|
||||
.execute(pool)
|
||||
.await
|
||||
.unwrap()
|
||||
.rows_affected();
|
||||
println!("Deleted {} rows of old class data", rows);
|
||||
|
||||
let keys: Vec<String> = con.keys("v:*").unwrap();
|
||||
for key in keys {
|
||||
println!("-> Pruning vehicle {}", key);
|
||||
let removed_items: u64 = con.zrembyscore(key, 0, prune_after).unwrap();
|
||||
println!("==> Removed {} items", removed_items);
|
||||
}
|
||||
let rows = query("DELETE FROM vehicles WHERE time < NOW() - INTERVAL '15 minutes';")
|
||||
.execute(pool)
|
||||
.await
|
||||
.unwrap()
|
||||
.rows_affected();
|
||||
println!("Deleted {} rows of old vehicle data", rows);
|
||||
|
||||
let keys: Vec<String> = con.keys("c:*").unwrap();
|
||||
for key in keys {
|
||||
println!("-> Pruning class {}", key);
|
||||
let removed_items: u64 = con.zrembyscore(key, 0, prune_after).unwrap();
|
||||
println!("==> Removed {} items", removed_items);
|
||||
}
|
||||
let rows = query("DELETE FROM analytics WHERE time < NOW() - INTERVAL '1 day';")
|
||||
.execute(pool)
|
||||
.await
|
||||
.unwrap()
|
||||
.rows_affected();
|
||||
println!("Deleted {} rows of old analytics data", rows);
|
||||
}
|
||||
|
||||
fn cmd_help() {
|
||||
|
@ -72,7 +64,7 @@ async fn main() {
|
|||
|
||||
match command.as_str() {
|
||||
"help" => cmd_help(),
|
||||
"prune" => cmd_prune(),
|
||||
"prune" => cmd_prune().await,
|
||||
"migrate" => cmd_migrate().await,
|
||||
_ => {
|
||||
println!("Unknown command: {}", command);
|
||||
|
|
|
@ -4,7 +4,12 @@ use sqlx::query;
|
|||
pub async fn cmd_migrate() {
|
||||
println!("Migrating database...");
|
||||
|
||||
tokio::join!(migrate_players(), migrate_classes(), migrate_vehicles(),);
|
||||
tokio::join!(
|
||||
migrate_players(),
|
||||
migrate_classes(),
|
||||
migrate_vehicles(),
|
||||
migrate_analytics()
|
||||
);
|
||||
}
|
||||
|
||||
async fn migrate_players() {
|
||||
|
@ -21,8 +26,8 @@ async fn migrate_players() {
|
|||
println!("PLAYERS => CREATE TABLE players");
|
||||
query(
|
||||
"CREATE TABLE players (
|
||||
time TIMESTAMPTZ NOT NULL,
|
||||
character_id TEXT NOT NULL,
|
||||
time TIMESTAMPTZ NOT NULL,
|
||||
world_id INT NOT NULL,
|
||||
faction_id INT NOT NULL,
|
||||
zone_id INT NOT NULL);",
|
||||
|
@ -63,8 +68,8 @@ async fn migrate_classes() {
|
|||
println!("CLASSES => CREATE TABLE classes");
|
||||
query(
|
||||
"CREATE TABLE classes (
|
||||
time TIMESTAMPTZ NOT NULL,
|
||||
character_id TEXT NOT NULL,
|
||||
time TIMESTAMPTZ NOT NULL,
|
||||
world_id INT NOT NULL,
|
||||
faction_id INT NOT NULL,
|
||||
zone_id INT NOT NULL,
|
||||
|
@ -106,8 +111,8 @@ async fn migrate_vehicles() {
|
|||
println!("VEHICLES => CREATE TABLE vehicles");
|
||||
query(
|
||||
"CREATE TABLE vehicles (
|
||||
time TIMESTAMPTZ NOT NULL,
|
||||
character_id TEXT NOT NULL,
|
||||
time TIMESTAMPTZ NOT NULL,
|
||||
world_id INT NOT NULL,
|
||||
faction_id INT NOT NULL,
|
||||
zone_id INT NOT NULL,
|
||||
|
@ -135,3 +140,36 @@ async fn migrate_vehicles() {
|
|||
|
||||
println!("VEHICLES => done!");
|
||||
}
|
||||
|
||||
async fn migrate_analytics() {
|
||||
let pool = PG.get().await;
|
||||
|
||||
println!("-> Migrating analytics");
|
||||
println!("ANALYTICS => CREATE TABLE IF NOT EXISTS analytics");
|
||||
query(
|
||||
"CREATE TABLE IF NOT EXISTS analytics (
|
||||
time TIMESTAMPTZ NOT NULL,
|
||||
event_name TEXT NOT NULL,
|
||||
world_id INT NOT NULL);",
|
||||
)
|
||||
.execute(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
println!("ANALYTICS => create_hypertable");
|
||||
query(
|
||||
"SELECT create_hypertable('analytics', 'time',
|
||||
chunk_time_interval => INTERVAL '1 hour', if_not_exists => TRUE);",
|
||||
)
|
||||
.execute(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
println!("ANALYTICS => add_retention_policy");
|
||||
query("SELECT add_retention_policy('analytics', INTERVAL '1 day', if_not_exists => TRUE);")
|
||||
.execute(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
println!("ANALYTICS => done!");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue