From d5d604eb405915d360a006173c2d11280979e7b2 Mon Sep 17 00:00:00 2001 From: Pavel Gnedov Date: Mon, 27 Jan 2025 21:25:34 +0700 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=B8=D1=81=D1=82=D0=BE=D1=87=D0=BD=D0=B8=D0=BA=20?= =?UTF-8?q?=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85=20daily-eccm-reports-v2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/couchdb-datasources/couchdb.ts | 13 +++-- .../daily-eccm-reports-v2.datasource.ts | 55 ++++++++++++++++++- 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/libs/event-emitter/src/couchdb-datasources/couchdb.ts b/libs/event-emitter/src/couchdb-datasources/couchdb.ts index 19732bc..bff94ce 100644 --- a/libs/event-emitter/src/couchdb-datasources/couchdb.ts +++ b/libs/event-emitter/src/couchdb-datasources/couchdb.ts @@ -9,20 +9,25 @@ export class CouchDb { private static logger = new Logger(CouchDb.name); private static couchdb: nano.ServerScope | null = null; private static initialized = false; + private static url: string | null = null; static getCouchDb(): nano.ServerScope | null { if (CouchDb.initialized) { return CouchDb.couchdb; } CouchDb.initialized = true; - const url = config.couchDb?.url; - if (!url) { + this.url = config.couchDb?.url; + if (!this.url) { return null; } else { - const n = nano(url); - CouchDb.logger.log(`CouchDb connected by url ${url} ...`); + const n = nano(this.url); + CouchDb.logger.log(`CouchDb connected by url ${this.url} ...`); CouchDb.couchdb = n; return n; } } + + static getCouchDbUrl(): string | null { + return this.url; + } } diff --git a/src/couchdb-datasources/daily-eccm-reports-v2.datasource.ts b/src/couchdb-datasources/daily-eccm-reports-v2.datasource.ts index 7e697a6..fbca808 100644 --- a/src/couchdb-datasources/daily-eccm-reports-v2.datasource.ts +++ b/src/couchdb-datasources/daily-eccm-reports-v2.datasource.ts @@ -1,7 +1,12 @@ import { Injectable, Logger } from '@nestjs/common'; import { CouchDb } from '@app/event-emitter/couchdb-datasources/couchdb'; import nano from 'nano'; -import { Report } from 'src/reports/daily-eccm-v2-report-task-runner.service'; +import axios from 'axios'; + +const DB_NAME = 'eccm_daily_reports_v2'; +const DATETIME_INDEX_ASC = 'datetime-json-index'; + +type Report = any; // TODO fix this type @Injectable() export class DailyEccmReportsV2Datasource { @@ -15,15 +20,61 @@ export class DailyEccmReportsV2Datasource { } DailyEccmReportsV2Datasource.initilized = true; const n = CouchDb.getCouchDb(); - const dbName = 'eccm_daily_reports_v2'; + const dbName = DB_NAME; const dbs = await n.db.list(); if (!dbs.includes(dbName)) { await n.db.create(dbName); } DailyEccmReportsV2Datasource.db = await n.db.use(dbName); + await this.checkAndCreateIndex(); DailyEccmReportsV2Datasource.logger.log( `Connected to daily reports db - ${dbName}`, ); return DailyEccmReportsV2Datasource.db; } + + static async checkAndCreateIndex(): Promise { + const couchDbUrl = CouchDb.getCouchDbUrl(); + const indexes = await DailyEccmReportsV2Datasource.getIndexes(couchDbUrl); + const index = indexes.find( + (index: any) => index.name === DATETIME_INDEX_ASC, + ); + if (!index) { + await DailyEccmReportsV2Datasource.createIndex(couchDbUrl); + } + } + + private static async getIndexes(couchDbUrl: string): Promise { + const response = await axios.get( + `${couchDbUrl}/${DB_NAME}/_index?skip=0&limit=999999`, + { + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + }, + }, + ); + this.logger.debug(`Indexes: ${JSON.stringify(response.data.indexes)}`); + return response.data.indexes; + } + + private static async createIndex(couchDbUrl: string): Promise { + this.logger.debug(`Creating index ${DATETIME_INDEX_ASC}`); + const body = { + index: { fields: [{ datetime: 'asc' }] }, + name: DATETIME_INDEX_ASC, + type: 'json', + }; + const response = await axios.post(`${couchDbUrl}/${DB_NAME}/_index`, body, { + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + }, + }); + this.logger.debug( + `Index ${DATETIME_INDEX_ASC} created ` + + `with response: ${JSON.stringify(response.data)}`, + ); + return true; + } }