-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
383 lines (353 loc) · 15.8 KB
/
index.js
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import debug from 'debug'
// import FP from 'functional-promises';
// import { detectTypes } from './type-helpers.js'
// import StatsMap from 'stats-map';
// import mem from 'mem';
import { detectTypes, MetaChecks, typeRankings } from './type-helpers.js'
const log = debug('schema-builder:index')
// const cache = new StatsMap();
// const detectTypesCached = mem(_detectTypes, { cache, maxAge: 1000 * 600 }) // keep cache up to 10 minutes
export { schemaBuilder, pivotFieldDataByType, getNumberRangeStats, isValidDate }
function isValidDate (date) {
date = date instanceof Date ? date : new Date(date)
return isNaN(date.getFullYear()) ? false : date
}
const parseDate = (date) => {
date = isValidDate(date)
return date && date.toISOString && date.toISOString()
}
/**
* Includes the results of input analysis.
* @typedef TypeSummary
* @type {{ fields: Object.<string, FieldTypeSummary>; totalRows: number; }}
*/
/**
* This is an internal intermediate structure.
* It mirrors the `FieldSummary` type it will become.
* @private
* @typedef FieldTypeData
* @type {Object}
* @property {number[]} [value] - array of values, pre processing into an AggregateSummary
* @property {number[]} [length] - array of string (or decimal) sizes, pre processing into an AggregateSummary
* @property {number[]} [precision] - only applies to Float types. Array of sizes of the value both before and after the decimal.
* @property {number[]} [scale] - only applies to Float types. Array of sizes of the value after the decimal.
* @property {number} [count] - number of times the type was matched
* @property {number} [rank] - absolute priority of the detected TypeName, defined in the object `typeRankings`
*
*/
/**
*
* @typedef FieldTypeSummary
* @type {Object}
* @property {AggregateSummary} [value] - summary of array of values, pre processing into an AggregateSummary
* @property {AggregateSummary} [length] - summary of array of string (or decimal) sizes, pre processing into an AggregateSummary
* @property {AggregateSummary} [precision] - only applies to Float types. Summary of array of sizes of the value both before and after the decimal.
* @property {AggregateSummary} [scale] - only applies to Float types. Summary of array of sizes of the value after the decimal.
* @property {string[]|number[]} [enum] - if enum rules were triggered will contain the detected unique values.
* @property {number} count - number of times the type was matched
* @property {number} rank - absolute priority of the detected TypeName, defined in the object `typeRankings`
*
*/
/**
* @typedef FieldInfo
* @type {object}
* @property {Object.<string, FieldTypeSummary>} types - field stats organized by type
* @property {boolean} nullable - is the field nullable
* @property {boolean} unique - is the field unique
* @property {string[]|number[]} [enum] - enumeration detected, the values are listed on this property.
*/
/**
* Used to represent a number series of any size.
* Includes the lowest (`min`), highest (`max`), mean/average (`mean`) and measurements at certain `percentiles`.
* @typedef AggregateSummary
* @type {{min: number, max: number, mean: number, p25: number, p33: number, p50: number, p66: number, p75: number, p99: number}}
*/
/**
* This callback is displayed as a global member.
* @callback progressCallback
* @param {{totalRows: number, currentRow: number}} progress - The current progress of processing.
*/
/**
* schemaBuilder is the main function and where all the analysis & processing happens.
* @param {Array<Object>} input - The input data to analyze. Must be an array of objects.
* @param {{ onProgress?: progressCallback, enumMinimumRowCount?: number, enumAbsoluteLimit?: number, enumPercentThreshold?: number, nullableRowsThreshold?: number, uniqueRowsThreshold?: number, strictMatching?: boolean }} [options] - Optional parameters
* @returns {Promise<TypeSummary>} Returns and
*/
function schemaBuilder (
input,
{
onProgress = ({ totalRows, currentRow }) => {},
strictMatching = true,
enumMinimumRowCount = 100, enumAbsoluteLimit = 10, enumPercentThreshold = 0.01,
nullableRowsThreshold = 0.02,
uniqueRowsThreshold = 1.0
} = {
onProgress: ({ totalRows, currentRow }) => {},
strictMatching: true,
enumMinimumRowCount: 100,
enumAbsoluteLimit: 10,
enumPercentThreshold: 0.01,
nullableRowsThreshold: 0.02,
uniqueRowsThreshold: 1.0
}
) {
if (!Array.isArray(input) || typeof input !== 'object') throw Error('Input Data must be an Array of Objects')
if (typeof input[0] !== 'object') throw Error('Input Data must be an Array of Objects')
if (input.length < 5) throw Error('Analysis requires at least 5 records. (Use 200+ for great results.)')
const isEnumEnabled = input.length >= enumMinimumRowCount
log('Starting')
return Promise.resolve(input)
.then(pivotRowsGroupedByType)
.then(condenseFieldData)
.then((schema) => {
log('Built summary from Field Type data.')
// console.log('genSchema', JSON.stringify(genSchema, null, 2))
const fields = Object.keys(schema.fields)
.reduce((fieldInfo, fieldName) => {
const types = schema.fields[fieldName]
/** @type {FieldInfo} */
fieldInfo[fieldName] = {
types
}
fieldInfo[fieldName] = MetaChecks.TYPE_ENUM.check(fieldInfo[fieldName],
{ rowCount: input.length, uniques: schema.uniques && schema.uniques[fieldName] },
{ enumAbsoluteLimit, enumPercentThreshold })
fieldInfo[fieldName] = MetaChecks.TYPE_NULLABLE.check(fieldInfo[fieldName],
{ rowCount: input.length, uniques: schema.uniques && schema.uniques[fieldName] },
{ nullableRowsThreshold })
fieldInfo[fieldName] = MetaChecks.TYPE_UNIQUE.check(fieldInfo[fieldName],
{ rowCount: input.length, uniques: schema.uniques && schema.uniques[fieldName] },
{ uniqueRowsThreshold })
const isIdentity = (types.Number || types.UUID) && fieldInfo[fieldName].unique && /id$/i.test(fieldName)
if (isIdentity) {
fieldInfo[fieldName].identity = true
}
if (schema.uniques && schema.uniques[fieldName]) {
fieldInfo[fieldName].uniqueCount = schema.uniques[fieldName].length
}
return fieldInfo
}, {})
return {
fields,
totalRows: schema.totalRows
// uniques: uniques,
// fields: schema.fields
}
})
/**
* @param {object[]} docs
* @returns {{ totalRows: number; uniques: { [x: string]: any[]; }; fieldsData: { [x: string]: FieldTypeData[]; }; }} schema
*/
function pivotRowsGroupedByType (docs) {
const detectedSchema = { uniques: isEnumEnabled ? {} : null, fieldsData: {}, totalRows: null }
log(` About to examine every row & cell. Found ${docs.length} records...`)
const pivotedSchema = docs.reduce(evaluateSchemaLevel, detectedSchema)
log(' Extracted data points from Field Type analysis')
return pivotedSchema
}
/**
* @param {{ totalRows: number; uniques: { [x: string]: any[]; }; fieldsData: { [x: string]: FieldTypeData[]; }; }} schema
* @param {{ [x: string]: any; }} row
* @param {number} index
* @param {any[]} array
*/
function evaluateSchemaLevel (schema, row, index, array) { // eslint-disable-line
schema.totalRows = schema.totalRows || array.length
const fieldNames = Object.keys(row)
log(`Processing Row # ${index + 1}/${schema.totalRows}...`)
fieldNames.forEach((fieldName, index, array) => {
if (index === 0) log(`Found ${array.length} Column(s)!`)
const typeFingerprint = getFieldMetadata({
value: row[fieldName],
strictMatching
})
const typeNames = Object.keys(typeFingerprint)
const isEnumType = typeNames.includes('Number') || typeNames.includes('String')
if (isEnumEnabled && isEnumType) {
schema.uniques[fieldName] = schema.uniques[fieldName] || []
if (!schema.uniques[fieldName].includes(row[fieldName])) schema.uniques[fieldName].push(row[fieldName])
// } else {
// schema.uniques[fieldName] = null
}
schema.fieldsData[fieldName] = schema.fieldsData[fieldName] || []
schema.fieldsData[fieldName].push(typeFingerprint)
})
onProgress({ totalRows: schema.totalRows, currentRow: index + 1 })
return schema
}
}
/**
*
* @param {{ fieldsData: Object.<string, FieldTypeData[]>, uniques: Object.<string, any[]>, totalRows: number}} schema
* @returns {{fields: Object.<string, FieldTypeSummary>, uniques: Object.<string, any[]>, totalRows: number}}
*/
function condenseFieldData (schema) {
const { fieldsData } = schema
const fieldNames = Object.keys(fieldsData)
/** @type {Object.<string, FieldTypeSummary>} */
const fieldSummary = {}
log(`Pre-condenseFieldSizes(fields[fieldName]) for ${fieldNames.length} columns`)
fieldNames
.forEach((fieldName) => {
/** @type {Object.<string, FieldTypeData>} */
const pivotedData = pivotFieldDataByType(fieldsData[fieldName])
fieldSummary[fieldName] = condenseFieldSizes(pivotedData)
})
log('Post-condenseFieldSizes(fields[fieldName])')
log('Replaced fieldData with fieldSummary')
return { fields: fieldSummary, uniques: schema.uniques, totalRows: schema.totalRows }
}
/**
* @param {Object.<string, { value?, length?, scale?, precision?, invalid? }>[]} typeSizeData - An object containing the
* @returns {Object.<string, FieldTypeData>}
*/
function pivotFieldDataByType (typeSizeData) {
// const blankTypeSums = () => ({ length: 0, scale: 0, precision: 0 })
log(`Processing ${typeSizeData.length} type guesses`)
return typeSizeData.reduce((pivotedData, currentTypeGuesses) => {
Object.entries(currentTypeGuesses)
.map(([typeName, { value, length, scale, precision }]) => {
// console.log(typeName, JSON.stringify({ length, scale, precision }))
pivotedData[typeName] = pivotedData[typeName] || { typeName, count: 0 }
// if (!pivotedData[typeName].count) pivotedData[typeName].count = 0
if (Number.isFinite(length) && !pivotedData[typeName].length) pivotedData[typeName].length = []
if (Number.isFinite(scale) && !pivotedData[typeName].scale) pivotedData[typeName].scale = []
if (Number.isFinite(precision) && !pivotedData[typeName].precision) pivotedData[typeName].precision = []
if (Number.isFinite(value) && !pivotedData[typeName].value) pivotedData[typeName].value = []
pivotedData[typeName].count++
// if (invalid != null) pivotedData[typeName].invalid++
if (length) pivotedData[typeName].length.push(length)
if (scale) pivotedData[typeName].scale.push(scale)
if (precision) pivotedData[typeName].precision.push(precision)
if (value) pivotedData[typeName].value.push(value)
// pivotedData[typeName].rank = typeRankings[typeName]
return pivotedData[typeName]
})
return pivotedData
}, {})
/*
> Example of sumCounts at this point
{
Float: { count: 4, scale: [ 5, 5, 5, 5 ], precision: [ 2, 2, 2, 2 ] },
String: { count: 3, length: [ 2, 3, 6 ] },
Number: { count: 1, length: [ 6 ] }
}
*/
}
/**
* Internal function which analyzes and summarizes each columns data by type. Sort of a histogram of significant points.
* @private
* @param {Object.<string, FieldTypeData>} pivotedDataByType - a map organized by Type keys (`TypeName`), containing extracted data for the returned `FieldSummary`.
* @returns {Object.<string, FieldTypeSummary>} - The final output, with histograms of significant points
*/
function condenseFieldSizes (pivotedDataByType) {
/** @type {Object.<string, FieldTypeSummary>} */
const aggregateSummary = {}
log('Starting condenseFieldSizes()')
Object.keys(pivotedDataByType)
.map((typeName) => {
aggregateSummary[typeName] = {
// typeName,
rank: typeRankings[typeName],
count: pivotedDataByType[typeName].count
}
if (pivotedDataByType[typeName].value) aggregateSummary[typeName].value = getNumberRangeStats(pivotedDataByType[typeName].value)
if (pivotedDataByType[typeName].length) aggregateSummary[typeName].length = getNumberRangeStats(pivotedDataByType[typeName].length, true)
if (pivotedDataByType[typeName].scale) aggregateSummary[typeName].scale = getNumberRangeStats(pivotedDataByType[typeName].scale, true)
if (pivotedDataByType[typeName].precision) aggregateSummary[typeName].precision = getNumberRangeStats(pivotedDataByType[typeName].precision, true)
// if (pivotedDataByType[typeName].invalid) { aggregateSummary[typeName].invalid = pivotedDataByType[typeName].invalid }
if (['Timestamp', 'Date'].indexOf(typeName) > -1) {
aggregateSummary[typeName].value = formatRangeStats(aggregateSummary[typeName].value, parseDate)
}
})
log('Done condenseFieldSizes()...')
return aggregateSummary
}
function getFieldMetadata ({
value,
strictMatching
}) {
// Get initial pass at the data with the TYPE_* `.check()` methods.
const typeGuesses = detectTypes(value, strictMatching)
// Assign initial metadata for each matched type below
return typeGuesses.reduce((analysis, typeGuess, rank) => {
let length
let precision
let scale
analysis[typeGuess] = { rank: rank + 1 }
if (typeGuess === 'Float') {
value = parseFloat(value)
analysis[typeGuess] = { ...analysis[typeGuess], value }
const significandAndMantissa = String(value).split('.')
if (significandAndMantissa.length === 2) {
precision = significandAndMantissa.join('').length // total # of numeric positions before & after decimal
scale = significandAndMantissa[1].length
analysis[typeGuess] = { ...analysis[typeGuess], precision, scale }
}
}
if (typeGuess === 'Number') {
value = Number(value)
analysis[typeGuess] = { ...analysis[typeGuess], value }
}
if (typeGuess === 'Date' || typeGuess === 'Timestamp') {
const checkedDate = isValidDate(value)
if (checkedDate) {
analysis[typeGuess] = { ...analysis[typeGuess], value: checkedDate.getTime() }
// } else {
// analysis[typeGuess] = { ...analysis[typeGuess], invalid: true, value: value }
}
}
if (typeGuess === 'String' || typeGuess === 'Email') {
length = String(value).length
analysis[typeGuess] = { ...analysis[typeGuess], length }
}
if (typeGuess === 'Array') {
length = value.length
analysis[typeGuess] = { ...analysis[typeGuess], length }
}
return analysis
}, {})
}
/**
* Accepts an array of numbers and returns summary data about
* the range & spread of points in the set.
*
* @param {number[]} numbers - sequence of unsorted data points
* @returns {AggregateSummary}
*/
function getNumberRangeStats (numbers, useSortedDataForPercentiles = false) {
if (!numbers || numbers.length < 1) return undefined
const sortedNumbers = numbers.slice().sort((a, b) => a < b ? -1 : a === b ? 0 : 1)
const sum = numbers.reduce((a, b) => a + b, 0)
if (useSortedDataForPercentiles) numbers = sortedNumbers
return {
// size: numbers.length,
min: sortedNumbers[0],
mean: sum / numbers.length,
max: sortedNumbers[numbers.length - 1],
p25: numbers[parseInt(String(numbers.length * 0.25), 10)],
p33: numbers[parseInt(String(numbers.length * 0.33), 10)],
p50: numbers[parseInt(String(numbers.length * 0.50), 10)],
p66: numbers[parseInt(String(numbers.length * 0.66), 10)],
p75: numbers[parseInt(String(numbers.length * 0.75), 10)],
p99: numbers[parseInt(String(numbers.length * 0.99), 10)]
}
}
/**
*
*/
function formatRangeStats (stats, formatter) {
// if (!stats || !formatter) return undefined
return {
// size: stats.size,
min: formatter(stats.min),
mean: formatter(stats.mean),
max: formatter(stats.max),
p25: formatter(stats.p25),
p33: formatter(stats.p33),
p50: formatter(stats.p50),
p66: formatter(stats.p66),
p75: formatter(stats.p75),
p99: formatter(stats.p99)
}
}