mirror of
https://github.com/morten-olsen/bob.git
synced 2026-02-08 01:46:29 +01:00
feat: init
This commit is contained in:
49
packages/playground/src/utils/graph.ts
Normal file
49
packages/playground/src/utils/graph.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { CalulationResult } from 'bob-the-algorithm';
|
||||
|
||||
function msToHMS(ms: number) {
|
||||
// 1- Convert to seconds:
|
||||
let seconds = ms / 1000;
|
||||
// 2- Extract hours:
|
||||
const hours = seconds / 3600; // 3,600 seconds in 1 hour
|
||||
seconds = seconds % 3600; // seconds remaining after extracting hours
|
||||
// 3- Extract minutes:
|
||||
const minutes = seconds / 60; // 60 seconds in 1 minute
|
||||
// 4- Keep only seconds not extracted to minutes:
|
||||
seconds = seconds % 60;
|
||||
return hours + ':' + minutes + ':' + seconds;
|
||||
}
|
||||
const convertResult = (result: CalulationResult<any>) => {
|
||||
const nodes = result.nodes.map((node) => {
|
||||
let label = `root (${node.location})`;
|
||||
if (node.type === 'planable') {
|
||||
label = `task: ${node.planable!.toString()}`;
|
||||
} else if (node.type === 'travel') {
|
||||
label = `travel->${node.location}`;
|
||||
}
|
||||
return {
|
||||
id: node.id,
|
||||
label: `${msToHMS(node.time)}: ${label}`,
|
||||
data: {
|
||||
type: node.type,
|
||||
exploreId: node.exploreId,
|
||||
completed: node.completed,
|
||||
deadEnd: node.deadEnd,
|
||||
},
|
||||
};
|
||||
});
|
||||
const edges = result.nodes
|
||||
.filter((n) => n.parent)
|
||||
.map((node) => ({
|
||||
id: `${node.id}->${node.parent}`,
|
||||
source: node.parent!,
|
||||
target: node.id,
|
||||
label: node.score.toFixed(2),
|
||||
}));
|
||||
|
||||
return { nodes, edges, result };
|
||||
};
|
||||
|
||||
type ConvertedResult = ReturnType<typeof convertResult>;
|
||||
|
||||
export type { ConvertedResult };
|
||||
export { convertResult };
|
||||
Reference in New Issue
Block a user