This commit is contained in:
Seth Trowbridge 2025-06-26 10:11:16 -04:00
commit 603eaaa5bb
5 changed files with 161 additions and 0 deletions

92
app.js Normal file
View File

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

56
db.js Normal file
View File

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

1
db_user.csv Normal file
View File

@ -0,0 +1 @@
seth trowbridge, seth111@gmail.com
1 seth trowbridge seth111@gmail.com

5
db_work.csv Normal file
View File

@ -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"}
Can't render this file because it contains an unexpected character in line 1 and column 10.

7
db_work.json Normal file
View File

@ -0,0 +1,7 @@
{
"p1":{
"u1":{
"1":{"key1": "value1", "key2": "value2"}
}
}
}