refactor(ocp): change the way logs are brought in and other changes to clean the code up

This commit is contained in:
2025-04-09 17:49:03 -05:00
parent bad390ec17
commit 2e2699ab3c
20 changed files with 307 additions and 124 deletions

View File

@@ -1,7 +1,7 @@
import {eq, sql} from "drizzle-orm";
import {db} from "../../../../database/dbclient.js";
import {logs} from "../../../../database/schema/logs.js";
import {createLog} from "../logger.js";
import { eq, sql } from "drizzle-orm";
import { db } from "../../../../database/dbclient.js";
import { logs } from "../../../../database/schema/logs.js";
import { createLog } from "../logger.js";
export const clearLog = async (id: string) => {
/**
@@ -10,13 +10,21 @@ export const clearLog = async (id: string) => {
try {
const clear = await db
.update(logs)
.set({checked: true, checkedAt: sql`NOW()`})
.where(eq(logs.log_id, id));
.update(logs)
.set({ checked: true, created_at: sql`NOW()` })
.where(eq(logs.log_id, id));
createLog("info", "lst", "logger", "Log just cleared.");
return {success: true, message: "Log was just cleared."};
return { success: true, message: "Log was just cleared." };
} catch (error) {
createLog("error", "lst", "logger", "There was an error clearing the log.");
return {success: false, message: "There was an error clearing the log."};
createLog(
"error",
"lst",
"logger",
"There was an error clearing the log."
);
return {
success: false,
message: "There was an error clearing the log.",
};
}
};

View File

@@ -1,4 +1,4 @@
import { and, eq, gte, inArray, lte, sql } from "drizzle-orm";
import { and, desc, eq, gte, inArray, lte, sql } from "drizzle-orm";
import { db } from "../../../../database/dbclient.js";
import { logs } from "../../../../database/schema/logs.js";
import { createLog } from "../logger.js";
@@ -21,7 +21,8 @@ export const getLogs = async (data: any) => {
inArray(logs.level, data.level),
eq(logs.checked, checked)
)
);
)
.orderBy(desc(logs.created_at));
return { success: true, message: "logs returned", data: logData };
} catch (error) {

View File

@@ -3,6 +3,7 @@ import { ocmeData } from "../../../../database/schema/ocme.js";
import { differenceInMinutes } from "date-fns";
import { createLog } from "../../logger/logger.js";
import { eq } from "drizzle-orm";
import { timeZoneFix } from "../../../globalUtils/timeZoneFix.js";
export const getInfo = async () => {
let ocmeInfo: any = [];
@@ -16,7 +17,7 @@ export const getInfo = async () => {
ocmeInfo = ocmeInfo.map((o: any) => {
const now = new Date(Date.now());
//const strippedDate = o.add_Date.replace("Z", "");
const diff = differenceInMinutes(now, o.add_Date);
const diff = differenceInMinutes(timeZoneFix(), o.add_Date);
return { ...o, waitingFor: diff };
});
createLog(

View File

@@ -60,11 +60,11 @@ export const labelingProcess = async ({
"error",
"labeling",
"ocp",
`There is not a lot assigned to ${macId[0]?.Name}.`
`There is not a lot assigned to ${line}.`
);
return {
success: false,
message: `There is not a lot assigned to ${macId[0]?.Name}.`,
message: `There is not a lot assigned to ${line}.`,
};
}
}
@@ -128,7 +128,7 @@ export const labelingProcess = async ({
}
// if there are more than 2 lots it might be an auto labeler, autolabeler will be defined by its id for now only dayton
if (filteredLot.length > 2 && plantToken[0].value !== "usday1") {
if (filteredLot?.length > 2 && plantToken[0].value !== "usday1") {
createLog(
"error",
"labeling",
@@ -214,15 +214,15 @@ export const labelingProcess = async ({
// create the label
const label = await createLabel(filteredLot[0], userPrinted);
if (!label.success) {
createLog(
"error",
"labeling",
"ocp",
`There was an error creating the label: ${label.message}`
);
return { sucess: false, message: label.message, data: label.data };
}
// if (!label.success) {
// createLog(
// "error",
// "labeling",
// "ocp",
// `There was an error creating the label: ${label.message}`
// );
// return { sucess: false, message: label.message, data: label.data };
// }
// send over to be booked in if we can do it.
const bookin = settingData.filter((s) => s.name === "bookin");

View File

@@ -75,8 +75,16 @@ export const labelerTagRead = async (tagData: any) => {
// check if we need to manual check due to 20 pallets.
if (currentPalletCheck <= cameraPalletCheck) {
currentPalletCheck = currentPalletCheck + 1;
currentPalletCheck++;
labelingProcess({ line: numericString });
createLog(
"info",
"dyco",
"ocp",
`You have printed ${currentPalletCheck} pallets, remaining until ${
cameraPalletCheck - currentPalletCheck
}.`
);
} else {
currentPalletCheck = 0;
createLog(

View File

@@ -17,6 +17,9 @@ import { assignedPrinters } from "./utils/checkAssignments.js";
import { printerCycle } from "./controller/printers/printerCycle.js";
import stopPrinterCycle from "./routes/printers/stopCycle.js";
import startPrinterCycle from "./routes/printers/startCycle.js";
import { printerCycleAutoLabelers } from "./controller/printers/printerCycleAutoLabelers.js";
import AutostartPrinterCycle from "./routes/printers/autoLabelerStart.js";
import AutostopPrinterCycle from "./routes/printers/autoLabelerStop.js";
const app = new OpenAPIHono();
@@ -27,6 +30,8 @@ const routes = [
updateprinters,
startPrinterCycle,
stopPrinterCycle,
AutostartPrinterCycle,
AutostopPrinterCycle,
// lots
getLots,
// labeling
@@ -76,6 +81,7 @@ setTimeout(async () => {
await updatePrinters();
await assignedPrinters();
printerCycle();
printerCycleAutoLabelers();
}
}, 10 * 1000);

View File

@@ -0,0 +1,41 @@
// an external way to creating logs
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
import { authMiddleware } from "../../../auth/middleware/authMiddleware.js";
import { responses } from "../../../../globalUtils/routeDefs/responses.js";
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
import { printerCycle } from "../../controller/printers/printerCycle.js";
const app = new OpenAPIHono({ strict: false });
app.openapi(
createRoute({
tags: ["ocp:printers"],
summary: "starts the printers cycling.",
method: "get",
path: "/startsprintercycle",
middleware: authMiddleware,
//description: "This might be a temp soltuin during the transtion between versions",
// request: {
// body: {content: {"application/json": {schema: CreateLog}}},
// },
responses: responses(),
}),
async (c) => {
const { data, error } = await tryCatch(printerCycle());
const dataError: any = error;
if (error) {
return c.json({
success: false,
message: "Error in stopping the printer cycle",
data: dataError?.data,
});
}
const getData: any = data;
return c.json({
success: getData?.success,
message: getData?.message,
data: getData.data ?? [],
});
}
);
export default app;

View File

@@ -0,0 +1,41 @@
// an external way to creating logs
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
import { authMiddleware } from "../../../auth/middleware/authMiddleware.js";
import { responses } from "../../../../globalUtils/routeDefs/responses.js";
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
import { stopPrinterCycle } from "../../controller/printers/printerCycle.js";
const app = new OpenAPIHono({ strict: false });
app.openapi(
createRoute({
tags: ["ocp:printers"],
summary: "Stops the printers cycling.",
method: "get",
path: "/stopprintercycle",
middleware: authMiddleware,
//description: "This might be a temp soltuin during the transtion between versions",
// request: {
// body: {content: {"application/json": {schema: CreateLog}}},
// },
responses: responses(),
}),
async (c) => {
const { data, error } = await tryCatch(stopPrinterCycle());
const dataError: any = error;
if (error) {
return c.json({
success: false,
message: "Error in stopping the printer cycle",
data: dataError?.data,
});
}
const getData: any = data;
return c.json({
success: getData?.success,
message: getData?.message,
data: getData.data ?? [],
});
}
);
export default app;

View File

@@ -49,7 +49,7 @@ app.openapi(
return c.json({
success: getData?.success,
message: getData?.message,
data: getData.data ?? [],
data: getData?.data ?? [],
});
}
);

View File

@@ -28,8 +28,8 @@ export const assignedPrinters = async () => {
};
}
const printers: any = print.data;
const lots: any = l.data;
const printers: any = print.data ?? [];
const lots: any = l.data ?? [];
for (let i = 0; i < printers.length; i++) {
// is the printer assinged in alplalabel online?