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

@ -1,6 +1,7 @@
use serde::{Deserialize, Deserializer}; use serde::{Deserialize, Deserializer};
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
use std::str; use std::str;
use super::PromMetric;
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct Worlds { pub struct Worlds {
@ -18,8 +19,8 @@ pub enum WorldLocation {
impl<'de> Deserialize<'de> for WorldLocation { impl<'de> Deserialize<'de> for WorldLocation {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where where
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
let field = i16::deserialize(deserializer)?; let field = i16::deserialize(deserializer)?;
let world = match field { let world = match field {
@ -66,8 +67,8 @@ pub enum WorldType {
impl<'de> Deserialize<'de> for WorldType { impl<'de> Deserialize<'de> for WorldType {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where where
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
let field = String::deserialize(deserializer)?; let field = String::deserialize(deserializer)?;
let res = match field.as_str() { let res = match field.as_str() {
@ -101,15 +102,11 @@ pub struct World {
pub types: Vec<WorldType>, pub types: Vec<WorldType>,
} }
pub trait PromMetric {
fn to_metric_string(self: &Self) -> String;
}
impl PromMetric for World { impl PromMetric for World {
fn to_metric_string(&self) -> String { fn to_metric_string(&self) -> String {
//"player_count{id=\"301\",location=\"Germany\,"isMembers=\"true\"} 123" //"player_count{id=\"301\",location=\"Germany\,"isMembers=\"true\"} 123"
format!( format!(
"player_count{{id=\"{}\",location=\"{}\",isMembers=\"{:?}\"}} {}", "osrs_world_players{{id=\"{}\",location=\"{}\",isMembers=\"{:?}\"}} {}",
self.id, self.id,
self.location, self.location,
self.is_members(), self.is_members(),
@ -123,3 +120,16 @@ impl World {
self.types.contains(&WorldType::Members) 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)
}

View file

@ -2,27 +2,34 @@ mod collector;
use axum::routing::get; use axum::routing::get;
use axum::Router; use axum::Router;
use collector::{PromMetric, Worlds}; use axum::extract::Path;
use axum::http::StatusCode;
use axum::response::IntoResponse;
use collectors::PromMetric;
async fn metrics() -> String { fn convert_into_metrics(data: Vec<impl PromMetric>) -> String {
let resp = reqwest::get("https://api.runelite.net/runelite-1.9.13/worlds.js") let metrics: Vec<String> = data
.await
.unwrap()
.json::<Worlds>()
.await
.unwrap();
let metric_strings: Vec<String> = resp
.worlds
.into_iter() .into_iter()
.map(|w| w.to_metric_string()) .map(|w| w.to_metric_string())
.collect(); .collect();
metric_strings.join("\n")
metrics.join("\n")
}
async fn worlds() -> impl IntoResponse {
let resp = match collectors::player_count::get_player_count().await {
Ok(r) => r,
Err(_) => return (StatusCode::INTERNAL_SERVER_ERROR, "Nope".to_string())
};
(StatusCode::OK, convert_into_metrics(resp))
} }
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let app = Router::new().route("/metrics", get(metrics)); let app = Router::new()
.route("/worlds", get(worlds));
// .route("/stats/:rsn", get(stats));
println!("Starting..."); println!("Starting...");