@remnawave/xtls-sdk
Version:
A Typescript SDK for XRAY (XTLS) Core GRPC Api
47 lines (46 loc) • 1.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetUserStatsResponseModel = void 0;
/**
* Model class for handling user statistics response data from the Xray server.
*/
class GetUserStatsResponseModel {
/**
* Creates a new GetUserStatsResponseModel instance.
*
* @param data - The raw query stats response from the Xray server
*/
constructor(data) {
const users = this.parseData(data);
this.user = users[0] ?? null;
}
/**
* Parses the raw stats response data into formatted user statistics.
*
* @param data - The raw query stats response to parse
* @returns An array of formatted user statistics
* @private
*/
parseData(data) {
const formattedStats = data.stat.reduce((acc, curr) => {
const nameParts = curr.name.split('>>>');
const username = nameParts[1];
const type = nameParts[3];
const value = curr.value;
let user = acc.find((item) => item.username === username);
if (!user) {
user = { username, uplink: 0, downlink: 0 };
acc.push(user);
}
if (type === 'uplink') {
user.uplink += value;
}
else if (type === 'downlink') {
user.downlink += value;
}
return acc;
}, []);
return formattedStats;
}
}
exports.GetUserStatsResponseModel = GetUserStatsResponseModel;