Skip to content

Commit

Permalink
Make /stats/continuous return a map instead of an array
Browse files Browse the repository at this point in the history
  • Loading branch information
konsumlamm committed Feb 6, 2024
1 parent 96d6008 commit f7dfbcf
Showing 1 changed file with 6 additions and 51 deletions.
57 changes: 6 additions & 51 deletions ns2-stat-api/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
mod data;

use std::collections::BTreeMap;
use std::io;
use std::net::{IpAddr, SocketAddr};
use std::ops::Bound;
use std::path::PathBuf;
use std::io;

use actix_web::web::Json;
use actix_web::{
Expand All @@ -17,8 +17,8 @@ use actix_web::{
};
use clap::Parser;
use notify::Watcher;
use ns2_stat::{GameSummary, summarize_game};
use ns2_stat::{input_types::GameStats, Games, NS2Stats};
use ns2_stat::{summarize_game, GameSummary};
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};

Expand All @@ -38,46 +38,6 @@ struct AppData {
path: PathBuf,
}

#[derive(Debug, Serialize)]
struct DatedData<T> {
date: u32,
data: T,
}

trait Dated {
fn date(&self) -> u32;
}

impl<T> Dated for DatedData<T> {
fn date(&self) -> u32 {
self.date
}
}

impl Dated for GameStats {
fn date(&self) -> u32 {
self.round_info.round_date
}
}

impl Dated for NS2Stats {
fn date(&self) -> u32 {
self.latest_game
}
}

impl<T: Dated> Dated for &T {
fn date(&self) -> u32 {
(*self).date()
}
}

impl<T: Dated> From<T> for DatedData<T> {
fn from(data: T) -> Self {
Self { date: data.date(), data }
}
}

#[derive(Clone, Copy, Debug, Deserialize)]
struct DateQuery {
from: Option<u32>,
Expand Down Expand Up @@ -105,17 +65,12 @@ async fn get_stats(data: Data<AppData>) -> impl Responder {
}

#[get("/stats/continuous")]
async fn get_continuous_stats(data: Data<AppData>, query: Query<DateQuery>) -> Json<Vec<DatedData<NS2Stats>>> {
async fn get_continuous_stats(data: Data<AppData>, query: Query<DateQuery>) -> Json<BTreeMap<u32, NS2Stats>> {
let games = data.games.read();
let game_stats = Games(games.range(query.to_range_bounds()).map(|(_, game)| game))
.genuine()
.collect::<Vec<_>>();
let game_stats = Games(games.range(query.to_range_bounds()).map(|(_, game)| game)).genuine().collect::<Vec<_>>();
let continuous_stats = (0..game_stats.len())
.map(|i| DatedData {
date: game_stats[i].round_info.round_date,
data: NS2Stats::compute(Games(game_stats[..=i].iter().copied())),
})
.collect::<Vec<_>>();
.map(|i| (game_stats[i].round_info.round_date, NS2Stats::compute(Games(game_stats[..=i].iter().copied()))))
.collect::<BTreeMap<_, _>>();
Json(continuous_stats)
}

Expand Down

0 comments on commit f7dfbcf

Please sign in to comment.