From 61f0b7f06b8f0a811387dc3f17fcacb00804d146 Mon Sep 17 00:00:00 2001 From: Blake Matthes Date: Mon, 14 Jul 2025 12:37:38 -0500 Subject: [PATCH] feat(labeling): ratios reset for labeling implemeneted --- database/schema/ratios.ts | 4 +- .../components/production/ocp/LabelRatio.tsx | 27 ++++ .../src/components/production/ocp/OcpPage.tsx | 2 + .../utils/querys/production/labelRatio.tsx | 20 +++ .../labelRatio/labelRatioColumns.tsx | 87 ++++++++++++ .../production/labelRatio/labelRatioData.tsx | 124 ++++++++++++++++++ .../ocp/controller/labeling/getLabelRatio.ts | 8 +- .../ocp/controller/labeling/labelRatio.ts | 33 ++++- server/services/ocp/ocpService.ts | 4 + .../ocp/routes/labeling/getLabelRatio.ts | 1 - .../ocp/routes/labeling/resetLabelRatio.ts | 38 ++++++ 11 files changed, 340 insertions(+), 8 deletions(-) create mode 100644 frontend/src/components/production/ocp/LabelRatio.tsx create mode 100644 frontend/src/utils/querys/production/labelRatio.tsx create mode 100644 frontend/src/utils/tableData/production/labelRatio/labelRatioColumns.tsx create mode 100644 frontend/src/utils/tableData/production/labelRatio/labelRatioData.tsx create mode 100644 server/services/ocp/routes/labeling/resetLabelRatio.ts diff --git a/database/schema/ratios.ts b/database/schema/ratios.ts index f9dab99..80b7be9 100644 --- a/database/schema/ratios.ts +++ b/database/schema/ratios.ts @@ -14,8 +14,8 @@ export const labelRatio = pgTable( { ratio_id: uuid(" ratio_id").defaultRandom().primaryKey(), name: text("name").default("labels"), - autoLabel: integer("autoLabel").default(1), - manualLabel: integer("manualLabel").default(1), + autoLabel: integer("autoLabel").default(0), + manualLabel: integer("manualLabel").default(0), lastReset: timestamp().defaultNow(), }, (table) => [ diff --git a/frontend/src/components/production/ocp/LabelRatio.tsx b/frontend/src/components/production/ocp/LabelRatio.tsx new file mode 100644 index 0000000..3c81bae --- /dev/null +++ b/frontend/src/components/production/ocp/LabelRatio.tsx @@ -0,0 +1,27 @@ +import { getlabelRatio } from "@/utils/querys/production/labelRatio"; +import { labelRatioColumns } from "@/utils/tableData/production/labelRatio/labelRatioColumns"; +import { LabelRatioTable } from "@/utils/tableData/production/labelRatio/labelRatioData"; +import { useQuery } from "@tanstack/react-query"; + +export default function LabelRatio() { + const { data, isError, isLoading } = useQuery(getlabelRatio()); + + const ratioData = data ? data : []; + if (isError) { + return
Error
; + } + + if (isLoading) { + return
Loading
; + } + + return ( +
+ +
+ ); +} diff --git a/frontend/src/components/production/ocp/OcpPage.tsx b/frontend/src/components/production/ocp/OcpPage.tsx index d35e491..c2d63b4 100644 --- a/frontend/src/components/production/ocp/OcpPage.tsx +++ b/frontend/src/components/production/ocp/OcpPage.tsx @@ -11,6 +11,7 @@ import { ResizablePanel, ResizablePanelGroup, } from "@/components/ui/resizable-panels"; +import LabelRatio from "./LabelRatio"; export default function OCPPage() { const { settings } = useSettingStore(); @@ -97,6 +98,7 @@ export default function OCPPage() { + diff --git a/frontend/src/utils/querys/production/labelRatio.tsx b/frontend/src/utils/querys/production/labelRatio.tsx new file mode 100644 index 0000000..8d29673 --- /dev/null +++ b/frontend/src/utils/querys/production/labelRatio.tsx @@ -0,0 +1,20 @@ +import { queryOptions } from "@tanstack/react-query"; +import axios from "axios"; + +export function getlabelRatio() { + return queryOptions({ + queryKey: ["labelRatio"], + queryFn: () => fetchSettings(), + + //staleTime: 1000, + refetchInterval: 2 * 2000, + refetchOnWindowFocus: true, + }); +} + +const fetchSettings = async () => { + const { data } = await axios.get(`/api/ocp/labelratio`); + // if we are not localhost ignore the devDir setting. + //const url: string = window.location.host.split(":")[0]; + return data.data ?? []; +}; diff --git a/frontend/src/utils/tableData/production/labelRatio/labelRatioColumns.tsx b/frontend/src/utils/tableData/production/labelRatio/labelRatioColumns.tsx new file mode 100644 index 0000000..fa79976 --- /dev/null +++ b/frontend/src/utils/tableData/production/labelRatio/labelRatioColumns.tsx @@ -0,0 +1,87 @@ +//import { fixTime } from "@/utils/fixTime"; +import { Button } from "@/components/ui/button"; +import { getReaders } from "@/utils/querys/rfid/getReaders"; +import { useQuery } from "@tanstack/react-query"; +import { ColumnDef } from "@tanstack/react-table"; +import axios from "axios"; +import { useState } from "react"; +import { toast } from "sonner"; + +// This type is used to define the shape of our data. +// You can use a Zod schema here if you want. +export type Adjustmnets = { + ratio_id: string; + name: string; + autoLabel: number; + manualLabel: string; +}; + +export const labelRatioColumns: ColumnDef[] = [ + // { + // accessorKey: "line", + // header: () =>
Line
, + // }, + { + accessorKey: "autoLabel", + header: "Auto Labels", + }, + { + accessorKey: "manualLabel", + header: "Manual Labels", + }, + { + accessorKey: "goodRatio", + header: "Ratio", + cell: ({ row }) => { + const goodRatio = + (parseInt(row.getValue("autoLabel")) / + (parseInt(row.getValue("autoLabel")) + + parseInt(row.getValue("manualLabel")))) * + 100; + return ( +
+ {isNaN(goodRatio) ? 0 : goodRatio.toFixed(2)}% +
+ ); + }, + }, + { + accessorKey: "reset", + header: "Reset Reads", + cell: () => { + const { refetch } = useQuery(getReaders()); + const [readerReset, setReaderReset] = useState(false); + const resetReads = async () => { + setReaderReset(true); + try { + const res = await axios.post("/api/ocp/resetlabelratio", { + reader: name, + }); + + if (res.status === 200) { + toast.success(res.data.message); + setReaderReset(false); + } else { + toast.error(res.data.message); + setReaderReset(false); + } + } catch (error: any) { + toast.error(error.data.message); + setReaderReset(false); + } + refetch(); + }; + return ( +
+ +
+ ); + }, + }, +]; diff --git a/frontend/src/utils/tableData/production/labelRatio/labelRatioData.tsx b/frontend/src/utils/tableData/production/labelRatio/labelRatioData.tsx new file mode 100644 index 0000000..c94803e --- /dev/null +++ b/frontend/src/utils/tableData/production/labelRatio/labelRatioData.tsx @@ -0,0 +1,124 @@ +import { + ColumnDef, + flexRender, + getCoreRowModel, + useReactTable, + getPaginationRowModel, +} from "@tanstack/react-table"; +import { + Table, + TableBody, + TableCell, + TableHead, + TableHeader, + TableRow, +} from "@/components/ui/table"; + +import { useState } from "react"; + +import { ScrollArea } from "@/components/ui/scroll-area"; +import { LstCard } from "@/components/extendedUI/LstCard"; + +interface DataTableProps { + columns: ColumnDef[]; + data: TData[]; + //style: any; +} + +export function LabelRatioTable({ + columns, + data, + //style, +}: DataTableProps) { + const [pagination, setPagination] = useState({ + pageIndex: 0, //initial page index + pageSize: 5, //default page size + }); + const table = useReactTable({ + data, + columns, + getCoreRowModel: getCoreRowModel(), + getPaginationRowModel: getPaginationRowModel(), + onPaginationChange: setPagination, + state: { + //... + pagination, + }, + }); + + //console.log(parseInt(style.height.replace("px", "")) - 50); + return ( + +
+
+ {data.length === 0 ? ( + + No labels printed since last reset. + + ) : ( + Label Ratio + )} +
+ + + + {table.getHeaderGroups().map((headerGroup) => ( + + {headerGroup.headers.map((header) => { + return ( + + {header.isPlaceholder + ? null + : flexRender( + header.column + .columnDef.header, + header.getContext() + )} + + ); + })} + + ))} + + + {table.getRowModel().rows?.length ? ( + table.getRowModel().rows.map((row) => ( + + {row.getVisibleCells().map((cell) => ( + + {flexRender( + cell.column.columnDef.cell, + cell.getContext() + )} + + ))} + + )) + ) : ( + + + No labels. + + + )} + +
+
+
+
+ ); +} diff --git a/server/services/ocp/controller/labeling/getLabelRatio.ts b/server/services/ocp/controller/labeling/getLabelRatio.ts index 1bd368e..d13b20f 100644 --- a/server/services/ocp/controller/labeling/getLabelRatio.ts +++ b/server/services/ocp/controller/labeling/getLabelRatio.ts @@ -1,23 +1,23 @@ +import { eq } from "drizzle-orm"; import { db } from "../../../../../database/dbclient.js"; import { labelRatio } from "../../../../../database/schema/ratios.js"; import { tryCatch } from "../../../../globalUtils/tryCatch.js"; export const getLabelRatio = async () => { const { data: labelInfo, error: labelError } = await tryCatch( - db.select().from(labelRatio) + db.select().from(labelRatio).where(eq(labelRatio.name, "label")) ); if (labelError) { return { success: false, - message: "There was an error getting the labels", + message: "There was an error getting the labelratio", data: [labelError], }; } return { success: true, - message: "Current labels order by upd_Date.", - count: labelInfo.length, + message: "Current labelratio.", data: labelInfo, }; }; diff --git a/server/services/ocp/controller/labeling/labelRatio.ts b/server/services/ocp/controller/labeling/labelRatio.ts index b15587b..558a18e 100644 --- a/server/services/ocp/controller/labeling/labelRatio.ts +++ b/server/services/ocp/controller/labeling/labelRatio.ts @@ -1,4 +1,4 @@ -import { sql } from "drizzle-orm"; +import { eq, sql } from "drizzle-orm"; import { db } from "../../../../../database/dbclient.js"; import { labelRatio } from "../../../../../database/schema/ratios.js"; import { tryCatch } from "../../../../globalUtils/tryCatch.js"; @@ -51,3 +51,34 @@ export const manualLabelCreated = async () => { ); } }; + +export const resetLabelRatio = async () => { + const { error } = await tryCatch( + db + .update(labelRatio) + .set({ + name: sql`'label-' || (SELECT count(*) FROM ${labelRatio})`, + lastReset: sql`NOW()`, + }) + .where(eq(labelRatio.name, "label")) + ); + + if (error) { + console.log(error); + createLog( + "error", + "labeling", + "ocp", + "There was an error resetting Label Ratio" + ); + return { + success: false, + message: "There was an issue resetting the label data.", + }; + } + + return { + success: true, + message: "Label Ratio has been reset.", + }; +}; diff --git a/server/services/ocp/ocpService.ts b/server/services/ocp/ocpService.ts index bbe0ad6..37c192f 100644 --- a/server/services/ocp/ocpService.ts +++ b/server/services/ocp/ocpService.ts @@ -22,6 +22,8 @@ import AutostartPrinterCycle from "./routes/printers/autoLabelerStart.js"; import AutostopPrinterCycle from "./routes/printers/autoLabelerStop.js"; import { deleteLabels } from "../../globalUtils/dbCleanUp/labelCleanUp.js"; import bookInLabel from "./routes/labeling/bookIn.js"; +import labelRatio from "./routes/labeling/getLabelRatio.js"; +import resetRatio from "./routes/labeling/resetLabelRatio.js"; const app = new OpenAPIHono(); @@ -40,6 +42,8 @@ const routes = [ getLabels, manualprint, bookInLabel, + labelRatio, + resetRatio, //dyco dycoCon, dycoClose, diff --git a/server/services/ocp/routes/labeling/getLabelRatio.ts b/server/services/ocp/routes/labeling/getLabelRatio.ts index 19b2ef4..c72fec2 100644 --- a/server/services/ocp/routes/labeling/getLabelRatio.ts +++ b/server/services/ocp/routes/labeling/getLabelRatio.ts @@ -31,7 +31,6 @@ app.openapi( return c.json({ success: labelData.success, message: labelData.message, - count: labelData.count, data: labelData.data, }); } diff --git a/server/services/ocp/routes/labeling/resetLabelRatio.ts b/server/services/ocp/routes/labeling/resetLabelRatio.ts new file mode 100644 index 0000000..4abc518 --- /dev/null +++ b/server/services/ocp/routes/labeling/resetLabelRatio.ts @@ -0,0 +1,38 @@ +// an external way to creating logs +import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi"; +import { responses } from "../../../../globalUtils/routeDefs/responses.js"; +import { tryCatch } from "../../../../globalUtils/tryCatch.js"; +import { apiHit } from "../../../../globalUtils/apiHits.js"; +import { getLabelRatio } from "../../controller/labeling/getLabelRatio.js"; +import { resetLabelRatio } from "../../controller/labeling/labelRatio.js"; + +const app = new OpenAPIHono({ strict: false }); + +app.openapi( + createRoute({ + tags: ["ocp"], + summary: "Resets the label Ratio", + method: "post", + path: "/resetlabelratio", + responses: responses(), + }), + async (c) => { + const { data: labelData, error: labelError } = await tryCatch( + resetLabelRatio() + ); + apiHit(c, { endpoint: "/labelratio" }); + + if (labelError) { + return c.json({ + success: false, + message: "There was an error getting the printers", + }); + } + + return c.json({ + success: labelData.success, + message: labelData.message, + }); + } +); +export default app;