From 39c9ccdf01f5fcd71ac3c79db56dbc34707292da Mon Sep 17 00:00:00 2001 From: Seth Trowbridge Date: Wed, 2 Jul 2025 15:35:38 -0400 Subject: [PATCH] cleanup --- app.js | 210 ++++++++++++++++++++++++---------------------------- db.js | 26 ++++++- db_desk.csv | 2 +- types.d.ts | 5 +- 4 files changed, 123 insertions(+), 120 deletions(-) diff --git a/app.js b/app.js index 1037298..7cb5c7f 100644 --- a/app.js +++ b/app.js @@ -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, make:string[], ...role:string[]]} DeskJSON */ +/** @typedef {{name:string, mode:DeskMode, need:Record, 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, 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); \ No newline at end of file + let makeMin = Infinity; + let needMax = -Infinity; + + for(let i=0; i needMax) needMax = part.time; + } + + for(let i=0; i 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(); diff --git a/db.js b/db.js index 57b0227..91f6332 100644 --- a/db.js +++ b/db.js @@ -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(){}, + }; +} \ No newline at end of file diff --git a/db_desk.csv b/db_desk.csv index 9ac4d9e..d3a4735 100644 --- a/db_desk.csv +++ b/db_desk.csv @@ -1 +1 @@ -d1,Desk One,[{"p1":1, "p2":1}, ["p3"], "all", ["r1"]] +d1,Desk One,all,[{"p1":1, "p2":1},["p3"],"r1"] diff --git a/types.d.ts b/types.d.ts index 08f199f..ca3a51b 100644 --- a/types.d.ts +++ b/types.d.ts @@ -6,10 +6,11 @@ export type Builder = >(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, + find:(id:string)=>Loaded, load:()=>Promise, save:()=>Promise } \ No newline at end of file