-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into k8s-add-external-ports
- Loading branch information
Showing
85 changed files
with
1,750 additions
and
972 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* eslint-disable no-console */ | ||
|
||
'use strict'; | ||
|
||
const { pDelay, toHumanTime } = require('@terascope/utils'); | ||
const MultiMap = require('mnemonist/multi-map'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const { DataFrame } = require('./src'); | ||
|
||
function readFile(fileName) { | ||
const filePath = fs.existsSync(path.join(__dirname, 'fixtures', `.local.${fileName}`)) | ||
? path.join(__dirname, 'fixtures', `.local.${fileName}`) | ||
: path.join(__dirname, 'fixtures', fileName); | ||
|
||
return async function _readFile() { | ||
console.time(`readFile ${fileName}`); | ||
try { | ||
return await new Promise((resolve, reject) => { | ||
fs.readFile(filePath, { encoding: 'utf8' }, (err, buf) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(buf); | ||
}); | ||
}); | ||
} finally { | ||
console.timeEnd(`readFile ${fileName}`); | ||
} | ||
}; | ||
} | ||
|
||
function readFileStream(fileName) { | ||
const filePath = fs.existsSync(path.join(__dirname, 'fixtures', `.local.${fileName}`)) | ||
? path.join(__dirname, 'fixtures', `.local.${fileName}`) | ||
: path.join(__dirname, 'fixtures', fileName); | ||
|
||
return async function* _readFile() { | ||
console.time(`readFileStream ${fileName}`); | ||
try { | ||
const stream = fs.createReadStream(filePath, { encoding: 'utf8' }); | ||
let chunks = ''; | ||
for await (const chunk of stream) { | ||
const parts = chunk.split('\n'); | ||
if (parts.length === 0) { | ||
// do nothing | ||
} else if (parts.length === 1) { | ||
chunks += chunk; | ||
} else { | ||
for (let i = 0; i < parts.length; i++) { | ||
if (i === (parts.length - 1)) { | ||
chunks += parts[i]; | ||
} else { | ||
yield chunks + parts[i]; | ||
chunks = ''; | ||
} | ||
} | ||
} | ||
} | ||
} finally { | ||
console.timeEnd(`readFileStream ${fileName}`); | ||
} | ||
}; | ||
} | ||
|
||
async function fromJSON(buf) { | ||
console.time('fromJSON'); | ||
try { | ||
const { data, config } = JSON.parse(buf); | ||
return DataFrame.fromJSON(config, data); | ||
} finally { | ||
console.timeEnd('fromJSON'); | ||
} | ||
} | ||
|
||
async function deserialize(buf) { | ||
console.time('deserialize'); | ||
try { | ||
return await DataFrame.deserialize(buf); | ||
} finally { | ||
console.timeEnd('deserialize'); | ||
} | ||
} | ||
|
||
async function deserializeStream(iterator) { | ||
console.time('deserializeStream'); | ||
try { | ||
return await DataFrame.deserializeIterator(iterator); | ||
} finally { | ||
console.timeEnd('deserializeStream'); | ||
} | ||
} | ||
|
||
async function runTest(times) { | ||
let start; | ||
return Promise.resolve() | ||
.then(() => { start = Date.now(); }) | ||
.then(readFile('data.json')) | ||
.then(fromJSON) | ||
.then(() => times.set('row', Date.now() - start)) | ||
.then(() => pDelay(100)) | ||
.then(() => { start = Date.now(); }) | ||
.then(readFile('data.dfjson')) | ||
.then(deserialize) | ||
.then(() => times.set('column', Date.now() - start)) | ||
.then(() => { start = Date.now(); }) | ||
.then(readFileStream('data.dfjson')) | ||
.then(deserializeStream) | ||
.then(() => times.set('column stream', Date.now() - start)); | ||
} | ||
|
||
(async function runTests() { | ||
const times = new MultiMap(); | ||
for (let i = 0; i < 3; i++) { | ||
await runTest(times); | ||
} | ||
for (const [group, groupTimes] of times.associations()) { | ||
let min; | ||
let max; | ||
let sum = 0; | ||
for (const time of groupTimes) { | ||
sum += time; | ||
if (max == null || time > max) max = time; | ||
if (min == null || time < min) min = time; | ||
} | ||
const avg = sum / groupTimes.length; | ||
console.log(`[${group}] | ||
avg: ${toHumanTime(avg)} | ||
min: ${toHumanTime(min)} | ||
max: ${toHumanTime(max)}`); | ||
} | ||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,35 @@ | ||
import { DateValue, WritableData } from '../../core'; | ||
import { | ||
getTypeOf, isNumber, isString, isValidDateInstance, makeISODate | ||
} from '@terascope/utils'; | ||
import { DateFormat } from '@terascope/types'; | ||
import { WritableData } from '../../core'; | ||
import { VectorType } from '../../vector'; | ||
import { BuilderOptions } from '../Builder'; | ||
import { BuilderWithCache } from '../BuilderWithCache'; | ||
|
||
export class DateBuilder extends BuilderWithCache<DateValue> { | ||
referenceDate = new Date(); | ||
import { Builder, BuilderOptions } from '../Builder'; | ||
|
||
export class DateBuilder extends Builder<number|string> { | ||
constructor( | ||
data: WritableData<DateValue>, | ||
data: WritableData<number>, | ||
options: BuilderOptions | ||
) { | ||
super(VectorType.Date, data, options); | ||
} | ||
|
||
_valueFrom(value: unknown): DateValue { | ||
// FIXME this should validate the format is correct | ||
if (value instanceof DateValue) return value; | ||
_valueFrom(value: unknown): number|string { | ||
if (value instanceof Date) { | ||
if (isValidDateInstance(value)) return value.toISOString(); | ||
|
||
throw new TypeError(`Expected ${value} (${getTypeOf(value)}) to be a valid date instance`); | ||
} | ||
|
||
if (!isString(value) && !isNumber(value)) { | ||
throw new TypeError(`Expected ${value} (${getTypeOf(value)}) to be a valid date`); | ||
} | ||
|
||
if (this.config.format) { | ||
return DateValue.fromValueWithFormat( | ||
value, | ||
this.config.format, | ||
this.referenceDate | ||
); | ||
// ensure we stored the iso 8601 format where possible | ||
if (this.config.format === DateFormat.iso_8601 || !this.config.format) { | ||
return makeISODate(value); | ||
} | ||
|
||
return DateValue.fromValue(value as any); | ||
return value; | ||
} | ||
} |
Oops, something went wrong.