1- // Copyright 2024 The Perses Authors
1+ // Copyright 2025 The Perses Authors
22// Licensed under the Apache License, Version 2.0 (the "License");
33// you may not use this file except in compliance with the License.
44// You may obtain a copy of the License at
@@ -50,7 +50,9 @@ export function useStatusHistoryDataModel(
5050 spec : StatusHistoryChartOptions
5151) : StatusHistoryDataModel {
5252 return useMemo ( ( ) => {
53- if ( ! queryResults || queryResults . length === 0 ) {
53+ const allQueriesResolved = queryResults . every ( ( q ) => ! q . isLoading && ! q . isFetching ) ;
54+
55+ if ( ! queryResults || queryResults . length === 0 || ! allQueriesResolved ) {
5456 return {
5557 legendItems : [ ] ,
5658 statusHistoryData : [ ] ,
@@ -60,6 +62,25 @@ export function useStatusHistoryDataModel(
6062 } ;
6163 }
6264
65+ const allSeries = queryResults . reduce < TimeSeriesData [ 'series' ] > ( ( acc , { data } ) => {
66+ if ( data && data . series ) {
67+ acc . push ( ...data . series ) ;
68+ }
69+ return acc ;
70+ } , [ ] ) ;
71+
72+ if ( spec . sorting ) {
73+ allSeries . sort ( ( a , b ) => {
74+ const nameA = a . formattedName || '' ;
75+ const nameB = b . formattedName || '' ;
76+ if ( spec . sorting === 'asc' ) {
77+ return nameA . localeCompare ( nameA ) ;
78+ } else {
79+ return nameB . localeCompare ( nameB ) ;
80+ }
81+ } ) ;
82+ }
83+
6384 const timeScale = getCommonTimeScaleForQueries ( queryResults ) ;
6485 const statusHistoryData : StatusHistoryDataItem [ ] = [ ] ;
6586 const yAxisCategories : string [ ] = [ ] ;
@@ -68,33 +89,24 @@ export function useStatusHistoryDataModel(
6889
6990 const xAxisCategories = generateCompleteTimestamps ( timeScale ) ;
7091
71- queryResults . forEach ( ( { data } ) => {
72- if ( ! data ) {
73- return ;
74- }
75-
76- data . series . forEach ( ( item ) => {
77- const instance = item . formattedName || '' ;
78-
79- yAxisCategories . push ( instance ) ;
80-
81- const yIndex = yAxisCategories . length - 1 ;
82-
83- item . values . forEach ( ( [ time , value ] ) => {
84- const itemIndexOnXaxis = xAxisCategories . findIndex ( ( v ) => v === time ) ;
85- if ( value !== null && itemIndexOnXaxis !== - 1 ) {
86- let itemLabel : string | number = value ;
87- if ( hasValueMappings ) {
88- const mappedValue = applyValueMapping ( value , spec . mappings ) ;
89- itemLabel = mappedValue . value ;
90- }
91- legendSet . add ( value ) ;
92- statusHistoryData . push ( {
93- value : [ itemIndexOnXaxis , yIndex , value ] ,
94- label : String ( itemLabel ) ,
95- } ) ;
92+ allSeries . forEach ( ( item ) => {
93+ const instance = item . formattedName || '' ;
94+ yAxisCategories . push ( instance ) ;
95+ const yIndex = yAxisCategories . length - 1 ;
96+ item . values . forEach ( ( [ time , value ] ) => {
97+ const itemIndexOnXaxis = xAxisCategories . findIndex ( ( v ) => v === time ) ;
98+ if ( value !== null && itemIndexOnXaxis !== - 1 ) {
99+ let itemLabel : string | number = value ;
100+ if ( hasValueMappings ) {
101+ const mappedValue = applyValueMapping ( value , spec . mappings ) ;
102+ itemLabel = mappedValue . value ;
96103 }
97- } ) ;
104+ legendSet . add ( value ) ;
105+ statusHistoryData . push ( {
106+ value : [ itemIndexOnXaxis , yIndex , value ] ,
107+ label : String ( itemLabel ) ,
108+ } ) ;
109+ }
98110 } ) ;
99111 } ) ;
100112
@@ -141,5 +153,5 @@ export function useStatusHistoryDataModel(
141153 timeScale,
142154 colors,
143155 } ;
144- } , [ queryResults , spec . mappings , themeColors ] ) ;
156+ } , [ queryResults , themeColors , spec ] ) ;
145157}
0 commit comments