-
Notifications
You must be signed in to change notification settings - Fork 44
Add count-aborts
binary
#3911
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
Merged
Merged
Add count-aborts
binary
#3911
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
38edffe
Add context-logs binary which can count aborts and collate logs into …
goodlyrottenapple 62af7ea
Format with fourmolu
invalid-email-address 70c966d
add map from rule-ids to rule labels/locations
goodlyrottenapple 8b0fc6f
Format with fourmolu
invalid-email-address bc9fd98
export whole module
goodlyrottenapple 36795ce
Merge branch 'master' into sam/context-log-tools
goodlyrottenapple b0a8225
renamed binary and removed tree functionality as it isn't performant …
goodlyrottenapple 6c066e0
fourmolu
goodlyrottenapple 15db384
add -h/--help info
goodlyrottenapple 938979c
hlint
goodlyrottenapple 2bc4a7c
more hlint
goodlyrottenapple File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{- | Stand-alone parser executable for testing and profiling | ||
|
||
Copyright : (c) Runtime Verification, 2022 | ||
License : BSD-3-Clause | ||
-} | ||
module Main ( | ||
main, | ||
) where | ||
|
||
import Control.Monad (foldM, forM_) | ||
import Data.Aeson (ToJSON (toJSON), decode) | ||
import Data.Aeson.Encode.Pretty | ||
import Data.ByteString.Lazy.Char8 qualified as BS | ||
import Data.List (foldl') | ||
import Data.Map qualified as Map | ||
import Data.Maybe (fromMaybe) | ||
import Data.Text (unpack) | ||
import System.Environment (getArgs) | ||
import Types | ||
|
||
{- | Tests textual kore parser with given arguments and reports | ||
internalisation results. | ||
|
||
* Files given as arguments are parsed and internalised. When a | ||
* directory is given as an argument, it is (recursively) searched | ||
* for files named "*.kore", which are parsed and internalised. | ||
-} | ||
main :: IO () | ||
main = | ||
getArgs >>= \case | ||
["tree", file] -> do | ||
nested <- foldl' (foldl' toNested) (Nested mempty) . map decode . BS.lines <$> BS.readFile file | ||
BS.putStrLn $ encodePretty' defConfig{confIndent = Spaces 2} $ toJSON nested | ||
"aborts" : files -> do | ||
let countContexts m f = foldl' (foldl' countAborts) m . map decode . BS.lines <$> BS.readFile f | ||
(counts, rIdTorLoc) <- foldM countContexts mempty files | ||
forM_ (Map.toList counts) $ \(k, v) -> | ||
putStrLn $ unpack k <> " | " <> unpack (fromMaybe "-" $ Map.lookup k rIdTorLoc) <> " | " <> show v | ||
_ -> error "invalid option" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
module Types (module Types) where | ||
|
||
import Data.Aeson (FromJSON (..), ToJSON (..), Value (..), object, (.=)) | ||
import Data.Aeson.Key (fromText, toString) | ||
import Data.Aeson.KeyMap (toList) | ||
import Data.Map (Map) | ||
import Data.Map qualified as Map | ||
import Data.Sequence (Seq (..)) | ||
import Data.Text (Text, pack) | ||
import GHC.Generics (Generic) | ||
|
||
data Context = Plain Text | Ref Text Text deriving (Eq, Ord) | ||
|
||
toText :: Context -> Text | ||
toText = \case | ||
Plain t -> t | ||
Ref k v -> k <> " " <> v | ||
|
||
instance FromJSON Context where | ||
parseJSON (String t) = pure $ Plain t | ||
parseJSON (Object o) | [(k, String v)] <- toList o = pure $ Ref (pack $ toString k) v | ||
parseJSON _ = fail "Invalid context" | ||
|
||
data LogMessage = LogMessage | ||
{ context :: Seq Context | ||
, message :: Value | ||
} | ||
deriving (Generic, FromJSON) | ||
|
||
newtype Nested = Nested (Seq Value, Map Context Nested) | ||
|
||
instance ToJSON Nested where | ||
toJSON (Nested (vs, m)) | ||
| null vs = object m' | ||
| otherwise = object $ "logs" .= vs : m' | ||
where | ||
m' = [(fromText $ toText k, toJSON v) | (k, v) <- Map.toList m] | ||
|
||
insertAt :: Seq Context -> Value -> Nested -> Nested | ||
insertAt Empty v (Nested (vs, nested)) = Nested (vs :|> v, nested) | ||
insertAt (c :<| cs) v (Nested (vs, nested)) = | ||
Nested | ||
( vs | ||
, flip (Map.insert c) nested $ | ||
insertAt cs v $ | ||
case Map.lookup c nested of | ||
Nothing -> Nested mempty | ||
Just subtree -> subtree | ||
) | ||
|
||
toNested :: Nested -> LogMessage -> Nested | ||
toNested n LogMessage{context, message} = | ||
insertAt context message n | ||
|
||
countAborts :: (Map Text Int, Map Text Text) -> LogMessage -> (Map Text Int, Map Text Text) | ||
countAborts maps@(countMap, ruleMap) LogMessage{context, message} = case context of | ||
(_ :|> Ref "rewrite" ruleId :|> Plain "match" :|> Plain "abort") -> increment ruleId | ||
(_ :|> Ref "rewrite" ruleId :|> Plain "abort") -> increment ruleId | ||
(_ :|> Ref "rewrite" ruleId :|> Plain "detail") | String ruleLoc <- message -> (countMap, Map.insert ruleId ruleLoc ruleMap) | ||
_ -> maps | ||
where | ||
increment rid = (Map.alter (maybe (Just 1) (Just . (+ 1))) rid countMap, ruleMap) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.