|
| 1 | +module Types (module Types) where |
| 2 | + |
| 3 | +import Data.Aeson (FromJSON (..), Value (..)) |
| 4 | +import Data.Aeson.Key (toString) |
| 5 | +import Data.Aeson.KeyMap (toList) |
| 6 | +import Data.Map (Map) |
| 7 | +import Data.Map qualified as Map |
| 8 | +import Data.Sequence (Seq (..)) |
| 9 | +import Data.Text (Text, pack) |
| 10 | +import GHC.Generics (Generic) |
| 11 | + |
| 12 | +data Context = Plain Text | Ref Text Text deriving (Eq, Ord) |
| 13 | + |
| 14 | +instance FromJSON Context where |
| 15 | + parseJSON (String t) = pure $ Plain t |
| 16 | + parseJSON (Object o) | [(k, String v)] <- toList o = pure $ Ref (pack $ toString k) v |
| 17 | + parseJSON _ = fail "Invalid context" |
| 18 | + |
| 19 | +data LogMessage = LogMessage |
| 20 | + { context :: Seq Context |
| 21 | + , message :: Value |
| 22 | + } |
| 23 | + deriving (Generic, FromJSON) |
| 24 | + |
| 25 | +countAborts :: |
| 26 | + (Map Text Int, Map Text (Text, Text)) -> LogMessage -> (Map Text Int, Map Text (Text, Text)) |
| 27 | +countAborts maps@(countMap, ruleMap) LogMessage{context, message} = case context of |
| 28 | + (_ :|> Ref "rewrite" ruleId :|> Plain "match" :|> Plain "abort") -> increment ruleId |
| 29 | + (_ :|> Ref "rewrite" ruleId :|> Plain "abort") -> increment ruleId |
| 30 | + (_ :|> Ref "function" ruleId :|> Plain "failure" :|> Plain "break") -> increment ruleId |
| 31 | + (_ :|> Ref "simplification" ruleId :|> Plain "failure" :|> Plain "break") -> increment ruleId |
| 32 | + (_ :|> Ref "function" ruleId :|> Plain "match" :|> Plain "failure" :|> Plain "break") -> increment ruleId |
| 33 | + (_ :|> Ref "simplification" ruleId :|> Plain "match" :|> Plain "failure" :|> Plain "break") -> increment ruleId |
| 34 | + (_ :|> Ref "rewrite" ruleId :|> Plain "detail") | String ruleLoc <- message -> (countMap, Map.insert ruleId ("rewrite", ruleLoc) ruleMap) |
| 35 | + (_ :|> Ref "function" ruleId :|> Plain "detail") | String ruleLoc <- message -> (countMap, Map.insert ruleId ("function", ruleLoc) ruleMap) |
| 36 | + (_ :|> Ref "simplification" ruleId :|> Plain "detail") | String ruleLoc <- message -> (countMap, Map.insert ruleId ("simplification", ruleLoc) ruleMap) |
| 37 | + _ -> maps |
| 38 | + where |
| 39 | + increment rid = (Map.alter (maybe (Just 1) (Just . (+ 1))) rid countMap, ruleMap) |
0 commit comments