From 50a54370e426c48164dcf10edd8857aa92747696 Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Thu, 26 Jan 2017 22:16:04 +0100 Subject: Fetch states for all ticks at once All experiment states are now fetched in one go. Additionally, the experiments are now expected to start at tick 1 (this was wrongly assumed to be 0, before). --- src/scripts/controllers/simulation/statecache.ts | 180 ++++++++++++++++++----- 1 file changed, 147 insertions(+), 33 deletions(-) (limited to 'src/scripts/controllers/simulation/statecache.ts') diff --git a/src/scripts/controllers/simulation/statecache.ts b/src/scripts/controllers/simulation/statecache.ts index 32f8f4e4..6d34dc03 100644 --- a/src/scripts/controllers/simulation/statecache.ts +++ b/src/scripts/controllers/simulation/statecache.ts @@ -2,7 +2,7 @@ import {SimulationController} from "../simulationcontroller"; export class StateCache { - public static CACHE_INTERVAL = 3000; + public static CACHE_INTERVAL = 10000; private static PREFERRED_CACHE_ADVANCE = 5; public stateList: {[key: number]: ITickState}; @@ -22,7 +22,7 @@ export class StateCache { constructor(simulationController: SimulationController) { this.stateList = {}; - this.lastCachedTick = -1; + this.lastCachedTick = 0; this.cacheBlock = true; this.simulationController = simulationController; this.caching = false; @@ -81,16 +81,22 @@ export class StateCache { return; } - this.fetchAllStatesOfTick(tick).then((data: ITickState) => { - this.stateList[tick] = data; + this.fetchAllAvailableStates().then((data) => { + this.stateList = data; this.updateTasks(tick); + // Determine last cached tick + let ticks = Object.keys(this.stateList).sort((a, b) => { + return parseInt(a) - parseInt(b); + }); + if (ticks.length > 0) { + this.lastCachedTick = parseInt(ticks[ticks.length - 1]); + } + // Update chart cache this.simulationController.chartController.tickUpdated(tick); - this.lastCachedTick++; - if (!this.cacheBlock && this.lastCachedTick - this.simulationController.currentTick <= 0) { this.cacheBlock = true; return; @@ -160,6 +166,106 @@ export class StateCache { }); } + private fetchAllAvailableStates(): Promise<{[key: number]: ITickState}> { + let machineStates, rackStates, roomStates, taskStates; + const promises = []; + + promises.push( + this.simulationController.mapController.api.getMachineStates( + this.simulationController.mapView.simulation.id, this.simulationController.currentExperiment.id, + this.machineCache + ).then((states: IMachineState[]) => { + machineStates = states; + }) + ); + + promises.push( + this.simulationController.mapController.api.getRackStates( + this.simulationController.mapView.simulation.id, this.simulationController.currentExperiment.id, + this.rackCache + ).then((states: IRackState[]) => { + rackStates = states; + }) + ); + + promises.push( + this.simulationController.mapController.api.getRoomStates( + this.simulationController.mapView.simulation.id, this.simulationController.currentExperiment.id, + this.roomCache + ).then((states: IRoomState[]) => { + roomStates = states; + }) + ); + + promises.push( + this.simulationController.mapController.api.getTaskStates( + this.simulationController.mapView.simulation.id, this.simulationController.currentExperiment.id, + this.taskCache + ).then((states: ITaskState[]) => { + taskStates = states; + }) + ); + + return Promise.all(promises).then(() => { + let tickStates: {[key: number]: ITickState} = {}; + + machineStates.forEach((machineState: IMachineState) => { + if (tickStates[machineState.tick] === undefined) { + tickStates[machineState.tick] = { + tick: machineState.tick, + machineStates: [machineState], + rackStates: [], + roomStates: [], + taskStates: [] + }; + } else { + tickStates[machineState.tick].machineStates.push(machineState); + } + }); + rackStates.forEach((rackState: IRackState) => { + if (tickStates[rackState.tick] === undefined) { + tickStates[rackState.tick] = { + tick: rackState.tick, + machineStates: [], + rackStates: [rackState], + roomStates: [], + taskStates: [] + }; + } else { + tickStates[rackState.tick].rackStates.push(rackState); + } + }); + roomStates.forEach((roomState: IRoomState) => { + if (tickStates[roomState.tick] === undefined) { + tickStates[roomState.tick] = { + tick: roomState.tick, + machineStates: [], + rackStates: [], + roomStates: [roomState], + taskStates: [] + }; + } else { + tickStates[roomState.tick].roomStates.push(roomState); + } + }); + taskStates.forEach((taskState: ITaskState) => { + if (tickStates[taskState.tick] === undefined) { + tickStates[taskState.tick] = { + tick: taskState.tick, + machineStates: [], + rackStates: [], + roomStates: [], + taskStates: [taskState] + }; + } else { + tickStates[taskState.tick].taskStates.push(taskState); + } + }); + + return tickStates; + }); + } + private fetchAllStatesOfTick(tick: number): Promise { let tickState: ITickState = { tick, @@ -170,33 +276,41 @@ export class StateCache { }; const promises = []; - promises.push(this.simulationController.mapController.api.getMachineStatesByTick( - this.simulationController.mapView.simulation.id, this.simulationController.currentExperiment.id, - tick, this.machineCache - ).then((states: IMachineState[]) => { - tickState.machineStates = states; - })); - - promises.push(this.simulationController.mapController.api.getRackStatesByTick( - this.simulationController.mapView.simulation.id, this.simulationController.currentExperiment.id, - tick, this.rackCache - ).then((states: IRackState[]) => { - tickState.rackStates = states; - })); - - promises.push(this.simulationController.mapController.api.getRoomStatesByTick( - this.simulationController.mapView.simulation.id, this.simulationController.currentExperiment.id, - tick, this.roomCache - ).then((states: IRoomState[]) => { - tickState.roomStates = states; - })); - - promises.push(this.simulationController.mapController.api.getTaskStatesByTick( - this.simulationController.mapView.simulation.id, this.simulationController.currentExperiment.id, - tick, this.taskCache - ).then((states: ITaskState[]) => { - tickState.taskStates = states; - })); + promises.push( + this.simulationController.mapController.api.getMachineStates( + this.simulationController.mapView.simulation.id, this.simulationController.currentExperiment.id, + this.machineCache, tick + ).then((states: IMachineState[]) => { + tickState.machineStates = states; + }) + ); + + promises.push( + this.simulationController.mapController.api.getRackStates( + this.simulationController.mapView.simulation.id, this.simulationController.currentExperiment.id, + this.rackCache, tick + ).then((states: IRackState[]) => { + tickState.rackStates = states; + }) + ); + + promises.push( + this.simulationController.mapController.api.getRoomStates( + this.simulationController.mapView.simulation.id, this.simulationController.currentExperiment.id, + this.roomCache, tick + ).then((states: IRoomState[]) => { + tickState.roomStates = states; + }) + ); + + promises.push( + this.simulationController.mapController.api.getTaskStates( + this.simulationController.mapView.simulation.id, this.simulationController.currentExperiment.id, + this.taskCache, tick + ).then((states: ITaskState[]) => { + tickState.taskStates = states; + }) + ); return Promise.all(promises).then(() => { return tickState; -- cgit v1.2.3 From fd6005292dbbee09a8b3f3d63f22a7c4e95faa1d Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Thu, 26 Jan 2017 22:59:22 +0100 Subject: Fix task state generation --- src/scripts/controllers/simulation/statecache.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'src/scripts/controllers/simulation/statecache.ts') diff --git a/src/scripts/controllers/simulation/statecache.ts b/src/scripts/controllers/simulation/statecache.ts index 6d34dc03..06dbd424 100644 --- a/src/scripts/controllers/simulation/statecache.ts +++ b/src/scripts/controllers/simulation/statecache.ts @@ -84,8 +84,6 @@ export class StateCache { this.fetchAllAvailableStates().then((data) => { this.stateList = data; - this.updateTasks(tick); - // Determine last cached tick let ticks = Object.keys(this.stateList).sort((a, b) => { return parseInt(a) - parseInt(b); @@ -94,6 +92,10 @@ export class StateCache { this.lastCachedTick = parseInt(ticks[ticks.length - 1]); } + for (let i = 1; i <= this.lastCachedTick; i++) { + this.updateTasksForNewTick(i); + } + // Update chart cache this.simulationController.chartController.tickUpdated(tick); @@ -111,7 +113,7 @@ export class StateCache { }); } - private updateTasks(tick: number): void { + private updateTasksForNewTick(tick: number): void { const taskIDsInTick = []; this.stateList[tick].taskStates.forEach((taskState: ITaskState) => { -- cgit v1.2.3 From 027b379cf9d53a12782781e586f79051461ab661 Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Thu, 26 Jan 2017 23:11:29 +0100 Subject: Refactor controllers to use 'const' when possible --- src/scripts/controllers/simulation/statecache.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/scripts/controllers/simulation/statecache.ts') diff --git a/src/scripts/controllers/simulation/statecache.ts b/src/scripts/controllers/simulation/statecache.ts index 06dbd424..19fe0d8f 100644 --- a/src/scripts/controllers/simulation/statecache.ts +++ b/src/scripts/controllers/simulation/statecache.ts @@ -73,7 +73,7 @@ export class StateCache { } private cache(): void { - let tick = this.lastCachedTick + 1; + const tick = this.lastCachedTick + 1; this.updateLastTick().then(() => { // Check if end of simulated region has been reached @@ -85,7 +85,7 @@ export class StateCache { this.stateList = data; // Determine last cached tick - let ticks = Object.keys(this.stateList).sort((a, b) => { + const ticks = Object.keys(this.stateList).sort((a, b) => { return parseInt(a) - parseInt(b); }); if (ticks.length > 0) { @@ -209,7 +209,7 @@ export class StateCache { ); return Promise.all(promises).then(() => { - let tickStates: {[key: number]: ITickState} = {}; + const tickStates: {[key: number]: ITickState} = {}; machineStates.forEach((machineState: IMachineState) => { if (tickStates[machineState.tick] === undefined) { @@ -269,7 +269,7 @@ export class StateCache { } private fetchAllStatesOfTick(tick: number): Promise { - let tickState: ITickState = { + const tickState: ITickState = { tick, machineStates: [], rackStates: [], -- cgit v1.2.3 From b462c9183ec7c2e41f14daad49f03d8afaa4ec59 Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Fri, 27 Jan 2017 09:46:38 +0100 Subject: Fix graph view in building mode --- src/scripts/controllers/simulation/statecache.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/scripts/controllers/simulation/statecache.ts') diff --git a/src/scripts/controllers/simulation/statecache.ts b/src/scripts/controllers/simulation/statecache.ts index 19fe0d8f..092bfe32 100644 --- a/src/scripts/controllers/simulation/statecache.ts +++ b/src/scripts/controllers/simulation/statecache.ts @@ -94,10 +94,10 @@ export class StateCache { for (let i = 1; i <= this.lastCachedTick; i++) { this.updateTasksForNewTick(i); - } - // Update chart cache - this.simulationController.chartController.tickUpdated(tick); + // Update chart cache + this.simulationController.chartController.tickUpdated(i); + } if (!this.cacheBlock && this.lastCachedTick - this.simulationController.currentTick <= 0) { this.cacheBlock = true; -- cgit v1.2.3