generated from gapitio/utility-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ♻️ add type condtition to get time field * ✅ add tests to time field conditions * fix: fix wrong time returned The time field is not retrieved when there are multiple series and the data is retrieved based on the field name instead of the series name. Co-authored-by: Flesaker <[email protected]>
- Loading branch information
Showing
5 changed files
with
316 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export { ReducerID } from "./utils/field"; | ||
export * from "./utils/metricValue"; | ||
export * from "./utils/metricData"; | ||
export * from "./utils/getDataFieldsFromName"; | ||
export * from "./utils/getFieldFromName"; | ||
export * from "./utils/getSeriesFromName"; | ||
export * from "./utils/evaluateString"; |
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,87 @@ | ||
import { DataFrame, Field, PanelData } from "@grafana/data"; | ||
|
||
import { SearchOptions } from "./getFieldFromName"; | ||
import { getSeriesFromName } from "./getSeriesFromName"; | ||
|
||
declare const data: PanelData; | ||
|
||
export interface DataFields { | ||
valueField?: Field; | ||
timeField?: Field; | ||
} | ||
|
||
export interface DataFieldOptions extends SearchOptions { | ||
/** | ||
* Get time field | ||
* | ||
* @default true | ||
*/ | ||
getTime?: boolean; | ||
} | ||
|
||
function getValueField( | ||
series: DataFrame, | ||
name: string, | ||
{ searchLabels = true }: SearchOptions = {} | ||
) { | ||
return series.fields.find((field) => | ||
[ | ||
field.name, | ||
...(searchLabels && field.labels ? [field.labels.name] : []), | ||
].includes(name) | ||
); | ||
} | ||
|
||
function getTimeField(series: DataFrame) { | ||
return ( | ||
series.fields.find((field) => field.type == "time") ?? | ||
series.fields.find((field) => field.name == "Time" || field.name == "time") | ||
); | ||
} | ||
|
||
function getSeriesAndValueField( | ||
name: string, | ||
{ searchLabels = true }: SearchOptions = {} | ||
) { | ||
const series = getSeriesFromName(name); | ||
if (series) { | ||
const valueField = | ||
getValueField(series, "Value", { searchLabels: false }) ?? | ||
getValueField(series, name, { searchLabels }); | ||
|
||
return { series, valueField }; | ||
} | ||
|
||
for (const series of data.series) { | ||
const valueField = getValueField(series, name, { searchLabels }); | ||
if (valueField) return { series, valueField }; | ||
} | ||
|
||
return {}; | ||
} | ||
|
||
/** | ||
* Gets the series that contains the name (searches through series, fields and labels) | ||
* | ||
* @example | ||
* ```ts | ||
* getDataFieldsFromName("series-name"); | ||
* ``` | ||
* | ||
* @param name | ||
* @param DataFieldOptions | ||
* | ||
* @returns value and time field | ||
*/ | ||
export function getDataFieldsFromName( | ||
name: string, | ||
{ searchLabels = true, getTime = true }: DataFieldOptions = {} | ||
): DataFields | Record<string, never> { | ||
const { series, valueField } = getSeriesAndValueField(name, { searchLabels }); | ||
if (series && valueField) | ||
return { | ||
valueField, | ||
timeField: getTime ? getTimeField(series) : undefined, | ||
}; | ||
return {}; | ||
} |
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.