Skip to content

Commit

Permalink
add patients datastructure (not complete)
Browse files Browse the repository at this point in the history
  • Loading branch information
anonym-HPI committed Jun 19, 2022
1 parent 0d8594e commit fa61a42
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
27 changes: 27 additions & 0 deletions shared/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
"dependencies": {
"class-transformer": "^0.5.1",
"class-validator": "^0.13.2",
"d3-quadtree": "^3.0.1",
"immer": "^9.0.12",
"lodash-es": "^4.17.21",
"reflect-metadata": "^0.1.13",
"uuid": "^8.3.2"
},
"devDependencies": {
"@types/d3-quadtree": "^3.0.2",
"@types/jest": "^27.4.1",
"@types/lodash-es": "^4.17.6",
"@types/uuid": "^8.3.4",
Expand Down
100 changes: 100 additions & 0 deletions shared/src/models/patients-datastructure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { IsDefined, IsUUID } from 'class-validator';
import { Quadtree, quadtree } from 'd3-quadtree';
import type { ExerciseState } from '../state';
import { UUID, uuid, uuidValidationOptions } from '../utils';
import type { Position } from './utils';
import { getCreate } from './utils';
import type { Patient } from '.';

/**
*
* @param id PatientId
* @param x x-coordinate of patient
* @param y y-coordinate of patient
*/
interface PatientData {
id: UUID;
x: number;
y: number;
}

export class PatientsDataStructure {
@IsUUID(4, uuidValidationOptions)
public readonly id: UUID = uuid();

@IsDefined()
public readonly dataStructure: Quadtree<PatientData>;

static readonly create = getCreate(this);

/**
* @deprecated Use {@link create} instead
*/
constructor() {
// initialize and setting accessors to correct value in PatientData
this.dataStructure = quadtree<PatientData>()
.x((d) => d.x)
.y((d) => d.y);
}

fillDataStructure(state: ExerciseState) {
const patients = Object.values(state.patients).filter(
(patient) => patient.position !== undefined
);
const data: PatientData[] = [];
for (const patient of patients) {
if (patient.position?.x && patient.position.y) {
data.push({
id: patient.id,
x: patient.position?.x,
y: patient.position.y,
});
}
}
// addAll calculates the extend before adding the data resulting in a more compact quadtree
// see https://github.com/d3/d3-quadtree#quadtree_addAll
this.dataStructure.addAll(data);
}

/**
* @param patient needs a patient with valid position
*/
addPatient(patient: Patient) {
// TODO: check if patient got added sucessfully,
// maybe lazy via length checking (something got added)
if (patient.position?.x && patient.position.y) {
const x = patient.position.x;
const y = patient.position.y;
this.dataStructure.add({ id: patient.id, x, y });
} else throw new TypeError('Expected patient to have a valid position');
}

/**
* @param patient needs a patient with valid position
*/
removePatient(patient: Patient) {
// TODO: check if patient got deleted sucessfully,
// as function remove does nothing when datum does not exist in quadtree
// maybe lazy via length checking (something got deleted)
if (patient.position?.x && patient.position.y) {
const x = patient.position.x;
const y = patient.position.y;
this.dataStructure.remove({ id: patient.id, x, y });
} else throw new TypeError('Expected patient to have a valid position');
}

movePatient(patient: Patient) {
// TODO: check if patient got moved sucessfully
if (patient.position?.x && patient.position.y) {
const x = patient.position.x;
const y = patient.position.y;
// first add then remove, to not have maybe an unnecessary extend
this.dataStructure.add({ id: patient.id, x, y });
this.dataStructure.remove({ id: patient.id, x, y });
} else throw new TypeError('Expected patient to have a valid position');
}

findAllPatientsInCircle(position: Position, radius: number) {
// TODO: implement search
}
}
4 changes: 4 additions & 0 deletions shared/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { getCreate } from './models/utils';
import type { UUID } from './utils';
import { uuidValidationOptions, uuid } from './utils';
import { PatientCategory } from './models/patient-category';
import { PatientsDataStructure } from './models/patients-datastructure';

export class ExerciseState {
@IsUUID(4, uuidValidationOptions)
Expand Down Expand Up @@ -100,6 +101,9 @@ export class ExerciseState {
@Type(() => StatisticsEntry)
public readonly statistics: readonly StatisticsEntry[] = [];
@ValidateNested()
@Type(() => PatientsDataStructure)
public readonly patientsDataStructure = PatientsDataStructure.create();
@ValidateNested()
@Type(() => ExerciseConfiguration)
public readonly configuration = ExerciseConfiguration.create();

Expand Down

0 comments on commit fa61a42

Please sign in to comment.