feat(labeling): ratios reset for labeling implemeneted

This commit is contained in:
2025-07-14 12:37:38 -05:00
parent a7f8e39bac
commit 61f0b7f06b
11 changed files with 340 additions and 8 deletions

View File

@@ -14,8 +14,8 @@ export const labelRatio = pgTable(
{ {
ratio_id: uuid(" ratio_id").defaultRandom().primaryKey(), ratio_id: uuid(" ratio_id").defaultRandom().primaryKey(),
name: text("name").default("labels"), name: text("name").default("labels"),
autoLabel: integer("autoLabel").default(1), autoLabel: integer("autoLabel").default(0),
manualLabel: integer("manualLabel").default(1), manualLabel: integer("manualLabel").default(0),
lastReset: timestamp().defaultNow(), lastReset: timestamp().defaultNow(),
}, },
(table) => [ (table) => [

View File

@@ -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 <div>Error</div>;
}
if (isLoading) {
return <div>Loading</div>;
}
return (
<div className="m-2">
<LabelRatioTable
columns={labelRatioColumns}
data={ratioData}
//style={style}
/>
</div>
);
}

View File

@@ -11,6 +11,7 @@ import {
ResizablePanel, ResizablePanel,
ResizablePanelGroup, ResizablePanelGroup,
} from "@/components/ui/resizable-panels"; } from "@/components/ui/resizable-panels";
import LabelRatio from "./LabelRatio";
export default function OCPPage() { export default function OCPPage() {
const { settings } = useSettingStore(); const { settings } = useSettingStore();
@@ -97,6 +98,7 @@ export default function OCPPage() {
<ResizableHandle /> <ResizableHandle />
<ResizablePanel> <ResizablePanel>
<PrinterStatus /> <PrinterStatus />
<LabelRatio />
</ResizablePanel> </ResizablePanel>
</ResizablePanelGroup> </ResizablePanelGroup>
</ResizablePanel> </ResizablePanel>

View File

@@ -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 ?? [];
};

View File

@@ -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<Adjustmnets>[] = [
// {
// accessorKey: "line",
// header: () => <div className="text-left">Line</div>,
// },
{
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 (
<div className="text-center font-medium">
{isNaN(goodRatio) ? 0 : goodRatio.toFixed(2)}%
</div>
);
},
},
{
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 (
<div className="text-left font-medium">
<Button
className="h-4"
onClick={resetReads}
disabled={readerReset}
>
Reset
</Button>
</div>
);
},
},
];

View File

@@ -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<TData, TValue> {
columns: ColumnDef<TData, TValue>[];
data: TData[];
//style: any;
}
export function LabelRatioTable<TData, TValue>({
columns,
data,
//style,
}: DataTableProps<TData, TValue>) {
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 (
<LstCard>
<div>
<div className="flex flex-row justify-between">
{data.length === 0 ? (
<span className="text-center">
No labels printed since last reset.
</span>
) : (
<span>Label Ratio</span>
)}
</div>
<ScrollArea className="max-h-32 rounded-md border m-2">
<Table
// style={{
// width: `${parseInt(style.width.replace("px", "")) - 50}px`,
// height: `${parseInt(style.height.replace("px", "")) - 200}px`,
// cursor: "move",
// }}
>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<TableHead key={header.id}>
{header.isPlaceholder
? null
: flexRender(
header.column
.columnDef.header,
header.getContext()
)}
</TableHead>
);
})}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows?.length ? (
table.getRowModel().rows.map((row) => (
<TableRow
key={row.id}
data-state={
row.getIsSelected() && "selected"
}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}>
{flexRender(
cell.column.columnDef.cell,
cell.getContext()
)}
</TableCell>
))}
</TableRow>
))
) : (
<TableRow>
<TableCell
colSpan={columns.length}
className="h-24 text-center"
>
No labels.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</ScrollArea>
</div>
</LstCard>
);
}

View File

@@ -1,23 +1,23 @@
import { eq } from "drizzle-orm";
import { db } from "../../../../../database/dbclient.js"; import { db } from "../../../../../database/dbclient.js";
import { labelRatio } from "../../../../../database/schema/ratios.js"; import { labelRatio } from "../../../../../database/schema/ratios.js";
import { tryCatch } from "../../../../globalUtils/tryCatch.js"; import { tryCatch } from "../../../../globalUtils/tryCatch.js";
export const getLabelRatio = async () => { export const getLabelRatio = async () => {
const { data: labelInfo, error: labelError } = await tryCatch( const { data: labelInfo, error: labelError } = await tryCatch(
db.select().from(labelRatio) db.select().from(labelRatio).where(eq(labelRatio.name, "label"))
); );
if (labelError) { if (labelError) {
return { return {
success: false, success: false,
message: "There was an error getting the labels", message: "There was an error getting the labelratio",
data: [labelError], data: [labelError],
}; };
} }
return { return {
success: true, success: true,
message: "Current labels order by upd_Date.", message: "Current labelratio.",
count: labelInfo.length,
data: labelInfo, data: labelInfo,
}; };
}; };

View File

@@ -1,4 +1,4 @@
import { sql } from "drizzle-orm"; import { eq, sql } from "drizzle-orm";
import { db } from "../../../../../database/dbclient.js"; import { db } from "../../../../../database/dbclient.js";
import { labelRatio } from "../../../../../database/schema/ratios.js"; import { labelRatio } from "../../../../../database/schema/ratios.js";
import { tryCatch } from "../../../../globalUtils/tryCatch.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.",
};
};

View File

@@ -22,6 +22,8 @@ import AutostartPrinterCycle from "./routes/printers/autoLabelerStart.js";
import AutostopPrinterCycle from "./routes/printers/autoLabelerStop.js"; import AutostopPrinterCycle from "./routes/printers/autoLabelerStop.js";
import { deleteLabels } from "../../globalUtils/dbCleanUp/labelCleanUp.js"; import { deleteLabels } from "../../globalUtils/dbCleanUp/labelCleanUp.js";
import bookInLabel from "./routes/labeling/bookIn.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(); const app = new OpenAPIHono();
@@ -40,6 +42,8 @@ const routes = [
getLabels, getLabels,
manualprint, manualprint,
bookInLabel, bookInLabel,
labelRatio,
resetRatio,
//dyco //dyco
dycoCon, dycoCon,
dycoClose, dycoClose,

View File

@@ -31,7 +31,6 @@ app.openapi(
return c.json({ return c.json({
success: labelData.success, success: labelData.success,
message: labelData.message, message: labelData.message,
count: labelData.count,
data: labelData.data, data: labelData.data,
}); });
} }

View File

@@ -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;