summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2017-10-08 14:15:03 +0200
committerYorhel <git@yorhel.nl>2017-10-08 14:15:03 +0200
commit8570bb0d4922c84225daf1506378c47dd1ef53d4 (patch)
tree40686e3eb079998467d39e06db330a9ec3116eee
parent02005e7021f639c5b10bc4edf5aba5c72f5f516e (diff)
Allow multiple variables in show and group statements
-rw-r--r--Logstat/Eval.hs4
-rw-r--r--Logstat/Main.hs12
-rw-r--r--Logstat/Parse.hs4
-rw-r--r--Logstat/Types.hs4
-rw-r--r--logstat.conf12
5 files changed, 17 insertions, 19 deletions
diff --git a/Logstat/Eval.hs b/Logstat/Eval.hs
index f60f7c3..ffd80e3 100644
--- a/Logstat/Eval.hs
+++ b/Logstat/Eval.hs
@@ -133,7 +133,7 @@ step ev = get >>= \stmt -> case stmt of
else hp
SGroup f st -> do
- v <- getField ev f
+ v <- mapM (getField ev) f
put $ SGroup f $ Map.insert v () st
throwError Filtered
@@ -149,7 +149,7 @@ final = get >>= \stmt -> case stmt of
where
f hp = return $ map (\(SortItem _ ev) -> ev) $ reverse $ toList hp
- SGroup f st -> return $ map (\(k,_) -> Map.fromList [(f, k)]) $ Map.toList st
+ SGroup f st -> return $ map (\(k,_) -> Map.fromList $ zip f k) $ Map.toList st
_ -> return []
diff --git a/Logstat/Main.hs b/Logstat/Main.hs
index 47fe5f4..cd84a00 100644
--- a/Logstat/Main.hs
+++ b/Logstat/Main.hs
@@ -24,10 +24,10 @@ import Logstat.Eval
-- Extract the final 'show' statement or provide a default expression to display.
-- Returns an error if there is a non-last 'show' statement.
-extractShow :: [Stmt] -> Either String (Expr, [Stmt])
-extractShow proc = f [] "msg" proc
+extractShow :: [Stmt] -> Either String ([Expr], [Stmt])
+extractShow proc = f [] ["msg"] proc
where
- f l v [] = Right (EField v, reverse l)
+ f l v [] = Right (map EField v, reverse l)
f l _ ((SShow e):[]) = Right (e, reverse l)
f _ _ ((SShow _):_) = Left $ "Error: 'show' must not be followed by other statements" -- TODO: CONTEXT!
f l _ (x@(SGroup v _):xs) = f (x:l) v xs
@@ -35,7 +35,7 @@ extractShow proc = f [] "msg" proc
-run :: Expr -> [Stmt] -> IO ()
+run :: [Expr] -> [Stmt] -> IO ()
run sh = loop
where
loop comp = do
@@ -48,9 +48,9 @@ run sh = loop
Just comp' -> loop comp'
eval ev =
- case runExcept (evalExpr ev sh) of
+ case runExcept (mapM (evalExpr ev) sh) of
Left e -> hPutStrLn stderr $ show e ++ " in show statement"
- Right v -> C.putStrLn $ asBS v
+ Right v -> C.putStrLn $ B.intercalate ", " $ map asBS v
line comp = do
l <- B.getLine
diff --git a/Logstat/Parse.hs b/Logstat/Parse.hs
index 85bd778..6484948 100644
--- a/Logstat/Parse.hs
+++ b/Logstat/Parse.hs
@@ -99,11 +99,11 @@ stmts end = concat <$> sepEndBy1 stmt (symbol ";") <* end
Just s -> return s
stmtFilter = symbol "filter" >> SFilter <$> expr
- stmtShow = symbol "show" >> SShow <$> expr
+ stmtShow = symbol "show" >> SShow <$> sepBy1 expr (symbol ",")
stmtGroup = do
symbol "group"
- n <- identifier
+ n <- sepBy1 identifier (symbol ",")
return (SGroup n mempty)
stmtRegex = do
diff --git a/Logstat/Types.hs b/Logstat/Types.hs
index 62c4233..c835933 100644
--- a/Logstat/Types.hs
+++ b/Logstat/Types.hs
@@ -87,8 +87,8 @@ data Stmt
| SSet !Field !Expr
| SFilter !Expr
| SSort !Int !Expr !SortState
- | SGroup !Field !(Map Val ()) -- TODO: Should add more state than '()'
- | SShow !Expr
+ | SGroup ![Field] !(Map [Val] ()) -- TODO: Should add more state than '()'
+ | SShow ![Expr]
deriving(Show)
data Config = Config
diff --git a/logstat.conf b/logstat.conf
index 1031002..58b7a82 100644
--- a/logstat.conf
+++ b/logstat.conf
@@ -36,7 +36,7 @@ proc foo {
filter size != 0;
## Aggregation syntax:
- group ip;
+ group ip, code;
## After a 'group', all grouped-on fields are directly acessible, the
## fields from above the 'group' statement must be accessed through
## aggregation functions (like in SQL). e.g.
@@ -54,17 +54,15 @@ proc foo {
#sort string ip asc limit 10;
#sort string ip desc limit 10;
- # The expression to output. Only one 'show' is allowed in a program, and it
+ # The expression(s) to output. Only one 'show' is allowed in a program, and it
# must be the last statement.
- show ip;
- ## Multiple expressions should also be possible, and should generate a
- ## suitably-sized table:
- # show fmtsize(avgsize), count()
+ show ip, code == 200;
+ ## Multiple expressions should generate a suitably-sized table.
## ("suitably-sized" is hard to tell when there's no top-n-sort or
## aggregation going on. Probably stick to CSV or so in that case?)
## If no 'show' statement is provided, then by default it should behave as
## if a 'show msg' command was used for non-aggregated procedures. For
- ## aggregated procedures it could be 'show <expr-grouped-on>, count()'.
+ ## aggregated procedures it could be 'show <expr-grouped-on>'.
}