Compare commits

...

18 Commits

Author SHA1 Message Date
c9a9434cd9 1.0.30 2019-11-03 00:51:18 +01:00
5d98dd9089 fix(core): update 2019-11-03 00:51:18 +01:00
2d635fdf7c 1.0.29 2019-11-03 00:49:35 +01:00
1dbf3724d0 fix(core): update 2019-11-03 00:49:35 +01:00
cc7eb8c139 1.0.28 2019-11-03 00:47:19 +01:00
0e01ecbd1a fix(core): update 2019-11-03 00:47:18 +01:00
2d21b40a76 1.0.27 2019-11-02 01:42:04 +01:00
2d1a5cdc50 fix(core): update 2019-11-02 01:42:03 +01:00
20a41d3381 1.0.26 2018-12-13 00:19:50 +01:00
b2019b33f8 fix(core): update 2018-12-13 00:19:50 +01:00
1ab582db51 1.0.25 2018-11-26 12:24:38 +01:00
9c87f5ee5e fix(core): update 2018-11-26 12:24:37 +01:00
ef9cb193d5 1.0.24 2018-11-25 22:06:25 +01:00
9f706e0a70 fix(core): update 2018-11-25 22:06:25 +01:00
b2d4b82532 1.0.23 2018-11-25 22:02:57 +01:00
63713f4cd2 fix(core): update 2018-11-25 22:02:57 +01:00
5d3bc13126 1.0.22 2018-11-11 02:00:41 +01:00
4b30234cb6 fix(core): update 2018-11-11 02:00:40 +01:00
11 changed files with 1363 additions and 662 deletions

19
.gitignore vendored
View File

@ -1,5 +1,22 @@
.nogit/ .nogit/
node_modules/
# artifacts
coverage/ coverage/
public/ public/
pages/ pages/
# installs
node_modules/
# caches
.yarn/
.cache/
.rpt2_cache
# builds
dist/
dist_web/
dist_serve/
dist_ts_web/
# custom

View File

@ -1,10 +1,10 @@
# gitzone standard # gitzone ci_default
image: hosttoday/ht-docker-node:npmci image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
cache: cache:
paths: paths:
- .npmci_cache/ - .npmci_cache/
key: "$CI_BUILD_STAGE" key: '$CI_BUILD_STAGE'
stages: stages:
- security - security
@ -20,6 +20,7 @@ mirror:
script: script:
- npmci git mirror - npmci git mirror
tags: tags:
- lossless
- docker - docker
- notpriv - notpriv
@ -31,38 +32,15 @@ snyk:
- npmci command npm install --ignore-scripts - npmci command npm install --ignore-scripts
- npmci command snyk test - npmci command snyk test
tags: tags:
- lossless
- docker - docker
- notpriv - notpriv
# ==================== # ====================
# test stage # test stage
# ==================== # ====================
testLEGACY:
stage: test
script:
- npmci npm prepare
- npmci node install legacy
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv
allow_failure: true
testLTS: testStable:
stage: test
script:
- npmci npm prepare
- npmci node install lts
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv
testSTABLE:
stage: test stage: test
script: script:
- npmci npm prepare - npmci npm prepare
@ -71,6 +49,20 @@ testSTABLE:
- npmci npm test - npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/ coverage: /\d+.?\d+?\%\s*coverage/
tags: tags:
- lossless
- docker
- priv
testBuild:
stage: test
script:
- npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci command npm run build
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- lossless
- docker - docker
- notpriv - notpriv
@ -82,6 +74,7 @@ release:
only: only:
- tags - tags
tags: tags:
- lossless
- docker - docker
- notpriv - notpriv
@ -90,20 +83,14 @@ release:
# ==================== # ====================
codequality: codequality:
stage: metadata stage: metadata
image: docker:stable
allow_failure: true allow_failure: true
services:
- docker:stable-dind
script: script:
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/') - npmci command npm install -g tslint typescript
- docker run - npmci npm prepare
--env SOURCE_CODE="$PWD" - npmci npm install
--volume "$PWD":/code - npmci command "tslint -c tslint.json ./ts/**/*.ts"
--volume /var/run/docker.sock:/var/run/docker.sock
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
artifacts:
paths: [codeclimate.json]
tags: tags:
- lossless
- docker - docker
- priv - priv
@ -114,18 +101,20 @@ trigger:
only: only:
- tags - tags
tags: tags:
- lossless
- docker - docker
- notpriv - notpriv
pages: pages:
image: hosttoday/ht-docker-node:npmci
stage: metadata stage: metadata
script: script:
- npmci command npm install -g typedoc typescript - npmci node install lts
- npmci command npm install -g @gitzone/tsdoc
- npmci npm prepare - npmci npm prepare
- npmci npm install - npmci npm install
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/ - npmci command tsdoc
tags: tags:
- lossless
- docker - docker
- notpriv - notpriv
only: only:
@ -135,13 +124,3 @@ pages:
paths: paths:
- public - public
allow_failure: true allow_failure: true
windowsCompatibility:
image: stefanscherer/node-windows:10-build-tools
stage: metadata
script:
- npm install & npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- windows
allow_failure: true

