Skip to content

Commit

Permalink
Add sort by number
Browse files Browse the repository at this point in the history
Allow sorting the legend by interpreting the labels as numbers if possible.
This is helpful when viewing era profiles, as these only have a number
label.
  • Loading branch information
fendor committed Jan 29, 2024
1 parent 8060561 commit de0d9a5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Eventlog/Args.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ data Option
= ShowVersion
| Run Args

data Sort = Size | StdDev | Name | Gradient
data Sort = Size | StdDev | Name | Gradient | Number

data Args = Args
{
Expand All @@ -47,7 +47,7 @@ argParser :: Parser Option
argParser = Run <$> (Args
<$> option parseSort
( long "sort"
<> help "How to sort the bands. One of: size (default), stddev, name, gradient."
<> help "How to sort the bands. One of: size (default), stddev, name, number, gradient."
<> value Size
<> metavar "FIELD" )
<*> switch
Expand Down Expand Up @@ -118,8 +118,9 @@ parseSort = eitherReader $ \s -> case s of
"size" -> Right Size
"stddev" -> Right StdDev
"name" -> Right Name
"number" -> Right Number
"gradient" -> Right Gradient
_ -> Left "expected one of: size, stddev, name"
_ -> Left "expected one of: size, stddev, name, number"

args :: IO Option
args = execParser argsInfo
Expand Down
9 changes: 9 additions & 0 deletions src/Eventlog/Prune.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE TypeApplications #-}
module Eventlog.Prune
( pruneBands, pruneDetailed
) where
Expand All @@ -11,6 +12,9 @@ import Data.Map (Map, fromList, (!), toList)

import Eventlog.Args (Args(..), Sort(..))
import Data.Maybe
import Data.Word (Word64)
import Text.Read (readMaybe)
import qualified Data.Text as T

type Compare a = a -> a -> Ordering

Expand All @@ -21,15 +25,20 @@ getComparison Args { sorting = StdDev, reversing = False } = cmpStdDevDescendin
getComparison Args { sorting = StdDev, reversing = True } = cmpStdDevAscending
getComparison Args { sorting = Name, reversing = True } = cmpNameDescending
getComparison Args { sorting = Name, reversing = False } = cmpNameAscending
getComparison Args { sorting = Number, reversing = True } = cmpNumberDescending
getComparison Args { sorting = Number, reversing = False } = cmpNumberAscending
getComparison Args { sorting = Gradient, reversing = True } = cmpGradientAscending
getComparison Args { sorting = Gradient, reversing = False } = cmpGradientDescending

cmpNameAscending, cmpNameDescending,
cmpNumberAscending, cmpNumberDescending,
cmpStdDevAscending, cmpStdDevDescending,
cmpSizeAscending, cmpSizeDescending,
cmpGradientAscending, cmpGradientDescending :: Compare (Bucket, BucketInfo)
cmpNameAscending = comparing fst
cmpNameDescending = flip cmpNameAscending
cmpNumberAscending (Bucket a, _) (Bucket b, _) = comparing (readMaybe @Word64 . T.unpack) a b <> compare a b
cmpNumberDescending = flip cmpNumberAscending
cmpStdDevAscending = comparing (bucketStddev . snd)
cmpStdDevDescending = flip cmpStdDevAscending
cmpSizeAscending = comparing (bucketTotal . snd)
Expand Down

0 comments on commit de0d9a5

Please sign in to comment.