2023-10-24 12:27:34 +01:00
|
|
|
use std::collections::HashMap;
|
2023-10-20 22:08:51 +01:00
|
|
|
use std::error::Error;
|
|
|
|
use std::{env, fmt, fs};
|
|
|
|
|
|
|
|
use axum::extract::State;
|
|
|
|
use axum::{
|
|
|
|
body::Full,
|
|
|
|
http::{Response, StatusCode},
|
|
|
|
response::IntoResponse,
|
|
|
|
routing::get,
|
|
|
|
Router,
|
|
|
|
};
|
2023-10-21 19:25:06 +01:00
|
|
|
use lazy_static::lazy_static;
|
2023-10-20 22:08:51 +01:00
|
|
|
use maud::html;
|
|
|
|
use maud::Markup;
|
2023-10-24 13:03:16 +01:00
|
|
|
use prometheus::{
|
|
|
|
register_gauge_vec, register_int_counter_vec, Encoder, GaugeVec, IntCounterVec, TextEncoder,
|
|
|
|
};
|
2023-10-20 22:08:51 +01:00
|
|
|
use reqwest::header::USER_AGENT;
|
|
|
|
use serde::Deserialize;
|
|
|
|
use url::Url;
|
|
|
|
|
2023-10-24 13:03:16 +01:00
|
|
|
lazy_static! {
|
2023-10-20 22:08:51 +01:00
|
|
|
static ref PLAYER_COUNT: GaugeVec = register_gauge_vec!(
|
|
|
|
"vrchat_playercount",
|
|
|
|
"Current number of players in instance.",
|
2023-10-24 13:03:16 +01:00
|
|
|
&["instance", "world", "name", "group"],
|
2023-10-20 22:08:51 +01:00
|
|
|
)
|
2023-10-24 13:03:16 +01:00
|
|
|
.unwrap();
|
2023-10-20 22:08:51 +01:00
|
|
|
static ref VRCDN_VIEWERS: GaugeVec = register_gauge_vec!(
|
|
|
|
"vrcdn_viewers",
|
|
|
|
"Current number viewers according to VRCDN's API.",
|
2023-10-24 13:03:16 +01:00
|
|
|
&["region", "group"],
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
static ref WORLD_VISITS: IntCounterVec = register_int_counter_vec!(
|
|
|
|
"vrchat_world_visits",
|
|
|
|
"Number of times a world has been visited.",
|
|
|
|
&["world", "name"],
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
static ref WORLD_OCCUPANTS: GaugeVec = register_gauge_vec!(
|
|
|
|
"vrchat_world_occupants",
|
|
|
|
"Occupants currently in the world",
|
|
|
|
&["world", "name", "type"]
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
static ref WORLD_FAVORITES: IntCounterVec = register_int_counter_vec!(
|
|
|
|
"vrchat_world_favorites",
|
|
|
|
"Number of times a world has been favorited.",
|
|
|
|
&["world", "name"],
|
2023-10-20 22:08:51 +01:00
|
|
|
)
|
2023-10-24 13:03:16 +01:00
|
|
|
.unwrap();
|
2023-10-20 22:08:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum WsError {
|
|
|
|
Reqwest(reqwest::Error),
|
|
|
|
Url(url::ParseError),
|
|
|
|
Custom(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<url::ParseError> for WsError {
|
|
|
|
fn from(value: url::ParseError) -> Self {
|
|
|
|
Self::Url(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<reqwest::Error> for WsError {
|
|
|
|
fn from(value: reqwest::Error) -> Self {
|
|
|
|
Self::Reqwest(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error for WsError {}
|
|
|
|
|
|
|
|
impl fmt::Display for WsError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
WsError::Reqwest(e) => write!(f, "Reqwest error: {}", e),
|
|
|
|
WsError::Custom(e) => write!(f, "Error: {}", e),
|
|
|
|
WsError::Url(_) => todo!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
|
|
struct Config {
|
2023-10-24 12:27:34 +01:00
|
|
|
/// Groups we want to track.
|
|
|
|
groups: Option<HashMap<String, VrcGroup>>,
|
|
|
|
/// List of worlds we want to track.
|
|
|
|
worlds: Option<HashMap<String, String>>,
|
|
|
|
#[serde(skip)]
|
2023-10-20 22:08:51 +01:00
|
|
|
vrchat_token: Option<String>,
|
|
|
|
}
|
|
|
|
|
2023-10-24 12:27:34 +01:00
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
|
|
struct VrcGroup {
|
|
|
|
id: String,
|
|
|
|
vrcdn: Option<String>,
|
|
|
|
}
|
|
|
|
|
2023-10-20 22:08:51 +01:00
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
|
|
struct VrcInstance {
|
|
|
|
/// Instance ID.
|
|
|
|
instance: Option<String>,
|
|
|
|
/// World ID.
|
|
|
|
world: Option<String>,
|
2023-10-21 19:25:06 +01:00
|
|
|
/// Raw location
|
|
|
|
location: Option<String>,
|
2023-10-20 22:08:51 +01:00
|
|
|
/// Custom name for the instance.
|
2023-10-21 19:25:06 +01:00
|
|
|
name: Option<String>,
|
2023-10-20 22:08:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
|
|
struct VrcInstanceData {
|
2023-10-21 02:17:44 +01:00
|
|
|
#[serde(rename = "userCount")]
|
|
|
|
user_count: Option<f64>,
|
2023-10-20 22:08:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
|
|
struct VrCdnData {
|
|
|
|
viewers: Vec<VrCdnRegion>,
|
|
|
|
}
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
|
|
struct VrCdnRegion {
|
|
|
|
region: String,
|
|
|
|
total: f64,
|
|
|
|
}
|
|
|
|
|
2023-10-21 19:25:06 +01:00
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
|
|
struct VrcGroupInstance {
|
|
|
|
location: String,
|
|
|
|
}
|
|
|
|
|
2023-10-24 12:27:34 +01:00
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
|
|
struct VrcWorldData {
|
2023-10-24 13:03:16 +01:00
|
|
|
favorites: u64,
|
2023-10-24 12:27:34 +01:00
|
|
|
version: f64,
|
2023-10-24 13:03:16 +01:00
|
|
|
visits: u64,
|
2023-10-24 12:27:34 +01:00
|
|
|
popularity: f64,
|
|
|
|
heat: f64,
|
|
|
|
#[serde(rename = "publicOccupants")]
|
|
|
|
public_occupants: f64,
|
|
|
|
#[serde(rename = "privateOccupants")]
|
|
|
|
private_occupants: f64,
|
|
|
|
#[serde(rename = "occupants")]
|
|
|
|
total_occupants: f64,
|
|
|
|
instances: Vec<(String, f64)>,
|
|
|
|
}
|
|
|
|
|
2023-10-20 22:08:51 +01:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<(), ()> {
|
|
|
|
let content = fs::read_to_string("config.toml").unwrap();
|
|
|
|
let mut config: Config = toml::from_str(&content).unwrap();
|
|
|
|
config.vrchat_token = Some(env::var("VRCHAT_AUTH_TOKEN").unwrap());
|
|
|
|
|
|
|
|
let app = Router::new()
|
|
|
|
.route("/", get(homepage))
|
|
|
|
.route("/metrics", get(metrics_handler))
|
|
|
|
.with_state(config);
|
2023-10-21 19:25:06 +01:00
|
|
|
|
2023-10-20 22:08:51 +01:00
|
|
|
let _ = axum::Server::bind(&"0.0.0.0:6534".parse().unwrap())
|
|
|
|
.serve(app.into_make_service())
|
|
|
|
.await;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn metrics_handler(State(config): State<Config>) -> Result<impl IntoResponse, StatusCode> {
|
|
|
|
match metrics(config).await {
|
|
|
|
Ok(b) => Ok(Response::builder()
|
2023-10-24 13:03:16 +01:00
|
|
|
.header("content-type", "text/plain")
|
|
|
|
.status(StatusCode::OK)
|
|
|
|
.body(Full::from(b))
|
|
|
|
.unwrap()),
|
2023-10-20 22:08:51 +01:00
|
|
|
Err(_e) => Err(StatusCode::INTERNAL_SERVER_ERROR),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn homepage() -> Markup {
|
|
|
|
html! {
|
|
|
|
body {
|
|
|
|
main {
|
|
|
|
a href = "/metrics" { "metrics" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn metrics(config: Config) -> Result<Vec<u8>, WsError> {
|
2023-10-24 12:29:23 +01:00
|
|
|
PLAYER_COUNT.reset();
|
|
|
|
VRCDN_VIEWERS.reset();
|
2023-10-24 13:03:16 +01:00
|
|
|
WORLD_VISITS.reset();
|
2023-10-20 22:08:51 +01:00
|
|
|
let encoder = TextEncoder::new();
|
|
|
|
let client = reqwest::Client::new();
|
|
|
|
let auth_cookie = format!("auth={}", &config.vrchat_token.unwrap());
|
2023-10-24 13:03:16 +01:00
|
|
|
|
2023-10-24 12:27:34 +01:00
|
|
|
if config.groups.is_some() {
|
|
|
|
for (name, group) in config.groups.unwrap() {
|
|
|
|
let _ = group_metrics(&client, &auth_cookie, name, group).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if config.worlds.is_some() {
|
|
|
|
for (name, id) in config.worlds.unwrap() {
|
2023-10-24 13:03:16 +01:00
|
|
|
let _ = world_metrics(&client, &auth_cookie, name, id).await;
|
2023-10-24 12:27:34 +01:00
|
|
|
}
|
|
|
|
}
|
2023-10-20 22:08:51 +01:00
|
|
|
|
2023-10-24 12:27:34 +01:00
|
|
|
let metric_families = prometheus::gather();
|
|
|
|
let mut buffer = vec![];
|
|
|
|
encoder.encode(&metric_families, &mut buffer).unwrap();
|
|
|
|
Ok(buffer)
|
|
|
|
}
|
|
|
|
|
2023-10-24 13:03:16 +01:00
|
|
|
async fn group_metrics(
|
|
|
|
client: &reqwest::Client,
|
|
|
|
auth_cookie: &String,
|
|
|
|
name: String,
|
|
|
|
group: VrcGroup,
|
|
|
|
) -> Result<(), WsError> {
|
2023-10-24 12:27:34 +01:00
|
|
|
let instance_list_url = format!(
|
|
|
|
"https://api.vrchat.cloud/api/1/groups/{}/instances",
|
|
|
|
group.id
|
|
|
|
);
|
|
|
|
let url = Url::parse(&instance_list_url).unwrap();
|
|
|
|
let req = client
|
|
|
|
.get(url)
|
|
|
|
.header(
|
|
|
|
USER_AGENT,
|
|
|
|
"vr-event-tracker(git.gmem.ca/arch/vr-event-tracker)",
|
|
|
|
)
|
|
|
|
.header("Cookie", auth_cookie)
|
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let data: Vec<VrcGroupInstance> = req.json().await?;
|
|
|
|
let instances: Vec<VrcInstance> = data
|
|
|
|
.into_iter()
|
|
|
|
.map(|f| {
|
2023-10-21 19:25:06 +01:00
|
|
|
let spl: Vec<&str> = f.location.split(":").collect();
|
2023-10-24 12:27:34 +01:00
|
|
|
VrcInstance {
|
|
|
|
instance: Some(spl[0].to_owned()),
|
|
|
|
world: Some(spl[1].to_owned()),
|
|
|
|
location: Some(f.location),
|
|
|
|
name: None,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2023-10-21 19:25:06 +01:00
|
|
|
|
|
|
|
for instance in instances {
|
2023-10-20 22:08:51 +01:00
|
|
|
let api_url = format!(
|
2023-10-21 19:25:06 +01:00
|
|
|
"https://api.vrchat.cloud/api/1/instances/{}",
|
|
|
|
&instance.location.clone().unwrap()
|
2023-10-20 22:08:51 +01:00
|
|
|
);
|
|
|
|
let url = Url::parse(&api_url).unwrap();
|
|
|
|
|
|
|
|
let req = client
|
|
|
|
.get(url)
|
2023-10-21 19:25:06 +01:00
|
|
|
.header(
|
|
|
|
USER_AGENT,
|
|
|
|
"vr-event-tracker(git.gmem.ca/arch/vr-event-tracker)",
|
|
|
|
)
|
2023-10-24 12:27:34 +01:00
|
|
|
.header("Cookie", auth_cookie)
|
2023-10-20 22:08:51 +01:00
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let data: VrcInstanceData = req.json().await?;
|
2023-10-24 12:27:34 +01:00
|
|
|
let instance_name = instance.name.unwrap_or(instance.location.unwrap());
|
2023-10-20 22:08:51 +01:00
|
|
|
PLAYER_COUNT
|
2023-10-24 13:03:16 +01:00
|
|
|
.with_label_values(&[
|
|
|
|
&instance.world.unwrap(),
|
|
|
|
&instance.instance.unwrap(),
|
|
|
|
&instance_name,
|
|
|
|
&name,
|
|
|
|
])
|
2023-10-21 02:17:44 +01:00
|
|
|
.set(data.user_count.unwrap_or(0 as f64));
|
2023-10-20 22:08:51 +01:00
|
|
|
}
|
|
|
|
|
2023-10-24 12:27:34 +01:00
|
|
|
if group.vrcdn.is_some() {
|
2023-10-24 13:03:16 +01:00
|
|
|
let vrcdn_url = format!("https://api.vrcdn.live/v1/viewers/{}", group.vrcdn.unwrap());
|
2023-10-24 12:27:34 +01:00
|
|
|
let req = client
|
|
|
|
.get(vrcdn_url)
|
|
|
|
.header(
|
|
|
|
USER_AGENT,
|
|
|
|
"vr-event-tracker(git.gmem.ca/arch/vr-event-tracker)",
|
|
|
|
)
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let vrcdn_data: VrCdnData = req.json().await.unwrap();
|
|
|
|
for region in vrcdn_data.viewers {
|
|
|
|
VRCDN_VIEWERS
|
2023-10-24 13:03:16 +01:00
|
|
|
.with_label_values(&[®ion.region, &name])
|
2023-10-24 12:27:34 +01:00
|
|
|
.set(region.total);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-10-24 13:03:16 +01:00
|
|
|
async fn world_metrics(
|
|
|
|
client: &reqwest::Client,
|
|
|
|
auth_cookie: &String,
|
|
|
|
name: String,
|
|
|
|
id: String,
|
|
|
|
) -> Result<(), WsError> {
|
|
|
|
let api_url = format!("https://api.vrchat.cloud/api/1/worlds/{}", &id);
|
2023-10-24 12:27:34 +01:00
|
|
|
let url = Url::parse(&api_url).unwrap();
|
2023-10-24 13:03:16 +01:00
|
|
|
let req = client
|
2023-10-24 12:27:34 +01:00
|
|
|
.get(url)
|
2023-10-21 19:25:06 +01:00
|
|
|
.header(
|
|
|
|
USER_AGENT,
|
|
|
|
"vr-event-tracker(git.gmem.ca/arch/vr-event-tracker)",
|
|
|
|
)
|
2023-10-24 12:27:34 +01:00
|
|
|
.header("Cookie", auth_cookie)
|
2023-10-20 22:08:51 +01:00
|
|
|
.send()
|
2023-10-24 13:03:16 +01:00
|
|
|
.await?;
|
|
|
|
let world_data: VrcWorldData = req.json().await?;
|
2023-10-20 22:08:51 +01:00
|
|
|
|
2023-10-24 12:27:34 +01:00
|
|
|
for instance in world_data.instances {
|
|
|
|
PLAYER_COUNT
|
2023-10-24 13:03:16 +01:00
|
|
|
.with_label_values(&[&instance.0, &id, &name, ""])
|
2023-10-24 12:27:34 +01:00
|
|
|
.set(instance.1);
|
2023-10-20 22:08:51 +01:00
|
|
|
}
|
2023-10-24 13:03:16 +01:00
|
|
|
let current_visits: u64 = match WORLD_VISITS.get_metric_with_label_values(&[&id, &name]) {
|
|
|
|
Ok(v) => v.get(),
|
|
|
|
Err(_) => 0,
|
|
|
|
};
|
|
|
|
WORLD_VISITS
|
|
|
|
.with_label_values(&[&id, &name])
|
|
|
|
.inc_by(world_data.visits - current_visits);
|
|
|
|
let current_favorites: u64 = match WORLD_FAVORITES.get_metric_with_label_values(&[&id, &name]) {
|
|
|
|
Ok(v) => v.get(),
|
|
|
|
Err(_) => 0,
|
|
|
|
};
|
|
|
|
WORLD_FAVORITES
|
|
|
|
.with_label_values(&[&id, &name])
|
|
|
|
.inc_by(world_data.favorites - current_favorites);
|
|
|
|
|
|
|
|
WORLD_OCCUPANTS
|
|
|
|
.with_label_values(&[&id, &name, "private"])
|
|
|
|
.set(world_data.private_occupants);
|
|
|
|
WORLD_OCCUPANTS
|
|
|
|
.with_label_values(&[&id, &name, "public"])
|
|
|
|
.set(world_data.public_occupants);
|
|
|
|
WORLD_OCCUPANTS
|
|
|
|
.with_label_values(&[&id, &name, "total"])
|
|
|
|
.set(world_data.total_occupants);
|
|
|
|
|
2023-10-24 12:27:34 +01:00
|
|
|
Ok(())
|
2023-10-20 22:08:51 +01:00
|
|
|
}
|