Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
JannikStreek committed Sep 23, 2022
2 parents cfa88ba + c0cdd0a commit ddb5ee3
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 15 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,21 @@ TeamMapper is based on mindmapp (https://github.com/cedoor/mindmapp , discontinu
```bash
docker-compose exec app_prod npm -prefix teammapper-backend run typeorm:prod:migrate
```
#### Running jobs

Trigger delete job (also executed daily with cron task scheduler):

```
docker-compose --file docker-compose-prod.yml --env-file .env.prod exec app_prod npm --prefix teammapper-backend run prod:data:maps:cleanup
```

#### Running further queries

Example of running sql via typeorm:

```
npx typeorm query "select * from mmp_node" --config dist/ormconfig.js
```

### Further details

Expand Down
2 changes: 1 addition & 1 deletion entrypoint.prod.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
npm --prefix teammapper-backend run typeorm:prod:migrate
npm --prefix teammapper-backend run prod:typeorm:migrate
npm run start:prod
2 changes: 1 addition & 1 deletion teammapper-backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ npm run typeorm schema:sync
For pruction environments, run migrations, see https://github.com/typeorm/typeorm/blob/master/docs/migrations.md

```bash
npm run typeorm:prod:migrate
npm run prod:typeorm:migrate
```

or run migrations on dev:
Expand Down
7 changes: 5 additions & 2 deletions teammapper-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json",
"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js --config src/ormconfig.ts",
"typeorm:prod:migrate": "typeorm migration:run --config dist/ormconfig.js"
"dev:typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js --config src/ormconfig.ts",
"dev:data:maps:cleanup": "ts-node ./src/jobs/deleteMaps.job.ts",
"dev:data:maps:seed": "ts-node ./src/jobs/seedMapData.job.ts",
"prod:data:maps:cleanup": "node ./dist/jobs/deleteMaps.job.js",
"prod:typeorm:migrate": "typeorm migration:run --config dist/ormconfig.js"
},
"dependencies": {
"@nestjs/common": "^8.0.0",
Expand Down
2 changes: 1 addition & 1 deletion teammapper-backend/src/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class ConfigService {

extra: {
query_timeout: 20000,
statement_timeout: 20000
statement_timeout: 20000,
},

synchronize: !this.isProduction(),
Expand Down
25 changes: 25 additions & 0 deletions teammapper-backend/src/jobs/deleteMaps.job.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { NestFactory } from '@nestjs/core';
import { MapsService } from '../map/services/maps.service';
import AppModule from '../app.module';
import { Logger } from '@nestjs/common';
import configService from '../config.service';
import { DeleteResult } from 'typeorm';

async function bootstrap() {
const application = await NestFactory.createApplicationContext(
AppModule,
);

const logger = new Logger('TaskRunner');
const mapsService = application.get(MapsService);

logger.log('--- Deleting old maps ... ---');
const result: DeleteResult = await mapsService.deleteOutdatedMaps(configService.deleteAfterDays());
logger.log('Deleted rows: ' + result.affected);
logger.log('--- Finished deleting maps ---');

await application.close();
process.exit(0);
}

bootstrap();
69 changes: 69 additions & 0 deletions teammapper-backend/src/jobs/seedMapData.job.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { NestFactory } from '@nestjs/core';
import { MapsService } from '../map/services/maps.service';
import AppModule from '../app.module';
import { Logger } from '@nestjs/common';
import { IMmpClientMap, IMmpClientNode } from 'src/map/types';
import * as crypto from 'crypto';

const createNode: any = (isRoot: boolean, parentId: string, x: number, y: number) => {
return {
colors: {
name: '#000000',
background: '#FF0000',
branch: '#000000',
},
coordinates: { x: x, y: y },
font: {
style: '',
size: 10,
weight: 'bold',
},
id: crypto.randomUUID(),
image: {
src: '',
size: 0,
},
k: 0,
locked: false,
name: 'Seed Data',
parent: parentId,
isRoot: isRoot,
};
};

const createMap: any = (nodes: IMmpClientNode[]) => {
return {
uuid: crypto.randomUUID(),
lastModified: new Date(),
deleteAfterDays: 30,
data: nodes,
deletedAt: undefined,
};
};

async function bootstrap() {
const application = await NestFactory.createApplicationContext(
AppModule,
);

const logger = new Logger('TaskRunner');
const mapsService = application.get(MapsService);

logger.log('--- Creating maps ... ---');

for (let i = 0; i < 5000; i++) {
const rootNode: IMmpClientNode = createNode(true, '', 0, 0);
const childNode: IMmpClientNode = createNode(false, rootNode.id, 150, 150);
const secondChildNode: IMmpClientNode = createNode(false, childNode.id, 250, 250);
const mapData: IMmpClientMap = createMap([rootNode, childNode, secondChildNode]);
await mapsService.createMap(mapData);
logger.log(`--- Map created with id ${mapData.uuid} ---`);
}

logger.log('--- Finished creating maps ---');

await application.close();
process.exit(0);
}

bootstrap();
1 change: 1 addition & 0 deletions teammapper-backend/src/map/entities/mmpNode.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class MmpNode {
{ name: 'nodeMapId', referencedColumnName: 'nodeMapId' },
{ name: 'nodeParentId', referencedColumnName: 'id' },
])
@Index()
nodeParent: MmpNode;
/* eslint-enable @typescript-eslint/no-unused-vars */

Expand Down
2 changes: 1 addition & 1 deletion teammapper-backend/src/map/services/maps.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class MapsService {
}

async deleteOutdatedMaps(afterDays: number = 30): Promise<DeleteResult> {
return await this.mapsRepository
return this.mapsRepository
.createQueryBuilder()
.where("(lastModified + (INTERVAL '1 day' * :afterDays)) < :today", { afterDays: afterDays, today: new Date() })
.delete()
Expand Down
2 changes: 1 addition & 1 deletion teammapper-backend/src/map/services/tasks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class TasksService {

// every day midnight
@Cron('0 0 * * *')
async handleCron() {
async deleteOldMapsInInterval() {
this.logger.log('--- Deleting old maps ... ---');
const result: DeleteResult = await this.mapsService.deleteOutdatedMaps(configService.deleteAfterDays());
this.logger.log('Deleted rows: ' + result.affected);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {MigrationInterface, QueryRunner} from "typeorm";
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddIndexToForeignKeysOnMmpNode1663839669273 implements MigrationInterface {
name = 'AddIndexToForeignKeysOnMmpNode1663839669273'
name = 'AddIndexToForeignKeysOnMmpNode1663839669273';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE INDEX "IDX_19c5208da416d32ea491315716" ON "mmp_node" ("nodeMapId") `);
}
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query('CREATE INDEX "IDX_19c5208da416d32ea491315716" ON "mmp_node" ("nodeMapId") ');
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP INDEX "public"."IDX_19c5208da416d32ea491315716"`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query('DROP INDEX "public"."IDX_19c5208da416d32ea491315716"');
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {MigrationInterface, QueryRunner} from "typeorm";

export class AddIndexForNodesParents1663927754319 implements MigrationInterface {
name = 'AddIndexForNodesParents1663927754319'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE INDEX "IDX_336300b82c56a05f0317f22942" ON "mmp_node" ("nodeMapId", "nodeParentId") `);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP INDEX "public"."IDX_336300b82c56a05f0317f22942"`);
}
}

0 comments on commit ddb5ee3

Please sign in to comment.