Took 5 hours 14 minutes
This commit is contained in:
2022-11-21 12:13:30 +01:00
parent a4ac8d083b
commit a722caf2d4
17 changed files with 421 additions and 106 deletions

View File

@ -3,6 +3,11 @@ import {WebSocketEvent} from "./WebSocketEvent";
import {iTender} from "./iTender";
import {Settings} from "./Settings";
import Container from "./database/Container";
import Job from "./database/Job";
import Drink from "./database/Drink";
import {IDrink} from "./database/IDrink";
import Ingredient from "./database/Ingredient";
import {RequestType} from "./RequestType";
export class WebSocketHandler {
private static _ws: WebSocket;
@ -28,6 +33,11 @@ export class WebSocketHandler {
});
}
public static answerRequest(type: RequestType, content: any) {
WebSocketHandler.send(new WebSocketPayload(WebSocketEvent.RESPONSE, false, {type: type, content: content}));
}
public static sendStatus() {
return new Promise(resolve => {
let payload = new WebSocketPayload(WebSocketEvent.STATUS, false, {status: iTender.status});
@ -39,12 +49,52 @@ export class WebSocketHandler {
return new Promise(resolve => {
let payload = new WebSocketPayload(WebSocketEvent.CONFIG, false, Settings.json);
WebSocketHandler.send(payload).then(resolve);
})
});
}
static sendStats() {
return new Promise(async resolve => {
let counts: IDrink[] = [];
for (let drink of (await Drink.find())) {
// @ts-ignore
drink.count = (await Job.countDocuments({drink: drink}));
counts.push(drink)
}
counts.sort((a: IDrink, b: IDrink) => {
// @ts-ignore
if (a.count < b.count)
return -1;
// @ts-ignore
else if (a.count > b.count)
return 1;
else
return 0;
});
let stats = {
"drinks_finished": (await Job.countDocuments({successful: true})),
"drink_most": counts[0].name,
"count_ingredients": (await Ingredient.countDocuments()),
"count_cocktails": (await Drink.countDocuments())
};
let payload = new WebSocketPayload(WebSocketEvent.RESPONSE, false, {
type: RequestType.STATS,
content: stats
});
WebSocketHandler.send(payload).then(resolve);
});
}
static sendContainers() {
return new Promise(async resolve => {
let payload = new WebSocketPayload(WebSocketEvent.CONTAINERS, false, ( await Container.find() ));
let payload = new WebSocketPayload(WebSocketEvent.CONTAINERS, false, (await Container.find()));
WebSocketHandler.send(payload).then(resolve);
})
}
static sendDrinks() {
return new Promise(async resolve => {
let payload = new WebSocketPayload(WebSocketEvent.DRINKS, false, iTender.drinks);
WebSocketHandler.send(payload).then(resolve);
})
}