This commit is contained in:
Seth Trowbridge 2025-07-02 15:35:38 -04:00
parent 8fde0f73c5
commit 39c9ccdf01
4 changed files with 123 additions and 120 deletions

210
app.js
View File

@ -1,123 +1,105 @@
/** @import DBTypes from "./types.d.ts" */
import DB from "./db.js";
/** @type {DBTypes.Builder} */
function DBBuilder(params)
{
return {
list:{},
load(){
const l = this.list;
const loader =(id, data)=>l[id] = data;
return DB(import.meta.resolve(params.path), params.cols, (...args)=>{
params.load(loader, ...args);
});
},
save(){},
};
}
const parts = DBBuilder({
/** @typedef {{name:string, time:number, work:WorkData[], need:string[], make:string[]}} PartData */
const Part = DB({
path:"./db_part.csv",
cols:2,
load:(commit, id, name)=>commit(id, {name}),
save:(commit, params)=> commit("a", params.name)
load:(_i, id, name)=>[id, /**@type{PartData}*/({
name,
time:0,
work:[],
need:[],
make:[]
})],
});
await parts.load();
console.log(parts.list);
/**@typedef {{part:string, user:string, time:number, data:string}} WorkData*/
const Work = DB({
path:"./db_work.csv",
cols:4,
load(_i, part, user, time, data)
{
const parsedTime = parseInt(time);
/** @type {WorkData} */
const workObj = {part, user, time:parsedTime, data};
const partObj = Part.list[part];
if(parsedTime > partObj.time)
{
partObj.time = parsedTime;
partObj.work.unshift(workObj);
}
else
{
partObj.work.push(workObj);
}
return ["", workObj];
}
});
/** @typedef {"all"|"one"} DeskMode*/
/** @typedef {[need:Record<string, number>, make:string[], ...role:string[]]} DeskJSON */
/** @typedef {{name:string, mode:DeskMode, need:Record<string, number>, make:string[], role:string[]}} DeskData */
/** @type {(desk:DeskData)=>[dirtyNeed:string[], dirtyMake:string[]]} */
const Scan =(desk)=>
{
const need = Object.keys(desk.need);
const make = desk.make;
const dirtyNeed = [];
const dirtyMake = [];
// // build parts
// const Part = {};
// function CreatePart(id, name)
// {
// const part = {name, _:{id, time:0, work:[], need:[], make:[]}};
// Part[id] = part;
// return part;
// }
// await DB(import.meta.resolve('./db_part.csv'), 2, CreatePart);
//
//
// // 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;
// }
// await DB(import.meta.resolve('./db_work.csv'), 3, CreateWork);
//
//
// // 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];
// };
//
// /** @typedef {[need:Record<string, number]>, make:string[], mode:"one"|"all", role:string[]} StoredDesk */
//
// /** @type {(id:string, name:string, data:StoredDesk)=>void} */
// function CreateDesk(id, name, data, role, need, time, make, mode="one")
// {
// const need = Object.keys(data[0])
// const time = Object.values(data[0]);
// const desk = {name, need, time, 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;
// }
//
// await DB(import.meta.resolve('./db_work.csv'), 3, (id, name, /**@type{StoredDesk}*/data)=>{
//
// const need = Object.keys(data[0])
// CreateDesk(id, name, need, data[1], );
// });
//
//
//
// console.log(Part);
// console.log(Desk);
let makeMin = Infinity;
let needMax = -Infinity;
for(let i=0; i<need.length; i++)
{
const id = need[i];
const part = Part.find(id);
if(part.time > needMax) needMax = part.time;
}
for(let i=0; i<make.length; i++)
{
const id = make[i];
const part = Part.find(make[i]);
if(part.time < makeMin) makeMin = part.time;
if(part.time < needMax)
{
dirtyMake.push(id);
}
}
for(let i=0; i<need.length; i++)
{
const id = need[i];
const part = Part.find(id);
if(part.time > makeMin)
{
dirtyNeed.push(id);
}
}
return [dirtyNeed, dirtyMake];
};
const Desk = DB({
path:"./db_desk.csv",
cols:4,
load(_i, id, name, /** @type {DeskMode}*/mode, data)
{
/**@type {DeskJSON} */
const [need, make, ...role] = JSON.parse(data);
const deskObj = { name, mode, need, make, role };
make.forEach(partId=>Part.find(partId).make.push(id));
Object.keys(need).forEach(partId=>Part.find(partId).need.push(id));
const check = Scan(deskObj);
console.log(check);
return [id, deskObj];
}
});
await Part.load();
await Work.load();
await Desk.load();

26
db.js
View File

@ -12,7 +12,7 @@ function createCSVScanner(onRow, cols) {
let start = 0;
let col = 0;
for (let i = 0; i < line.length; i++) {
if (line[i] === ',' && col < cols) {
if (line[i] === ',' && col < cols-1) {
fields.push(line.slice(start, i));
start = i + 1;
col++;
@ -25,7 +25,7 @@ function createCSVScanner(onRow, cols) {
};
}
export default async function(url, nonJsonCols, onRow) {
async function DB(url, cols, onRow) {
const response = await fetch(url);
if (!response.ok) {
@ -35,7 +35,7 @@ export default async function(url, nonJsonCols, onRow) {
const reader = response.body.getReader();
const decoder = new TextDecoder("utf-8");
const scanner = createCSVScanner(onRow, nonJsonCols);
const scanner = createCSVScanner(onRow, cols);
while (true)
{
@ -45,3 +45,23 @@ export default async function(url, nonJsonCols, onRow) {
scanner(chunk);
}
}
/** @import DBTypes from "./types.d.ts" */
/** @type {DBTypes.Builder} */
export default function(params)
{
return {
list:{},
load(){
let index = 0;
this.list = {};
return DB(import.meta.resolve(params.path), params.cols, (...args)=>{
const [id, data] = params.load(index, ...args);
id && (this.list[id] = data);
index++;
});
},
find(id){return this.list[id];},
save(){},
};
}

View File

@ -1 +1 @@
d1,Desk One,[{"p1":1, "p2":1}, ["p3"], "all", ["r1"]]
d1,Desk One,all,[{"p1":1, "p2":1},["p3"],"r1"]

Can't render this file because it contains an unexpected character in line 1 and column 15.

5
types.d.ts vendored
View File

@ -6,10 +6,11 @@ export type Builder = <Count extends number, Loaded, Sized extends Row<Count>>(p
{
path:string,
cols:Count,
load:(commitLoad:(id:string, data:Loaded)=>Sized, ...args:Sized)=>[id:string, data:Loaded],
save:(commitSave:(...args:Sized)=>Sized, internal:Loaded) => Sized
load:(index:number, ...args:Sized)=>[id:string, data:Loaded],
//save:(commitSave:(...args:Sized)=>Sized, id:string, data:Loaded) => Sized
}) => {
list:Record<string, Loaded>,
find:(id:string)=>Loaded,
load:()=>Promise<void>,
save:()=>Promise<void>
}