-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial implementation of database-integrity service
- Loading branch information
Showing
5 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
packages/server-core/src/services/database-integrity/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
87
packages/server-core/src/services/database-integrity/module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |