work-graph-simple/app.js
2025-06-26 10:11:16 -04:00

92 lines
2.2 KiB
JavaScript

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