Skip to content

Commit

Permalink
Timeseries panels: Map field display names to color (#937)
Browse files Browse the repository at this point in the history
* feat: Map series name to color
  • Loading branch information
gtk-grafana authored Dec 10, 2024
1 parent 58ce56e commit b0b25df
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Components/ServiceScene/ServiceScene.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import {
import { replaceSlash } from '../../services/extensions/links';
import { ShowLogsButtonScene } from '../IndexScene/ShowLogsButtonScene';

const LOGS_PANEL_QUERY_REFID = 'logsPanelQuery';
export const LOGS_PANEL_QUERY_REFID = 'logsPanelQuery';
const PATTERNS_QUERY_REFID = 'patterns';
const DETECTED_LABELS_QUERY_REFID = 'detectedLabels';
const DETECTED_FIELDS_QUERY_REFID = 'detectedFields';
Expand Down
2 changes: 0 additions & 2 deletions src/services/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
SceneDataTransformer,
SceneObject,
} from '@grafana/scenes';
import { getColorByIndex } from './scenes';
import { AddToFiltersButton, VariableFilterType } from 'Components/ServiceScene/Breakdowns/AddToFiltersButton';
import {
DetectedFieldType,
Expand Down Expand Up @@ -148,7 +147,6 @@ export function getFilterBreakdownValueScene(
transformations: [() => selectFrameTransformation(frame)],
})
)
.setColor({ mode: 'fixed', fixedColor: getColorByIndex(frameIndex) })
.setOverrides(setLevelColorOverrides)
.setMenu(new PanelMenu({ frame, fieldName: getTitle(frame), labelName: labelKey }))
.setHeaderActions([new AddToFiltersButton({ frame, variableName })]);
Expand Down
53 changes: 52 additions & 1 deletion src/services/panel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { DataFrame, FieldConfig, FieldMatcherID } from '@grafana/data';
import {
DataFrame,
FieldColorModeId,
FieldConfig,
FieldMatcherID,
FieldType,
getFieldDisplayName,
} from '@grafana/data';
import {
FieldConfigBuilder,
FieldConfigBuilders,
Expand All @@ -18,6 +25,7 @@ import { LogsSceneQueryRunner } from './LogsSceneQueryRunner';
import { DrawStyle, StackingMode } from '@grafana/ui';
import { getLabelsFromSeries, getVisibleLevels } from './levels';
import { LokiQuery } from './lokiQuery';
import { LOGS_PANEL_QUERY_REFID } from '../Components/ServiceScene/ServiceScene';

const UNKNOWN_LEVEL_LOGS = 'logs';
export function setLevelColorOverrides(overrides: FieldConfigOverridesBuilder<FieldConfig>) {
Expand Down Expand Up @@ -93,6 +101,36 @@ export function syncLogsPanelVisibleSeries(panel: VizPanel, series: DataFrame[],
}
}
}
function setColorByDisplayNameTransformation() {
return (source: Observable<DataFrame[]>) => {
return source.pipe(
map((data: DataFrame[]) => {
return data.map((frame, frameIndex) => {
return {
...frame,
fields: frame.fields.map((f, fieldIndex) => {
// Time fields do not have color config
if (f.type === FieldType.time) {
return f;
}
const displayName = getFieldDisplayName(f, frame, data);
return {
...f,
config: {
...f.config,
displayName,
color: {
mode: FieldColorModeId.PaletteClassicByName,
},
},
};
}),
};
});
})
);
};
}

export function sortLevelTransformation() {
return (source: Observable<DataFrame[]>) => {
Expand Down Expand Up @@ -137,6 +175,7 @@ export function getQueryRunner(queries: LokiQuery[], queryRunnerOptions?: Partia
// `level` label.

const hasLevel = queries.find((query) => query.legendFormat?.toLowerCase().includes('level'));
const isLogPanelQuery = queries.find((query) => query.refId === LOGS_PANEL_QUERY_REFID);

if (hasLevel) {
return new SceneDataTransformer({
Expand All @@ -149,7 +188,19 @@ export function getQueryRunner(queries: LokiQuery[], queryRunnerOptions?: Partia
});
}

if (!isLogPanelQuery) {
return new SceneDataTransformer({
$data: getSceneQueryRunner({
datasource: { uid: WRAPPED_LOKI_DS_UID },
queries: queries,
...queryRunnerOptions,
}),
transformations: [setColorByDisplayNameTransformation],
});
}

return getSceneQueryRunner({
datasource: { uid: WRAPPED_LOKI_DS_UID },
queries: queries,
...queryRunnerOptions,
});
Expand Down

0 comments on commit b0b25df

Please sign in to comment.