Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions statushistorychart/schemas/migrate/migrate.cue
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import "list"

kind: "StatusHistoryChart"
spec: {
sorting: "asc"
#showLegend: *#panel.options.legend.showLegend | true
if #showLegend {
legend: {
Expand Down
1 change: 1 addition & 0 deletions statushistorychart/schemas/status-history.cue
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ kind: "StatusHistoryChart"
spec: close({
legend?: common.#legend
mappings?: [...common.#mappings]
sorting?: "asc" | "desc"
})
3 changes: 2 additions & 1 deletion statushistorychart/schemas/status-history.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"spec": {
"legend": {
"position": "bottom"
}
},
"sorting": "asc"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@

import { LegendOptionsEditor, LegendOptionsEditorProps } from '@perses-dev/plugin-system';
import { produce } from 'immer';
import { OptionsEditorGroup, OptionsEditorGrid, OptionsEditorColumn } from '@perses-dev/components';
import {
OptionsEditorGroup,
OptionsEditorGrid,
OptionsEditorColumn,
SortSelector,
SortOption,
SortSelectorProps,
} from '@perses-dev/components';
import { Button } from '@mui/material';
import { ReactElement } from 'react';
import { StatusHistoryChartOptions, StatusHistroyChartEditorProps } from './status-history-model.js';
Expand All @@ -30,10 +37,19 @@ export function StatusHistoryChartOptionsEditorSettings(props: StatusHistroyChar
);
};

const handleSortChange: SortSelectorProps['onChange'] = (newSort: SortOption) => {
onChange(
produce(value, (draft: StatusHistoryChartOptions) => {
draft.sorting = newSort;
})
);
};

return (
<OptionsEditorGrid>
<OptionsEditorColumn>
<LegendOptionsEditor showValuesEditor={false} value={value.legend} onChange={handleLegendChange} />
<SortSelector value={value.sorting} onChange={handleSortChange} />
</OptionsEditorColumn>
<OptionsEditorColumn>
<OptionsEditorGroup title="Reset Settings">
Expand Down
3 changes: 3 additions & 0 deletions statushistorychart/src/status-history-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export function createInitialStatusHistoryChartOptions(): Record<string, unknown
export interface StatusHistoryChartOptions {
legend?: LegendSpecOptions;
mappings?: ValueMapping[];
sorting?: StatusHistorySorting;
}

export type StatusHistorySorting = 'asc' | 'desc';

export type StatusHistroyChartEditorProps = OptionsEditorProps<StatusHistoryChartOptions>;
66 changes: 38 additions & 28 deletions statushistorychart/src/utils/data-transform.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 The Perses Authors
// Copyright 2025 The Perses Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -60,6 +60,25 @@ export function useStatusHistoryDataModel(
};
}

const allSeries = queryResults.reduce<TimeSeriesData['series']>((acc, { data }) => {
if (data && data.series) {
acc.push(...data.series);
}
return acc;
}, []);

if (spec.sorting) {
allSeries.sort((a, b) => {
const nameA = a.formattedName || '';
const nameB = b.formattedName || '';
if (spec.sorting === 'asc') {
return nameA.localeCompare(nameA);
} else {
return nameB.localeCompare(nameB);
}
});
}

const timeScale = getCommonTimeScaleForQueries(queryResults);
const statusHistoryData: StatusHistoryDataItem[] = [];
const yAxisCategories: string[] = [];
Expand All @@ -68,33 +87,24 @@ export function useStatusHistoryDataModel(

const xAxisCategories = generateCompleteTimestamps(timeScale);

queryResults.forEach(({ data }) => {
if (!data) {
return;
}

data.series.forEach((item) => {
const instance = item.formattedName || '';

yAxisCategories.push(instance);

const yIndex = yAxisCategories.length - 1;

item.values.forEach(([time, value]) => {
const itemIndexOnXaxis = xAxisCategories.findIndex((v) => v === time);
if (value !== null && itemIndexOnXaxis !== -1) {
let itemLabel: string | number = value;
if (hasValueMappings) {
const mappedValue = applyValueMapping(value, spec.mappings);
itemLabel = mappedValue.value;
}
legendSet.add(value);
statusHistoryData.push({
value: [itemIndexOnXaxis, yIndex, value],
label: String(itemLabel),
});
allSeries.forEach((item) => {
const instance = item.formattedName || '';
yAxisCategories.push(instance);
const yIndex = yAxisCategories.length - 1;
item.values.forEach(([time, value]) => {
const itemIndexOnXaxis = xAxisCategories.findIndex((v) => v === time);
if (value !== null && itemIndexOnXaxis !== -1) {
let itemLabel: string | number = value;
if (hasValueMappings) {
const mappedValue = applyValueMapping(value, spec.mappings);
itemLabel = mappedValue.value;
}
});
legendSet.add(value);
statusHistoryData.push({
value: [itemIndexOnXaxis, yIndex, value],
label: String(itemLabel),
});
}
});
});

Expand Down Expand Up @@ -141,5 +151,5 @@ export function useStatusHistoryDataModel(
timeScale,
colors,
};
}, [queryResults, spec.mappings, themeColors]);
}, [queryResults, themeColors, spec]);
}