Добавлен источник данных daily-eccm-reports-v2
This commit is contained in:
parent
159e2ea17a
commit
d5d604eb40
2 changed files with 62 additions and 6 deletions
|
|
@ -9,20 +9,25 @@ export class CouchDb {
|
||||||
private static logger = new Logger(CouchDb.name);
|
private static logger = new Logger(CouchDb.name);
|
||||||
private static couchdb: nano.ServerScope | null = null;
|
private static couchdb: nano.ServerScope | null = null;
|
||||||
private static initialized = false;
|
private static initialized = false;
|
||||||
|
private static url: string | null = null;
|
||||||
|
|
||||||
static getCouchDb(): nano.ServerScope | null {
|
static getCouchDb(): nano.ServerScope | null {
|
||||||
if (CouchDb.initialized) {
|
if (CouchDb.initialized) {
|
||||||
return CouchDb.couchdb;
|
return CouchDb.couchdb;
|
||||||
}
|
}
|
||||||
CouchDb.initialized = true;
|
CouchDb.initialized = true;
|
||||||
const url = config.couchDb?.url;
|
this.url = config.couchDb?.url;
|
||||||
if (!url) {
|
if (!this.url) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
const n = nano(url);
|
const n = nano(this.url);
|
||||||
CouchDb.logger.log(`CouchDb connected by url ${url} ...`);
|
CouchDb.logger.log(`CouchDb connected by url ${this.url} ...`);
|
||||||
CouchDb.couchdb = n;
|
CouchDb.couchdb = n;
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static getCouchDbUrl(): string | null {
|
||||||
|
return this.url;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,12 @@
|
||||||
import { Injectable, Logger } from '@nestjs/common';
|
import { Injectable, Logger } from '@nestjs/common';
|
||||||
import { CouchDb } from '@app/event-emitter/couchdb-datasources/couchdb';
|
import { CouchDb } from '@app/event-emitter/couchdb-datasources/couchdb';
|
||||||
import nano from 'nano';
|
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()
|
@Injectable()
|
||||||
export class DailyEccmReportsV2Datasource {
|
export class DailyEccmReportsV2Datasource {
|
||||||
|
|
@ -15,15 +20,61 @@ export class DailyEccmReportsV2Datasource {
|
||||||
}
|
}
|
||||||
DailyEccmReportsV2Datasource.initilized = true;
|
DailyEccmReportsV2Datasource.initilized = true;
|
||||||
const n = CouchDb.getCouchDb();
|
const n = CouchDb.getCouchDb();
|
||||||
const dbName = 'eccm_daily_reports_v2';
|
const dbName = DB_NAME;
|
||||||
const dbs = await n.db.list();
|
const dbs = await n.db.list();
|
||||||
if (!dbs.includes(dbName)) {
|
if (!dbs.includes(dbName)) {
|
||||||
await n.db.create(dbName);
|
await n.db.create(dbName);
|
||||||
}
|
}
|
||||||
DailyEccmReportsV2Datasource.db = await n.db.use(dbName);
|
DailyEccmReportsV2Datasource.db = await n.db.use(dbName);
|
||||||
|
await this.checkAndCreateIndex();
|
||||||
DailyEccmReportsV2Datasource.logger.log(
|
DailyEccmReportsV2Datasource.logger.log(
|
||||||
`Connected to daily reports db - ${dbName}`,
|
`Connected to daily reports db - ${dbName}`,
|
||||||
);
|
);
|
||||||
return DailyEccmReportsV2Datasource.db;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue