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]]
|
||||
name = "tf-log-parser"
|
||||
version = "0.1.0"
|
||||
version = "0.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "99c6c9ff48911dd252aa097e836eb7acb5743126cc9c11b01f1895463ca3168c"
|
||||
checksum = "bdf88d9bce2693006421a7361e1d05809ca50549a70fbb97690135a8e719d5c1"
|
||||
dependencies = [
|
||||
"ahash",
|
||||
"chrono",
|
||||
|
|
|
|||
|
|
@ -8,6 +8,13 @@ use steamid_ng::SteamID;
|
|||
use tf_log_parser::module::Kill;
|
||||
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))]
|
||||
pub async fn store_log(
|
||||
pool: &PgPool,
|
||||
|
|
|
|||
40
src/main.rs
40
src/main.rs
|
|
@ -4,7 +4,7 @@ pub mod file;
|
|||
mod normalized;
|
||||
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::normalized::NormalizedLog;
|
||||
use anyhow::{Context, Error};
|
||||
|
|
@ -12,7 +12,7 @@ use main_error::MainError;
|
|||
use sqlx::PgPool;
|
||||
use sqlx::pool::PoolOptions;
|
||||
use tokio::time::{Duration, sleep};
|
||||
use tracing::{error, info, instrument};
|
||||
use tracing::{debug, error, info, instrument};
|
||||
|
||||
const OLD_VERSION: i16 = 2;
|
||||
const VERSION: i16 = 3;
|
||||
|
|
@ -23,9 +23,16 @@ async fn main() -> Result<(), MainError> {
|
|||
let database_url = dotenvy::var("DATABASE_URL")?;
|
||||
let raw_database_url = dotenvy::var("RAW_DATABASE_URL")?;
|
||||
let log_directory = dotenvy::var("LOG_DIRECTORY")?;
|
||||
let migrate_sparse = dotenvy::var("MIGRATE_SPARSE").unwrap_or_default() == "1";
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -34,6 +41,7 @@ async fn normalize(
|
|||
database_url: &str,
|
||||
raw_database_url: &str,
|
||||
log_directory: &str,
|
||||
migrate_sparse: bool,
|
||||
) -> Result<(), Error> {
|
||||
let pool = PoolOptions::new()
|
||||
.max_connections(2)
|
||||
|
|
@ -60,19 +68,23 @@ async fn normalize(
|
|||
|
||||
if let Some(old) = old {
|
||||
for id in old..=from {
|
||||
info!(id = id, from = OLD_VERSION, to = VERSION, "migrating");
|
||||
if let Some(log) = get_log(&raw_pool, id).await? {
|
||||
let kills = if OLD_VERSION == 2 {
|
||||
log_kills(log_directory, id)
|
||||
.inspect_err(|error| error!(%error, "error parsing log file"))
|
||||
.ok()
|
||||
.flatten()
|
||||
if !migrate_sparse || log_version(&pool, id).await.unwrap_or_default() < VERSION {
|
||||
if let Some(log) = get_log(&raw_pool, id).await? {
|
||||
let kills = if OLD_VERSION == 2 {
|
||||
log_kills(log_directory, id)
|
||||
.inspect_err(|error| error!(%error, "error parsing log file"))
|
||||
.ok()
|
||||
.flatten()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
info!(id = id, from = OLD_VERSION, to = VERSION, "migrating");
|
||||
upgrade(&pool, id, &log, OLD_VERSION, VERSION, kills, &mut weapons).await?;
|
||||
} else {
|
||||
None
|
||||
};
|
||||
upgrade(&pool, id, &log, OLD_VERSION, VERSION, kills, &mut weapons).await?;
|
||||
error!(id = id, "invalid");
|
||||
}
|
||||
} else {
|
||||
error!(id = id, "invalid");
|
||||
debug!(id = id, version = VERSION, "already on latest version");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue