-
Notifications
You must be signed in to change notification settings - Fork 64
Custom Traces- Sorting/Toast #2397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,6 +79,12 @@ | |
const [pageIndex, setPageIndex] = useState(0); | ||
const [pageSize, setPageSize] = useState(10); | ||
|
||
const getDefaultSort = () => { | ||
return tracesTableMode === 'traces' | ||
? { field: 'last_updated', direction: 'desc' as const } | ||
: { field: 'endTime', direction: 'desc' as const }; | ||
}; | ||
|
||
const onSort = (sortColumns: Array<{ id: string; direction: 'desc' | 'asc' }>) => { | ||
if (!sortColumns || sortColumns.length === 0) { | ||
setSortingColumns([]); | ||
|
@@ -95,43 +101,40 @@ | |
|
||
setSortingColumns(sortColumns); | ||
|
||
const localOnlyFields = ['trace_group', 'percentile_in_trace_group', 'trace_id']; | ||
|
||
if (tracesTableMode === 'traces') { | ||
const sortedItems = [...tableItems].sort((a, b) => { | ||
let valueA = a[sortField]; | ||
let valueB = b[sortField]; | ||
|
||
if (sortField === 'last_updated') { | ||
const dateA = new Date(valueA); | ||
const dateB = new Date(valueB); | ||
|
||
const isValidA = !isNaN(dateA.getTime()); | ||
const isValidB = !isNaN(dateB.getTime()); | ||
|
||
// Treat invalid dates as the lowest value | ||
valueA = isValidA ? dateA.getTime() : -Infinity; | ||
valueB = isValidB ? dateB.getTime() : -Infinity; | ||
} else if (sortField === 'trace_group') { | ||
valueA = typeof valueA === 'string' ? valueA.toLowerCase() : ''; | ||
valueB = typeof valueB === 'string' ? valueB.toLowerCase() : ''; | ||
} | ||
|
||
if (typeof valueA === 'number' && typeof valueB === 'number') { | ||
return sortDirection === 'asc' ? valueA - valueB : valueB - valueA; | ||
} | ||
|
||
if (typeof valueA === 'string' && typeof valueB === 'string') { | ||
return sortDirection === 'asc' | ||
? valueA.localeCompare(valueB) | ||
: valueB.localeCompare(valueA); | ||
} | ||
|
||
return 0; | ||
}); | ||
// Client-side sort if field is local-only | ||
if (localOnlyFields.includes(sortField)) { | ||
const sorted = [...tableItems].sort((a, b) => { | ||
let valueA = a[sortField]; | ||
let valueB = b[sortField]; | ||
|
||
if (typeof valueA === 'string' && typeof valueB === 'string') { | ||
valueA = valueA.toLowerCase(); | ||
valueB = valueB.toLowerCase(); | ||
return sortDirection === 'asc' | ||
? valueA.localeCompare(valueB) | ||
: valueB.localeCompare(valueA); | ||
} | ||
|
||
if (typeof valueA === 'number' && typeof valueB === 'number') { | ||
return sortDirection === 'asc' ? valueA - valueB : valueB - valueA; | ||
} | ||
|
||
return 0; | ||
}); | ||
|
||
setTableItems(sorted); | ||
return; | ||
} | ||
|
||
setTableItems(sortedItems); | ||
// Server-side sort for supported fields | ||
const sort = { field: sortField, direction: sortDirection }; | ||
refreshTracesTableData(sort, 0, pageSize); | ||
} else { | ||
const { DSL, isUnderOneHour } = generateDSLs(); | ||
refreshTableDataOnly(pageIndex, pageSize, DSL, isUnderOneHour, { | ||
refreshSpanTableData(pageIndex, pageSize, DSL, isUnderOneHour, { | ||
field: sortField, | ||
direction: sortDirection, | ||
}); | ||
|
@@ -168,7 +171,7 @@ | |
const currentSort = sortingColumns[0] | ||
? { field: sortingColumns[0].id, direction: sortingColumns[0].direction } | ||
: undefined; | ||
refreshTableDataOnly(newPage, pageSize, DSL, isUnderOneHour, currentSort); | ||
refreshSpanTableData(newPage, pageSize, DSL, isUnderOneHour, currentSort); | ||
} | ||
}, | ||
onChangeItemsPerPage: (newSize) => { | ||
|
@@ -179,7 +182,7 @@ | |
const currentSort = sortingColumns[0] | ||
? { field: sortingColumns[0].id, direction: sortingColumns[0].direction } | ||
: undefined; | ||
refreshTableDataOnly(0, newSize, DSL, isUnderOneHour, currentSort); | ||
refreshSpanTableData(0, newSize, DSL, isUnderOneHour, currentSort); | ||
} | ||
}, | ||
}; | ||
|
@@ -252,16 +255,17 @@ | |
setFilters(newFilters); | ||
}; | ||
|
||
const refreshTableDataOnly = async ( | ||
const refreshSpanTableData = async ( | ||
newPageIndex: number, | ||
newPageSize: number, | ||
DSL: any, | ||
isUnderOneHour: boolean, | ||
sortParams?: { field: string; direction: 'desc' | 'asc' } | ||
) => { | ||
setPageIndex(newPageIndex); | ||
setPageSize(newPageSize); | ||
setIsTraceTableLoading(true); | ||
const sort = sortParams ?? getDefaultSort(); | ||
|
||
handleCustomIndicesTracesRequest( | ||
http, | ||
|
@@ -273,12 +277,57 @@ | |
newPageSize, | ||
setTotalHits, | ||
props.dataSourceMDSId[0]?.id, | ||
sortParams, | ||
sort, | ||
tracesTableMode, | ||
isUnderOneHour | ||
).finally(() => setIsTraceTableLoading(false)); | ||
}; | ||
|
||
const refreshTracesTableData = async ( | ||
sortParams?: { field: string; direction: 'desc' | 'asc' }, | ||
newPageIndex: number = pageIndex, | ||
newPageSize: number = pageSize | ||
) => { | ||
setPageIndex(newPageIndex); | ||
setPageSize(newPageSize); | ||
setIsTraceTableLoading(true); | ||
const sort = sortParams ?? getDefaultSort(); | ||
|
||
const DSL = filtersToDsl( | ||
mode, | ||
filters, | ||
query, | ||
processTimeStamp(startTime, mode), | ||
processTimeStamp(endTime, mode), | ||
page, | ||
appConfigs | ||
); | ||
|
||
const timeFilterDSL = filtersToDsl( | ||
mode, | ||
[], | ||
'', | ||
processTimeStamp(startTime, mode), | ||
processTimeStamp(endTime, mode), | ||
page | ||
); | ||
|
||
const isUnderOneHour = datemath.parse(endTime)?.diff(datemath.parse(startTime), 'hours')! < 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. util functions like this can be extracted, i see the same logic is also used in other places There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added helper function isUnderOneHourRange
|
||
|
||
await handleTracesRequest( | ||
http, | ||
DSL, | ||
timeFilterDSL, | ||
tableItems, | ||
setTableItems, | ||
mode, | ||
maxTraces, | ||
props.dataSourceMDSId[0].id, | ||
sort, | ||
isUnderOneHour | ||
).finally(() => setIsTraceTableLoading(false)); | ||
}; | ||
|
||
const refresh = async ( | ||
sort?: PropertySort, | ||
overrideQuery?: string, | ||
|
@@ -304,6 +353,7 @@ | |
page | ||
); | ||
const isUnderOneHour = datemath.parse(endTime)?.diff(datemath.parse(startTime), 'hours')! < 1; | ||
const newSort = sort ?? getDefaultSort(); | ||
|
||
setIsTraceTableLoading(true); | ||
|
||
|
@@ -311,7 +361,7 @@ | |
// Remove serviceName filter from service map query | ||
const serviceMapDSL = cloneDeep(DSL); | ||
serviceMapDSL.query.bool.must = serviceMapDSL.query.bool.must.filter( | ||
(must: any) => !must?.term?.serviceName | ||
); | ||
|
||
const tracesRequest = | ||
|
@@ -326,7 +376,7 @@ | |
newPageSize, | ||
setTotalHits, | ||
props.dataSourceMDSId[0]?.id, | ||
sort, | ||
newSort, | ||
tracesTableMode, | ||
isUnderOneHour | ||
) | ||
|
@@ -339,7 +389,7 @@ | |
mode, | ||
maxTraces, | ||
props.dataSourceMDSId[0].id, | ||
sort, | ||
newSort, | ||
isUnderOneHour | ||
); | ||
tracesRequest.finally(() => setIsTraceTableLoading(false)); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe add some comments to explain what local only means, for future reference
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added comment
// The columns that can not be used in a query, only rendered on page