View File

@ -2,5 +2,15 @@
"npmci": { "npmci": {
"npmGlobalTools": [], "npmGlobalTools": [],
"npmAccessLevel": "public" "npmAccessLevel": "public"
},
"gitzone": {
"module": {
"githost": "gitlab.com",
"gitscope": "mojoio",
"gitrepo": "elasticsearch",
"shortDescription": "log to elasticsearch in a kibana compatible format",
"npmPackagename": "@mojoio/elasticsearch",
"license": "MIT"
}
} }
} }

1649
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{ {
"name": "@mojoio/elasticsearch", "name": "@mojoio/elasticsearch",
"version": "1.0.21", "version": "1.0.30",
"private": false, "private": false,
"description": "log to elasticsearch in a kibana compatible format", "description": "log to elasticsearch in a kibana compatible format",
"main": "dist/index.js", "main": "dist/index.js",
@ -13,22 +13,33 @@
"build": "(tsbuild)" "build": "(tsbuild)"
}, },
"devDependencies": { "devDependencies": {
"@gitzone/tsbuild": "^2.0.22", "@gitzone/tsbuild": "^2.1.17",
"@gitzone/tsrun": "^1.1.13", "@gitzone/tsrun": "^1.2.8",
"@gitzone/tstest": "^1.0.15", "@gitzone/tstest": "^1.0.28",
"@pushrocks/qenv": "^2.0.2", "@pushrocks/qenv": "^4.0.6",
"@pushrocks/tapbundle": "^3.0.7", "@pushrocks/tapbundle": "^3.0.13",
"@types/node": "^10.12.2", "@types/node": "^12.12.5",
"tslint": "^5.11.0", "tslint": "^5.20.0",
"tslint-config-prettier": "^1.15.0" "tslint-config-prettier": "^1.18.0"
}, },
"dependencies": { "dependencies": {
"@pushrocks/lik": "^3.0.1", "@pushrocks/lik": "^3.0.11",
"@pushrocks/smartdelay": "^2.0.2", "@pushrocks/smartdelay": "^2.0.3",
"@pushrocks/smartlog-interfaces": "^2.0.2", "@pushrocks/smartlog-interfaces": "^2.0.9",
"@pushrocks/smartpromise": "^2.0.5", "@pushrocks/smartpromise": "^3.0.6",
"@pushrocks/smarttime": "^3.0.2", "@pushrocks/smarttime": "^3.0.12",
"@types/elasticsearch": "^5.0.28", "@types/elasticsearch": "^5.0.35",
"elasticsearch": "^15.2.0" "elasticsearch": "^16.5.0"
} },
"files": [
"ts/**/*",
"ts_web/**/*",
"dist/**/*",
"dist_web/**/*",
"dist_ts_web/**/*",
"assets/**/*",
"cli.js",
"npmextra.json",
"readme.md"
]
} }

View File

@ -1,4 +1,4 @@
vars: required:
- ELK_DOMAIN - ELK_DOMAIN
- ELK_PORT - ELK_PORT
- ELK_USER - ELK_USER

