Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(stats): collect drill stats #373

Open
wants to merge 20 commits into
base: v5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/stats/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@tracespace/stats",
"publishConfig": {
"access": "public"
},
"version": "5.0.0-next.0",
"description": "Collect drill stats",
"source": "./src/index.ts",
mcous marked this conversation as resolved.
Show resolved Hide resolved
"sideEffects": false,
"repository": {
"type": "git",
"url": "git+https://github.com/tracespace/tracespace.git",
"directory": "packages/stats"
},
"keywords": [
"gerber",
"drill",
"stats"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/tracespace/tracespace/issues"
},
"homepage": "https://github.com/tracespace/tracespace#readme",
"dependencies": {
"@tracespace/parser": "^5.0.0-next.0"
mcous marked this conversation as resolved.
Show resolved Hide resolved
}
}
109 changes: 109 additions & 0 deletions packages/stats/src/collector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import * as Parser from '@tracespace/parser'
import {DrillUsage, DrillStats} from './types'

/**
* Method to collect drill stats of parsed drill files.
*
* @example
* ```ts
* import {createParser} from '@tracespace/parser'
* import {collectDrillStats} from '@tracespace/stats'
*
* const parser = createParser()
*
* parser.feed(...)
*
* const tree = parser.results()
* const stats = collectDrillStats([tree])
* ```
*
* @category Stats
*/
export function collectDrillStats(trees: Parser.Root[]): DrillStats {
let toolsMap = new Map<string, Parser.ToolShape>()
let drillsPerTool = new Map<string, number>()
let routesPerTool = new Map<string, number>()
KN4CK3R marked this conversation as resolved.
Show resolved Hide resolved
let minDrillSize: number = null
let maxDrillSize: number = null

for (const tree of trees) {
if (tree.filetype !== Parser.DRILL) {
continue
}

let currentTool = ''
KN4CK3R marked this conversation as resolved.
Show resolved Hide resolved
let currentMode: Parser.InterpolateModeType = null
for (const node of tree.children) {
if (node.type === Parser.TOOL_DEFINITION) {
KN4CK3R marked this conversation as resolved.
Show resolved Hide resolved
currentTool = node.code

toolsMap.set(currentTool, node.shape)

if (node.shape.type === Parser.CIRCLE) {
const diameter = node.shape.diameter
if (minDrillSize === null || diameter < minDrillSize) {
minDrillSize = diameter
}
if (maxDrillSize === null || diameter > maxDrillSize) {
maxDrillSize = diameter
}
}
} else if (node.type === Parser.TOOL_CHANGE) {
currentTool = node.code
} else if (node.type === Parser.INTERPOLATE_MODE) {
KN4CK3R marked this conversation as resolved.
Show resolved Hide resolved
currentMode = node.mode
} else if (node.type === Parser.GRAPHIC) {
if (node.graphic !== null) {
mcous marked this conversation as resolved.
Show resolved Hide resolved
continue
}
if (currentTool === '' || currentMode === null) {
continue
}

switch (currentMode) {
case Parser.DRILL:
const drillCount = drillsPerTool.get(currentTool) ?? 0
drillsPerTool.set(currentTool, drillCount + 1)
break
case Parser.LINE:
case Parser.CW_ARC:
case Parser.CCW_ARC:
const routeCount = routesPerTool.get(currentTool) ?? 0
routesPerTool.set(currentTool, routeCount + 1)
break
}
}
}
}

let totalDrills = 0
KN4CK3R marked this conversation as resolved.
Show resolved Hide resolved
const drillHits: DrillUsage[] = []
drillsPerTool.forEach((count: number, key: string) => {
totalDrills += count
drillHits.push({
toolShape: toolsMap.get(key),
count: count,
})
})

let totalRoutes = 0
const drillRoutes: DrillUsage[] = []
routesPerTool.forEach((count: number, key: string) => {
totalRoutes += count
drillRoutes.push({
toolShape: toolsMap.get(key),
count: count,
})
})

const stats: DrillStats = {
drillHits: drillHits,
drillRoutes: drillRoutes,
totalDrills: totalDrills,
totalRoutes: totalRoutes,
minDrillSize: minDrillSize ?? 0,
maxDrillSize: maxDrillSize ?? 0,
}

return stats
}
2 changes: 2 additions & 0 deletions packages/stats/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './collector'
export * from './types'
17 changes: 17 additions & 0 deletions packages/stats/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// common types

import {ToolShape} from '@tracespace/parser'

export interface DrillUsage {
toolShape: ToolShape,
mcous marked this conversation as resolved.
Show resolved Hide resolved
count: number,
}

export interface DrillStats {
drillHits: DrillUsage[]
drillRoutes: DrillUsage[]
totalDrills: number
totalRoutes: number
minDrillSize: number
maxDrillSize: number
}
13 changes: 13 additions & 0 deletions packages/stats/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "@tracespace/config/typescript.json",
mcous marked this conversation as resolved.
Show resolved Hide resolved
"references": [
{ "path": "../parser" },
],
"compilerOptions": {
"composite": true,
"emitDeclarationOnly": true,
"rootDir": "src",
"outDir": "lib"
},
"include": ["src"]
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{"path": "./packages/plotter"},
{"path": "./packages/xml-id"},
{"path": "./packages/whats-that-gerber"},
{"path": "./packages/stats"},
{"path": "./www"}
]
}