Compare commits
No commits in common. "master" and "v1.0.29" have entirely different histories.
@ -1,66 +0,0 @@
|
||||
name: Default (not tags)
|
||||
|
||||
on:
|
||||
push:
|
||||
tags-ignore:
|
||||
- '**'
|
||||
|
||||
env:
|
||||
IMAGE: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@gitea.lossless.digital/${{gitea.repository}}.git
|
||||
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
|
||||
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
|
||||
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
|
||||
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
|
||||
|
||||
jobs:
|
||||
security:
|
||||
runs-on: ubuntu-latest
|
||||
continue-on-error: true
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Install pnpm and npmci
|
||||
run: |
|
||||
pnpm install -g pnpm
|
||||
pnpm install -g @shipzone/npmci
|
||||
|
||||
- name: Run npm prepare
|
||||
run: npmci npm prepare
|
||||
|
||||
- name: Audit production dependencies
|
||||
run: |
|
||||
npmci command npm config set registry https://registry.npmjs.org
|
||||
npmci command pnpm audit --audit-level=high --prod
|
||||
continue-on-error: true
|
||||
|
||||
- name: Audit development dependencies
|
||||
run: |
|
||||
npmci command npm config set registry https://registry.npmjs.org
|
||||
npmci command pnpm audit --audit-level=high --dev
|
||||
continue-on-error: true
|
||||
|
||||
test:
|
||||
if: ${{ always() }}
|
||||
needs: security
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Test stable
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
npmci npm test
|
||||
|
||||
- name: Test build
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
npmci npm build
|
@ -1,124 +0,0 @@
|
||||
name: Default (tags)
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- '*'
|
||||
|
||||
env:
|
||||
IMAGE: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@gitea.lossless.digital/${{gitea.repository}}.git
|
||||
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
|
||||
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
|
||||
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
|
||||
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
|
||||
|
||||
jobs:
|
||||
security:
|
||||
runs-on: ubuntu-latest
|
||||
continue-on-error: true
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Prepare
|
||||
run: |
|
||||
pnpm install -g pnpm
|
||||
pnpm install -g @shipzone/npmci
|
||||
npmci npm prepare
|
||||
|
||||
- name: Audit production dependencies
|
||||
run: |
|
||||
npmci command npm config set registry https://registry.npmjs.org
|
||||
npmci command pnpm audit --audit-level=high --prod
|
||||
continue-on-error: true
|
||||
|
||||
- name: Audit development dependencies
|
||||
run: |
|
||||
npmci command npm config set registry https://registry.npmjs.org
|
||||
npmci command pnpm audit --audit-level=high --dev
|
||||
continue-on-error: true
|
||||
|
||||
test:
|
||||
if: ${{ always() }}
|
||||
needs: security
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Prepare
|
||||
run: |
|
||||
pnpm install -g pnpm
|
||||
pnpm install -g @shipzone/npmci
|
||||
npmci npm prepare
|
||||
|
||||
- name: Test stable
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
npmci npm test
|
||||
|
||||
- name: Test build
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
npmci npm build
|
||||
|
||||
release:
|
||||
needs: test
|
||||
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Prepare
|
||||
run: |
|
||||
pnpm install -g pnpm
|
||||
pnpm install -g @shipzone/npmci
|
||||
npmci npm prepare
|
||||
|
||||
- name: Release
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm publish
|
||||
|
||||
metadata:
|
||||
needs: test
|
||||
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
continue-on-error: true
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Prepare
|
||||
run: |
|
||||
pnpm install -g pnpm
|
||||
pnpm install -g @shipzone/npmci
|
||||
npmci npm prepare
|
||||
|
||||
- name: Code quality
|
||||
run: |
|
||||
npmci command npm install -g typescript
|
||||
npmci npm install
|
||||
|
||||
- name: Trigger
|
||||
run: npmci trigger
|
||||
|
||||
- name: Build docs and upload artifacts
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
pnpm install -g @gitzone/tsdoc
|
||||
npmci command tsdoc
|
||||
continue-on-error: true
|
4
.gitignore
vendored
4
.gitignore
vendored
@ -15,6 +15,8 @@ node_modules/
|
||||
|
||||
# builds
|
||||
dist/
|
||||
dist_*/
|
||||
dist_web/
|
||||
dist_serve/
|
||||
dist_ts_web/
|
||||
|
||||
# custom
|
@ -12,38 +12,29 @@ stages:
|
||||
- release
|
||||
- metadata
|
||||
|
||||
before_script:
|
||||
- pnpm install -g pnpm
|
||||
- pnpm install -g @shipzone/npmci
|
||||
- npmci npm prepare
|
||||
|
||||
# ====================
|
||||
# security stage
|
||||
# ====================
|
||||
# ====================
|
||||
# security stage
|
||||
# ====================
|
||||
auditProductionDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
mirror:
|
||||
stage: security
|
||||
script:
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command pnpm audit --audit-level=high --prod
|
||||
- npmci git mirror
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
allow_failure: true
|
||||
- notpriv
|
||||
|
||||
auditDevDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
snyk:
|
||||
stage: security
|
||||
script:
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command pnpm audit --audit-level=high --dev
|
||||
- npmci npm prepare
|
||||
- npmci command npm install -g snyk
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command snyk test
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
allow_failure: true
|
||||
- notpriv
|
||||
|
||||
# ====================
|
||||
# test stage
|
||||
@ -52,24 +43,33 @@ auditDevDependencies:
|
||||
testStable:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- priv
|
||||
|
||||
testBuild:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm build
|
||||
- npmci command npm run build
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
release:
|
||||
stage: release
|
||||
script:
|
||||
- npmci node install stable
|
||||
- npmci npm publish
|
||||
only:
|
||||
- tags
|
||||
@ -84,12 +84,11 @@ release:
|
||||
codequality:
|
||||
stage: metadata
|
||||
allow_failure: true
|
||||
only:
|
||||
- tags
|
||||
script:
|
||||
- npmci command npm install -g typescript
|
||||
- npmci command npm install -g tslint typescript
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
@ -109,9 +108,11 @@ trigger:
|
||||
pages:
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci node install stable
|
||||
- npmci node install lts
|
||||
- npmci command npm install -g @gitzone/tsdoc
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command npm run buildDocs
|
||||
- npmci command tsdoc
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
|
4
.snyk
Normal file
4
.snyk
Normal file
@ -0,0 +1,4 @@
|
||||
# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities.
|
||||
version: v1.12.0
|
||||
ignore: {}
|
||||
patch: {}
|
11
.vscode/launch.json
vendored
11
.vscode/launch.json
vendored
@ -1,11 +0,0 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"command": "npm test",
|
||||
"name": "Run npm test",
|
||||
"request": "launch",
|
||||
"type": "node-terminal"
|
||||
}
|
||||
]
|
||||
}
|
26
.vscode/settings.json
vendored
26
.vscode/settings.json
vendored
@ -1,26 +0,0 @@
|
||||
{
|
||||
"json.schemas": [
|
||||
{
|
||||
"fileMatch": ["/npmextra.json"],
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"npmci": {
|
||||
"type": "object",
|
||||
"description": "settings for npmci"
|
||||
},
|
||||
"gitzone": {
|
||||
"type": "object",
|
||||
"description": "settings for gitzone",
|
||||
"properties": {
|
||||
"projectType": {
|
||||
"type": "string",
|
||||
"enum": ["website", "element", "service", "npm", "wcc"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
@ -4,12 +4,11 @@
|
||||
"npmAccessLevel": "public"
|
||||
},
|
||||
"gitzone": {
|
||||
"projectType": "npm",
|
||||
"module": {
|
||||
"githost": "gitlab.com",
|
||||
"gitscope": "mojoio",
|
||||
"gitrepo": "elasticsearch",
|
||||
"description": "log to elasticsearch in a kibana compatible format",
|
||||
"shortDescription": "log to elasticsearch in a kibana compatible format",
|
||||
"npmPackagename": "@mojoio/elasticsearch",
|
||||
"license": "MIT"
|
||||
}
|
||||
|
1805
package-lock.json
generated
Normal file
1805
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
45
package.json
45
package.json
@ -1,48 +1,45 @@
|
||||
{
|
||||
"name": "@apiclient.xyz/elasticsearch",
|
||||
"version": "2.0.16",
|
||||
"name": "@mojoio/elasticsearch",
|
||||
"version": "1.0.29",
|
||||
"private": false,
|
||||
"description": "log to elasticsearch in a kibana compatible format",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"main": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
"author": "Lossless GmbH",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"test": "(tstest test/)",
|
||||
"format": "(gitzone format)",
|
||||
"build": "(tsbuild --allowimplicitany)",
|
||||
"buildDocs": "tsdoc"
|
||||
"build": "(tsbuild)"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@git.zone/tsbuild": "^2.1.70",
|
||||
"@git.zone/tsrun": "^1.2.46",
|
||||
"@git.zone/tstest": "^1.0.80",
|
||||
"@push.rocks/qenv": "^6.0.2",
|
||||
"@push.rocks/tapbundle": "^5.0.15",
|
||||
"@types/node": "^20.5.7"
|
||||
"@gitzone/tsbuild": "^2.1.17",
|
||||
"@gitzone/tsrun": "^1.2.8",
|
||||
"@gitzone/tstest": "^1.0.28",
|
||||
"@pushrocks/qenv": "^4.0.6",
|
||||
"@pushrocks/tapbundle": "^3.0.13",
|
||||
"@types/node": "^12.12.5",
|
||||
"tslint": "^5.20.0",
|
||||
"tslint-config-prettier": "^1.18.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@elastic/elasticsearch": "8.9.0",
|
||||
"@push.rocks/lik": "^6.0.5",
|
||||
"@push.rocks/smartdelay": "^3.0.5",
|
||||
"@push.rocks/smartlog-interfaces": "^3.0.0",
|
||||
"@push.rocks/smartpromise": "^4.0.2",
|
||||
"@push.rocks/smarttime": "^4.0.5"
|
||||
"@pushrocks/lik": "^3.0.11",
|
||||
"@pushrocks/smartdelay": "^2.0.3",
|
||||
"@pushrocks/smartlog-interfaces": "^2.0.9",
|
||||
"@pushrocks/smartpromise": "^3.0.6",
|
||||
"@pushrocks/smarttime": "^3.0.12",
|
||||
"@types/elasticsearch": "^5.0.35",
|
||||
"elasticsearch": "^16.5.0"
|
||||
},
|
||||
"files": [
|
||||
"ts/**/*",
|
||||
"ts_web/**/*",
|
||||
"dist/**/*",
|
||||
"dist_*/**/*",
|
||||
"dist_ts/**/*",
|
||||
"dist_web/**/*",
|
||||
"dist_ts_web/**/*",
|
||||
"assets/**/*",
|
||||
"cli.js",
|
||||
"npmextra.json",
|
||||
"readme.md"
|
||||
],
|
||||
"type": "module",
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
]
|
||||
}
|
||||
|
5870
pnpm-lock.yaml
generated
5870
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
33
readme.md
33
readme.md
@ -8,19 +8,13 @@ log to elasticsearch in a kibana compatible format
|
||||
* [docs (typedoc)](https://mojoio.gitlab.io/elasticsearch/)
|
||||
|
||||
## Status for master
|
||||
|
||||
Status Category | Status Badge
|
||||
-- | --
|
||||
GitLab Pipelines | [](https://lossless.cloud)
|
||||
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||
npm | [](https://lossless.cloud)
|
||||
Snyk | [](https://lossless.cloud)
|
||||
TypeScript Support | [](https://lossless.cloud)
|
||||
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
Code Style | [](https://lossless.cloud)
|
||||
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
||||
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
||||
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
||||
[](https://gitlab.com/mojoio/elasticsearch/commits/master)
|
||||
[](https://gitlab.com/mojoio/elasticsearch/commits/master)
|
||||
[](https://www.npmjs.com/package/@mojoio/elasticsearch)
|
||||
[](https://snyk.io/test/npm/@mojoio/elasticsearch)
|
||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
[](https://prettier.io/)
|
||||
|
||||
## Usage
|
||||
|
||||
@ -33,16 +27,13 @@ For further information read the linked docs at the top of this README.
|
||||
|
||||
[](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)
|
||||
|
||||
## Contribution
|
||||
|
||||
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: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/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.
|
||||
|
||||
## Legal
|
||||
> MIT licensed | **©** [Task Venture Capital GmbH](https://task.vc)
|
||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||
|
||||
[](https://maintainedby.lossless.com)
|
||||
|
@ -1,89 +0,0 @@
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
import { Qenv } from '@push.rocks/qenv';
|
||||
import * as elasticsearch from '../ts/index.js';
|
||||
|
||||
let testElasticLog: elasticsearch.ElsSmartlogDestination<any>;
|
||||
let testElasticDoc: elasticsearch.ElasticDoc;
|
||||
|
||||
tap.test('first test', async () => {
|
||||
testElasticLog = new elasticsearch.ElsSmartlogDestination({
|
||||
indexPrefix: 'testprefix',
|
||||
indexRetention: 7,
|
||||
node: 'http://localhost:9200',
|
||||
auth: {
|
||||
username: 'elastic',
|
||||
password: 'YourPassword'
|
||||
}
|
||||
});
|
||||
expect(testElasticLog).toBeInstanceOf(elasticsearch.ElsSmartlogDestination);
|
||||
});
|
||||
|
||||
tap.test('should send a message to Elasticsearch', async () => {
|
||||
await testElasticLog.log({
|
||||
timestamp: Date.now(),
|
||||
type: 'increment',
|
||||
level: 'info',
|
||||
context: {
|
||||
company: 'Lossless GmbH',
|
||||
companyunit: 'lossless.cloud',
|
||||
containerName: 'testcontainer',
|
||||
environment: 'test',
|
||||
runtime: 'node',
|
||||
zone: 'ship.zone',
|
||||
},
|
||||
message: 'GET https://myroute.to.a.cool.destination/sorare?hello=there',
|
||||
correlation: null,
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('should create an ElasticDoc instance', async () => {
|
||||
testElasticDoc = new elasticsearch.ElasticDoc({
|
||||
index: 'testindex',
|
||||
node: 'http://localhost:9200',
|
||||
auth: {
|
||||
username: 'elastic',
|
||||
password: 'YourPassword'
|
||||
}
|
||||
});
|
||||
expect(testElasticDoc).toBeInstanceOf(elasticsearch.ElasticDoc);
|
||||
});
|
||||
|
||||
tap.test('should add and update documents in a piping session', async () => {
|
||||
await testElasticDoc.startPipingSession({});
|
||||
await testElasticDoc.pipeDocument({
|
||||
docId: '1',
|
||||
timestamp: new Date().toISOString(),
|
||||
doc: { name: 'doc1' }
|
||||
});
|
||||
await testElasticDoc.pipeDocument({
|
||||
docId: '2',
|
||||
timestamp: new Date().toISOString(),
|
||||
doc: { name: 'doc2' }
|
||||
});
|
||||
await testElasticDoc.pipeDocument({
|
||||
docId: '1',
|
||||
timestamp: new Date().toISOString(),
|
||||
doc: { name: 'updated doc1' }
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('should delete documents not part of the piping session', async () => {
|
||||
await testElasticDoc.endPipingSession();
|
||||
});
|
||||
|
||||
tap.test('should take and store snapshot', async () => {
|
||||
await testElasticDoc.takeSnapshot(async (iterator, prevSnapshot) => {
|
||||
const aggregationData = [];
|
||||
for await (const doc of iterator) {
|
||||
// Sample aggregation: counting documents
|
||||
aggregationData.push(doc);
|
||||
}
|
||||
const snapshot = {
|
||||
date: new Date().toISOString(),
|
||||
aggregationData,
|
||||
};
|
||||
return snapshot;
|
||||
});
|
||||
});
|
||||
|
||||
tap.start();
|
37
test/test.ts
Normal file
37
test/test.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import { Qenv } from '@pushrocks/qenv';
|
||||
import * as elasticsearch from '../ts/index';
|
||||
|
||||
const testQenv = new Qenv('./', './.nogit/');
|
||||
|
||||
let testElasticLog: elasticsearch.ElasticSearch<any>;
|
||||
|
||||
tap.test('first test', async () => {
|
||||
testElasticLog = new elasticsearch.ElasticSearch({
|
||||
indexPrefix: 'smartlog',
|
||||
indexRetention: 7,
|
||||
domain: testQenv.getEnvVarOnDemand('ELK_DOMAIN'),
|
||||
port: parseInt(testQenv.getEnvVarOnDemand('ELK_PORT'), 10),
|
||||
ssl: true
|
||||
});
|
||||
expect(testElasticLog).to.be.instanceOf(elasticsearch.ElasticSearch);
|
||||
});
|
||||
|
||||
tap.test('should send a message to Elasticsearch', async () => {
|
||||
testElasticLog.log({
|
||||
timestamp: Date.now(),
|
||||
type: 'increment',
|
||||
level: 'info',
|
||||
context: {
|
||||
company: 'Lossless GmbH',
|
||||
companyunit: 'lossless.cloud',
|
||||
containerName: 'testcontainer',
|
||||
environment: 'test',
|
||||
runtime: 'node',
|
||||
zone: 'ship.zone'
|
||||
},
|
||||
message: 'GET https://myroute.to.a.cool.destination/sorare?hello=there'
|
||||
});
|
||||
});
|
||||
|
||||
tap.start();
|
@ -1,8 +0,0 @@
|
||||
/**
|
||||
* autocreated commitinfo by @pushrocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@apiclient.xyz/elasticsearch',
|
||||
version: '2.0.16',
|
||||
description: 'log to elasticsearch in a kibana compatible format'
|
||||
}
|
103
ts/elasticsearch.classes.elasticindex.ts
Normal file
103
ts/elasticsearch.classes.elasticindex.ts
Normal file
@ -0,0 +1,103 @@
|
||||
import * as plugins from './elasticsearch.plugins';
|
||||
import { ElasticSearch } from './elasticsearch.classes.elasticsearch';
|
||||
import { ILogPackage } from '@pushrocks/smartlog-interfaces';
|
||||
|
||||
import { Stringmap } from '@pushrocks/lik';
|
||||
|
||||
export class ElasticIndex {
|
||||
private stringmap = new Stringmap();
|
||||
private elasticSearchRef: ElasticSearch<any>;
|
||||
|
||||
constructor(elasticSearchInstanceArg: ElasticSearch<ILogPackage>) {
|
||||
this.elasticSearchRef = elasticSearchInstanceArg;
|
||||
}
|
||||
|
||||
public async ensureIndex(indexArg: string) {
|
||||
const done = plugins.smartpromise.defer();
|
||||
if (this.stringmap.checkString(indexArg)) {
|
||||
done.resolve();
|
||||
return;
|
||||
}
|
||||
this.elasticSearchRef.client.cat.indices(
|
||||
{
|
||||
format: 'json',
|
||||
bytes: 'm'
|
||||
},
|
||||
async (err, responseArg: any[]) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
return;
|
||||
}
|
||||
|
||||
// lets delete indexes that violate the retention
|
||||
if (Array.isArray(responseArg)) {
|
||||
const filteredIndices = responseArg.filter(indexObjectArg => {
|
||||
return indexObjectArg.index.startsWith('smartlog');
|
||||
});
|
||||
const filteredIndexNames = filteredIndices.map(indexObjectArg => {
|
||||
return indexObjectArg.index;
|
||||
});
|
||||
this.deleteOldIndices(filteredIndexNames);
|
||||
}
|
||||
|
||||
let index = null;
|
||||
|
||||
if (Array.isArray(responseArg)) {
|
||||
index = responseArg.find(indexObject => {
|
||||
return indexObject.index === indexArg;
|
||||
});
|
||||
}
|
||||
|
||||
if (!index) {
|
||||
const done2 = plugins.smartpromise.defer();
|
||||
this.elasticSearchRef.client.indices.create(
|
||||
{
|
||||
waitForActiveShards: '1',
|
||||
index: indexArg
|
||||
},
|
||||
(error, response) => {
|
||||
// console.lof(response)
|
||||
done2.resolve();
|
||||
}
|
||||
);
|
||||
await done2.promise;
|
||||
}
|
||||
this.stringmap.addString(indexArg);
|
||||
done.resolve();
|
||||
}
|
||||
);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
44
ts/elasticsearch.classes.elasticscheduler.ts
Normal file
44
ts/elasticsearch.classes.elasticscheduler.ts
Normal file
@ -0,0 +1,44 @@
|
||||
import { ElasticSearch, IStandardLogParams } from './elasticsearch.classes.elasticsearch';
|
||||
|
||||
export class ElasticScheduler {
|
||||
elasticSearchRef: ElasticSearch<any>;
|
||||
docsScheduled = false;
|
||||
docsStorage: any[] = [];
|
||||
|
||||
constructor(elasticLogRefArg: ElasticSearch<any>) {
|
||||
this.elasticSearchRef = elasticLogRefArg;
|
||||
}
|
||||
|
||||
public addFailedDoc(objectArg: any | IStandardLogParams) {
|
||||
this.docsStorage.push(objectArg);
|
||||
this.setRetry();
|
||||
}
|
||||
public scheduleDoc(logObject: any) {
|
||||
this.docsStorage.push(logObject);
|
||||
}
|
||||
|
||||
public setRetry() {
|
||||
setTimeout(() => {
|
||||
const oldStorage = this.docsStorage;
|
||||
this.docsStorage = [];
|
||||
for (let logObject of oldStorage) {
|
||||
this.elasticSearchRef.log(logObject, true);
|
||||
}
|
||||
if (this.docsStorage.length === 0) {
|
||||
console.log('ElasticLog retry success!!!');
|
||||
this.docsScheduled = false;
|
||||
} else {
|
||||
console.log('ElasticLog retry failed');
|
||||
this.setRetry();
|
||||
}
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
public deferSend() {
|
||||
if (!this.docsScheduled) {
|
||||
console.log('Retry ElasticLog in 5 seconds!');
|
||||
this.docsScheduled = true;
|
||||
this.setRetry();
|
||||
}
|
||||
}
|
||||
}
|
105
ts/elasticsearch.classes.elasticsearch.ts
Normal file
105
ts/elasticsearch.classes.elasticsearch.ts
Normal file
@ -0,0 +1,105 @@
|
||||
// interfaces
|
||||
import { Client as ElasticClient } from 'elasticsearch';
|
||||
import { ILogContext, ILogPackage, ILogDestination } from '@pushrocks/smartlog-interfaces';
|
||||
|
||||
// other classes
|
||||
import { ElasticScheduler } from './elasticsearch.classes.elasticscheduler';
|
||||
import { ElasticIndex } from './elasticsearch.classes.elasticindex';
|
||||
|
||||
export interface IStandardLogParams {
|
||||
message: string;
|
||||
severity: string;
|
||||
}
|
||||
|
||||
export interface IElasticSearchConstructorOptions {
|
||||
indexPrefix: string;
|
||||
indexRetention: number;
|
||||
port: number;
|
||||
domain: string;
|
||||
ssl: boolean;
|
||||
user?: string;
|
||||
pass?: string;
|
||||
}
|
||||
|
||||
export class ElasticSearch<T> {
|
||||
public client: ElasticClient;
|
||||
public elasticScheduler = new ElasticScheduler(this);
|
||||
public elasticIndex: ElasticIndex = new ElasticIndex(this);
|
||||
|
||||
public indexPrefix: string;
|
||||
public indexRetention: number;
|
||||
|
||||
/**
|
||||
* sets up an instance of Elastic log
|
||||
* @param optionsArg
|
||||
*/
|
||||
constructor(optionsArg: IElasticSearchConstructorOptions) {
|
||||
this.client = new ElasticClient({
|
||||
host: this.computeHostString(optionsArg)
|
||||
// log: 'trace'
|
||||
});
|
||||
this.indexPrefix = optionsArg.indexPrefix;
|
||||
this.indexRetention = optionsArg.indexRetention;
|
||||
}
|
||||
|
||||
/**
|
||||
* computes the host string from the constructor options
|
||||
* @param optionsArg
|
||||
*/
|
||||
private computeHostString(optionsArg: IElasticSearchConstructorOptions): string {
|
||||
let hostString = `${optionsArg.domain}:${optionsArg.port}`;
|
||||
if (optionsArg.user && optionsArg.pass) {
|
||||
hostString = `${optionsArg.user}:${optionsArg.pass}@${hostString}`;
|
||||
}
|
||||
if (optionsArg.ssl) {
|
||||
hostString = `https://${hostString}`;
|
||||
} else {
|
||||
hostString = `http://${hostString}`;
|
||||
}
|
||||
console.log(hostString);
|
||||
return hostString;
|
||||
}
|
||||
|
||||
public async log(logPackageArg: ILogPackage, scheduleOverwrite = false) {
|
||||
const now = new Date();
|
||||
const indexToUse = `${this.indexPrefix}-${now.getFullYear()}.${(
|
||||
'0' +
|
||||
(now.getMonth() + 1)
|
||||
).slice(-2)}.${('0' + now.getDate()).slice(-2)}`;
|
||||
|
||||
if (this.elasticScheduler.docsScheduled && !scheduleOverwrite) {
|
||||
this.elasticScheduler.scheduleDoc(logPackageArg);
|
||||
return;
|
||||
}
|
||||
|
||||
await this.elasticIndex.ensureIndex(indexToUse);
|
||||
|
||||
this.client.index(
|
||||
{
|
||||
index: indexToUse,
|
||||
type: 'log',
|
||||
body: {
|
||||
'@timestamp': new Date(logPackageArg.timestamp).toISOString(),
|
||||
...logPackageArg
|
||||
}
|
||||
},
|
||||
(error, response) => {
|
||||
if (error) {
|
||||
console.log('ElasticLog encountered an error:');
|
||||
console.log(error);
|
||||
this.elasticScheduler.addFailedDoc(logPackageArg);
|
||||
} else {
|
||||
// console.log(`ElasticLog: ${logPackageArg.message}`);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
get logDestination(): ILogDestination {
|
||||
return {
|
||||
handleLog: (smartlogPackageArg: ILogPackage) => {
|
||||
this.log(smartlogPackageArg);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
8
ts/elasticsearch.plugins.ts
Normal file
8
ts/elasticsearch.plugins.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import * as elasticsearch from 'elasticsearch';
|
||||
import * as lik from '@pushrocks/lik';
|
||||
import * as smartdelay from '@pushrocks/smartdelay';
|
||||
import * as smartlogInterfaces from '@pushrocks/smartlog-interfaces';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as smarttime from '@pushrocks/smarttime';
|
||||
|
||||
export { elasticsearch, lik, smartdelay, smartlogInterfaces, smartpromise, smarttime };
|
@ -1,249 +0,0 @@
|
||||
import { Client as ElasticClient } from '@elastic/elasticsearch';
|
||||
|
||||
export interface IElasticDocConstructorOptions {
|
||||
index: string;
|
||||
node: string;
|
||||
auth?: {
|
||||
username: string;
|
||||
password: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface ISnapshot {
|
||||
date: string;
|
||||
aggregationData: any;
|
||||
}
|
||||
|
||||
export type SnapshotProcessor = (
|
||||
iterator: AsyncIterable<any>,
|
||||
prevSnapshot: ISnapshot | null
|
||||
) => Promise<ISnapshot>;
|
||||
|
||||
export class ElasticDoc {
|
||||
public client: ElasticClient;
|
||||
public index: string;
|
||||
private sessionDocs: Set<string> = new Set();
|
||||
private indexInitialized: boolean = false;
|
||||
private latestTimestamp: string | null = null; // Store the latest timestamp
|
||||
private onlyNew: boolean = false; // Whether to only pipe new docs
|
||||
public fastForward: boolean = false; // Whether to fast forward to the latest timestamp
|
||||
|
||||
private BATCH_SIZE = 1000;
|
||||
|
||||
constructor(options: IElasticDocConstructorOptions) {
|
||||
this.client = new ElasticClient({
|
||||
node: options.node,
|
||||
...(options.auth && { auth: options.auth }),
|
||||
});
|
||||
this.index = options.index;
|
||||
}
|
||||
|
||||
private async ensureIndexExists(doc: any) {
|
||||
if (!this.indexInitialized) {
|
||||
const indexExists = await this.client.indices.exists({ index: this.index });
|
||||
if (!indexExists) {
|
||||
const mappings = this.createMappingsFromDoc(doc);
|
||||
await this.client.indices.create({
|
||||
index: this.index,
|
||||
body: {
|
||||
// mappings,
|
||||
settings: {
|
||||
// You can define the settings according to your requirements here
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
this.indexInitialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
private createMappingsFromDoc(doc: any): any {
|
||||
const properties: any = {};
|
||||
for (const key in doc) {
|
||||
if (key === '@timestamp') {
|
||||
properties[key] = { type: 'date' };
|
||||
continue;
|
||||
}
|
||||
properties[key] = { type: typeof doc[key] === 'number' ? 'float' : 'text' };
|
||||
}
|
||||
return { properties };
|
||||
}
|
||||
|
||||
async startPipingSession(options: { onlyNew?: boolean }) {
|
||||
this.sessionDocs.clear();
|
||||
this.onlyNew = options.onlyNew;
|
||||
const indexExists = await this.client.indices.exists({ index: this.index });
|
||||
if (this.onlyNew && indexExists) {
|
||||
const response = await this.client.search({
|
||||
index: this.index,
|
||||
sort: '@timestamp:desc',
|
||||
size: 1,
|
||||
});
|
||||
|
||||
// If the search query succeeded, the index exists.
|
||||
const hit = response.hits.hits[0];
|
||||
this.latestTimestamp = hit?._source?.['@timestamp'] || null;
|
||||
|
||||
if (this.latestTimestamp) {
|
||||
console.log(`Working in "onlyNew" mode. Hence we are omitting documents prior to ${this.latestTimestamp}`);
|
||||
} else {
|
||||
console.log(`Working in "onlyNew" mode, but no documents found in index ${this.index}. Hence processing all documents now.`);
|
||||
}
|
||||
} else if (this.onlyNew && !indexExists) {
|
||||
console.log(`Working in "onlyNew" mode, but index ${this.index} does not exist. Hence processing all documents now.`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
async pipeDocument(optionsArg: { docId: string; timestamp?: string | number; doc: any }) {
|
||||
await this.ensureIndexExists(optionsArg.doc);
|
||||
|
||||
const documentBody = {
|
||||
...optionsArg.doc,
|
||||
...(optionsArg.timestamp && { '@timestamp': optionsArg.timestamp }),
|
||||
};
|
||||
|
||||
// If 'onlyNew' is true, compare the document timestamp with the latest timestamp
|
||||
if (this.onlyNew) {
|
||||
if (this.latestTimestamp && optionsArg.timestamp <= this.latestTimestamp) {
|
||||
this.fastForward = true;
|
||||
} else {
|
||||
this.fastForward = false;
|
||||
await this.client.index({
|
||||
index: this.index,
|
||||
id: optionsArg.docId,
|
||||
body: documentBody,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
this.fastForward = false;
|
||||
await this.client.index({
|
||||
index: this.index,
|
||||
id: optionsArg.docId,
|
||||
body: documentBody,
|
||||
});
|
||||
}
|
||||
this.sessionDocs.add(optionsArg.docId);
|
||||
}
|
||||
|
||||
async endPipingSession() {
|
||||
const allDocIds: string[] = [];
|
||||
const responseQueue = [];
|
||||
let response = await this.client.search({
|
||||
index: this.index,
|
||||
scroll: '1m',
|
||||
size: this.BATCH_SIZE,
|
||||
});
|
||||
while (true) {
|
||||
response.hits.hits.forEach((hit: any) => allDocIds.push(hit._id));
|
||||
if (!response.hits.hits.length) {
|
||||
break;
|
||||
}
|
||||
response = await this.client.scroll({ scroll_id: response._scroll_id, scroll: '1m' });
|
||||
}
|
||||
|
||||
for (const docId of allDocIds) {
|
||||
if (!this.sessionDocs.has(docId)) {
|
||||
responseQueue.push({
|
||||
delete: {
|
||||
_index: this.index,
|
||||
_id: docId,
|
||||
},
|
||||
});
|
||||
|
||||
if (responseQueue.length >= this.BATCH_SIZE) {
|
||||
await this.client.bulk({ refresh: true, body: responseQueue });
|
||||
responseQueue.length = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (responseQueue.length > 0) {
|
||||
await this.client.bulk({ refresh: true, body: responseQueue });
|
||||
}
|
||||
|
||||
this.sessionDocs.clear();
|
||||
}
|
||||
|
||||
async takeSnapshot(processIterator: SnapshotProcessor) {
|
||||
const snapshotIndex = `${this.index}_snapshots`;
|
||||
|
||||
const indexExists = await this.client.indices.exists({ index: snapshotIndex });
|
||||
if (!indexExists) {
|
||||
await this.client.indices.create({
|
||||
index: snapshotIndex,
|
||||
body: {
|
||||
mappings: {
|
||||
properties: {
|
||||
date: {
|
||||
type: 'date',
|
||||
},
|
||||
aggregationData: {
|
||||
type: 'object',
|
||||
enabled: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const documentIterator = this.getDocumentIterator();
|
||||
|
||||
const newSnapshot = await processIterator(documentIterator, await this.getLastSnapshot());
|
||||
|
||||
await this.storeSnapshot(newSnapshot);
|
||||
}
|
||||
|
||||
private async getLastSnapshot(): Promise<ISnapshot | null> {
|
||||
const snapshotIndex = `${this.index}_snapshots`;
|
||||
const indexExists = await this.client.indices.exists({ index: snapshotIndex });
|
||||
|
||||
if (!indexExists) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const response = await this.client.search({
|
||||
index: snapshotIndex,
|
||||
sort: 'date:desc',
|
||||
size: 1,
|
||||
});
|
||||
|
||||
if (response.hits.hits.length > 0) {
|
||||
const hit = response.hits.hits[0];
|
||||
return {
|
||||
date: hit._source['date'],
|
||||
aggregationData: hit._source['aggregationData'],
|
||||
};
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private async *getDocumentIterator() {
|
||||
let response = await this.client.search({
|
||||
index: this.index,
|
||||
scroll: '1m',
|
||||
size: this.BATCH_SIZE,
|
||||
});
|
||||
while (true) {
|
||||
for (const hit of response.hits.hits) {
|
||||
yield hit._source;
|
||||
}
|
||||
|
||||
if (!response.hits.hits.length) {
|
||||
break;
|
||||
}
|
||||
|
||||
response = await this.client.scroll({ scroll_id: response._scroll_id, scroll: '1m' });
|
||||
}
|
||||
}
|
||||
|
||||
private async storeSnapshot(snapshot: ISnapshot) {
|
||||
await this.client.index({
|
||||
index: `${this.index}_snapshots`,
|
||||
body: snapshot,
|
||||
});
|
||||
}
|
||||
}
|
@ -1,103 +0,0 @@
|
||||
import * as plugins from './els.plugins.js';
|
||||
import { ElsSmartlogDestination } from './els.classes.smartlogdestination.js';
|
||||
import { type ILogPackage } from '@push.rocks/smartlog-interfaces';
|
||||
import { Stringmap } from '@push.rocks/lik';
|
||||
|
||||
export class ElasticIndex {
|
||||
private stringmap = new Stringmap();
|
||||
private elasticSearchRef: ElsSmartlogDestination<any>;
|
||||
|
||||
constructor(elasticSearchInstanceArg: ElsSmartlogDestination<ILogPackage>) {
|
||||
this.elasticSearchRef = elasticSearchInstanceArg;
|
||||
}
|
||||
|
||||
public async ensureIndex(prefixArg: string, indexNameArg: string) {
|
||||
if (this.stringmap.checkString(indexNameArg)) {
|
||||
return indexNameArg;
|
||||
}
|
||||
|
||||
const responseArg = await this.elasticSearchRef.client.cat.indices({
|
||||
format: 'json',
|
||||
bytes: 'mb',
|
||||
}).catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
|
||||
if (!responseArg) {
|
||||
throw new Error('Could not get valid response from elastic search');
|
||||
}
|
||||
|
||||
if (Array.isArray(responseArg)) {
|
||||
const filteredIndices = responseArg.filter((indexObjectArg) => {
|
||||
return indexObjectArg.index.startsWith(prefixArg);
|
||||
});
|
||||
const filteredIndexNames = filteredIndices.map((indexObjectArg) => {
|
||||
return indexObjectArg.index;
|
||||
});
|
||||
await this.deleteOldIndices(prefixArg, filteredIndexNames);
|
||||
}
|
||||
|
||||
let index = null;
|
||||
|
||||
if (Array.isArray(responseArg)) {
|
||||
index = responseArg.find((indexItemArg) => {
|
||||
return indexItemArg.index === indexNameArg;
|
||||
});
|
||||
}
|
||||
|
||||
if (!index) {
|
||||
await this.createNewIndex(indexNameArg);
|
||||
}
|
||||
|
||||
this.stringmap.addString(indexNameArg);
|
||||
return index;
|
||||
}
|
||||
|
||||
public async createNewIndex(indexNameArg: string) {
|
||||
const response = await this.elasticSearchRef.client.indices.create({
|
||||
wait_for_active_shards: 1,
|
||||
index: indexNameArg,
|
||||
body: {
|
||||
mappings: {
|
||||
properties: {
|
||||
'@timestamp': {
|
||||
type: 'date',
|
||||
},
|
||||
logPackageArg: {
|
||||
properties: {
|
||||
payload: {
|
||||
type: 'object',
|
||||
dynamic: true
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
public async deleteOldIndices(prefixArg: string, indicesArray: string[]) {
|
||||
const todayAsUnix: number = Date.now();
|
||||
const rententionPeriodAsUnix: number = plugins.smarttime.units.days(
|
||||
this.elasticSearchRef.indexRetention
|
||||
);
|
||||
for (const indexName of indicesArray) {
|
||||
if (!indexName.startsWith(prefixArg)) continue;
|
||||
const indexRegex = new RegExp(`^${prefixArg}-([0-9]*)-([0-9]*)-([0-9]*)$`)
|
||||
const regexResult = indexRegex.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 response = await this.elasticSearchRef.client.indices.delete(
|
||||
{
|
||||
index: indexName,
|
||||
}).catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,63 +0,0 @@
|
||||
import { ElsSmartlogDestination, type IStandardLogParams } from './els.classes.smartlogdestination.js';
|
||||
|
||||
export class ElasticScheduler {
|
||||
elasticSearchRef: ElsSmartlogDestination<any>;
|
||||
docsScheduled = false;
|
||||
docsStorage: any[] = [];
|
||||
|
||||
// maximum size of the buffer
|
||||
maxBufferSize = 500;
|
||||
|
||||
constructor(elasticLogRefArg: ElsSmartlogDestination<any>) {
|
||||
this.elasticSearchRef = elasticLogRefArg;
|
||||
}
|
||||
|
||||
public addFailedDoc(objectArg: any | IStandardLogParams) {
|
||||
this.addToStorage(objectArg);
|
||||
this.setRetry();
|
||||
}
|
||||
|
||||
public scheduleDoc(logObject: any) {
|
||||
this.addToStorage(logObject);
|
||||
}
|
||||
|
||||
private addToStorage(logObject: any) {
|
||||
this.docsStorage.push(logObject);
|
||||
|
||||
// if buffer is full, send logs immediately
|
||||
if (this.docsStorage.length >= this.maxBufferSize) {
|
||||
this.flushLogsToElasticSearch();
|
||||
}
|
||||
}
|
||||
|
||||
private flushLogsToElasticSearch() {
|
||||
const oldStorage = this.docsStorage;
|
||||
this.docsStorage = [];
|
||||
|
||||
for (let logObject of oldStorage) {
|
||||
this.elasticSearchRef.log(logObject, true);
|
||||
}
|
||||
}
|
||||
|
||||
public setRetry() {
|
||||
setTimeout(() => {
|
||||
this.flushLogsToElasticSearch();
|
||||
|
||||
if (this.docsStorage.length === 0) {
|
||||
console.log('ElasticLog retry success!!!');
|
||||
this.docsScheduled = false;
|
||||
} else {
|
||||
console.log('ElasticLog retry failed');
|
||||
this.setRetry();
|
||||
}
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
public deferSend() {
|
||||
if (!this.docsScheduled) {
|
||||
console.log('Retry ElasticLog in 5 seconds!');
|
||||
this.docsScheduled = true;
|
||||
this.setRetry();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,68 +0,0 @@
|
||||
import { Client as ElasticClient } from '@elastic/elasticsearch';
|
||||
|
||||
interface FastPushOptions {
|
||||
deleteOldData?: boolean; // Clear the index
|
||||
deleteIndex?: boolean; // Delete the entire index
|
||||
}
|
||||
|
||||
export class FastPush {
|
||||
private client: ElasticClient;
|
||||
|
||||
constructor(node: string, auth?: { username: string; password: string }) {
|
||||
this.client = new ElasticClient({
|
||||
node: node,
|
||||
...(auth && { auth: auth }),
|
||||
});
|
||||
}
|
||||
|
||||
async pushToIndex(indexName: string, docArray: any[], options?: FastPushOptions) {
|
||||
if (docArray.length === 0) return;
|
||||
|
||||
const indexExists = await this.client.indices.exists({ index: indexName });
|
||||
|
||||
if (indexExists) {
|
||||
if (options?.deleteIndex) {
|
||||
await this.client.indices.delete({ index: indexName });
|
||||
} else if (options?.deleteOldData) {
|
||||
await this.client.deleteByQuery({
|
||||
index: indexName,
|
||||
body: {
|
||||
query: {
|
||||
match_all: {}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (!indexExists || options?.deleteIndex) {
|
||||
// Create index with mappings (for simplicity, we use dynamic mapping)
|
||||
await this.client.indices.create({
|
||||
index: indexName,
|
||||
body: {
|
||||
mappings: {
|
||||
dynamic: "true"
|
||||
// ... other specific mappings
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Bulk insert documents
|
||||
const bulkBody = [];
|
||||
for (const doc of docArray) {
|
||||
bulkBody.push({
|
||||
index: {
|
||||
_index: indexName,
|
||||
},
|
||||
});
|
||||
bulkBody.push(doc);
|
||||
}
|
||||
|
||||
await this.client.bulk({ body: bulkBody });
|
||||
}
|
||||
}
|
||||
|
||||
// Usage example:
|
||||
// const fastPush = new FastPush('http://localhost:9200', { username: 'elastic', password: 'password' });
|
||||
// fastPush.pushToIndex('my_index', [{ name: 'John', age: 30 }, { name: 'Jane', age: 25 }], { deleteOldData: true });
|
@ -1,111 +0,0 @@
|
||||
import * as plugins from './els.plugins.js';
|
||||
import { Client as ElasticClient } from '@elastic/elasticsearch';
|
||||
|
||||
export interface IElasticKVStoreConstructorOptions {
|
||||
index: string;
|
||||
node: string;
|
||||
auth?: {
|
||||
username: string;
|
||||
password: string;
|
||||
};
|
||||
}
|
||||
|
||||
export class ElasticKVStore {
|
||||
public client: ElasticClient;
|
||||
public index: string;
|
||||
private readyDeferred: any;
|
||||
|
||||
constructor(options: IElasticKVStoreConstructorOptions) {
|
||||
this.client = new ElasticClient({
|
||||
node: options.node,
|
||||
...(options.auth && { auth: options.auth }),
|
||||
});
|
||||
this.index = options.index;
|
||||
this.readyDeferred = plugins.smartpromise.defer();
|
||||
this.setupIndex();
|
||||
}
|
||||
|
||||
private async setupIndex() {
|
||||
try {
|
||||
const indexExists = await this.client.indices.exists({ index: this.index });
|
||||
|
||||
if (!indexExists) {
|
||||
await this.client.indices.create({
|
||||
index: this.index,
|
||||
body: {
|
||||
mappings: {
|
||||
properties: {
|
||||
key: {
|
||||
type: 'keyword'
|
||||
},
|
||||
value: {
|
||||
type: 'text'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
this.readyDeferred.resolve();
|
||||
} catch (err) {
|
||||
this.readyDeferred.reject(err);
|
||||
}
|
||||
}
|
||||
|
||||
async set(key: string, value: string) {
|
||||
await this.readyDeferred.promise;
|
||||
await this.client.index({
|
||||
index: this.index,
|
||||
id: key,
|
||||
body: {
|
||||
key,
|
||||
value
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async get(key: string): Promise<string | null> {
|
||||
await this.readyDeferred.promise;
|
||||
|
||||
try {
|
||||
const response = await this.client.get({
|
||||
index: this.index,
|
||||
id: key
|
||||
});
|
||||
return response._source['value'];
|
||||
} catch (error) {
|
||||
if (error.meta && error.meta.statusCode === 404) {
|
||||
return null;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async delete(key: string) {
|
||||
await this.readyDeferred.promise;
|
||||
|
||||
try {
|
||||
await this.client.delete({
|
||||
index: this.index,
|
||||
id: key
|
||||
});
|
||||
} catch (error) {
|
||||
if (error.meta && error.meta.statusCode !== 404) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async clear() {
|
||||
await this.readyDeferred.promise;
|
||||
|
||||
await this.client.deleteByQuery({
|
||||
index: this.index,
|
||||
body: {
|
||||
query: {
|
||||
match_all: {}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -1,75 +0,0 @@
|
||||
import { Client as ElasticClient } from '@elastic/elasticsearch';
|
||||
import type { ILogContext, ILogPackage, ILogDestination } from '@push.rocks/smartlog-interfaces';
|
||||
import { ElasticScheduler } from './els.classes.elasticscheduler.js';
|
||||
import { ElasticIndex } from './els.classes.elasticindex.js';
|
||||
|
||||
export interface IStandardLogParams {
|
||||
message: string;
|
||||
severity: string;
|
||||
}
|
||||
|
||||
export interface IElasticSearchConstructorOptions {
|
||||
indexPrefix: string;
|
||||
indexRetention: number;
|
||||
node: string;
|
||||
auth?: {
|
||||
username: string;
|
||||
password: string;
|
||||
};
|
||||
}
|
||||
|
||||
export class ElsSmartlogDestination<T> {
|
||||
public client: ElasticClient;
|
||||
public elasticScheduler = new ElasticScheduler(this);
|
||||
public elasticIndex: ElasticIndex = new ElasticIndex(this);
|
||||
|
||||
public indexPrefix: string;
|
||||
public indexRetention: number;
|
||||
|
||||
constructor(optionsArg: IElasticSearchConstructorOptions) {
|
||||
this.client = new ElasticClient({
|
||||
node: optionsArg.node,
|
||||
...(optionsArg.auth && { auth: optionsArg.auth }),
|
||||
});
|
||||
this.indexPrefix = `${optionsArg.indexPrefix}`;
|
||||
this.indexRetention = optionsArg.indexRetention;
|
||||
this.setupDataStream();
|
||||
}
|
||||
|
||||
private async setupDataStream() {
|
||||
// Define an index template
|
||||
await this.client.indices.putIndexTemplate({
|
||||
name: `${this.indexPrefix}_template`,
|
||||
index_patterns: [`${this.indexPrefix}-*`],
|
||||
data_stream: {},
|
||||
});
|
||||
}
|
||||
|
||||
public async log(logPackageArg: ILogPackage, scheduleOverwrite = false) {
|
||||
const now = new Date();
|
||||
const indexToUse = `${this.indexPrefix}-data-stream`; // Use data stream name
|
||||
|
||||
if (this.elasticScheduler.docsScheduled && !scheduleOverwrite) {
|
||||
this.elasticScheduler.scheduleDoc(logPackageArg);
|
||||
return;
|
||||
}
|
||||
|
||||
await this.client.index(
|
||||
{
|
||||
index: indexToUse,
|
||||
body: {
|
||||
'@timestamp': new Date(logPackageArg.timestamp).toISOString(),
|
||||
...logPackageArg,
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
get logDestination(): ILogDestination {
|
||||
return {
|
||||
handleLog: async (smartlogPackageArg: ILogPackage) => {
|
||||
await this.log(smartlogPackageArg);
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
import * as elasticsearch from '@elastic/elasticsearch';
|
||||
import * as lik from '@push.rocks/lik';
|
||||
import * as smartdelay from '@push.rocks/smartdelay';
|
||||
import * as smartlogInterfaces from '@push.rocks/smartlog-interfaces';
|
||||
import * as smartpromise from '@push.rocks/smartpromise';
|
||||
import * as smarttime from '@push.rocks/smarttime';
|
||||
|
||||
export { elasticsearch, lik, smartdelay, smartlogInterfaces, smartpromise, smarttime };
|
@ -1,4 +1 @@
|
||||
export * from './els.classes.smartlogdestination.js';
|
||||
export * from './els.classes.fastpush.js';
|
||||
export * from './els.classes.elasticdoc.js';
|
||||
export * from './els.classes.kvstore.js';
|
||||
export * from './elasticsearch.classes.elasticsearch';
|
||||
|
@ -1,11 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"experimentalDecorators": true,
|
||||
"useDefineForClassFields": false,
|
||||
"target": "ES2022",
|
||||
"module": "ES2022",
|
||||
"moduleResolution": "nodenext",
|
||||
"esModuleInterop": true,
|
||||
"verbatimModuleSyntax": true,
|
||||
}
|
||||
}
|
17
tslint.json
Normal file
17
tslint.json
Normal file
@ -0,0 +1,17 @@
|
||||
{
|
||||
"extends": ["tslint:latest", "tslint-config-prettier"],
|
||||
"rules": {
|
||||
"semicolon": [true, "always"],
|
||||
"no-console": false,
|
||||
"ordered-imports": false,
|
||||
"object-literal-sort-keys": false,
|
||||
"member-ordering": {
|
||||
"options":{
|
||||
"order": [
|
||||
"static-method"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"defaultSeverity": "warning"
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user