View File

@ -1,26 +1,20 @@
# elasticlog # @mojoio/elasticsearch
log to elasticsearch in a kibana compatible format log to elasticsearch in a kibana compatible format
## Availabililty ## Availabililty and Links
* [npmjs.org (npm package)](https://www.npmjs.com/package/@mojoio/elasticsearch)
[![npm](https://pushrocks.gitlab.io/assets/repo-button-npm.svg)](https://www.npmjs.com/package/elasticlog) * [gitlab.com (source)](https://gitlab.com/mojoio/elasticsearch)
[![git](https://pushrocks.gitlab.io/assets/repo-button-git.svg)](https://GitLab.com/pushrocks/elasticlog) * [github.com (source mirror)](https://github.com/mojoio/elasticsearch)
[![git](https://pushrocks.gitlab.io/assets/repo-button-mirror.svg)](https://github.com/pushrocks/elasticlog) * [docs (typedoc)](https://mojoio.gitlab.io/elasticsearch/)
[![docs](https://pushrocks.gitlab.io/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/elasticlog/)
## Status for master ## Status for master
[![build status](https://gitlab.com/mojoio/elasticsearch/badges/master/build.svg)](https://gitlab.com/mojoio/elasticsearch/commits/master)
[![build status](https://GitLab.com/pushrocks/elasticlog/badges/master/build.svg)](https://GitLab.com/pushrocks/elasticlog/commits/master) [![coverage report](https://gitlab.com/mojoio/elasticsearch/badges/master/coverage.svg)](https://gitlab.com/mojoio/elasticsearch/commits/master)
[![coverage report](https://GitLab.com/pushrocks/elasticlog/badges/master/coverage.svg)](https://GitLab.com/pushrocks/elasticlog/commits/master) [![npm downloads per month](https://img.shields.io/npm/dm/@mojoio/elasticsearch.svg)](https://www.npmjs.com/package/@mojoio/elasticsearch)
[![npm downloads per month](https://img.shields.io/npm/dm/elasticlog.svg)](https://www.npmjs.com/package/elasticlog) [![Known Vulnerabilities](https://snyk.io/test/npm/@mojoio/elasticsearch/badge.svg)](https://snyk.io/test/npm/@mojoio/elasticsearch)
[![Dependency Status](https://david-dm.org/pushrocks/elasticlog.svg)](https://david-dm.org/pushrocks/elasticlog) [![TypeScript](https://img.shields.io/badge/TypeScript->=%203.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![bitHound Dependencies](https://www.bithound.io/github/pushrocks/elasticlog/badges/dependencies.svg)](https://www.bithound.io/github/pushrocks/elasticlog/master/dependencies/npm) [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![bitHound Code](https://www.bithound.io/github/pushrocks/elasticlog/badges/code.svg)](https://www.bithound.io/github/pushrocks/elasticlog) [![JavaScript Style Guide](https://img.shields.io/badge/code%20style-prettier-ff69b4.svg)](https://prettier.io/)
[![Known Vulnerabilities](https://snyk.io/test/npm/elasticlog/badge.svg)](https://snyk.io/test/npm/elasticlog)
[![TypeScript](https://img.shields.io/badge/TypeScript-2.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%206.x.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
## Usage ## Usage
@ -32,3 +26,14 @@ For further information read the linked docs at the top of this README.
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html) > | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
[![repo-footer](https://pushrocks.gitlab.io/assets/repo-footer.svg)](https://push.rocks) [![repo-footer](https://pushrocks.gitlab.io/assets/repo-footer.svg)](https://push.rocks)
## Contribute
We are always happy for code contributions. If you are not the code contributing type that is ok. + Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: [Contribute monthly :)](https://lossless.link/contribute)
For further information read the linked docs at the top of this readme.
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
[![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.svg)](https://maintainedby.lossless.com)

View File

@ -10,11 +10,9 @@ tap.test('first test', async () => {
testElasticLog = new elasticsearch.ElasticSearch({ testElasticLog = new elasticsearch.ElasticSearch({
indexPrefix: 'smartlog', indexPrefix: 'smartlog',
indexRetention: 7, indexRetention: 7,
domain: process.env.ELK_DOMAIN, domain: testQenv.getEnvVarOnDemand('ELK_DOMAIN'),
port: parseInt(process.env.ELK_PORT, 10), port: parseInt(testQenv.getEnvVarOnDemand('ELK_PORT'), 10),
ssl: true, ssl: true
user: process.env.ELK_USER,
pass: process.env.ELK_PASS
}); });
expect(testElasticLog).to.be.instanceOf(elasticsearch.ElasticSearch); expect(testElasticLog).to.be.instanceOf(elasticsearch.ElasticSearch);
}); });
@ -32,7 +30,7 @@ tap.test('should send a message to Elasticsearch', async () => {
runtime: 'node', runtime: 'node',
zone: 'ship.zone' zone: 'ship.zone'
}, },
message: 'hi, this is a testMessage' message: 'GET https://myroute.to.a.cool.destination/sorare?hello=there'
}); });
}); });

View File

@ -15,6 +15,7 @@ export class ElasticIndex {
public async ensureIndex(indexArg: string) { public async ensureIndex(indexArg: string) {
const done = plugins.smartpromise.defer(); const done = plugins.smartpromise.defer();
if (this.stringmap.checkString(indexArg)) { if (this.stringmap.checkString(indexArg)) {
done.resolve();
return; return;
} }
this.elasticSearchRef.client.cat.indices( this.elasticSearchRef.client.cat.indices(
@ -22,47 +23,36 @@ export class ElasticIndex {
format: 'json', format: 'json',
bytes: 'm' bytes: 'm'
}, },
async (err, response: any[]) => { async (err, responseArg: any[]) => {
if (err) {
console.log(err);
return;
}
// lets delete indexes that violate the retention // lets delete indexes that violate the retention
const filteredIndices = response.filter(indexObjectArg => { if (Array.isArray(responseArg)) {
const filteredIndices = responseArg.filter(indexObjectArg => {
return indexObjectArg.index.startsWith('smartlog'); return indexObjectArg.index.startsWith('smartlog');
}); });
const filteredIndexNames = filteredIndices.map(indexObjectArg => { const filteredIndexNames = filteredIndices.map(indexObjectArg => {
return indexObjectArg.name; return indexObjectArg.index;
}); });
const todayAsUnix: number = Date.now(); this.deleteOldIndices(filteredIndexNames);
const rententionPeriodAsUnix: number = plugins.smarttime.units.days(
this.elasticSearchRef.indexRetention
);
for (const indexName of filteredIndexNames) {
const regexResult = /^smartlog-([0-9]*)\.([0-9]*)\.([0-9]*)$/;
const dateAsUnix: number = new Date(
`${regexResult[1]}-${regexResult[2]}-${regexResult[3]}`
).getTime();
if (todayAsUnix - rententionPeriodAsUnix > dateAsUnix) {
const done2 = plugins.smartpromise.defer();
this.elasticSearchRef.client.indices.delete({
index: indexName
}, (err2, response2) => {
if(err2) {
console.log(err2);
}
done.resolve();
});
await done2.promise;
}
} }
// console.log(response); let index = null;
const index = response.find(indexObject => {
if (Array.isArray(responseArg)) {
index = responseArg.find(indexObject => {
return indexObject.index === indexArg; return indexObject.index === indexArg;
}); });
}
if (!index) { if (!index) {
const done2 = plugins.smartpromise.defer(); const done2 = plugins.smartpromise.defer();
this.elasticSearchRef.client.indices.create( this.elasticSearchRef.client.indices.create(
{ {
waitForActiveShards: '2', waitForActiveShards: '1',
index: indexArg index: indexArg
}, },
(error, response) => { (error, response) => {
@ -78,4 +68,36 @@ export class ElasticIndex {
); );
await done.promise; await done.promise;
} }
public createNewIndex(indexNameArg: string) {}
public async deleteOldIndices(indicesArray: string[]) {
const todayAsUnix: number = Date.now();
const rententionPeriodAsUnix: number = plugins.smarttime.units.days(
this.elasticSearchRef.indexRetention
);
for (const indexName of indicesArray) {
const regexResult = /^smartlog-([0-9]*)\.([0-9]*)\.([0-9]*)$/.exec(indexName);
const dateAsUnix: number = new Date(
`${regexResult[1]}-${regexResult[2]}-${regexResult[3]}`
).getTime();
if (todayAsUnix - rententionPeriodAsUnix > dateAsUnix) {
console.log(`found old index ${indexName}`);
const done2 = plugins.smartpromise.defer();
this.elasticSearchRef.client.indices.delete(
{
index: indexName
},
(err2, response2) => {
if (err2) {
console.log(err2);
}
console.log(`deleted ${indexName}`);
done2.resolve();
}
);
await done2.promise;
}
}
}
} }

View File

@ -35,8 +35,8 @@ export class ElasticSearch<T> {
*/ */
constructor(optionsArg: IElasticSearchConstructorOptions) { constructor(optionsArg: IElasticSearchConstructorOptions) {
this.client = new ElasticClient({ this.client = new ElasticClient({
host: this.computeHostString(optionsArg), host: this.computeHostString(optionsArg)
log: 'trace' // log: 'trace'
}); });
this.indexPrefix = optionsArg.indexPrefix; this.indexPrefix = optionsArg.indexPrefix;
this.indexRetention = optionsArg.indexRetention; this.indexRetention = optionsArg.indexRetention;
@ -56,15 +56,16 @@ export class ElasticSearch<T> {
} else { } else {
hostString = `http://${hostString}`; hostString = `http://${hostString}`;
} }
console.log(hostString);
return hostString; return hostString;
} }
public async log(logPackageArg: ILogPackage, scheduleOverwrite = false) { public async log(logPackageArg: ILogPackage, scheduleOverwrite = false) {
const now = new Date(); const now = new Date();
const indexToUse = `${this.indexPrefix}-${now.getFullYear()}.${('0' + (now.getMonth() + 1)).slice(-2)}.${( const indexToUse = `${this.indexPrefix}-${now.getFullYear()}.${(
'0' + now.getDate() '0' +
).slice(-2)}`; (now.getMonth() + 1)
).slice(-2)}.${('0' + now.getDate()).slice(-2)}`;
if (this.elasticScheduler.docsScheduled && !scheduleOverwrite) { if (this.elasticScheduler.docsScheduled && !scheduleOverwrite) {
this.elasticScheduler.scheduleDoc(logPackageArg); this.elasticScheduler.scheduleDoc(logPackageArg);
@ -88,7 +89,7 @@ export class ElasticSearch<T> {
console.log(error); console.log(error);
this.elasticScheduler.addFailedDoc(logPackageArg); this.elasticScheduler.addFailedDoc(logPackageArg);
} else { } else {
console.log(`ElasticLog: ${logPackageArg.message}`); // console.log(`ElasticLog: ${logPackageArg.message}`);
} }
} }
); );
@ -99,6 +100,6 @@ export class ElasticSearch<T> {
handleLog: (smartlogPackageArg: ILogPackage) => { handleLog: (smartlogPackageArg: ILogPackage) => {
this.log(smartlogPackageArg); this.log(smartlogPackageArg);
} }
} };
} }
} }

View File

@ -1,7 +1,8 @@
import * as elasticsearch from 'elasticsearch'; import * as elasticsearch from 'elasticsearch';
import * as lik from '@pushrocks/lik';
import * as smartdelay from '@pushrocks/smartdelay'; import * as smartdelay from '@pushrocks/smartdelay';
import * as smartlogInterfaces from '@pushrocks/smartlog-interfaces'; import * as smartlogInterfaces from '@pushrocks/smartlog-interfaces';
import * as smartpromise from '@pushrocks/smartpromise'; import * as smartpromise from '@pushrocks/smartpromise';
import * as smarttime from '@pushrocks/smarttime'; import * as smarttime from '@pushrocks/smarttime';
export { elasticsearch, smartdelay, smartlogInterfaces, smartpromise, smarttime }; export { elasticsearch, lik, smartdelay, smartlogInterfaces, smartpromise, smarttime };