Project: Headlamp (Observability and Analysis / Observability)
File: lib_util.md

title: "Module: lib/util" linkTitle: "lib/util" slug: "lib_util" DateFormatOptions: ``"brief"`` \| ``"mini"`` _ DateParam: `string` \| `number` \| `Date` CLUSTER\_ACTION\_GRACE\_PERIOD: ``5000`` compareUnits(`quantity1`, `quantity2`): `boolean` | Name | Type | | : | : | | `quantity1` | `string` | | `quantity2` | `string` | `boolean` _ filterGeneric<`T`\>(`item`, `filter`, `matchCriteria?`): `boolean` Filters a generic item based on the filter state. The item is considered to match if any of the matchCriteria (described as JSONPath) matches the filter.search contents. Case matching is insensitive. | Name | Type | | : | : | | `T` | extends `Object` = { `[key: string]`: `any`; } | | Name | Type | Description | | : | : | : | | `item` | `T` | The item to filter. | | `filter` | `FilterState` | The filter state. | | `matchCriteria?` | `string`[] | The JSONPath criteria to match. | `boolean` _ filterResource(`item`, `filter`, `matchCriteria?`): `boolean` Filters a resource based on the filter state. | Name | Type | Description | | : | : | : | | `item` | \| | The item to filter. | | `filter` | `FilterState` | The filter state. | | `matchCriteria?` | `string`[] | The JSONPath criteria to match. | `boolean` True if the item matches the filter, false otherwise. _ formatDuration(`duration`, `options?`): `string` Format a duration in milliseconds to a human-readable string. | Name | Type | Description | | : | : | : | | `duration` | `number` | The duration in milliseconds. | | `options` | | `format` takes "brief" or "mini". "brief" rounds the date and uses the largest suitable unit (e.g. "4 weeks"). "mini" uses something like "4w" (for 4 weeks). | `string` The formatted duration. _ getCluster(): `string` \| ``null`` `string` \| ``null`` The current cluster name, or null if not in a cluster context. _ getClusterPrefixedPath(`path?`): `string` | Name | Type | | : | : | | `path?` | ``null`` \| `string` | `string` A path prefixed with cluster path, and the given path. The given path does not start with a /, it will be

_ getPercentStr(`value`, `total`): ``null`` \| `string` | Name | Type | | : | : | | `value` | `number` | | `total` | `number` | ``null`` \| `string` _ getReadyReplicas(`item`): `any` | Name | Type | | : | : | | `item` | | `any` _ getResourceMetrics(`item`, `metrics`, `resourceType`): `any`[] | Name | Type | | : | : | | `item` | | | `metrics` | [] | | `resourceType` | ``"cpu"`` \| ``"memory"`` | `any`[] _ getResourceStr(`value`, `resourceType`): `string` | Name | Type | | : | : | | `value` | `number` | | `resourceType` | ``"cpu"`` \| ``"memory"`` | `string` _ getTotalReplicas(`item`): `any` | Name | Type | | : | : | | `item` | | `any` _ localeDate(`date`): `string` | Name | Type | | : | : | | `date` | | `string` _ normalizeUnit(`resourceType`, `quantity`): `string` | Name | Type | | : | : | | `resourceType` | `string` | | `quantity` | `string` | `string` _ timeAgo(`date`, `options?`): `string` Show the time passed since the given date, in the desired format. | Name | Type | Description | | : | : | : | | `date` | | The date since which to calculate the duration. | | `options` | | `format` takes "brief" or "mini". "brief" rounds the date and uses the largest suitable unit (e.g. "4 weeks"). "mini" uses something like "4w" (for 4 weeks). | `string` The formatted date. _ useErrorState(`dependentSetter?`): `any`[] | Name | Type | | : | : | | `dependentSetter?` | (...`args`: `any`) => `void` | `any`[] _ useFilterFunc<`T`\>(`matchCriteria?`): (`item`: `T`) => `boolean` | Name | Type | | : | : | | `T` | extends \| \| { ` \| | | Name | Type | Description | | : | : | : | | `matchCriteria?` | `string`[] | The JSONPath criteria to match. | `fn` A filter function that can be used to filter a list of items. (`item`): `boolean` | Name | Type | | : | : | | `item` | `T` | `boolean` _ useId(`prefix?`): `undefined` \| `string` Creates a unique ID, with the given prefix. If UNDER_TEST is set to true, it will return the same ID every time, so snapshots do not get invalidated. | Name | Type | Default value | | : | : | : | | `prefix` | `string` | `''` | `undefined` \| `string` _ useURLState(`key`, `defaultValue`): [`number`, `React.Dispatch`<`React.SetStateAction`<`number`\>\>] A hook to manage a state variable that is also stored in the URL. | Name | Type | Description | | : | : | : | | `key` | `string` | The name of the key in the URL. If empty, then the hook behaves like useState. | | `defaultValue` | `number` | - | [`number`, `React.Dispatch`<`React.SetStateAction`<`number`\>\>] useURLState(`key`, `valueOrParams`): [`number`, `React.Dispatch`<`React.SetStateAction`<`number`\>\>] A hook to manage a state variable that is also stored in the URL. | Name | Type | Description | | : | : | : | | `key` | `string` | The name of the key in the URL. If empty, then the hook behaves like useState. | | `valueOrParams` | `number` \| `URLStateParams`<`number`\> | - | [`number`, `React.Dispatch`<`React.SetStateAction`<`number`\>\>]