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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the sorting option available in grafana in some way? or is not and we default to "asc"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#showLegend: *#panel.options.legend.showLegend | true
if #showLegend {
legend: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"mode": "list",
"position": "bottom"
},
"sorting": "asc",
"mappings": [
{
"kind": "Value",
Expand Down Expand Up @@ -56,4 +57,4 @@
}
]
}
}
}
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/tests/valid/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,6 +37,14 @@ export function StatusHistoryChartOptionsEditorSettings(props: StatusHistroyChar
);
};

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

return (
<OptionsEditorGrid>
<OptionsEditorColumn>
Expand All @@ -39,6 +54,7 @@ export function StatusHistoryChartOptionsEditorSettings(props: StatusHistroyChar
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>;
64 changes: 37 additions & 27 deletions statushistorychart/src/utils/data-transform.ts
Original file line number Diff line number Diff line change
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]);
}