Skip to content

Commit

Permalink
restore date format
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo4815162342 committed Feb 27, 2024
1 parent f2e885d commit 3f7c27e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
12 changes: 10 additions & 2 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ import { processData } from '../processor';
import { formatBytes } from '../utils/formatBytes';
import chalk from 'chalk';
import debug from 'debug';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';

import { version } from '../../package.json';
import { BatchStreamWriter } from '../stream-writer';
import { BufferObject } from '../buffer-fetcher/types';
import { formatTimeDuration } from '../utils/formatTimeDuration';

dayjs.extend(utc);

const DEBUG_NAMESPACE = 'dukascopy-node:cli';

export async function run(argv: NodeJS.Process['argv']) {
Expand Down Expand Up @@ -47,7 +51,8 @@ export async function run(argv: NodeJS.Process['argv']) {
failAfterRetryCount,
retryOnEmpty,
pauseBetweenRetriesMs,
fileName: customFileName
fileName: customFileName,
dateFormat
} = input;

if (isDebugActive) {
Expand Down Expand Up @@ -187,7 +192,10 @@ export async function run(argv: NodeJS.Process['argv']) {
ignoreFlats
});

await batchStreamWriter.writeBatch(processedBatch);
await batchStreamWriter.writeBatch(
processedBatch,
dateFormat ? timeStamp => dayjs(timeStamp).utc().format(dateFormat) : undefined
);
}

if (isLastBatch) {
Expand Down
8 changes: 6 additions & 2 deletions src/cli/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface CliConfig extends ConfigBase {
debug: boolean;
inline: boolean;
fileName: string;
dateFormat: string;
}

const now = 'now';
Expand Down Expand Up @@ -44,6 +45,7 @@ const commanderSchema = program
.option('-bp, --batch-pause <value>', 'Pause between batches in ms', Number, 1000)
.option('-ch, --cache', 'Use cache', false)
.option('-chpath, --cache-path <value>', 'Folder path for cache data', './.dukascopy-cache')
.option('-df, --date-format <value>', 'Date format', './.dukascopy-cache')
.option('-r, --retries <value>', 'Number of retries for a failed artifact download', Number, 0)
.option('-rp, --retry-pause <value>', 'Pause between retries in milliseconds', Number, 500)
.option(
Expand Down Expand Up @@ -95,7 +97,8 @@ export function getConfigFromCliArgs(argv: NodeJS.Process['argv']) {
pauseBetweenRetriesMs: options.retryPause,
debug: options.debug,
inline: options.inline,
fileName: options.fileName
fileName: options.fileName,
dateFormat: options.dateFormat
};

const cliSchema: InputSchema<CliConfig> = {
Expand All @@ -105,7 +108,8 @@ export function getConfigFromCliArgs(argv: NodeJS.Process['argv']) {
silent: { type: 'boolean', required: false } as RuleBoolean,
debug: { type: 'boolean', required: false } as RuleBoolean,
inline: { type: 'boolean', required: false } as RuleBoolean,
fileName: { type: 'string', required: false } as RuleString
fileName: { type: 'string', required: false } as RuleString,
dateFormat: { type: 'string', required: false } as RuleString
}
};

Expand Down
16 changes: 12 additions & 4 deletions src/stream-writer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class BatchStreamWriter {
private initHeaders() {
const bodyHeaders =
this.timeframe === Timeframe.tick
? ['timestamp', 'askPrice', 'bidPrice', 'askVolume', 'bidVolume']
? ['timestamp', 'askPrice', 'bidPrice', 'askVolume', 'bidVolume'] // TODO: add custom header names as cli options
: ['timestamp', 'open', 'high', 'low', 'close', 'volume'];

if (!this.volumes) {
Expand All @@ -114,21 +114,25 @@ export class BatchStreamWriter {
return bodyHeaders;
}

public async writeBatch(batch: number[][]) {
const batchWithinRange: number[][] = [];
public async writeBatch(batch: number[][], dateFormatter?: (timestamp: number) => string) {
const batchWithinRange: (number | string)[][] = [];

for (let j = 0; j < batch.length; j++) {
const item = batch[j];
const isItemInRange =
item.length > 0 && item[0] >= this.startDateTs && item[0] < this.endDateTs;

if (isItemInRange) {
if (dateFormatter) {
//@ts-expect-error TODO: fix this
item[0] = dateFormatter(item[0]);
}
batchWithinRange.push(item);
}
}

for (let i = 0; i < batchWithinRange.length; i++) {
const item = batchWithinRange[i];
let item = batchWithinRange[i];

const isFirstItem = i === 0;
const isLastItem = i === batchWithinRange.length - 1;
Expand All @@ -152,6 +156,10 @@ export class BatchStreamWriter {
body += ',' + (!this.isInline ? '\n' : '');
}

if (dateFormatter && (this.format === Format.json || this.format === Format.array)) {
item[0] = `"${item[0]}"`;
}

if (this.format === Format.json) {
const jsonObjectBody = item.map((val, i) => `"${this.bodyHeaders[i]}":${val}`).join(',');
body += `{${jsonObjectBody}}`;
Expand Down

0 comments on commit 3f7c27e

Please sign in to comment.