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 2 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
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 }
)
)
);
}
}
}
227 changes: 0 additions & 227 deletions backend/src/database/state-migrations/migrations.ts

This file was deleted.

28 changes: 14 additions & 14 deletions backend/src/exercise/exercise-wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import type { EntityManager } from 'typeorm';
import { LessThan } from 'typeorm';
import type {
ExerciseAction,
StateExport,
ExerciseIds,
ExerciseTimeline,
Role,
StateExport,
UUID,
ExerciseTimeline,
} from 'digital-fuesim-manv-shared';
import {
ExerciseState,
cloneDeepMutable,
applyAction,
ReducerError,
cloneDeepMutable,
ExerciseState,
reduceExerciseState,
validateExerciseState,
ReducerError,
validateExerciseAction,
validateExerciseState,
} from 'digital-fuesim-manv-shared';
import { IncrementIdGenerator } from '../utils/increment-id-generator';
import { ValidationErrorWrapper } from '../utils/validation-error-wrapper';
import type { EntityManager } from 'typeorm';
import { LessThan } from 'typeorm';
import { Config } from '../config';
import type { ActionWrapperEntity } from '../database/entities/action-wrapper.entity';
import { ExerciseWrapperEntity } from '../database/entities/exercise-wrapper.entity';
import { migrateInDatabase } from '../database/migrate-in-database';
import { NormalType } from '../database/normal-type';
import type { DatabaseService } from '../database/services/database-service';
import { Config } from '../config';
import { pushAll, removeAll } from '../utils/array';
import { IncrementIdGenerator } from '../utils/increment-id-generator';
import { RestoreError } from '../utils/restore-error';
import { UserReadableIdGenerator } from '../utils/user-readable-id-generator';
import type { ActionWrapperEntity } from '../database/entities/action-wrapper.entity';
import { migrateInDatabase } from '../database/state-migrations/migrations';
import { pushAll, removeAll } from '../utils/array';
import { ValidationErrorWrapper } from '../utils/validation-error-wrapper';
import { ActionWrapper } from './action-wrapper';
import type { ClientWrapper } from './client-wrapper';
import { exerciseMap } from './exercise-map';
Expand Down
Loading