Add more configurable metadata to datasets

This commit is contained in:
Kiana Sheibani 2023-10-13 18:15:25 -04:00
parent fae735d29b
commit 3d97028b92
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
4 changed files with 68 additions and 24 deletions

View file

@ -11,7 +11,12 @@ pub struct DatasetMetadata {
pub game_id: VideogameId, pub game_id: VideogameId,
pub game_name: String, pub game_name: String,
pub country: Option<String>,
pub state: Option<String>, pub state: Option<String>,
pub decay_rate: f64,
pub period: f64,
pub tau: f64,
} }
/// Return the path to the datasets file. /// Return the path to the datasets file.
@ -33,16 +38,19 @@ fn datasets_path(config_dir: &Path) -> io::Result<PathBuf> {
pub fn open_datasets(config_dir: &Path) -> sqlite::Result<Connection> { pub fn open_datasets(config_dir: &Path) -> sqlite::Result<Connection> {
let path = datasets_path(config_dir).unwrap(); let path = datasets_path(config_dir).unwrap();
let query = " let query = "PRAGMA foreign_keys = ON;
PRAGMA foreign_keys = ON;
CREATE TABLE IF NOT EXISTS datasets ( CREATE TABLE IF NOT EXISTS datasets (
name TEXT UNIQUE NOT NULL, name TEXT UNIQUE NOT NULL,
last_sync INTEGER NOT NULL, last_sync INTEGER NOT NULL,
game_id INTEGER NOT NULL, game_id INTEGER NOT NULL,
game_name TEXT NOT NULL, game_name TEXT NOT NULL,
state TEXT country TEXT,
) STRICT;"; state TEXT,
decay_rate REAL NOT NULL,
period REAL NOT NULL,
tau REAL NOT NULL
) STRICT;";
let connection = sqlite::open(path)?; let connection = sqlite::open(path)?;
connection.execute(query)?; connection.execute(query)?;
@ -75,7 +83,11 @@ pub fn list_datasets(connection: &Connection) -> sqlite::Result<Vec<(String, Dat
last_sync: Timestamp(r_.read::<i64, _>("last_sync") as u64), last_sync: Timestamp(r_.read::<i64, _>("last_sync") as u64),
game_id: VideogameId(r_.read::<i64, _>("game_id") as u64), game_id: VideogameId(r_.read::<i64, _>("game_id") as u64),
game_name: r_.read::<&str, _>("game_name").to_owned(), game_name: r_.read::<&str, _>("game_name").to_owned(),
country: r_.read::<Option<&str>, _>("country").map(String::from),
state: r_.read::<Option<&str>, _>("state").map(String::from), state: r_.read::<Option<&str>, _>("state").map(String::from),
decay_rate: r_.read::<f64, _>("decay_rate"),
period: r_.read::<f64, _>("period"),
tau: r_.read::<f64, _>("tau"),
}, },
)) ))
}) })
@ -99,7 +111,7 @@ pub fn new_dataset(
dataset: &str, dataset: &str,
metadata: DatasetMetadata, metadata: DatasetMetadata,
) -> sqlite::Result<()> { ) -> sqlite::Result<()> {
let query1 = r#"INSERT INTO datasets VALUES (?, ?, ?, ?, ?)"#; let query1 = r#"INSERT INTO datasets VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"#;
let query2 = format!( let query2 = format!(
r#"CREATE TABLE "{0}_players" ( r#"CREATE TABLE "{0}_players" (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
@ -148,7 +160,11 @@ CREATE VIEW "{0}_view"
.bind((2, metadata.last_sync.0 as i64))? .bind((2, metadata.last_sync.0 as i64))?
.bind((3, metadata.game_id.0 as i64))? .bind((3, metadata.game_id.0 as i64))?
.bind((4, &metadata.game_name[..]))? .bind((4, &metadata.game_name[..]))?
.bind((5, metadata.state.as_deref()))? .bind((5, metadata.country.as_deref()))?
.bind((6, metadata.state.as_deref()))?
.bind((7, metadata.decay_rate))?
.bind((8, metadata.period))?
.bind((9, metadata.tau))?
.try_for_each(|x| x.map(|_| ()))?; .try_for_each(|x| x.map(|_| ()))?;
connection.execute(query2) connection.execute(query2)
@ -158,7 +174,7 @@ pub fn get_metadata(
connection: &Connection, connection: &Connection,
dataset: &str, dataset: &str,
) -> sqlite::Result<Option<DatasetMetadata>> { ) -> sqlite::Result<Option<DatasetMetadata>> {
let query = "SELECT last_sync, game_id, game_name, state FROM datasets WHERE name = ?"; let query = "SELECT * FROM datasets WHERE name = ?";
Ok(connection Ok(connection
.prepare(query)? .prepare(query)?
@ -171,7 +187,11 @@ pub fn get_metadata(
last_sync: Timestamp(r_.read::<i64, _>("last_sync") as u64), last_sync: Timestamp(r_.read::<i64, _>("last_sync") as u64),
game_id: VideogameId(r_.read::<i64, _>("game_id") as u64), game_id: VideogameId(r_.read::<i64, _>("game_id") as u64),
game_name: r_.read::<&str, _>("game_name").to_owned(), game_name: r_.read::<&str, _>("game_name").to_owned(),
country: r_.read::<Option<&str>, _>("country").map(String::from),
state: r_.read::<Option<&str>, _>("state").map(String::from), state: r_.read::<Option<&str>, _>("state").map(String::from),
decay_rate: r_.read::<f64, _>("decay_rate"),
period: r_.read::<f64, _>("period"),
tau: r_.read::<f64, _>("tau"),
}) })
}) })
.and_then(Result::ok)) .and_then(Result::ok))
@ -395,6 +415,7 @@ pub fn is_isolated(
pub fn hypothetical_advantage( pub fn hypothetical_advantage(
connection: &Connection, connection: &Connection,
dataset: &str, dataset: &str,
decay_rate: f64,
player1: PlayerId, player1: PlayerId,
player2: PlayerId, player2: PlayerId,
) -> sqlite::Result<f64> { ) -> sqlite::Result<f64> {
@ -444,16 +465,17 @@ pub fn hypothetical_advantage(
} }
let (final_adv, len) = final_path.unwrap(); let (final_adv, len) = final_path.unwrap();
Ok(final_adv * 0.5_f64.powi(len as i32)) Ok(final_adv * decay_rate.powi(len as i32))
} }
pub fn initialize_edge( pub fn initialize_edge(
connection: &Connection, connection: &Connection,
dataset: &str, dataset: &str,
decay_rate: f64,
player1: PlayerId, player1: PlayerId,
player2: PlayerId, player2: PlayerId,
) -> sqlite::Result<f64> { ) -> sqlite::Result<f64> {
let adv = hypothetical_advantage(connection, dataset, player1, player2)?; let adv = hypothetical_advantage(connection, dataset, decay_rate, player1, player2)?;
insert_advantage(connection, dataset, player1, player2, adv)?; insert_advantage(connection, dataset, player1, player2, adv)?;
Ok(adv) Ok(adv)
} }
@ -489,7 +511,11 @@ pub mod tests {
last_sync: Timestamp(1), last_sync: Timestamp(1),
game_id: VideogameId(0), game_id: VideogameId(0),
game_name: String::from("Test Game"), game_name: String::from("Test Game"),
country: None,
state: None, state: None,
decay_rate: 0.5,
period: (3600 * 24 * 30) as f64,
tau: 0.2,
} }
} }
@ -598,7 +624,7 @@ pub mod tests {
for i in 1..=num_players { for i in 1..=num_players {
for j in 1..=num_players { for j in 1..=num_players {
assert_eq!( assert_eq!(
hypothetical_advantage(&connection, "test", PlayerId(i), PlayerId(j))?, hypothetical_advantage(&connection, "test", 0.5, PlayerId(i), PlayerId(j))?,
0.0 0.0
); );
} }
@ -616,7 +642,7 @@ pub mod tests {
insert_advantage(&connection, "test", PlayerId(1), PlayerId(2), 1.0)?; insert_advantage(&connection, "test", PlayerId(1), PlayerId(2), 1.0)?;
assert_eq!( assert_eq!(
hypothetical_advantage(&connection, "test", PlayerId(1), PlayerId(2))?, hypothetical_advantage(&connection, "test", 0.5, PlayerId(1), PlayerId(2))?,
1.0 1.0
); );

View file

@ -193,7 +193,11 @@ fn dataset_new(name: Option<String>, auth_token: Option<String>) {
last_sync: Timestamp(1), last_sync: Timestamp(1),
game_id, game_id,
game_name, game_name,
country: None,
state: None, state: None,
decay_rate: 0.5,
period: (3600 * 24 * 30) as f64,
tau: 0.2,
}, },
) )
.expect("Error communicating with SQLite"); .expect("Error communicating with SQLite");

View file

@ -13,6 +13,7 @@ pub struct TournamentEventsVars<'a> {
pub last_sync: Timestamp, pub last_sync: Timestamp,
pub game_id: VideogameId, pub game_id: VideogameId,
pub country: Option<&'a str>,
pub state: Option<&'a str>, pub state: Option<&'a str>,
pub page: i32, pub page: i32,
} }
@ -30,6 +31,7 @@ pub struct TournamentEvents {
past: true, past: true,
afterDate: $last_sync, afterDate: $last_sync,
videogameIds: [$game_id], videogameIds: [$game_id],
countryCode: $country,
addrState: $state addrState: $state
}})] }})]
tournaments: Option<TournamentConnection>, tournaments: Option<TournamentConnection>,

