diff --git a/.vscode/settings.json b/.vscode/settings.json index fed39e5..aa0e936 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -52,6 +52,7 @@ "alpla", "alplamart", "alplaprod", + "Datamart", "intiallally", "ppoo", "prodlabels" diff --git a/backend/src/datamart/datamart.controller.ts b/backend/src/datamart/datamart.controller.ts new file mode 100644 index 0000000..df5a1d8 --- /dev/null +++ b/backend/src/datamart/datamart.controller.ts @@ -0,0 +1,51 @@ +/** + * each endpoint will be something like + * /api/datamart/{name}?{options} + * + * when getting the current queries we will need to map through the available queries we currently have and send back. + * example + *{ + * "name": "getopenorders", + * "endpoint": "/api/datamart/getopenorders", + * "description": "Returns open orders based on day count sent over, sDay 15 days in the past eDay 5 days in the future, can be left empty for this default days", + * "criteria": "sDay,eDay" + * }, + * + * when a criteria is password over we will handle it by counting how many were passed up to 3 then deal with each one respectively + */ + +import { returnFunc } from "../utils/returnHelper.utils.js"; + +type Data = { + name: string; + criteria: string; +}; + +export const runDatamartQuery = async (data: Data) => { + // search the query db for the query by name + const dummyquery = { + name: "something", + query: "select * from tableA where start=[start] and end=[end]", + }; + + // create the query with no changed just to have it here + let datamartQuery = dummyquery.query; + // split the criteria by "," then and then update the query + if (data.criteria) { + const params = new URLSearchParams(data.criteria); + + for (const [key, value] of params.entries()) { + datamartQuery = datamartQuery.replaceAll(`[${key}]`, value); + } + } + + return returnFunc({ + success: true, + level: "info", + module: "datamart", + subModule: "query", + message: `Data for: ${data.name}`, + data: [{ data: datamartQuery }], + notify: false, + }); +}; diff --git a/backend/src/datamart/datamart.routes.ts b/backend/src/datamart/datamart.routes.ts new file mode 100644 index 0000000..b51903b --- /dev/null +++ b/backend/src/datamart/datamart.routes.ts @@ -0,0 +1,13 @@ +import type { Express } from "express"; +import runQuery from "./getDatamart.route.js"; +export const setupDatamartRoutes = (baseUrl: string, app: Express) => { + //setup all the routes + + app.use(`${baseUrl}/api/datamart`, runQuery); + + app.all("*foo", (_, res) => { + res.status(400).json({ + message: "You have encountered a datamart route that dose not exist", + }); + }); +}; diff --git a/backend/src/datamart/datamartSync.controller.ts b/backend/src/datamart/datamartSync.controller.ts new file mode 100644 index 0000000..b634478 --- /dev/null +++ b/backend/src/datamart/datamartSync.controller.ts @@ -0,0 +1,22 @@ +/** + * If we are running in client mode we want to periodically check the SERVER_NAME for new/updates queries + * this will be on a cronner job, we will check 2 times a day for new data, we will also have a route we can trigger to check this manually incase we have + * queries we make for one plant but will eventually go to all plants. + * in client mode we will not be able to add, update, or delete + * + * if we are running on server mode we will provide all queries. + * allow for new queries to be added + * allow for queries to be updated by id + * table will be + * id + * name + * description + * query + * version + * active + * options (string ie start,end) + * add_date + * add_user + * upd_date + * upd_user + */ diff --git a/backend/src/datamart/getDatamart.route.ts b/backend/src/datamart/getDatamart.route.ts new file mode 100644 index 0000000..3a23a89 --- /dev/null +++ b/backend/src/datamart/getDatamart.route.ts @@ -0,0 +1,25 @@ +import { Router } from "express"; +import { apiReturn } from "../utils/returnHelper.utils.js"; +import { runDatamartQuery } from "./datamart.controller.js"; + +const r = Router(); + +r.get("/:name", async (req, res) => { + const { name } = req.params; + const criteria = new URLSearchParams( + req.query as Record, + ).toString(); + + const dataRan = await runDatamartQuery({ name, criteria }); + apiReturn(res, { + success: dataRan.success, + level: "info", + module: "datamart", + subModule: "query", + message: dataRan.message, + data: dataRan.data, + status: 200, + }); +}); + +export default r; diff --git a/backend/src/db/dbBackup.contoller.ts b/backend/src/db/dbBackup.contoller.ts new file mode 100644 index 0000000..c09888a --- /dev/null +++ b/backend/src/db/dbBackup.contoller.ts @@ -0,0 +1,6 @@ +/** + * while in client mode we will be connected directly to the postgres and do a nightly backup. + * we will only keep tables relevant, like silo data, inv history, manualPrinting, notifications, printerData,prodlabels, quality request, rfid tags, roles, serverData,...etc + * keeping only the last 7 backups + * + */ diff --git a/backend/src/routeHandler.routes.ts b/backend/src/routeHandler.routes.ts index e7d8f80..a8fb6de 100644 --- a/backend/src/routeHandler.routes.ts +++ b/backend/src/routeHandler.routes.ts @@ -2,6 +2,7 @@ import type { Express } from "express"; // import the routes and route setups import { setupApiDocsRoutes } from "./configs/scaler.config.js"; +import { setupDatamartRoutes } from "./datamart/datamart.routes.js"; import { setupProdSqlRoutes } from "./prodSql/prodSql.routes.js"; import stats from "./system/stats.route.js"; @@ -9,6 +10,7 @@ export const setupRoutes = (baseUrl: string, app: Express) => { //setup all the routes setupApiDocsRoutes(baseUrl, app); setupProdSqlRoutes(baseUrl, app); + setupDatamartRoutes(baseUrl, app); app.use(`${baseUrl}/api/stats`, stats); }; diff --git a/backend/src/utils/returnHelper.utils.ts b/backend/src/utils/returnHelper.utils.ts index 22b4973..bc49833 100644 --- a/backend/src/utils/returnHelper.utils.ts +++ b/backend/src/utils/returnHelper.utils.ts @@ -3,8 +3,8 @@ import { createLogger } from "../logger/logger.controller.js"; interface Data { success: boolean; - module: "system" | "ocp" | "routes"; - subModule: "db" | "labeling" | "printer" | "prodSql"; + module: "system" | "ocp" | "routes" | "datamart"; + subModule: "db" | "labeling" | "printer" | "prodSql" | "query"; level: "info" | "error" | "debug" | "fatal"; message: string; data?: unknown[];