Skip to content

Commit

Permalink
add: new chart
Browse files Browse the repository at this point in the history
  • Loading branch information
btwlouis committed Dec 29, 2024
1 parent 8896cf6 commit 3f1bcd4
Show file tree
Hide file tree
Showing 9 changed files with 408 additions and 82 deletions.
43 changes: 39 additions & 4 deletions backend/src/repositories/server.history.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ class ServerHistoryRepository implements IServerHistoryRepository {
}

async retrieveById(id: string, period: string): Promise<IServerHistory[]> {
if (!period) period = "30";
period = period.replace("d", "") || "30";
const { interval, unit } = parsePeriod(period);

const query: string = `SELECT * FROM server_history WHERE id = ? AND timestamp > DATE_SUB(NOW(), INTERVAL ? DAY) ORDER BY timestamp ASC`;
const query = buildQuery(unit);

return new Promise((resolve, reject) => {
connection.query<IServerHistory[]>(query, [id, period], (err, res) => {
connection.query<IServerHistory[]>(query, [id, interval], (err, res) => {
if (err) {
reject(err);
} else {
Expand All @@ -39,4 +38,40 @@ class ServerHistoryRepository implements IServerHistoryRepository {
}
}

/**
* Parse the period string into an interval (number) and unit (h or d).
* @param period - The period string (e.g., '24h', '7d').
* @returns An object containing the interval and unit.
*/
function parsePeriod(period: string): { interval: number; unit: string } {
const match = period.match(/^(\d+)([hd])$/);

if (!match) {
throw new Error(
"Invalid period format. Expected format: 'Xd' or 'Xh' where X is a number."
);
}

const interval = parseInt(match[1], 10);
const unit = match[2];

return { interval, unit };
}

/**
* Build the query string based on the unit (h or d).
* @param unit - The unit of time ('h' for hours or 'd' for days).
* @returns The query string for the SQL query.
*/
function buildQuery(unit: string): string {
switch (unit) {
case "d":
return `SELECT * FROM server_history WHERE id = ? AND timestamp > DATE_SUB(NOW(), INTERVAL ? DAY) ORDER BY timestamp ASC`;
case "h":
return `SELECT * FROM server_history WHERE id = ? AND timestamp > DATE_SUB(NOW(), INTERVAL ? HOUR) ORDER BY timestamp ASC`;
default:
throw new Error("Invalid time unit. Use 'h' for hours or 'd' for days.");
}
}

export default new ServerHistoryRepository();
4 changes: 0 additions & 4 deletions backend/src/services/fivem.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,6 @@ export async function getServers() {
return;
}

if (server.id === "g8lqro") {
//console.log(server);
}

// parse server to Server model
const serverModel: IServer = {
id: server.id,
Expand Down
Loading

0 comments on commit 3f1bcd4

Please sign in to comment.