View file

@ -39,10 +39,11 @@ fn glicko_adjust(
other_deviation: f64, other_deviation: f64,
won: bool, won: bool,
time: u64, time: u64,
metadata: &DatasetMetadata,
) -> (f64, f64, f64) { ) -> (f64, f64, f64) {
// TODO: Turn this into dataset metadata // TODO: Turn this into dataset metadata
let tau = 0.2; let period = metadata.period;
let period = (3600 * 24 * 30) as f64; let tau = metadata.tau;
let g_val = 1.0 / (1.0 + 3.0 * other_deviation * other_deviation / PI / PI).sqrt(); let g_val = 1.0 / (1.0 + 3.0 * other_deviation * other_deviation / PI / PI).sqrt();
let exp_val = 1.0 / (1.0 + f64::exp(-g_val * advantage)); let exp_val = 1.0 / (1.0 + f64::exp(-g_val * advantage));
@ -122,6 +123,7 @@ fn get_tournament_events(metadata: &DatasetMetadata, auth: &str) -> Option<Vec<E
TournamentEventsVars { TournamentEventsVars {
last_sync: metadata.last_sync, last_sync: metadata.last_sync,
game_id: metadata.game_id, game_id: metadata.game_id,
country: metadata.country.as_deref(),
state: metadata.state.as_deref(), state: metadata.state.as_deref(),
page: 1, page: 1,
}, },
@ -155,6 +157,7 @@ fn get_tournament_events(metadata: &DatasetMetadata, auth: &str) -> Option<Vec<E
TournamentEventsVars { TournamentEventsVars {
last_sync: metadata.last_sync, last_sync: metadata.last_sync,
game_id: metadata.game_id, game_id: metadata.game_id,
country: metadata.country.as_deref(),
state: metadata.state.as_deref(), state: metadata.state.as_deref(),
page, page,
}, },
@ -175,7 +178,12 @@ fn get_tournament_events(metadata: &DatasetMetadata, auth: &str) -> Option<Vec<E
// Dataset syncing // Dataset syncing
fn update_from_set(connection: &Connection, dataset: &str, results: SetData) -> sqlite::Result<()> { fn update_from_set(
connection: &Connection,
dataset: &str,
metadata: &DatasetMetadata,
results: SetData,
) -> sqlite::Result<()> {
let players_data = results.teams; let players_data = results.teams;
add_players(connection, dataset, &players_data, results.time)?; add_players(connection, dataset, &players_data, results.time)?;
@ -193,7 +201,7 @@ fn update_from_set(connection: &Connection, dataset: &str, results: SetData) ->
let (deviation2, volatility2, last_played2) = get_player_data(connection, dataset, player1)?; let (deviation2, volatility2, last_played2) = get_player_data(connection, dataset, player1)?;
let advantage = match get_advantage(connection, dataset, player1, player2) { let advantage = match get_advantage(connection, dataset, player1, player2) {
Err(e) => Err(e)?, Err(e) => Err(e)?,
Ok(None) => initialize_edge(connection, dataset, player1, player2)?, Ok(None) => initialize_edge(connection, dataset, metadata.decay_rate, player1, player2)?,
Ok(Some(adv)) => adv, Ok(Some(adv)) => adv,
}; };
let (adjust1, dev_new1, vol_new1) = glicko_adjust( let (adjust1, dev_new1, vol_new1) = glicko_adjust(
@ -203,6 +211,7 @@ fn update_from_set(connection: &Connection, dataset: &str, results: SetData) ->
deviation2, deviation2,
results.winner == 0, results.winner == 0,
results.time.0 - last_played1.0, results.time.0 - last_played1.0,
metadata,
); );
let (adjust2, dev_new2, vol_new2) = glicko_adjust( let (adjust2, dev_new2, vol_new2) = glicko_adjust(
advantage, advantage,
@ -211,6 +220,7 @@ fn update_from_set(connection: &Connection, dataset: &str, results: SetData) ->
deviation1, deviation1,
results.winner == 1, results.winner == 1,
results.time.0 - last_played2.0, results.time.0 - last_played2.0,
metadata,
); );
set_player_data( set_player_data(
@ -230,14 +240,15 @@ fn update_from_set(connection: &Connection, dataset: &str, results: SetData) ->
vol_new2, vol_new2,
)?; )?;
adjust_advantages(connection, dataset, player1, 0.5 * adjust1)?; let decay_rate = metadata.decay_rate;
adjust_advantages(connection, dataset, player2, 0.5 * adjust2)?; adjust_advantages(connection, dataset, player1, decay_rate * adjust1)?;
adjust_advantages(connection, dataset, player2, decay_rate * adjust2)?;
adjust_advantage( adjust_advantage(
connection, connection,
dataset, dataset,
player1, player1,
player2, player2,
(1.0 - 0.5) * (adjust2 - adjust1), (1.0 - decay_rate) * (adjust2 - adjust1),
) )
} }
@ -265,7 +276,7 @@ pub fn sync_dataset(
println!(" Updating ratings from event..."); println!(" Updating ratings from event...");
sets.into_iter() sets.into_iter()
.try_for_each(|set| update_from_set(connection, dataset, set))?; .try_for_each(|set| update_from_set(connection, dataset, &metadata, set))?;
} }
connection.execute("COMMIT;") connection.execute("COMMIT;")
} }
@ -285,6 +296,7 @@ mod tests {
update_from_set( update_from_set(
&connection, &connection,
"test", "test",
&metadata(),
SetData { SetData {
time: Timestamp(0), time: Timestamp(0),
teams: players, teams: players,