From 7d623069134cbe8b7b460fb9c2ea5e7d21533eb9 Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Sun, 4 Nov 2018 21:11:48 +0100 Subject: [PATCH] fix(core): update --- package.json | 1 + ts/logdna.aggregator.ts | 54 +++++++++++++++++++++++++++++++++ ts/logdna.classes.logmessage.ts | 2 +- ts/logdna.logdnaaccount.ts | 50 +++++++++--------------------- ts/logdna.plugins.ts | 3 +- 5 files changed, 73 insertions(+), 37 deletions(-) create mode 100644 ts/logdna.aggregator.ts diff --git a/package.json b/package.json index 237ad06..6a59ba5 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "tslint-config-prettier": "^1.15.0" }, "dependencies": { + "@pushrocks/lik": "^3.0.1", "@pushrocks/smartlog-interfaces": "^2.0.2", "@pushrocks/smartrequest": "^1.1.14", "@pushrocks/smartstring": "^3.0.4", diff --git a/ts/logdna.aggregator.ts b/ts/logdna.aggregator.ts new file mode 100644 index 0000000..d230dde --- /dev/null +++ b/ts/logdna.aggregator.ts @@ -0,0 +1,54 @@ +import * as plugins from './logdna.plugins'; +import { LogdnaAccount } from './logdna.logdnaaccount'; + +export interface ILogCandidate { + urlIdentifier: string; + logLines: any[]; +} + +export class LogAggregator { + private apiKey: string; + private baseUrl = 'https://logs.logdna.com/logs/ingest'; + + private logObjectMap = new plugins.lik.Objectmap(); + + constructor(apiKeyArg: string) { + this.apiKey = apiKeyArg; + } + + /** + * Create basic authentication + */ + private createBasicAuth() { + const userNamePasswordString = `${this.apiKey}:`; + return `Basic ${plugins.smartstring.base64.encode(userNamePasswordString)}`; + } + + public addLog(urlIdentifierArg: string, logLineArg: any) { + let existinglogCandidate = this.logObjectMap.find(logCandidate => { + return logCandidate.urlIdentifier === urlIdentifierArg; + }); + if (!existinglogCandidate) { + existinglogCandidate = { urlIdentifier: urlIdentifierArg, logLines: [] }; + this.logObjectMap.add(existinglogCandidate); + setTimeout(() => { + this.sendAggregatedLogs(existinglogCandidate); + }, 500); + } + existinglogCandidate.logLines.push(logLineArg); + } + + private async sendAggregatedLogs(logCandidate: ILogCandidate) { + this.logObjectMap.remove(logCandidate); + // lets post the message to logdna + await plugins.smartrequest.postJson(`${this.baseUrl}${logCandidate.urlIdentifier}&now=${Date.now()}` , { + headers: { + Authorization: this.createBasicAuth(), + charset: 'UTF-8' + }, + requestBody: { + lines: logCandidate.logLines + } + }); + } +} diff --git a/ts/logdna.classes.logmessage.ts b/ts/logdna.classes.logmessage.ts index af740d3..113ec1c 100644 --- a/ts/logdna.classes.logmessage.ts +++ b/ts/logdna.classes.logmessage.ts @@ -65,7 +65,7 @@ export class LogdnaMessage { * create lgdna messages from smartlog package * @param smartlogPackageArg */ - static fromSmartLogPackage(smartlogPackageArg: ILogPackage): LogdnaMessage { + public static fromSmartLogPackage(smartlogPackageArg: ILogPackage): LogdnaMessage { return new LogdnaMessage({ timestamp: smartlogPackageArg.timestamp, line: smartlogPackageArg.message, diff --git a/ts/logdna.logdnaaccount.ts b/ts/logdna.logdnaaccount.ts index c97bc5a..cb67979 100644 --- a/ts/logdna.logdnaaccount.ts +++ b/ts/logdna.logdnaaccount.ts @@ -1,29 +1,21 @@ import * as plugins from './logdna.plugins'; import { LogdnaMessage } from './logdna.classes.logmessage'; +import { LogAggregator } from './logdna.aggregator'; import { ILogPackage, ILogDestination } from '@pushrocks/smartlog-interfaces'; /** * the main logdna account */ export class LogdnaAccount { - private apiKey: string; - private baseUrl = 'https://logs.logdna.com/logs/ingest'; - - /** - * Create basic authentication - */ - private createBasicAuth() { - const userNamePasswordString = `${this.apiKey}:`; - return `Basic ${plugins.smartstring.base64.encode(userNamePasswordString)}`; - } + private logAggregator: LogAggregator; /** * * @param apiKeyArg */ constructor(apiKeyArg: string) { - this.apiKey = apiKeyArg; + this.logAggregator = new LogAggregator(apiKeyArg); } /** @@ -35,39 +27,27 @@ export class LogdnaAccount { const euc = encodeURIComponent; // let construct the request uri - const requestUrlWithParams = `${this.baseUrl}?hostname=${euc(lm.options.hostname)}&mac=${euc( + const requestUrlWithParams = `?hostname=${euc(lm.options.hostname)}&mac=${euc( lm.options.mac - )}&ip=1${euc(lm.options.ip)}&now=${Date.now()}&tags=${euc( + )}&ip=1${euc(lm.options.ip)}&tags=${euc( (() => { return lm.options.tags.reduce((reduced, newItem) => { return `${reduced},${newItem}`; }); })() )}`; - - const requestBodyObject = { - lines: [ - { - timestamp: lm.options.timestamp, - line: lm.options.line, - app: lm.options.app, - level: lm.options.level, - env: lm.options.env, - meta: lm.options.meta - } - ] + + const logLine = { + timestamp: lm.options.timestamp, + line: lm.options.line, + app: lm.options.app, + level: lm.options.level, + env: lm.options.env, + meta: lm.options.meta }; - // console.log(requestBodyObject); - - // lets post the message to logdna - await plugins.smartrequest.postJson(requestUrlWithParams, { - headers: { - Authorization: this.createBasicAuth(), - charset: 'UTF-8' - }, - requestBody: requestBodyObject - }); + this.logAggregator.addLog(requestUrlWithParams, logLine); + } /** diff --git a/ts/logdna.plugins.ts b/ts/logdna.plugins.ts index 9fa2d66..934bb85 100644 --- a/ts/logdna.plugins.ts +++ b/ts/logdna.plugins.ts @@ -1,5 +1,6 @@ +import * as lik from '@pushrocks/lik'; import * as smartrequest from '@pushrocks/smartrequest'; import * as smartstring from '@pushrocks/smartstring'; import * as smartlogInterfaces from '@pushrocks/smartlog-interfaces'; -export { smartrequest, smartstring, smartlogInterfaces }; +export { lik, smartrequest, smartstring, smartlogInterfaces };