From de0d9a5ee3f6b0d6f342deacc3e6d607cc20a75b Mon Sep 17 00:00:00 2001 From: Fendor Date: Wed, 24 Jan 2024 10:30:14 +0100 Subject: [PATCH] Add sort by number 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. --- src/Eventlog/Args.hs | 7 ++++--- src/Eventlog/Prune.hs | 9 +++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Eventlog/Args.hs b/src/Eventlog/Args.hs index d17e628..dbb4dfc 100644 --- a/src/Eventlog/Args.hs +++ b/src/Eventlog/Args.hs @@ -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 { @@ -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 @@ -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 diff --git a/src/Eventlog/Prune.hs b/src/Eventlog/Prune.hs index 76564f2..09bfd73 100644 --- a/src/Eventlog/Prune.hs +++ b/src/Eventlog/Prune.hs @@ -1,5 +1,6 @@ {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ViewPatterns #-} +{-# LANGUAGE TypeApplications #-} module Eventlog.Prune ( pruneBands, pruneDetailed ) where @@ -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 @@ -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)