mirror of https://github.com/grafana/grafana
Field: getFieldTitle as field / series display identity and use it in all field name matchers & field / series name displays (#24024)
* common title handling * show labels * update comment * Update changelog for v7.0.0-beta1 (#24007) Co-Authored-By: Marcus Efraimsson <marcus.efraimsson@gmail.com> Co-Authored-By: Andrej Ocenas <mr.ocenas@gmail.com> Co-Authored-By: Hugo Häggmark <hugo.haggmark@grafana.com> Co-authored-by: Tobias Skarhed <1438972+tskarhed@users.noreply.github.com> * verify-repo-update: Fix Dockerfile.deb (#24030) Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com> * CircleCI: Upgrade build pipeline tool (#24021) * CircleCI: Upgrade build pipeline tool * Devenv: ignore enterprise (#24037) * Add header icon to Add data source page (#24033) * latest.json: Update testing version (#24038) Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com> * Fix login page redirected from password reset (#24032) * Storybook: Rewrite stories to CSF (#23989) * ColorPicker to CSF format * Convert stories to CSF * Do not export ClipboardButton * Update ConfirmButton * Remove unused imports * Fix feedback * changelog enterprise 7.0.0-beta1 (#24039) * CircleCI: Bump grafana/build-container revision (#24043) Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com> * Changelog: Updates changelog with more feature details (#24040) * Changelog: Updates changelog with more feature details * spell fix * spell fix * Updates * Readme update * Updates * Select: fixes so component loses focus on selecting value or pressing outside of input. (#24008) * changed the value container to a class component to get it to work with focus (maybe something with context?). * added e2e tests to verify that the select focus is working as it should. * fixed according to feedback. * updated snapshot. * Devenv: add remote renderer to grafana (#24050) * NewPanelEditor: minor UI twekas (#24042) * Forward ref for tabs, use html props * Inspect: add inspect label to drawer title * Add tooltips to sidebar pane tabs, copy changes * Remove unused import * Place tooltips over tabs * Inspector: dont show transformations select if there is only one data frame * Review * Changelog: Add a breaking change (#24051) Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com> * CircleCI: Unpin grafana/docs-base (#24054) Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com> * Search: close overlay on Esc press (#24003) * Search: Close on Esc * Search: Increase bottom padding for the last item in section * Search: Move closing search to keybindingsSrv * Search: Fix folder view * Search: Do not move folders if already in folder * Docs: Adds deprecation notice to changelog and docs for scripted dashboards (#24060) * Update CHANGELOG.md (#24047) Fix typo Co-authored-by: Daniel Lee <dan.limerick@gmail.com> * Documentation: Alternative Team Sync Wording (#23960) * Alternative wording for team sync docs Signed-off-by: Joe Elliott <number101010@gmail.com> * Update docs/sources/auth/team-sync.md Co-Authored-By: Diana Payton <52059945+oddlittlebird@users.noreply.github.com> Co-authored-by: Diana Payton <52059945+oddlittlebird@users.noreply.github.com> * Fix misspell issues (#23905) * Fix misspell issues See, $ golangci-lint run --timeout 10m --disable-all -E misspell ./... Signed-off-by: Mario Trangoni <mjtrangoni@gmail.com> * Fix codespell issues See, $ codespell -S './.git*' -L 'uint,thru,pres,unknwon,serie,referer,uptodate,durationm' Signed-off-by: Mario Trangoni <mjtrangoni@gmail.com> * ci please? * non-empty commit - ci? * Trigger build Co-authored-by: bergquist <carl.bergquist@gmail.com> Co-authored-by: Kyle Brandt <kyle@grafana.com> * fix compile error * better series display * better display * now with prometheus and loki * a few more tests * Improvements and tests * thinking * More advanced and smart default title generation * Another fix * Progress but dam this will be hard * Reverting the time series Value field name change * revert revert going in circles * add a field state object * Use state title when converting back to legacy format * Improved the join (series to columsn) transformer * Got tests running again * Rewrite of seriesToColums that simplifies and fixing tests * Fixed the tricky problem of multiple time field when not used in join * Prometheus: Restoring prometheus formatting * Graphite: Disable Grafana's series naming * fixed imports * Fixed tests and made rename transform change title instead * Fixing more tests * fix more tests * fixed import issue * Fixed more circular dependencies * Renamed to getFieldTitle * More rename * Review feedback * Fix for showing field title in calculate field transformer * fieldOverride: Make it clear that state title after applying defaults & overrides * Fixed ts issue * Update packages/grafana-ui/src/components/TransformersUI/OrganizeFieldsTransformerEditor.tsx Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com> Co-authored-by: Andrej Ocenas <mr.ocenas@gmail.com> Co-authored-by: Hugo Häggmark <hugo.haggmark@grafana.com> Co-authored-by: Tobias Skarhed <1438972+tskarhed@users.noreply.github.com> Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com> Co-authored-by: Leonard Gram <leo@xlson.com> Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> Co-authored-by: Alexander Zobnin <alexanderzobnin@gmail.com> Co-authored-by: Torkel Ödegaard <torkel@grafana.com> Co-authored-by: Marcus Andersson <marcus.andersson@grafana.com> Co-authored-by: Dominik Prokop <dominik.prokop@grafana.com> Co-authored-by: Alex Khomenko <Clarity-89@users.noreply.github.com> Co-authored-by: Richard Hartmann <RichiH@users.noreply.github.com> Co-authored-by: Daniel Lee <dan.limerick@gmail.com> Co-authored-by: Joe Elliott <joe.elliott@grafana.com> Co-authored-by: Diana Payton <52059945+oddlittlebird@users.noreply.github.com> Co-authored-by: Mario Trangoni <mario@mariotrangoni.de> Co-authored-by: bergquist <carl.bergquist@gmail.com> Co-authored-by: Kyle Brandt <kyle@grafana.com>pull/24403/head
parent
184941eab4
commit
5dca59f720
@ -0,0 +1,136 @@ |
||||
import { DataFrame, TIME_SERIES_FIELD_NAME, FieldType } from '../types'; |
||||
import { getFieldTitle } from './fieldState'; |
||||
import { toDataFrame } from '../dataframe'; |
||||
|
||||
interface TitleScenario { |
||||
frames: DataFrame[]; |
||||
frameIndex?: number; // assume 0
|
||||
fieldIndex?: number; // assume 0
|
||||
} |
||||
|
||||
function checkScenario(scenario: TitleScenario): string { |
||||
const frame = scenario.frames[scenario.frameIndex ?? 0]; |
||||
const field = frame.fields[scenario.fieldIndex ?? 0]; |
||||
return getFieldTitle(field, frame, scenario.frames); |
||||
} |
||||
|
||||
describe('Check field state calculations (title and id)', () => { |
||||
it('should use field name if no frame name', () => { |
||||
const title = checkScenario({ |
||||
frames: [ |
||||
toDataFrame({ |
||||
fields: [{ name: 'Field 1' }], |
||||
}), |
||||
], |
||||
}); |
||||
expect(title).toEqual('Field 1'); |
||||
}); |
||||
|
||||
it('should use only field name if only one series', () => { |
||||
const title = checkScenario({ |
||||
frames: [ |
||||
toDataFrame({ |
||||
name: 'Series A', |
||||
fields: [{ name: 'Field 1' }], |
||||
}), |
||||
], |
||||
}); |
||||
expect(title).toEqual('Field 1'); |
||||
}); |
||||
|
||||
it('should use frame name and field name if more than one frame', () => { |
||||
const title = checkScenario({ |
||||
frames: [ |
||||
toDataFrame({ |
||||
name: 'Series A', |
||||
fields: [{ name: 'Field 1' }], |
||||
}), |
||||
toDataFrame({ |
||||
name: 'Series B', |
||||
fields: [{ name: 'Field 1' }], |
||||
}), |
||||
], |
||||
}); |
||||
expect(title).toEqual('Series A Field 1'); |
||||
}); |
||||
|
||||
it('should only use label value if only one label', () => { |
||||
const title = checkScenario({ |
||||
frames: [ |
||||
toDataFrame({ |
||||
fields: [{ name: 'Value', labels: { server: 'Server A' } }], |
||||
}), |
||||
], |
||||
}); |
||||
expect(title).toEqual('Server A'); |
||||
}); |
||||
|
||||
it('should use label value only if all series have same name', () => { |
||||
const title = checkScenario({ |
||||
frames: [ |
||||
toDataFrame({ |
||||
name: 'cpu', |
||||
fields: [{ name: 'Value', labels: { server: 'Server A' } }], |
||||
}), |
||||
toDataFrame({ |
||||
name: 'cpu', |
||||
fields: [{ name: 'Value', labels: { server: 'Server A' } }], |
||||
}), |
||||
], |
||||
}); |
||||
expect(title).toEqual('Server A'); |
||||
}); |
||||
|
||||
it('should use label name and value if more than one label', () => { |
||||
const title = checkScenario({ |
||||
frames: [ |
||||
toDataFrame({ |
||||
fields: [{ name: 'Value', labels: { server: 'Server A', mode: 'B' } }], |
||||
}), |
||||
], |
||||
}); |
||||
expect(title).toEqual('{mode="B", server="Server A"}'); |
||||
}); |
||||
|
||||
it('should use field name even when it is TIME_SERIES_FIELD_NAME if there are no labels', () => { |
||||
const title = checkScenario({ |
||||
frames: [ |
||||
toDataFrame({ |
||||
fields: [{ name: TIME_SERIES_FIELD_NAME, labels: {} }], |
||||
}), |
||||
], |
||||
}); |
||||
expect(title).toEqual('Value'); |
||||
}); |
||||
|
||||
it('should use series name when field name is TIME_SERIES_FIELD_NAME and there are no labels ', () => { |
||||
const title = checkScenario({ |
||||
frames: [ |
||||
toDataFrame({ |
||||
name: 'Series A', |
||||
fields: [{ name: TIME_SERIES_FIELD_NAME, labels: {} }], |
||||
}), |
||||
], |
||||
}); |
||||
expect(title).toEqual('Series A'); |
||||
}); |
||||
|
||||
it('should reder loki frames', () => { |
||||
const title = checkScenario({ |
||||
frames: [ |
||||
toDataFrame({ |
||||
refId: 'A', |
||||
fields: [ |
||||
{ name: 'time', type: FieldType.time }, |
||||
{ |
||||
name: 'line', |
||||
labels: { host: 'ec2-13-53-116-156.eu-north-1.compute.amazonaws.com', region: 'eu-north1' }, |
||||
}, |
||||
], |
||||
}), |
||||
], |
||||
fieldIndex: 1, |
||||
}); |
||||
expect(title).toEqual('line {host="ec2-13-53-116-156.eu-north-1.compute.amazonaws.com", region="eu-north1"}'); |
||||
}); |
||||
}); |
||||
@ -0,0 +1,153 @@ |
||||
import { DataFrame, Field, TIME_SERIES_FIELD_NAME, FieldType } from '../types'; |
||||
import { formatLabels } from '../utils/labels'; |
||||
|
||||
/** |
||||
* Get an appropriate display title |
||||
*/ |
||||
export function getFrameDisplayTitle(frame: DataFrame, index?: number) { |
||||
if (frame.name) { |
||||
return frame.name; |
||||
} |
||||
|
||||
// Single field with tags
|
||||
const valuesWithLabels = frame.fields.filter(f => f.labels !== undefined); |
||||
if (valuesWithLabels.length === 1) { |
||||
return formatLabels(valuesWithLabels[0].labels!); |
||||
} |
||||
|
||||
// list all the
|
||||
if (index === undefined) { |
||||
return frame.fields |
||||
.filter(f => f.type !== FieldType.time) |
||||
.map(f => getFieldTitle(f, frame)) |
||||
.join(', '); |
||||
} |
||||
|
||||
if (frame.refId) { |
||||
return `Series (${frame.refId})`; |
||||
} |
||||
|
||||
return `Series (${index})`; |
||||
} |
||||
|
||||
export function getFieldTitle(field: Field, frame?: DataFrame, allFrames?: DataFrame[]): string { |
||||
const existingTitle = field.state?.title; |
||||
|
||||
if (existingTitle) { |
||||
return existingTitle; |
||||
} |
||||
|
||||
const title = calculateFieldTitle(field, frame, allFrames); |
||||
field.state = { |
||||
...field.state, |
||||
title, |
||||
}; |
||||
|
||||
return title; |
||||
} |
||||
|
||||
/** |
||||
* Get an appropriate display title. If the 'title' is set, use that |
||||
*/ |
||||
function calculateFieldTitle(field: Field, frame?: DataFrame, allFrames?: DataFrame[]): string { |
||||
const hasConfigTitle = field.config?.title && field.config?.title.length; |
||||
|
||||
let title = hasConfigTitle ? field.config!.title! : field.name; |
||||
|
||||
if (hasConfigTitle) { |
||||
return title; |
||||
} |
||||
|
||||
// This is an ugly exception for time field
|
||||
// For time series we should normally treat time field with same name
|
||||
// But in case it has a join source we should handle it as normal field
|
||||
if (field.type === FieldType.time && !field.labels) { |
||||
return title ?? 'Time'; |
||||
} |
||||
|
||||
let parts: string[] = []; |
||||
let frameNamesDiffer = false; |
||||
|
||||
if (allFrames && allFrames.length > 1) { |
||||
for (let i = 1; i < allFrames.length; i++) { |
||||
const frame = allFrames[i]; |
||||
if (frame.name !== allFrames[i - 1].name) { |
||||
frameNamesDiffer = true; |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
|
||||
let frameNameAdded = false; |
||||
let labelsAdded = false; |
||||
|
||||
if (frameNamesDiffer && frame?.name) { |
||||
parts.push(frame.name); |
||||
frameNameAdded = true; |
||||
} |
||||
|
||||
if (field.name && field.name !== TIME_SERIES_FIELD_NAME) { |
||||
parts.push(field.name); |
||||
} |
||||
|
||||
if (field.labels && frame) { |
||||
let singleLabelName = getSingleLabelName(allFrames ?? [frame]); |
||||
|
||||
if (!singleLabelName) { |
||||
let allLabels = formatLabels(field.labels); |
||||
if (allLabels) { |
||||
parts.push(allLabels); |
||||
labelsAdded = true; |
||||
} |
||||
} else if (field.labels[singleLabelName]) { |
||||
parts.push(field.labels[singleLabelName]); |
||||
labelsAdded = true; |
||||
} |
||||
} |
||||
|
||||
// if we have not added frame name and no labels, and field name = Value, we should add frame name
|
||||
if (frame && !frameNameAdded && !labelsAdded && field.name === TIME_SERIES_FIELD_NAME) { |
||||
if (frame.name && frame.name.length > 0) { |
||||
parts.push(frame.name); |
||||
frameNameAdded = true; |
||||
} |
||||
} |
||||
|
||||
if (parts.length) { |
||||
title = parts.join(' '); |
||||
} else if (field.name) { |
||||
title = field.name; |
||||
} else { |
||||
title = TIME_SERIES_FIELD_NAME; |
||||
} |
||||
|
||||
return title; |
||||
} |
||||
|
||||
/** |
||||
* Checks all data frames and return name of label if there is only one label name in all frames |
||||
*/ |
||||
function getSingleLabelName(frames: DataFrame[]): string | null { |
||||
let singleName: string | null = null; |
||||
|
||||
for (let i = 0; i < frames.length; i++) { |
||||
const frame = frames[i]; |
||||
|
||||
for (const field of frame.fields) { |
||||
if (!field.labels) { |
||||
continue; |
||||
} |
||||
|
||||
// yes this should be in!
|
||||
for (const labelKey in field.labels) { |
||||
if (singleName === null) { |
||||
singleName = labelKey; |
||||
} else if (labelKey !== singleName) { |
||||
return null; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
return singleName; |
||||
} |
||||
Loading…
Reference in new issue