Skip to content

Commit 9c47a55

Browse files
Fix time to next stop calculation
Co-authored-by: Chris Morabito <chris.morabito@woolpert.com>
1 parent 7250135 commit 9c47a55

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

application/frontend/src/app/core/effects/download.effects.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import {
4949
import * as fromRoot from 'src/app/reducers';
5050
import * as fromDownload from '../selectors/download.selectors';
5151
import { unparse } from 'papaparse';
52-
import { durationSeconds, secondsToFormattedTime } from 'src/app/util';
52+
import { durationSeconds, formattedDurationSeconds } from 'src/app/util';
5353
import { Modal } from '../models';
5454
import * as fromUI from '../selectors/ui.selectors';
5555
import { MatDialog } from '@angular/material/dialog';
@@ -212,7 +212,7 @@ export class DownloadEffects {
212212
visitEnd: new Date(
213213
durationSeconds(route.vehicleStartTime).toNumber() * 1000
214214
).toUTCString(),
215-
timeToNextStop: secondsToFormattedTime(
215+
timeToNextStop: formattedDurationSeconds(
216216
durationSeconds(route.visits[0]?.startTime || route.vehicleEndTime)
217217
.subtract(durationSeconds(route.vehicleStartTime))
218218
.toNumber()
@@ -306,13 +306,13 @@ export class DownloadEffects {
306306
const endSeconds = durationSeconds(visitRequest.duration).add(startSeconds);
307307
let timeToNextStop = null;
308308
if (visitIndex < route.visits.length - 1) {
309-
timeToNextStop = secondsToFormattedTime(
309+
timeToNextStop = formattedDurationSeconds(
310310
durationSeconds(route.visits[visitIndex + 1].startTime)
311311
.subtract(endSeconds)
312312
.toNumber()
313313
);
314314
} else if (vehicle.endWaypoint) {
315-
timeToNextStop = secondsToFormattedTime(
315+
timeToNextStop = formattedDurationSeconds(
316316
durationSeconds(route.vehicleEndTime).subtract(endSeconds).toNumber()
317317
);
318318
}

application/frontend/src/app/core/models/csv.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export const CSV_DATA_LABELS = {
125125
visitEnd: 'Visit end',
126126
shipmentIndex: 'Shipment index',
127127
shipmentLabel: 'Shipment label',
128-
timeToNextStop: 'Time to next stop (HH:MM:SS)',
128+
timeToNextStop: 'Time to next stop',
129129
location: 'Visit location (lat, lng)',
130130
};
131131

application/frontend/src/app/util/duration.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
durationSeconds,
1414
durationToMinutes,
1515
durationToRequestString,
16+
formattedDurationSeconds,
1617
minutesToDuration,
1718
pad,
1819
secondsToDuration,
@@ -140,4 +141,22 @@ describe('duration util', () => {
140141
).toEqual([Long.fromValue(1), Long.fromValue(2)]);
141142
});
142143
});
144+
145+
describe('formattedDurationSeconds', () => {
146+
it('should return a defaul timestamp', () => {
147+
expect(formattedDurationSeconds(null)).toBe('0s');
148+
});
149+
it('should return a formatted duration', () => {
150+
expect(formattedDurationSeconds(0)).toBe('0s');
151+
expect(formattedDurationSeconds(30)).toBe('30s');
152+
expect(formattedDurationSeconds(60)).toBe('1m 0s');
153+
expect(formattedDurationSeconds(95)).toBe('1m 35s');
154+
expect(formattedDurationSeconds(601)).toBe('10m 1s');
155+
expect(formattedDurationSeconds(3600)).toBe('1h 0m 0s');
156+
expect(formattedDurationSeconds(4201)).toBe('1h 10m 1s');
157+
expect(formattedDurationSeconds(46800)).toBe('13h 0m 0s');
158+
expect(formattedDurationSeconds(172800)).toBe('2d 0h 0m 0s');
159+
expect(formattedDurationSeconds(173409)).toBe('2d 0h 10m 9s');
160+
});
161+
});
143162
});

application/frontend/src/app/util/duration.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,33 @@ export function secondsToFormattedTime(seconds: Long | number): string {
3838
return new Date(1000 * seconds).toISOString().substring(11, 19);
3939
}
4040

41+
export function formattedDurationSeconds(seconds: Long | number): string {
42+
if (!seconds) {
43+
return '0s';
44+
}
45+
46+
seconds = Long.fromValue(seconds).toNumber();
47+
48+
const days = Math.floor(seconds / 86400);
49+
seconds -= days * 86400;
50+
const hours = Math.floor(seconds / 3600);
51+
seconds -= hours * 3600;
52+
const minutes = Math.floor(seconds / 60);
53+
seconds -= minutes * 60;
54+
55+
// Format string based on the highest unit of time in the duration
56+
if (days) {
57+
return `${days}d ${hours}h ${minutes}m ${seconds}s`;
58+
}
59+
if (hours) {
60+
return `${hours}h ${minutes}m ${seconds}s`;
61+
}
62+
if (minutes) {
63+
return `${minutes}m ${seconds}s`;
64+
}
65+
return `${seconds}s`;
66+
}
67+
4168
export function durationSeconds(duration: IDuration | ITimestamp, defaultValue = Long.ZERO): Long {
4269
let seconds: Long;
4370
if (duration) {

0 commit comments

Comments
 (0)