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

Elysia/network analytics #57

Merged
merged 3 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
41 changes: 41 additions & 0 deletions src/api/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,47 @@ export async function getSimulationOutput(
return await response.json();
}

export async function getSimulationAnalytics(
simulationAnalytics: SimulationOutput,
): Promise<number[]> {
const tripInfo = simulationAnalytics.tripInfo;
const netState = simulationAnalytics.netstate;
// Run time: Measured in simulation seconds

// Average duration: The average time each vehicle needed to accomplish the route in simulation seconds
// Extract all durations into an array
const durations = tripInfo.map((trip) => trip.duration);

// Calculate the average duration
const totalDuration = durations.reduce((acc, duration) => acc + duration, 0);
const averageDuration = totalDuration / tripInfo.length;

// Waiting time: The average time in which vehicles speed was below or equal 0.1 m/s in simulation seconds
// Extract all durations into an array
const waitingTime = tripInfo.map((trip) => trip.waitingTime);

// Calculate the average duration
const totalWaiting = waitingTime.reduce((acc, time) => acc + time, 0);
const averageWaiting = totalWaiting / tripInfo.length;

// Time loss: The time lost due to driving below the ideal speed. (ideal speed includes the individual speedFactor; slowdowns due to intersections etc. will incur timeLoss, scheduled stops do not count) in simulation seconds
// Extract all durations into an array
const timeLoss = tripInfo.map((trip) => trip.timeLoss);

// Calculate the average duration
const totalTimeLoss = timeLoss.reduce((acc, time) => acc + time, 0);
const averageTimeLoss = totalTimeLoss / tripInfo.length;


// Total number of cars that reached their destination. Can work this out with vaporised variable
const noFinish = tripInfo.filter((trip) => trip.vaporized === true).length;
const totalNumberOfCarsThatCompleted = tripInfo.length - noFinish;


// Return the averages as an array
return [averageDuration, averageWaiting, averageTimeLoss, totalNumberOfCarsThatCompleted];
}

export async function getSimulationInfo(
simulationId: string,
): Promise<SimulationInfo> {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Canvas/BidirectionalRoad.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ export function BidirectionalRoad({
/>
</>
);
}
}
4 changes: 3 additions & 1 deletion src/components/FloatingPlayPause.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CircleLoader } from 'react-spinners';

import { PlayIcon, StopIcon } from '@heroicons/react/24/outline';

import { getSimulationOutput, uploadNetwork } from '~/api/network';
import { getSimulationAnalytics, getSimulationOutput, uploadNetwork } from '~/api/network';
import { extractCarsFromSumoMessage } from '~/helpers/sumo';
import { useSimulation } from '~/hooks/useSimulation';
import {
Expand Down Expand Up @@ -112,6 +112,7 @@ export const FloatingPlayPause = () => {
}

const simOutput = await getSimulationOutput(player.simulationId);
const simAnalytics = await getSimulationAnalytics(simOutput);

if (startTime && simulationInfo) {
simulationHistory.updateHistory({
Expand All @@ -125,6 +126,7 @@ export const FloatingPlayPause = () => {
}

console.log({ simOutput });
console.log(simAnalytics)
} catch (error: unknown) {
console.error(error);
} finally {
Expand Down