mirror of
https://codeberg.org/icewind/log-normalizer.git
synced 2026-11-30 10:30:47 +01:00
This commit is contained in:
parent
05c7c4be1e
commit
1731e96632
4 changed files with 63 additions and 16 deletions
28
.sqlx/query-ca7989c09fdb9760d0fd13f9640bb7b4f2abfe19a036e23ffdc7abdf22bddb9e.json
generated
Normal file
28
.sqlx/query-ca7989c09fdb9760d0fd13f9640bb7b4f2abfe19a036e23ffdc7abdf22bddb9e.json
generated
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "SELECT version FROM logs WHERE id = $1",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "version",
|
||||||
|
"type_info": "Int2",
|
||||||
|
"origin": {
|
||||||
|
"Table": {
|
||||||
|
"table": "logs",
|
||||||
|
"name": "version"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Int4"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
false
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "ca7989c09fdb9760d0fd13f9640bb7b4f2abfe19a036e23ffdc7abdf22bddb9e"
|
||||||
|
}
|
||||||
4
Cargo.lock
generated
4
Cargo.lock
generated
|
|
@ -1886,9 +1886,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tf-log-parser"
|
name = "tf-log-parser"
|
||||||
version = "0.1.0"
|
version = "0.1.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "99c6c9ff48911dd252aa097e836eb7acb5743126cc9c11b01f1895463ca3168c"
|
checksum = "bdf88d9bce2693006421a7361e1d05809ca50549a70fbb97690135a8e719d5c1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ahash",
|
"ahash",
|
||||||
"chrono",
|
"chrono",
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,13 @@ use steamid_ng::SteamID;
|
||||||
use tf_log_parser::module::Kill;
|
use tf_log_parser::module::Kill;
|
||||||
use tracing::instrument;
|
use tracing::instrument;
|
||||||
|
|
||||||
|
#[instrument(skip(pool))]
|
||||||
|
pub async fn log_version(pool: &PgPool, id: i32) -> Result<i16, sqlx::Error> {
|
||||||
|
sqlx::query_scalar!("SELECT version FROM logs WHERE id = $1", id)
|
||||||
|
.fetch_one(&*pool)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
#[instrument(skip(pool, log, kills, weapons))]
|
#[instrument(skip(pool, log, kills, weapons))]
|
||||||
pub async fn store_log(
|
pub async fn store_log(
|
||||||
pool: &PgPool,
|
pool: &PgPool,
|
||||||
|
|
|
||||||
20
src/main.rs
20
src/main.rs
|
|
@ -4,7 +4,7 @@ pub mod file;
|
||||||
mod normalized;
|
mod normalized;
|
||||||
pub mod raw;
|
pub mod raw;
|
||||||
|
|
||||||
use crate::database::{load_weapon_ids, store_log, upgrade};
|
use crate::database::{load_weapon_ids, log_version, store_log, upgrade};
|
||||||
use crate::file::log_kills;
|
use crate::file::log_kills;
|
||||||
use crate::normalized::NormalizedLog;
|
use crate::normalized::NormalizedLog;
|
||||||
use anyhow::{Context, Error};
|
use anyhow::{Context, Error};
|
||||||
|
|
@ -12,7 +12,7 @@ use main_error::MainError;
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
use sqlx::pool::PoolOptions;
|
use sqlx::pool::PoolOptions;
|
||||||
use tokio::time::{Duration, sleep};
|
use tokio::time::{Duration, sleep};
|
||||||
use tracing::{error, info, instrument};
|
use tracing::{debug, error, info, instrument};
|
||||||
|
|
||||||
const OLD_VERSION: i16 = 2;
|
const OLD_VERSION: i16 = 2;
|
||||||
const VERSION: i16 = 3;
|
const VERSION: i16 = 3;
|
||||||
|
|
@ -23,9 +23,16 @@ async fn main() -> Result<(), MainError> {
|
||||||
let database_url = dotenvy::var("DATABASE_URL")?;
|
let database_url = dotenvy::var("DATABASE_URL")?;
|
||||||
let raw_database_url = dotenvy::var("RAW_DATABASE_URL")?;
|
let raw_database_url = dotenvy::var("RAW_DATABASE_URL")?;
|
||||||
let log_directory = dotenvy::var("LOG_DIRECTORY")?;
|
let log_directory = dotenvy::var("LOG_DIRECTORY")?;
|
||||||
|
let migrate_sparse = dotenvy::var("MIGRATE_SPARSE").unwrap_or_default() == "1";
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
normalize(&database_url, &raw_database_url, &log_directory).await?;
|
normalize(
|
||||||
|
&database_url,
|
||||||
|
&raw_database_url,
|
||||||
|
&log_directory,
|
||||||
|
migrate_sparse,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
sleep(Duration::from_secs(15 * 60)).await;
|
sleep(Duration::from_secs(15 * 60)).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -34,6 +41,7 @@ async fn normalize(
|
||||||
database_url: &str,
|
database_url: &str,
|
||||||
raw_database_url: &str,
|
raw_database_url: &str,
|
||||||
log_directory: &str,
|
log_directory: &str,
|
||||||
|
migrate_sparse: bool,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
let pool = PoolOptions::new()
|
let pool = PoolOptions::new()
|
||||||
.max_connections(2)
|
.max_connections(2)
|
||||||
|
|
@ -60,7 +68,7 @@ async fn normalize(
|
||||||
|
|
||||||
if let Some(old) = old {
|
if let Some(old) = old {
|
||||||
for id in old..=from {
|
for id in old..=from {
|
||||||
info!(id = id, from = OLD_VERSION, to = VERSION, "migrating");
|
if !migrate_sparse || log_version(&pool, id).await.unwrap_or_default() < VERSION {
|
||||||
if let Some(log) = get_log(&raw_pool, id).await? {
|
if let Some(log) = get_log(&raw_pool, id).await? {
|
||||||
let kills = if OLD_VERSION == 2 {
|
let kills = if OLD_VERSION == 2 {
|
||||||
log_kills(log_directory, id)
|
log_kills(log_directory, id)
|
||||||
|
|
@ -70,10 +78,14 @@ async fn normalize(
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
info!(id = id, from = OLD_VERSION, to = VERSION, "migrating");
|
||||||
upgrade(&pool, id, &log, OLD_VERSION, VERSION, kills, &mut weapons).await?;
|
upgrade(&pool, id, &log, OLD_VERSION, VERSION, kills, &mut weapons).await?;
|
||||||
} else {
|
} else {
|
||||||
error!(id = id, "invalid");
|
error!(id = id, "invalid");
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
debug!(id = id, version = VERSION, "already on latest version");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue