feat(eom): added in historical inv data split accordingly

This commit is contained in:
2025-05-28 17:01:22 -05:00
parent 25cfee58d0
commit 96deca15f0
16 changed files with 6385 additions and 36 deletions

View File

@@ -0,0 +1,91 @@
import { db } from "../../../../../database/dbclient.js";
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
import { createLog } from "../../../logger/logger.js";
import { query } from "../../../sqlServer/prodSqlServer.js";
import { totalInvNoRn } from "../../../sqlServer/querys/dataMart/totalINV.js";
import { invHistoricalData } from "../../../../../database/schema/historicalINV.js";
import { format } from "date-fns-tz";
import { settings } from "../../../../../database/schema/settings.js";
import { sql } from "drizzle-orm";
import { createLogisticsJob } from "../../utils/logisticsIntervals.js";
export const runHistoricalData = async () => {
/**
* Runs a query at shift change on first shift each day this will be the closest date to the true historical data for blocked, consignment
*/
const { data: set, error: setError } = await tryCatch(
db.select().from(settings)
);
if (setError) {
createLog(
"error",
"lst",
"eom",
"There was an error getting eom historical inv data."
);
return;
}
const timeZone = set.filter((n: any) => n.name === "timezone");
createLogisticsJob("histInv", `0 6 * * *`, timeZone[0].value, async () => {
// remove the lotnumber from the query
const updatedQuery = totalInvNoRn.replaceAll(
",IdProdPlanung",
"--,IdProdPlanung"
);
const { data: inv, error: invError } = await tryCatch(
query(updatedQuery, "EOM historical inv")
);
if (invError) {
createLog(
"error",
"lst",
"eom",
"There was an error getting eom historical inv data."
);
return;
}
/**
* add the inv into the hist table
*/
const setting: any = set;
for (let i = 0; i < inv?.data.length; i++) {
const current = inv?.data[i];
const { data, error } = await tryCatch(
db.insert(invHistoricalData).values({
histDate: format(new Date(), "MM-dd-yyyy"),
plantToken: setting.filter(
(n: any) => n.name === "plantToken"
)[0].value,
article: current.av,
articleDescription: current.Alias,
total_QTY: current.Total_PalletQTY,
avaliable_QTY: current.Avaliable_PalletQTY,
coa_QTY: current.COA_QTY,
held_QTY: current.Held_QTY,
consignment: current.Consigment,
//location: integer("location"),
upd_user: "LST",
upd_date: sql`NOW()`,
})
);
if (error) {
createLog(
"error",
"lst",
"eom",
`Error addeding historical data, ${error}`
);
}
}
});
};

View File

@@ -16,6 +16,7 @@ import standardTemplate from "./route/dm/getStandardTemplate.js";
import standardForcasttemplate from "./route/dm/getStandardForecastTemplate.js";
import postForecast from "./route/dm/forecastIn.js";
import outbound from "./route/getOutbound.js";
import { runHistoricalData } from "./controller/eom/historicalInv.js";
const app = new OpenAPIHono();
@@ -49,7 +50,8 @@ const appRoutes = routes.forEach((route) => {
setTimeout(() => {
migrateAdjustments();
}, 120 * 1000);
runHistoricalData();
}, 120 * 1000); // starts 2 min after a server restart or crash.
/**
* Start the cycle count check

View File

@@ -0,0 +1,61 @@
import { Cron } from "croner";
import type { JobInfo } from "../../../types/JobInfo.js";
import { createLog } from "../../logger/logger.js";
export let runningLogisticsCrons: Record<string, Cron> = {};
export const createLogisticsJob = (
id: string, // this is just the name of the job running
schedule: string, // `*/30 * * * *`; // default to be every 30 min
timezone: string,
task: () => Promise<void>
) => {
// Destroy existing job if it exists
if (runningLogisticsCrons[id]) {
runningLogisticsCrons[id].stop(); // Croner uses .stop() instead of .destroy()
}
// Create new job with Croner
runningLogisticsCrons[id] = new Cron(
schedule,
{
timezone: timezone,
catch: true, // Prevents unhandled rejections
},
task
);
createLog(
"info",
"lst",
"logistics",
`Cron setup for ${id}, trigger time: ${schedule}`
);
// Optional: Add error handling (Croner emits 'error' events)
// runningNotifications[id].on("error", (err) => {
// console.error(`Job ${id} failed:`, err);
// });
};
export const getAllLogisticsJobs = (): JobInfo[] => {
return Object.entries(runningLogisticsCrons).map(([id, job]) => ({
id,
schedule: job.getPattern() || "invalid",
nextRun: job.nextRun() || null,
lastRun: job.previousRun() || null,
isRunning: job ? !job.isStopped() : false,
}));
};
// const removeNotification = (id: any) => {
// if (runningLogisticsCrons[id]) {
// runningLogisticsCrons[id].stop();
// delete runningLogisticsCrons[id];
// }
// };
export const stopAllLogisticsJobs = () => {
Object.values(runningLogisticsCrons).forEach((job: any) => job.stop());
runningLogisticsCrons = {}; // Clear the object
};

View File

@@ -1,11 +1,12 @@
import { db } from "../../../../database/dbclient.js";
import { notifications } from "../../../../database/schema/notifications.js";
import { tryCatch } from "../../../globalUtils/tryCatch.js";
import type { JobInfo } from "../../../types/JobInfo.js";
import { createLog } from "../../logger/logger.js";
import { Cron } from "croner";
// Store active timeouts by notification ID
export let runningNotifications: Record<string, Cron> = {};
export let runningCrons: Record<string, Cron> = {};
export const startNotificationMonitor = async () => {
// if restarted or crashed we need to make sure the running notifications is cleared
@@ -29,7 +30,7 @@ export const startNotificationMonitor = async () => {
for (const note of notes) {
//if we get deactivated remove it.
if (runningNotifications[note.name] && !note.active) {
if (runningCrons[note.name] && !note.active) {
createLog(
"info",
"notify",
@@ -44,12 +45,12 @@ export const startNotificationMonitor = async () => {
if (
!note.active ||
// note.emails === "" ||
runningNotifications[note.name]
runningCrons[note.name]
) {
continue;
}
if (!runningNotifications[note.name] && note.active) {
if (!runningCrons[note.name] && note.active) {
createLog(
"info",
"notify",
@@ -105,12 +106,12 @@ export const startNotificationMonitor = async () => {
const createJob = (id: string, schedule: string, task: () => Promise<void>) => {
// Destroy existing job if it exists
if (runningNotifications[id]) {
runningNotifications[id].stop(); // Croner uses .stop() instead of .destroy()
if (runningCrons[id]) {
runningCrons[id].stop(); // Croner uses .stop() instead of .destroy()
}
// Create new job with Croner
runningNotifications[id] = new Cron(
runningCrons[id] = new Cron(
schedule,
{
timezone: "America/Chicago",
@@ -125,15 +126,8 @@ const createJob = (id: string, schedule: string, task: () => Promise<void>) => {
// });
};
interface JobInfo {
id: string;
schedule: string;
nextRun: Date | null;
isRunning: boolean;
}
export const getAllJobs = (): JobInfo[] => {
return Object.entries(runningNotifications).map(([id, job]) => ({
return Object.entries(runningCrons).map(([id, job]) => ({
id,
schedule: job.getPattern() || "invalid",
nextRun: job.nextRun() || null,
@@ -143,15 +137,15 @@ export const getAllJobs = (): JobInfo[] => {
};
const removeNotification = (id: any) => {
if (runningNotifications[id]) {
runningNotifications[id].stop();
delete runningNotifications[id];
if (runningCrons[id]) {
runningCrons[id].stop();
delete runningCrons[id];
}
};
export const stopAllJobs = () => {
Object.values(runningNotifications).forEach((job: any) => job.stop());
runningNotifications = {}; // Clear the object
Object.values(runningCrons).forEach((job: any) => job.stop());
runningCrons = {}; // Clear the object
};
/*

View File

@@ -21,6 +21,13 @@ const newSettings = [
description: "What are we listening on",
moduleName: "server",
},
{
name: "timezone",
value: "America/Chicago",
description:
"What time zone is the server in this is used for cronjobs and some other time stuff",
moduleName: "server",
},
{
name: "dbUser",
value: "alplaprod",

View File

@@ -11,9 +11,10 @@ sum(VerfuegbareMengeSum) as Avaliable_PalletQTY,
sum(case when c.Description LIKE '%COA%' then GesperrteMengeVPKSum else 0 end) as COA_Pallets,
sum(case when c.Description LIKE '%COA%' then GesperrteMengeSum else 0 end) as COA_QTY,
sum(case when c.Description NOT LIKE '%COA%' or x.IdMainDefect = -1 then GesperrteMengeVPKSum else 0 end) as Held_Pallets,
sum(case when c.Description NOT LIKE '%COA%' or x.IdMainDefect = -1 then GesperrteMengeSum else 0 end) as Held_QTY,
IdProdPlanung as Lot,
IdAdressen,
sum(case when c.Description NOT LIKE '%COA%' or x.IdMainDefect = -1 then GesperrteMengeSum else 0 end) as Held_QTY
,sum(case when x.WarenLagerLagerTyp = 8 then VerfuegbareMengeSum else 0 end) as Consigment
,IdProdPlanung as Lot
--,IdAdressen,
x.AdressBez
--,*
from [AlplaPROD_test1].dbo.[V_LagerPositionenBarcodes] (nolock) x
@@ -33,8 +34,11 @@ The data below will be controlled by the user in excell by default everything wi
*/
where /*IdArtikelTyp = 1 and */x.IdWarenlager not in (6, 1)
group by x.idartikelVarianten, ArtikelVariantenAlias, IdProdPlanung, c.Description, IdAdressen,
x.AdressBez --, x.Lfdnr
group by x.idartikelVarianten, ArtikelVariantenAlias, c.Description
--,IdAdressen
,x.AdressBez
,IdProdPlanung
--, x.Lfdnr
order by x.IdArtikelVarianten
`;
@@ -50,9 +54,9 @@ sum(VerfuegbareMengeSum) as Avaliable_PalletQTY,
sum(case when c.Description LIKE '%COA%' then GesperrteMengeVPKSum else 0 end) as COA_Pallets,
sum(case when c.Description LIKE '%COA%' then GesperrteMengeSum else 0 end) as COA_QTY,
sum(case when c.Description NOT LIKE '%COA%' or x.IdMainDefect = -1 then GesperrteMengeVPKSum else 0 end) as Held_Pallets,
sum(case when c.Description NOT LIKE '%COA%' or x.IdMainDefect = -1 then GesperrteMengeSum else 0 end) as Held_QTY,
IdProdPlanung as Lot,
IdAdressen,
sum(case when c.Description NOT LIKE '%COA%' or x.IdMainDefect = -1 then GesperrteMengeSum else 0 end) as Held_QTY
,IdProdPlanung as Lot
,IdAdressen,
x.AdressBez
--,*
from [AlplaPROD_test1].dbo.[V_LagerPositionenBarcodes] (nolock) x
@@ -72,8 +76,9 @@ The data below will be controlled by the user in excell by default everything wi
*/
where IdArtikelTyp = 1 and x.IdWarenlager not in (6, 1)
group by x.idartikelVarianten, ArtikelVariantenAlias, IdProdPlanung, c.Description, IdAdressen,
x.AdressBez , x.Lfdnr
group by x.idartikelVarianten, ArtikelVariantenAlias, c.Description, IdAdressen,
x.AdressBez , x.Lfdnr,
IdProdPlanung -- this will be flagged as being removed when we do historical.
order by x.IdArtikelVarianten
`;

6
server/types/JobInfo.ts Normal file
View File

@@ -0,0 +1,6 @@
export interface JobInfo {
id: string;
schedule: string;
nextRun: Date | null;
isRunning: boolean;
}