Rename metrics and player_count to osrs_world_players

This commit is contained in:
Daan Boerlage 2023-04-06 02:31:53 +02:00
parent dccd2ca20c
commit 58f6d57571
Signed by: daan
GPG key ID: FCE070E1E4956606
3 changed files with 48 additions and 22 deletions

9
src/collectors/mod.rs Normal file
View file

@ -0,0 +1,9 @@
pub mod player_count;
// pub mod stats;
pub trait PromMetric {
fn to_metric_string(self: &Self) -> String;
}
pub const USER_AGENT: &str = "osrs-prometheus-exporter";
pub const RUNELITE_API_VERSION: &str = "runelite-1.9.13";

View file

@ -0,0 +1,135 @@
use serde::{Deserialize, Deserializer};
use std::fmt::{Display, Formatter};
use std::str;
use super::PromMetric;
#[derive(Deserialize, Debug)]
pub struct Worlds {
pub worlds: Vec<World>,
}
#[derive(Debug)]
pub enum WorldLocation {
Germany,
USA,
UnitedKingdom,
Australia,
Unknown,
}
impl<'de> Deserialize<'de> for WorldLocation {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let field = i16::deserialize(deserializer)?;
let world = match field {
0 => Self::USA,
1 => Self::UnitedKingdom,
3 => Self::Australia,
7 => Self::Germany,
_ => Self::Unknown,
};
Ok(world)
}
}
impl Display for WorldLocation {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
WorldLocation::Germany => write!(f, "Germany"),
WorldLocation::USA => write!(f, "USA"),
WorldLocation::UnitedKingdom => write!(f, "UK"),
WorldLocation::Australia => write!(f, "Australia"),
WorldLocation::Unknown => write!(f, "Unknown"),
}
}
}
#[derive(Debug, PartialEq)]
pub enum WorldType {
Members,
PVP,
Bounty,
PVPArena,
SkillTotal,
QuestSpeedrunning,
HighRisk,
LastManStanding,
NoSaveMode,
Tournament,
FreshStartWorld,
Deadman,
Seasonal,
Unknown,
}
impl<'de> Deserialize<'de> for WorldType {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let field = String::deserialize(deserializer)?;
let res = match field.as_str() {
"MEMBERS" => Self::Members,
"PVP" => Self::PVP,
"BOUNTY" => Self::Bounty,
"PVP_ARENA" => Self::PVPArena,
"SKILL_TOTAL" => Self::SkillTotal,
"QUEST_SPEEDRUNNING" => Self::QuestSpeedrunning,
"HIGH_RISK" => Self::HighRisk,
"LAST_MAN_STANDING" => Self::LastManStanding,
"NOSAVE_MODE" => Self::NoSaveMode,
"TOURNAMENT" => Self::Tournament,
"FRESH_START_WORLD" => Self::FreshStartWorld,
"DEADMAN" => Self::Deadman,
"SEASONAL" => Self::Seasonal,
_ => Self::Unknown,
};
Ok(res)
}
}
#[derive(Deserialize, Debug)]
pub struct World {
pub id: i16,
pub address: String,
pub activity: String,
pub location: WorldLocation,
pub players: i16,
pub types: Vec<WorldType>,
}
impl PromMetric for World {
fn to_metric_string(&self) -> String {
//"player_count{id=\"301\",location=\"Germany\,"isMembers=\"true\"} 123"
format!(
"osrs_world_players{{id=\"{}\",location=\"{}\",isMembers=\"{:?}\"}} {}",
self.id,
self.location,
self.is_members(),
self.players
)
}
}
impl World {
pub fn is_members(&self) -> bool {
self.types.contains(&WorldType::Members)
}
}
pub async fn get_player_count() -> eyre::Result<Vec<World>> {
let req_url = format!("https://api.runelite.net/{}/worlds.js", super::RUNELITE_API_VERSION);
let resp = reqwest::Client::new()
.get(req_url)
.header("User-Agent", super::USER_AGENT)
.send()
.await?
.json::<Worlds>()
.await?;
Ok(resp.worlds)
}