Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move some datetime functions to @openshift-console/plugin-shared
Move formatPrometheusDuration and parsePrometheusDuration to @openshift-console/plugin-shared
- Loading branch information
Showing
25 changed files
with
97 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...boards/object-service/activity-card/data-resiliency-activity/data-resiliency-activity.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './prometheus'; |
56 changes: 56 additions & 0 deletions
56
frontend/packages/console-plugin-shared/src/datetime/prometheus.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import * as _ from 'lodash'; | ||
|
||
// Conversions between units and milliseconds | ||
const s = 1000; | ||
const m = s * 60; | ||
const h = m * 60; | ||
const d = h * 24; | ||
const w = d * 7; | ||
const units = { w, d, h, m, s }; | ||
|
||
/** | ||
* Converts a duration in milliseconds to a Prometheus time duration string like "1h 10m" | ||
* @param {number} ms - Time duration in milliseconds | ||
* @returns {string} The duration converted to a Prometheus time duration string | ||
* @example | ||
* ``` | ||
* formatPrometheusDuration(65000) // Returns "1m 5s" | ||
* ``` | ||
*/ | ||
export const formatPrometheusDuration = (ms: number) => { | ||
if (!_.isFinite(ms) || ms < 0) { | ||
return ''; | ||
} | ||
let remaining = ms; | ||
let str = ''; | ||
_.each(units, (factor, unit) => { | ||
const n = Math.floor(remaining / factor); | ||
if (n > 0) { | ||
str += `${n}${unit} `; | ||
remaining -= n * factor; | ||
} | ||
}); | ||
return _.trim(str); | ||
}; | ||
|
||
/** | ||
* Converts a Prometheus time duration like "1h 10m 23s" to milliseconds | ||
* @param {string} duration - Prometheus time duration string | ||
* @returns {number} The duration converted to a Prometheus time duration string or 0 if the duration could not be parsed | ||
* @example | ||
* ``` | ||
* parsePrometheusDuration("1m 5s") // Returns 65000 | ||
* ``` | ||
*/ | ||
export const parsePrometheusDuration = (duration: string): number => { | ||
try { | ||
const parts = duration | ||
.trim() | ||
.split(/\s+/) | ||
.map((p) => p.match(/^(\d+)([wdhms])$/)); | ||
return _.sumBy(parts, (p) => parseInt(p[1], 10) * units[p[2]]); | ||
} catch (ignored) { | ||
// Invalid duration format | ||
return 0; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './components'; | ||
export * from './datetime'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
...nes-plugin/src/components/pipelines/pipeline-metrics/PipelineMetricsTimeRangeDropdown.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...s/pipelines-plugin/src/components/pipelines/pipeline-metrics/PipelineRunDurationGraph.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ines-plugin/src/components/pipelines/pipeline-metrics/__tests__/PipelineRunCount.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...s-plugin/src/components/pipelines/pipeline-metrics/__tests__/PipelineRunDuration.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ugin/src/components/pipelines/pipeline-metrics/__tests__/PipelineRunTaskRunGraph.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...in/src/components/pipelines/pipeline-metrics/__tests__/PipelineSuccessRatioDonut.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 4 additions & 1 deletion
5
frontend/public/components/monitoring/poll-interval-dropdown.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.