fix(core): update

This commit is contained in:
Philipp Kunz 2018-11-04 21:11:48 +01:00
parent 484e5d51cc
commit 7d62306913
5 changed files with 73 additions and 37 deletions

View File

@ -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",

54
ts/logdna.aggregator.ts Normal file
View File

@ -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<ILogCandidate>();
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
}
});
}
}

View File

@ -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,

View File

@ -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);
}
/**

View File

@ -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 };