Skip to content

Commit

Permalink
start adding some cool numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
mrhappyma committed Jun 24, 2024
1 parent 789094f commit 86e9609
Show file tree
Hide file tree
Showing 10 changed files with 824 additions and 568 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"aws-sdk": "^2.1565.0",
"discord.js": "^14.13.0",
"express": "^4.18.3",
"prom-client": "^15.1.2",
"sqids": "^0.3.0",
"zod": "^3.22.3"
},
Expand Down
1,308 changes: 741 additions & 567 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const bot = new Powercord(env.DSC_TOKEN, {
});
export default bot;
registerSentryButtons();
import "./modules/metrics";

import { finishPackOrder } from "./modules/pack"; // this file imports the bot, so it must be imported after the bot is created

Expand Down
7 changes: 7 additions & 0 deletions src/modules/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
handleIncomingApplication,
submittedApplicationSchema,
} from "./applications";
import { register } from "prom-client";

const api = express();
api.use(express.json());
Expand All @@ -17,6 +18,12 @@ api.post("/api/applications/submit", (req, res) => {
}
});

api.get("/metrics", async (req, res) => {
res
.header("Content-Type", register.contentType)
.send(await register.metrics());
});

api.get("/", (req, res) => {
res.send(200);
});
Expand Down
3 changes: 3 additions & 0 deletions src/modules/deliver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
clearKitchenMessages,
sendKitchenMessage,
} from "../utils/kitchenChannels";
import { updateProcessingOrders } from "./metrics";

const sendDeliveryContent = async (
o: order,
Expand Down Expand Up @@ -122,6 +123,7 @@ bot.registerButton(/order:(\d+):deliver/, async (interaction) => {
invite: invite.url,
},
});
updateProcessingOrders(orderStatus.DELIVERING, order.id);

await clearKitchenMessages(order.id);
const deliveringActionRow =
Expand Down Expand Up @@ -191,6 +193,7 @@ bot.registerButton(/order:(\d+):complete/, async (interaction) => {
status: orderStatus.DELIVERED,
},
});
updateProcessingOrders(orderStatus.DELIVERED, order.id);
} catch (e) {
return { success: false, message: "Failed to mark delivery finished" };
}
Expand Down
2 changes: 2 additions & 0 deletions src/modules/fill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
clearKitchenMessages,
sendKitchenMessage,
} from "../utils/kitchenChannels";
import { updateProcessingOrders } from "./metrics";

bot.registerButton("order:(\\d+):fill", async (interaction) => {
const orderId = interaction.customId.split(":")[1];
Expand Down Expand Up @@ -42,6 +43,7 @@ bot.registerButton("order:(\\d+):fill", async (interaction) => {
chefUsername: interaction.user.username,
},
});
updateProcessingOrders(orderStatus.FILLING, order.id);
} catch (e) {
return interaction.reply({
content: "Failed to claim order",
Expand Down
58 changes: 58 additions & 0 deletions src/modules/metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { OrderStatus } from "aws-sdk/clients/outposts";
import { Gauge, Histogram, collectDefaultMetrics } from "prom-client";
import { prisma } from "..";
import { orderStatus } from "@prisma/client";

collectDefaultMetrics();

const processingOrders = new Gauge({
name: "processing_orders",
help: "Gauge of processing orders",
labelNames: ["status"],
});
const ordersCache = new Map<number, OrderStatus>();
const initOrdersCache = async () => {
const orders = await prisma.order.findMany({
where: {
status: {
notIn: [
orderStatus.DELIVERED,
orderStatus.CANCELLED,
orderStatus.REJECTED,
],
},
},
});
orders.forEach((order) => {
ordersCache.set(order.id, order.status);
});
updateCounts();
};
initOrdersCache();

const updateCounts = () => {
for (const status of Object.values(orderStatus)) {
if (status === orderStatus.DELIVERED) continue;
if (status === orderStatus.CANCELLED) continue;
if (status === orderStatus.REJECTED) continue;
const count = [...ordersCache.entries()].filter(
([, s]) => s === status
).length;
processingOrders.set({ status }, count);
}
};
export const updateProcessingOrders = (
status: OrderStatus,
orderNumber: number
) => {
if (
status === orderStatus.DELIVERED ||
status === orderStatus.CANCELLED ||
status === orderStatus.REJECTED
) {
ordersCache.delete(orderNumber);
} else {
ordersCache.set(orderNumber, status);
}
updateCounts();
};
7 changes: 6 additions & 1 deletion src/modules/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { emojiInline } from "../utils/emoji";
import env from "../utils/env";
import { KitchenChannel, sendKitchenMessage } from "../utils/kitchenChannels";
import { closed, closedReason } from "./closed";
import { updateProcessingOrders } from "./metrics";
import { orderStatus } from "@prisma/client";

bot.addGlobalCommand(
new SlashCommandBuilder()
Expand All @@ -20,7 +22,9 @@ bot.addGlobalCommand(
.addStringOption((option) =>
option
.setName("order")
.setDescription("What would you like to order? - 1 reasonable item per order :)")
.setDescription(
"What would you like to order? - 1 reasonable item per order :)"
)
.setRequired(true)
.setMaxLength(241)
) as SlashCommandBuilder,
Expand Down Expand Up @@ -98,6 +102,7 @@ bot.addGlobalCommand(
statusMessageId: message.id,
},
});
updateProcessingOrders(orderStatus.ORDERED, record.id);

const kitchenActionRow =
new ActionRowBuilder<ButtonBuilder>().addComponents([
Expand Down
3 changes: 3 additions & 0 deletions src/modules/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
import s3 from "./s3";
import handleError from "./sentry";
import Sqids from "sqids";
import { updateProcessingOrders } from "./metrics";

// this took way too long to get copilot to spit out it had better work
const URL_REGEX =
Expand Down Expand Up @@ -108,6 +109,7 @@ bot.client.on("messageCreate", async (message) => {
fileUrl: c,
},
});
updateProcessingOrders(orderStatus.PACKING, order.id);
setTimeout(() => finishPackOrder(order.id), 1000 * 60 * 5);

clearKitchenMessages(order.id);
Expand Down Expand Up @@ -177,6 +179,7 @@ export const finishPackOrder = async (orderId: number) => {
status: orderStatus.PACKED,
},
});
updateProcessingOrders(orderStatus.PACKED, order.id);
} catch (e) {
sendKitchenMessage(KitchenChannel.logs, {
content: `:x: Failed to finish packing order ${orderId}!! <@!${
Expand Down
2 changes: 2 additions & 0 deletions src/modules/reject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
clearKitchenMessages,
sendKitchenMessage,
} from "../utils/kitchenChannels";
import { updateProcessingOrders } from "./metrics";

bot.registerButton(/order:(\d+):reject/, async (interaction) => {
const orderId = interaction.customId.split(":")[1];
Expand Down Expand Up @@ -48,6 +49,7 @@ bot.registerModal(/order:(\d+):reject:modal/, async (interaction) => {
rejectorId: interaction.user.id,
},
});
updateProcessingOrders(orderStatus.REJECTED, order.id);
} catch (e) {
return interaction.reply({
content: "Failed to reject order",
Expand Down

0 comments on commit 86e9609

Please sign in to comment.