Skip to content

Commit

Permalink
feat: initial implementation of database-integrity service
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Sep 24, 2024
1 parent f8a9fe0 commit 077fdd3
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/server-core/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { setupAuthupService, setupHarborService } from '../core';
import { buildDataSourceOptions } from '../database';
import { createRouter } from '../http/router';
import { createHttpServer } from '../http/server';
import { DatabaseIntegrityService } from '../services';

export async function startCommand() {
const config = createConfig();
Expand Down Expand Up @@ -74,6 +75,9 @@ export async function startCommand() {
logger.info('Applied database schema.');
}

const databaseIntegrity = new DatabaseIntegrityService(dataSource);
await databaseIntegrity.check();

// if (!check.schema) {
logger.info('Executing authup service setup...');
await setupAuthupService();
Expand Down
8 changes: 8 additions & 0 deletions packages/server-core/src/services/database-integrity/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* Copyright (c) 2024.
* Author Peter Placzek (tada5hi)
* For the full copyright and license information,
* view the LICENSE file that was distributed with this source code.
*/

export * from './module';
87 changes: 87 additions & 0 deletions packages/server-core/src/services/database-integrity/module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (c) 2024.
* Author Peter Placzek (tada5hi)
* For the full copyright and license information,
* view the LICENSE file that was distributed with this source code.
*/

import type { DataSource } from 'typeorm';
import {
AnalysisEntity, AnalysisNodeEntity, ProjectEntity, ProjectNodeEntity,
} from '../../domains';

export class DatabaseIntegrityService {
protected dataSource : DataSource;

constructor(datasource: DataSource) {
this.dataSource = datasource;
}

async check() {
await this.checkAnalyses();
await this.checkProjects();
}

async checkAnalyses() {
const analysisNodeRepository = this.dataSource.getRepository(AnalysisNodeEntity);
const analysisNodes = await analysisNodeRepository.find();

const nodeCountByAnalysisID = analysisNodes.reduce(
(stack, current) => {
if (!stack[current.analysis_id]) {
stack[current.analysis_id] = 0;
}

stack[current.analysis_id] += 1;

return stack;
},
{} as Record<string, number>,
);

const analysisRepository = this.dataSource.getRepository(AnalysisEntity);
const keys = Object.keys(nodeCountByAnalysisID);
for (let i = 0; i < keys.length; i++) {
await analysisRepository.update({
id: keys[i],
}, {
nodes: nodeCountByAnalysisID[keys[i]],
});
}
}

async checkProjects() {
const projectNodeRepository = this.dataSource.getRepository(ProjectNodeEntity);
const projectNodes = await projectNodeRepository.find();

const nodeCountByProjectID = projectNodes.reduce(
(stack, current) => {
if (!stack[current.project_id]) {
stack[current.project_id] = 0;
}

stack[current.project_id] += 1;

return stack;
},
{} as Record<string, number>,
);

const projectRepository = this.dataSource.getRepository(ProjectEntity);
const analysisRepository = this.dataSource.getRepository(AnalysisEntity);

const projects = await projectRepository.find();
for (let i = 0; i < projects.length; i++) {
const analyses = await analysisRepository.find({
where: {
project_id: projects[i].id,
},
});

projects[i].analyses = analyses.length;
projects[i].nodes = nodeCountByProjectID[projects[i].id] || 0;
}

await projectRepository.save(projects);
}
}
8 changes: 8 additions & 0 deletions packages/server-core/src/services/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* Copyright (c) 2024.
* Author Peter Placzek (tada5hi)
* For the full copyright and license information,
* view the LICENSE file that was distributed with this source code.
*/

export * from './database-integrity';
8 changes: 8 additions & 0 deletions packages/server-core/src/services/module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* Copyright (c) 2024.
* Author Peter Placzek (tada5hi)
* For the full copyright and license information,
* view the LICENSE file that was distributed with this source code.
*/

export * from './database-integrity';

0 comments on commit 077fdd3

Please sign in to comment.