Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/state benchmark #581

Merged
merged 20 commits into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslint/typescript.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = {
'no-console': [
'warn',
{
allow: ['log', 'warn', 'error', 'assert'],
allow: ['log', 'warn', 'error', 'assert', 'table'],
},
],
'no-promise-executor-return': 'warn',
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ backend/coverage

shared/coverage
shared/tsconfig.build.tsbuildinfo

benchmark/data/*
!benchmark/data/*.permanent.json
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ backend/src/database/migrations/
docker-compose.yml
# same, but probably not needed
.env.example

# The states in here should not be touched by humans
benchmark/data
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ If a test fails a new screenshot is taken and put in the `comparison` folder.
If the new screenshot is the new desired result, then you only have to move it in the `baseline` folder and replace the old reference screenshot with the same name.
In the `diff` folder you can see the changes between the baseline and the comparison screenshot.

## Benchmarking

You can run the benchmarks via `npm run benchmark` in the root folder.
Look at the [benchmark readme](./benchmark/README.md) for more information.

## Styleguide

- names are never unique, ids are
Expand Down Expand Up @@ -172,7 +177,8 @@ This repository is a monorepo that consists of the following packages:

- [frontend](./frontend) the browser-based client application ([Angular](https://angular.io/))
- [backend](./backend) the server-side application ([NodeJs](https://nodejs.org/))
- [shared](./shared) the shared code that is used by both frontend and backend
- [benchmark](./benchmark/) benchmarks and tests some parts of the application
- [shared](./shared) the shared code that is used by the frontend, backend and the benchmark package

Each package has its own `README.md` file with additional documentation. Please check them out before you start working on the project.

Expand Down
5 changes: 0 additions & 5 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,6 @@ If you want to, you can also disable the database.
Set the environment variable `DFM_USE_DB` (in [`../.env`](../.env)) to `false` to achieve this.
Note however that this results in a) all history being saved in memory instead of on disk, and b) once the backend exits, for whatever reason, all data is gone forever.

### Migrations

We use [state migrations](./src/database/state-migrations/) to convert outdated states to new versions.
Look at [`migrations.ts`](./src/database/state-migrations/migrations.ts) for more information.

### Note on long term storage

The current setup when using a database is that no exercises get deleted unless anyone deletes them from the UI (or, more precisely, using the HTTP request `DELETE /api/exercises/:exerciseId`).
Expand Down
94 changes: 94 additions & 0 deletions backend/src/database/migrate-in-database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import type { UUID } from 'digital-fuesim-manv-shared';
import { applyMigrations } from 'digital-fuesim-manv-shared';
import type { EntityManager } from 'typeorm';
import { RestoreError } from '../utils/restore-error';
import { ActionWrapperEntity } from './entities/action-wrapper.entity';
import { ExerciseWrapperEntity } from './entities/exercise-wrapper.entity';

export async function migrateInDatabase(
exerciseId: UUID,
entityManager: EntityManager
): Promise<void> {
const exercise = await entityManager.findOne(ExerciseWrapperEntity, {
where: { id: exerciseId },
});
if (exercise === null) {
throw new RestoreError(
'Cannot find exercise to convert in database',
exerciseId
);
}
const initialState = JSON.parse(exercise.initialStateString);
const currentState = JSON.parse(exercise.currentStateString);
const actions = (
await entityManager.find(ActionWrapperEntity, {
where: { exercise: { id: exerciseId } },
select: { actionString: true },
order: { index: 'ASC' },
})
).map((action) => JSON.parse(action.actionString));
const newVersion = applyMigrations(exercise.stateVersion, {
currentState,
history: {
initialState,
actions,
},
});
exercise.stateVersion = newVersion;
// Save exercise wrapper
const patch: Partial<ExerciseWrapperEntity> = {
stateVersion: exercise.stateVersion,
};
patch.initialStateString = JSON.stringify(initialState);
patch.currentStateString = JSON.stringify(currentState);
await entityManager.update(
ExerciseWrapperEntity,
{ id: exerciseId },
patch
);
// Save actions
if (actions !== undefined) {
let patchedActionsIndex = 0;
const indicesToRemove: number[] = [];
const actionsToUpdate: {
previousIndex: number;
newIndex: number;
actionString: string;
}[] = [];
actions.forEach((action, i) => {
if (action === null) {
indicesToRemove.push(i);
return;
}
actionsToUpdate.push({
previousIndex: i,
newIndex: patchedActionsIndex++,
actionString: JSON.stringify(action),
});
});
if (indicesToRemove.length > 0) {
await entityManager
.createQueryBuilder()
.delete()
.from(ActionWrapperEntity)
// eslint-disable-next-line unicorn/string-content
.where('index IN (:...ids)', { ids: indicesToRemove })
.execute();
}
if (actionsToUpdate.length > 0) {
await Promise.all(
actionsToUpdate.map(
async ({ previousIndex, newIndex, actionString }) =>
entityManager.update(
ActionWrapperEntity,
{
index: previousIndex,
exercise: { id: exerciseId },
},
{ actionString, index: newIndex }
)
)
);
}
}
}
18 changes: 0 additions & 18 deletions backend/src/database/state-migrations/impossible-migration.ts

This file was deleted.

227 changes: 0 additions & 227 deletions backend/src/database/state-migrations/migrations.ts

This file was deleted.

Loading