~jae/neos-metrics-exporter

dd4e85ee1f515c84df0541397d5bb7e0bf4e6467 — Jae Salokettu (J4) 1 year, 1 month ago 1cc51b2
SRC: add support for multiple users
2 files changed, 23 insertions(+), 16 deletions(-)

M src/environment.ts
M src/utils/neosApiClient.ts
M src/environment.ts => src/environment.ts +1 -1
@@ 7,4 7,4 @@ export const HOST: string = env.HOST ?? '::';
export const PORT: number = parseInt(env.PORT ?? '3001');
export const PRODUCTION: boolean = env.NODE_ENV === 'production';

export const USER: string = env.USER ?? 'USFN-Orion';
\ No newline at end of file
export const USERS: string[] = env.USERS?.split(',') || ['USFN-Orion'];
\ No newline at end of file

M src/utils/neosApiClient.ts => src/utils/neosApiClient.ts +22 -15
@@ 1,13 1,13 @@
import axios, { AxiosInstance } from 'axios';
import { NeosUserData } from '../types/neosUser';
import { USER } from '../environment';
import { USERS } from '../environment';

const neosApiClient = axios.create({
    baseURL: 'https://api.neos.com'
});

const getSessionInfo = async (): Promise<NeosUserData|void> => {
    const res = await neosApiClient.get(`/api/users/U-${USER}/status`).catch((error) => {
const getSessionInfo = async (user: string): Promise<NeosUserData|void> => {
    const res = await neosApiClient.get(`/api/users/U-${user}/status`).catch((error) => {
        console.error(error);
        return;
    });


@@ 21,21 21,28 @@ const getSessionInfo = async (): Promise<NeosUserData|void> => {
}

export const generatePrometheusString = async (): Promise<string> => {
    const userData = await getSessionInfo();
    let totalPlayers: number = 0;
    const fullPrometheusString = await Promise.all(USERS.map(async (user): Promise<string | undefined> => {
        const userData = await getSessionInfo(user);

    console.log(userData);
        if (!userData || userData.onlineStatus !== 'Online' || !userData.activeSessions) {
            console.log("no sessions found");
            return;
        }

    if (!userData || userData.onlineStatus !== 'Online' || !userData.activeSessions) {
        console.log("no sessions found");
        return "";
    }
        const prometheusString = userData.activeSessions.map((session) => {
            totalPlayers += session.joinedUsers;
            return `player_count{label="${session.name}"} ${session.joinedUsers}`;
        });

    let totalPlayers: number = 0;
        return prometheusString.join('\n');
    }));

    const prometheusString = userData.activeSessions.map((session) => {
        totalPlayers += session.joinedUsers;
        return `player_count{label="${session.name}"} ${session.joinedUsers}`;
    });
    console.log(fullPrometheusString);

    if (!fullPrometheusString) {
        return `player_count{label="total"} ${totalPlayers}`;
    }

    return `${prometheusString.join('\n')}\nplayer_count{label="total"} ${totalPlayers}`;
    return `${fullPrometheusString}\nplayer_count{label="total"} ${totalPlayers}`;
}