# longestRun

Industry: Workforce

## Signature

```ts
longestRun(log: DutyEntry[], window: Interval, statuses: string[]): { interval: Interval; duration: string; } | null
```

Return the longest unbroken stretch a duty log spends in any of `statuses` inside a window.

- A stretch is a run of touching entries whose statuses are all in `statuses`; another status or an unlogged gap ends it. Each run is cut to `window` (half-open) before it is measured.
- On a tie the earlier stretch is returned.
- `interval` is in UTC instants ending in "Z"; `duration` is an exact ISO 8601 duration with hours as the largest unit.
- Returns `null` when no stretch falls inside the window, which is also the sentinel: when the log fails `isValidDutyLog`, when `window` is not a valid `Interval`, and when `statuses` is not an array of strings.

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| `log` | `DutyEntry[]` | the duty log, ordered and non-overlapping |
| `window` | `Interval` | `{ start, end }` record of ISO 8601 instant strings to search inside |
| `statuses` | `string[]` | the statuses a stretch is made of |

## Returns

`{ interval, duration }` of the longest stretch, or null when there is none or on invalid input

**Members**

| Member | Type | Description |
| --- | --- | --- |
| `interval` | [`Interval`](/gmt/reference/types/Interval/) | The stretch, as UTC instants ending in "Z", cut to the window. |
| `duration` | `string` | Its length, an exact ISO 8601 duration with hours as the largest unit. |

## Related types

- [`DutyEntry`](/gmt/reference/types/DutyEntry/)
- [`Interval`](/gmt/reference/types/Interval/)

## Examples

```ts
import { longestRun } from "@northguild/gmt/workforce/calculate";

longestRun([{ status: "driving", start: "2024-01-01T00:00:00Z", end: "2024-01-01T03:00:00Z" }, { status: "onDuty", start: "2024-01-01T03:00:00Z", end: "2024-01-01T04:00:00Z" }, { status: "driving", start: "2024-01-01T04:00:00Z", end: "2024-01-01T08:00:00Z" }], { start: "2024-01-01T00:00:00Z", end: "2024-01-02T00:00:00Z" }, ["driving"]) // { interval: { start: "2024-01-01T04:00:00Z", end: "2024-01-01T08:00:00Z" }, duration: "PT4H" }
```

## Source

[workforce/calculate/longestRun.ts](https://github.com/northguild/gmt/blob/main/packages/gmt/src/workforce/calculate/longestRun.ts)
