Добавлен источник данных daily-eccm-reports-v2

This commit is contained in:
Pavel Gnedov 2025-01-27 21:25:34 +07:00
parent 159e2ea17a
commit d5d604eb40
2 changed files with 62 additions and 6 deletions

View file

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

View file

@ -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<void> {
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<any[]> {
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<boolean> {
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;
}
}