From f00f05c6295e03fb5760d8ace4f73efbf668815a Mon Sep 17 00:00:00 2001 From: Danila Danko Date: Mon, 29 Jul 2024 20:06:17 +0300 Subject: [PATCH 1/3] refactor(normalizer): use constants --- .../src/Language/EO/Phi/Report/Html.hs | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/eo-phi-normalizer/src/Language/EO/Phi/Report/Html.hs b/eo-phi-normalizer/src/Language/EO/Phi/Report/Html.hs index 6986cb7c..94c99f6f 100644 --- a/eo-phi-normalizer/src/Language/EO/Phi/Report/Html.hs +++ b/eo-phi-normalizer/src/Language/EO/Phi/Report/Html.hs @@ -100,11 +100,15 @@ instance ToMarkup Percent where -- "N/A\128995" toHtmlChange :: (ToMarkup a) => ReportFormat -> MetricsChangeCategory a -> Html toHtmlChange reportFormat = \case - MetricsChange'NA -> td ! class_ "number not-applicable" $ toHtml ("N/A" :: String) <> toHtml ['🟣' | isMarkdown] - MetricsChange'Bad{..} -> td ! class_ "number bad" $ toHtml change <> toHtml ['🔴' | isMarkdown] - MetricsChange'Good{..} -> td ! class_ "number good" $ toHtml change <> toHtml ['🟢' | isMarkdown] + MetricsChange'NA -> td ! class_ [fmt|{number} not-applicable|] $ toHtml na <> toHtml ['🟣' | isMarkdown] + MetricsChange'Bad{..} -> td ! class_ [fmt|{number} bad|] $ toHtml change <> toHtml ['🔴' | isMarkdown] + MetricsChange'Good{..} -> td ! class_ [fmt|{number} good|] $ toHtml change <> toHtml ['🟢' | isMarkdown] where isMarkdown = reportFormat == ReportFormat'Markdown + na :: String + na = "N/A" + number :: String + number = "number" toHtmlMetricsChange :: ReportFormat -> MetricsChangeCategorized -> [Html] toHtmlMetricsChange reportFormat change = toHtmlChange reportFormat <$> toListMetrics change @@ -121,8 +125,8 @@ toHtmlReportRow reportFormat index reportRow = ( td . toHtml <$> [ [fmt|{index}|] - , fromMaybe "[N/A]" reportRow.attributeInitial - , fromMaybe "[N/A]" reportRow.attributeNormalized + , fromMaybe na reportRow.attributeInitial + , fromMaybe na reportRow.attributeNormalized ] ) <> toHtmlMetricsChange reportFormat reportRow.metricsChange @@ -130,12 +134,16 @@ toHtmlReportRow reportFormat index reportRow = <> toHtmlMetrics reportRow.metricsNormalized <> ( td . toHtml - <$> [ fromMaybe "[all programs]" reportRow.fileInitial - , intercalate "." $ fromMaybe ["[whole program]"] reportRow.bindingsPathInitial - , fromMaybe "[all programs]" reportRow.fileNormalized - , intercalate "." $ fromMaybe ["[whole program]"] reportRow.bindingsPathNormalized + <$> [ fromMaybe allPrograms reportRow.fileInitial + , intercalate "." $ fromMaybe [wholeProgram] reportRow.bindingsPathInitial + , fromMaybe allPrograms reportRow.fileNormalized + , intercalate "." $ fromMaybe [wholeProgram] reportRow.bindingsPathNormalized ] ) + where + wholeProgram = "[whole program]" + allPrograms = "[all programs]" + na = "[N/A]" toHtmlReport :: ReportFormat -> PipelineConfig -> Report -> Html toHtmlReport reportFormat pipelineConfig report = From df8d5cb44890630f0a207a0db5dc8cd43e42a5b0 Mon Sep 17 00:00:00 2001 From: Danila Danko Date: Mon, 29 Jul 2024 22:13:37 +0300 Subject: [PATCH 2/3] fix(normalizer): sorting in numeric columns --- .../src/Language/EO/Phi/Report/Html.hs | 51 ++++++++++++++----- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/eo-phi-normalizer/src/Language/EO/Phi/Report/Html.hs b/eo-phi-normalizer/src/Language/EO/Phi/Report/Html.hs index 94c99f6f..a51eeee9 100644 --- a/eo-phi-normalizer/src/Language/EO/Phi/Report/Html.hs +++ b/eo-phi-normalizer/src/Language/EO/Phi/Report/Html.hs @@ -33,6 +33,7 @@ import Text.Blaze.Html5.Attributes (charset, class_, colspan, content, id, lang, import Text.Blaze.Html5.Attributes qualified as TBHA import Prelude hiding (div, id, span) import Prelude qualified +import Data.Functor ((<&>)) -- $setup -- >>> import Text.Blaze.Html.Renderer.String (renderHtml) @@ -89,22 +90,45 @@ instance ToMarkup Percent where toMarkup :: Percent -> Markup toMarkup = toMarkup . show --- >>> pipelineConfig = ReportConfig { general = ReportGeneralConfig { expectedMetricsChange = 0, format = ReportFormat'Markdown } } +class (Num a) => ToDataSort a where + toDataSort :: a -> Integer + +-- TODO #389:30m +-- I couldn't make PyF pad doubles with zeros using {n:05} syntax +-- because PyF also counts the digits after . +instance ToDataSort Double where + toDataSort :: Double -> Integer + toDataSort number = round $ number * 1000 + +instance ToDataSort Percent where + toDataSort :: Percent -> Integer + toDataSort (Percent number) = toDataSort number + +instance ToDataSort Integer where + toDataSort :: Integer -> Integer + toDataSort x = x + +mkDataSortAttribute :: AttributeValue -> Attribute +mkDataSortAttribute = dataAttribute "sort" + +-- >>> pipelineConfig = ReportFormat'Markdown -- --- >>> renderHtml $ toHtmlChange pipelineConfig (MetricsChange'Bad 0.2) --- "0.2\128308" +-- >>> renderHtml $ toHtmlChange pipelineConfig (MetricsChange'Bad (Percent 0.2)) +-- "20.00%\128308" -- --- >>> renderHtml $ toHtmlChange pipelineConfig (MetricsChange'Good 0.5) --- "0.5\128994" --- >>> renderHtml $ toHtmlChange pipelineConfig (MetricsChange'NA :: MetricsChangeCategory Double) --- "N/A\128995" -toHtmlChange :: (ToMarkup a) => ReportFormat -> MetricsChangeCategory a -> Html +-- >>> renderHtml $ toHtmlChange pipelineConfig (MetricsChange'Good (Percent 0.5)) +-- "50.00%\128994" +-- >>> renderHtml $ toHtmlChange pipelineConfig (MetricsChange'NA :: MetricsChangeCategory Percent) +-- "N/A\128995" +toHtmlChange :: forall a. (ToMarkup a, ToDataSort a) => ReportFormat -> MetricsChangeCategory a -> Html toHtmlChange reportFormat = \case - MetricsChange'NA -> td ! class_ [fmt|{number} not-applicable|] $ toHtml na <> toHtml ['🟣' | isMarkdown] - MetricsChange'Bad{..} -> td ! class_ [fmt|{number} bad|] $ toHtml change <> toHtml ['🔴' | isMarkdown] - MetricsChange'Good{..} -> td ! class_ [fmt|{number} good|] $ toHtml change <> toHtml ['🟢' | isMarkdown] + MetricsChange'NA -> td ! class_ [fmt|{number} not-applicable|] ! mkDataSortAttributeNA $ toHtml na <> toHtml ['🟣' | isMarkdown] + MetricsChange'Bad{..} -> td ! class_ [fmt|{number} bad|] ! mkDataSortAttributeChange change $ toHtml change <> toHtml ['🔴' | isMarkdown] + MetricsChange'Good{..} -> td ! class_ [fmt|{number} good|] ! mkDataSortAttributeChange change $ toHtml change <> toHtml ['🟢' | isMarkdown] where isMarkdown = reportFormat == ReportFormat'Markdown + mkDataSortAttributeChange change = mkDataSortAttribute [fmt|1{toDataSort change:06}|] + mkDataSortAttributeNA = mkDataSortAttribute [fmt|0{(toDataSort (Percent 0.0)):06}|] na :: String na = "N/A" number :: String @@ -115,9 +139,8 @@ toHtmlMetricsChange reportFormat change = toHtmlChange reportFormat <$> toListMe toHtmlMetrics :: MetricsCount -> [Html] toHtmlMetrics metrics = - (td ! class_ "number") - . toHtml - <$> toListMetrics metrics + toListMetrics metrics <&> + (\x -> td ! class_ "number" ! mkDataSortAttribute [fmt|{x:06}|] $ toHtml x) toHtmlReportRow :: ReportFormat -> Int -> ReportRow -> Html toHtmlReportRow reportFormat index reportRow = From 21ee7c3eddd3eceef389403473c8929187351a3d Mon Sep 17 00:00:00 2001 From: Danila Danko Date: Tue, 30 Jul 2024 11:13:42 +0300 Subject: [PATCH 3/3] refactor(normalizer): format file to make fourmoly happy --- eo-phi-normalizer/src/Language/EO/Phi/Report/Html.hs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/eo-phi-normalizer/src/Language/EO/Phi/Report/Html.hs b/eo-phi-normalizer/src/Language/EO/Phi/Report/Html.hs index a51eeee9..0a5482df 100644 --- a/eo-phi-normalizer/src/Language/EO/Phi/Report/Html.hs +++ b/eo-phi-normalizer/src/Language/EO/Phi/Report/Html.hs @@ -18,6 +18,7 @@ module Language.EO.Phi.Report.Html where import Data.FileEmbed (embedFileRelative) +import Data.Functor ((<&>)) import Data.List (intercalate) import Data.Maybe (catMaybes, fromMaybe) import Data.Text qualified as T @@ -33,7 +34,6 @@ import Text.Blaze.Html5.Attributes (charset, class_, colspan, content, id, lang, import Text.Blaze.Html5.Attributes qualified as TBHA import Prelude hiding (div, id, span) import Prelude qualified -import Data.Functor ((<&>)) -- $setup -- >>> import Text.Blaze.Html.Renderer.String (renderHtml) @@ -139,8 +139,8 @@ toHtmlMetricsChange reportFormat change = toHtmlChange reportFormat <$> toListMe toHtmlMetrics :: MetricsCount -> [Html] toHtmlMetrics metrics = - toListMetrics metrics <&> - (\x -> td ! class_ "number" ! mkDataSortAttribute [fmt|{x:06}|] $ toHtml x) + toListMetrics metrics + <&> (\x -> td ! class_ "number" ! mkDataSortAttribute [fmt|{x:06}|] $ toHtml x) toHtmlReportRow :: ReportFormat -> Int -> ReportRow -> Html toHtmlReportRow reportFormat index reportRow =