Compare commits

..

No commits in common. "master" and "v2.0.7" have entirely different histories.

6 changed files with 38 additions and 36 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@apiclient.xyz/elasticsearch", "name": "@apiclient.xyz/elasticsearch",
"version": "2.0.16", "version": "2.0.7",
"private": false, "private": false,
"description": "log to elasticsearch in a kibana compatible format", "description": "log to elasticsearch in a kibana compatible format",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@apiclient.xyz/elasticsearch', name: '@apiclient.xyz/elasticsearch',
version: '2.0.16', version: '2.0.7',
description: 'log to elasticsearch in a kibana compatible format' description: 'log to elasticsearch in a kibana compatible format'
} }

View File

@ -26,7 +26,6 @@ export class ElasticDoc {
private indexInitialized: boolean = false; private indexInitialized: boolean = false;
private latestTimestamp: string | null = null; // Store the latest timestamp private latestTimestamp: string | null = null; // Store the latest timestamp
private onlyNew: boolean = false; // Whether to only pipe new docs 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; private BATCH_SIZE = 1000;
@ -46,7 +45,7 @@ export class ElasticDoc {
await this.client.indices.create({ await this.client.indices.create({
index: this.index, index: this.index,
body: { body: {
// mappings, mappings,
settings: { settings: {
// You can define the settings according to your requirements here // You can define the settings according to your requirements here
}, },
@ -72,25 +71,34 @@ export class ElasticDoc {
async startPipingSession(options: { onlyNew?: boolean }) { async startPipingSession(options: { onlyNew?: boolean }) {
this.sessionDocs.clear(); this.sessionDocs.clear();
this.onlyNew = options.onlyNew; 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. if (this.onlyNew) {
const hit = response.hits.hits[0]; try {
this.latestTimestamp = hit?._source?.['@timestamp'] || null; const response = await this.client.search({
index: this.index,
sort: '@timestamp:desc',
size: 1,
});
if (this.latestTimestamp) { // If the search query succeeded, the index exists.
console.log(`Working in "onlyNew" mode. Hence we are omitting documents prior to ${this.latestTimestamp}`); const hit = response.hits.hits[0];
} else { this.latestTimestamp = hit?._source?.['@timestamp'] || null;
console.log(`Working in "onlyNew" mode, but no documents found in index ${this.index}. Hence processing all documents now.`);
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.`);
}
} catch (e) {
// If the search query failed, the index likely doesn't exist or some other error occurred.
if (e.meta && e.meta.statusCode === 404) {
console.log(`Index ${this.index} does not exist. Working in "onlyNew" mode, but will process all documents as the index is empty.`);
} else {
console.log(`An error occurred while trying to retrieve the latest timestamp: ${e}`);
}
this.latestTimestamp = null;
} }
} else if (this.onlyNew && !indexExists) {
console.log(`Working in "onlyNew" mode, but index ${this.index} does not exist. Hence processing all documents now.`);
} }
} }
@ -107,22 +115,15 @@ export class ElasticDoc {
// If 'onlyNew' is true, compare the document timestamp with the latest timestamp // If 'onlyNew' is true, compare the document timestamp with the latest timestamp
if (this.onlyNew) { if (this.onlyNew) {
if (this.latestTimestamp && optionsArg.timestamp <= this.latestTimestamp) { if (this.latestTimestamp && optionsArg.timestamp <= this.latestTimestamp) {
this.fastForward = true; // Omit the document
return;
} else { } else {
this.fastForward = false;
await this.client.index({ await this.client.index({
index: this.index, index: this.index,
id: optionsArg.docId, id: optionsArg.docId,
body: documentBody, body: documentBody,
}); });
} }
} else {
this.fastForward = false;
await this.client.index({
index: this.index,
id: optionsArg.docId,
body: documentBody,
});
} }
this.sessionDocs.add(optionsArg.docId); this.sessionDocs.add(optionsArg.docId);
} }
@ -212,6 +213,7 @@ export class ElasticDoc {
if (response.hits.hits.length > 0) { if (response.hits.hits.length > 0) {
const hit = response.hits.hits[0]; const hit = response.hits.hits[0];
console.log(hit);
return { return {
date: hit._source['date'], date: hit._source['date'],
aggregationData: hit._source['aggregationData'], aggregationData: hit._source['aggregationData'],

View File

@ -18,7 +18,7 @@ export class FastPush {
async pushToIndex(indexName: string, docArray: any[], options?: FastPushOptions) { async pushToIndex(indexName: string, docArray: any[], options?: FastPushOptions) {
if (docArray.length === 0) return; if (docArray.length === 0) return;
const indexExists = await this.client.indices.exists({ index: indexName }); const { body: indexExists } = await this.client.indices.exists({ index: indexName });
if (indexExists) { if (indexExists) {
if (options?.deleteIndex) { if (options?.deleteIndex) {

View File

@ -27,7 +27,7 @@ export class ElasticKVStore {
private async setupIndex() { private async setupIndex() {
try { try {
const indexExists = await this.client.indices.exists({ index: this.index }); const { body: indexExists } = await this.client.indices.exists({ index: this.index });
if (!indexExists) { if (!indexExists) {
await this.client.indices.create({ await this.client.indices.create({
@ -72,7 +72,7 @@ export class ElasticKVStore {
index: this.index, index: this.index,
id: key id: key
}); });
return response._source['value']; return response.body._source.value;
} catch (error) { } catch (error) {
if (error.meta && error.meta.statusCode === 404) { if (error.meta && error.meta.statusCode === 404) {
return null; return null;

View File

@ -54,7 +54,7 @@ export class ElsSmartlogDestination<T> {
return; return;
} }
await this.client.index( this.client.index(
{ {
index: indexToUse, index: indexToUse,
body: { body: {
@ -68,7 +68,7 @@ export class ElsSmartlogDestination<T> {
get logDestination(): ILogDestination { get logDestination(): ILogDestination {
return { return {
handleLog: async (smartlogPackageArg: ILogPackage) => { handleLog: async (smartlogPackageArg: ILogPackage) => {
await this.log(smartlogPackageArg); this.log(smartlogPackageArg);
}, },
}; };
} }