-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodes.ts
177 lines (172 loc) · 4.38 KB
/
nodes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { Copc, Hierarchy, Getter } from 'copc'
import { pointDataSuite, pointDataSuiteSource } from '../suites/index.js'
import { Check, AllNodesChecked } from '../types/index.js'
import {
loadAllHierarchyPages,
NodeVsBrowser,
runTasks,
workerParams,
WorkerSettings,
} from '../utils/index.js'
import sample from 'lodash.samplesize'
export type nodeParserParams = {
get: Getter
copc: Copc
file: string | File
deep?: boolean
worker?: WorkerSettings
workerCount?: number
queueLimit?: number
sampleSize?: number
showProgress?: boolean
}
export const nodeParser: Check.Parser<
nodeParserParams,
pointDataSuiteSource //{ data: AllNodesChecked; nonZero: string[]; allNodes?: Hierarchy.Node.Map }
> = async ({
get,
copc,
file,
deep = false,
worker,
workerCount,
queueLimit,
sampleSize,
showProgress = false,
}: nodeParserParams) => {
const nodes = await loadAllHierarchyPages(get, copc)
return {
source: await createPointDataSuiteSource(
{ nodes, file, copc },
{
deep,
worker,
workerCount,
queueLimit,
sampleSize,
withBar: showProgress,
},
),
suite: pointDataSuite,
}
}
// ========== A MESSY NEST OF UTILITIES ============
const { lazPerf } = NodeVsBrowser
/**
*
* @param nodes
* @returns Array containing `D-X-Y-Z` key of each node that contains points
*/
export const nonZeroNodes = (nodes: Hierarchy.Node.Map): string[] =>
Object.entries(nodes).reduce<string[]>((acc, [key, data]) => {
if (data && data.pointCount !== 0) acc.push(key)
return acc
}, [])
export type readPDRsParams = {
nodes: Hierarchy.Node.Map
file: string | File
copc: Copc
deep: boolean
worker?: WorkerSettings
workerCount?: number
queueLimit?: number
}
/**
*
* @param params readPDRsParams object
* @param withBar Turns `cli-progress` bar On (true) or Off (false)
* @returns
*/
export const readPointDataRecords = (
{ nodes, file, copc, deep, worker, workerCount, queueLimit }: readPDRsParams,
withBar = false,
): Promise<AllNodesChecked> => {
return runTasks(
// turn Hierarchy.Node.Map into Array of Worker tasks
Object.entries(nodes).map(([key, node]) => ({
file,
key,
node: node || { pointCount: 0, pointDataOffset: 0, pointDataLength: 0 },
copc,
lazPerfWasmFilename: lazPerf,
})),
{ deep, withBar, worker, workerCount, queueLimit },
)
}
type createPointDataSuiteSourceData = {
nodes: Hierarchy.Node.Map
file: string | File
copc: Copc
}
type createPointDataSuiteSourceOptions = {
deep: boolean
worker?: WorkerSettings
workerCount?: number
queueLimit?: number
withBar?: boolean
sampleSize?: number
}
/**
* Should be more performant than using readPointDataRecords as it collects the
* nonZero nodes while building the workerParams array
* @param param0
* @param param1
* @returns
*/
const createPointDataSuiteSource = async (
{ nodes, file, copc }: createPointDataSuiteSourceData,
{
deep,
worker,
workerCount,
queueLimit,
sampleSize,
withBar = false,
}: createPointDataSuiteSourceOptions,
) => {
const nodeEntries = Object.entries(nodes)
const [nonZero, data] = nodeEntries.reduce<[string[], workerParams[]]>(
(acc, [key, node]) => {
if (node && node.pointCount !== 0) acc[0].push(key)
acc[1].push({
file,
key,
node: node || { pointCount: 0, pointDataOffset: 0, pointDataLength: 0 },
copc,
lazPerfWasmFilename: lazPerf,
})
return acc
},
[[], []],
)
if (!sampleSize)
return {
data: await runTasks(data, {
deep,
withBar,
worker,
workerCount,
queueLimit,
}),
nonZero,
}
if (sampleSize < 1) sampleSize = 1
// unneccesary since _.sample will return the full array if size > length
// if(sampleSize > entries.length) sampleSize = entries.length
// but we want to compare anyway and only warn if sample is less than entire node set
if (sampleSize < nodeEntries.length)
console.warn(
`The following report is NOT a comprehensive validation of all the data in the Point Data Records. The validator has randomly selected ${sampleSize} nodes out of a possible ${nodeEntries.length}.`,
)
return {
data: await runTasks(sample(data, sampleSize), {
deep,
withBar,
worker,
workerCount,
queueLimit,
}),
nonZero,
allNodes: nodes,
}
}