Skip to content
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

Sort heatmap columns numerically #80

Closed
obaudys opened this issue Sep 2, 2022 · 3 comments
Closed

Sort heatmap columns numerically #80

obaudys opened this issue Sep 2, 2022 · 3 comments

Comments

@obaudys
Copy link
Contributor

obaudys commented Sep 2, 2022

First off, great work with the heatmaps feature!

I've been using heatmaps for numerical data, in particular nginx response times. I usually convert them it integers in rare by matching eg 0.053 (seconds) with (\d+)\.(\d{3}) and then using the expression {sumi {multi {1} 1000} {3}} to convert to milliseconds.

I've found that the table and heatmap sort the column names as strings, and not numerically if possible. The results in meaningless heatmaps:
image

I made a small change to a local checkout of rare to basically test if the column headers could be converted to integers and then sort them numerically if they can:

index 1be0be8..040b889 100644
--- a/pkg/aggregation/table.go
+++ b/pkg/aggregation/table.go
@@ -103,8 +103,23 @@ func (s *TableAggregator) OrderedColumns() []string {
 func (s *TableAggregator) OrderedColumnsByName() []string {
        keys := s.Columns()
 
+       // check if keys can be sorted numerically:
+       numeric := true
+       for _,k := range keys {
+               if _, err := strconv.Atoi(k); err != nil {
+                       numeric = false
+                       break
+               }
+       }
+
        sort.Slice(keys, func(i, j int) bool {
-               return keys[i] < keys[j]
+               if numeric {
+                       k0, _ := strconv.Atoi(keys[i])
+                       k1, _ := strconv.Atoi(keys[j])
+                       return k0 < k1
+               } else {
+                       return keys[i] < keys[j]
+               }
        })
 
        return keys

The same heatmap is now much more meaningful:
image

Would you be interested in incorporating the above diff into rare?

@zix99
Copy link
Owner

zix99 commented Sep 2, 2022

Hey, thanks for the feedback! Normally, I'd accept a diff like this very quickly. I'm currently working on display-sorting with #79 which completely overhauls sorting across all aggregators, and implements things like: string, by-value, numeric, date-parsing, etc. This also standardizes the sorting (Right now, sorting is slightly different on different aggregators, which only adds to the confusion)

You're welcome to check it out and see if it suits your needs (If not, let me know!) It's not quite ready to merge yet, I want to add the ability to sort rows and cols separately, as well as add documentation.

@obaudys
Copy link
Contributor Author

obaudys commented Sep 3, 2022

I've checked out the sorting branch. Heatmaps for my use case (response latencies) work out of the box for me with the "smart" sorting method. Thanks heaps for the awesome work! The code is a bit hard for me to understand, eg. what does by-value sorting actually mean? Make sure you include that in the documentation!

@zix99
Copy link
Owner

zix99 commented Sep 3, 2022

Glad it works for you!

I'll definitely include definitions in the docs.. I haven't landed on the exact naming yet, so it might change once I merge.

@zix99 zix99 closed this as completed Oct 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants