From 603eaaa5bbaa1f5ad08aadce02789f62d3e88cc9 Mon Sep 17 00:00:00 2001 From: Seth Trowbridge Date: Thu, 26 Jun 2025 10:11:16 -0400 Subject: [PATCH] init --- app.js | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++ db.js | 56 ++++++++++++++++++++++++++++++++ db_user.csv | 1 + db_work.csv | 5 +++ db_work.json | 7 ++++ 5 files changed, 161 insertions(+) create mode 100644 app.js create mode 100644 db.js create mode 100644 db_user.csv create mode 100644 db_work.csv create mode 100644 db_work.json diff --git a/app.js b/app.js new file mode 100644 index 0000000..a9e77cb --- /dev/null +++ b/app.js @@ -0,0 +1,92 @@ +import DB from "./db.js" + +// build parts +const Part = {}; +function CreatePart(id, name) +{ + const part = {name, _:{id, time:0, work:[], need:[], make:[]}}; + Part[id] = part; + return part; +} +CreatePart('p1', 'Part 1'); +CreatePart('p2', 'Part 2'); +CreatePart('p3', 'Part 3'); + + +// then build work +const Work = []; +function CreateWork(partId, user, time, data) +{ + const part = Part[partId]; + const work = {user: user, time, data} + part._.work.push(work); + if(time > part._.time) + { + part._.time = time; + } + Work.push(work); + return work; +} +async function LoadWork() +{ + return await DB(import.meta.resolve('./db_work.csv'), 3, CreateWork); +} +await LoadWork(); +// CreateWork('p1', 'u1', 1, {key: 'value1'}); +// CreateWork('p1', 'u1', 2, {key: 'value2'}); +// CreateWork('p2', 'u2', 3, {key: 'value3'}); +// CreateWork('p2', 'u2', 4, {key: 'value4'}); +// CreateWork('p3', 'u2', 3, {key: 'valueDONE'}); + +// then build desks +const Desk = {}; +/** @type {(need:string[], make:string[])=>[dirtyNeed:string[], dirtyMake:string[], needMax:number, needMin:number]} */ +const Range =(need, make)=> +{ + const dirtyNeed = []; + const dirtyMake = []; + + let makeMin = Infinity; + let needMax = -Infinity; + need.forEach(partId => { + const part = Part[partId]; + if(part._.time > needMax) needMax = part._.time; + }); + + make.forEach(partId => { + const part = Part[partId]; + if(part._.time < makeMin) makeMin = part._.time; + if(part._.time < needMax) + { + dirtyMake.push(partId); + } + }); + + need.forEach(partId => { + const part = Part[partId]; + if(part._.time > makeMin) + { + dirtyNeed.push(partId); + } + }); + + return [dirtyNeed, dirtyMake, needMax, makeMin]; +}; +/** @type {(id:string, name:string, need:string[], make:string[], role:string[])=>void} */ +function CreateDesk(id, name, need, make, role) +{ + const desk = {name, need, make, role, _:{id}}; + Desk[id] = desk; + need.forEach(partId => Part[partId]._.need.push(desk)); + make.forEach(partId => Part[partId]._.make.push(desk)); + + const check = Range(need, make); + console.log(check); + + return desk; +} +CreateDesk('d1', 'Desk 1', ['p1', 'p2'], ['p3'], ['role1']); + + + +console.log(Desk); \ No newline at end of file diff --git a/db.js b/db.js new file mode 100644 index 0000000..cb0309d --- /dev/null +++ b/db.js @@ -0,0 +1,56 @@ +function createCSVScanner(onRow, nonJsonCols) { + let buffer = ''; + + return function scanChunk(chunk) { + buffer += chunk; + const lines = buffer.split('\n'); + buffer = lines.pop(); // Save incomplete line + + for (const line of lines) { + const fields = []; + let start = 0; + let commaCount = 0; + + for (let i = 0; i < line.length; i++) { + if (line[i] === ',' && commaCount < nonJsonCols) { + fields.push(line.slice(start, i)); + start = i + 1; + commaCount++; + } + } + + // Push the remaining part as JSON string + const jsonStr = line.slice(start); + let jsonData = null; + try { + jsonData = JSON.parse(jsonStr); + } catch (_e) { + console.error('Invalid JSON:', jsonStr); + } + + onRow(...fields, jsonData); + + } + }; +} + +export default async function(url, nonJsonCols, onRow) { + const response = await fetch(url); + + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + + const reader = response.body.getReader(); + const decoder = new TextDecoder("utf-8"); + + const scanner = createCSVScanner(onRow, nonJsonCols); + + while (true) + { + const { done, value } = await reader.read(); + if (done) break; + const chunk = decoder.decode(value, { stream: true }); + scanner(chunk); + } +} diff --git a/db_user.csv b/db_user.csv new file mode 100644 index 0000000..07168a2 --- /dev/null +++ b/db_user.csv @@ -0,0 +1 @@ +seth trowbridge, seth111@gmail.com \ No newline at end of file diff --git a/db_work.csv b/db_work.csv new file mode 100644 index 0000000..a8e4be1 --- /dev/null +++ b/db_work.csv @@ -0,0 +1,5 @@ +p1,u1,1,{"data":"value1"} +p1,u1,2,{"data":"value2"} +p2,u2,3,{"key1":"value3"} +p2,u2,4,{"key1":"value4"} +p3,u2,3,{"key1":"value5"} diff --git a/db_work.json b/db_work.json new file mode 100644 index 0000000..91f912f --- /dev/null +++ b/db_work.json @@ -0,0 +1,7 @@ +{ + "p1":{ + "u1":{ + "1":{"key1": "value1", "key2": "value2"} + } + } +} \ No newline at end of file