Skip to content
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

First attempt at logging to files instead of std out #135

Merged
merged 7 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ cabal.sandbox.config
.envrc
dist-newstyle/
result/
logs/
3 changes: 3 additions & 0 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import System.Console.GetOpt ( ArgDescr(..)
, getOpt
, usageInfo
)
import System.Directory ( createDirectoryIfMissing )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 IT EXISTS FOR REAL! 🎉

import System.Environment ( getArgs )
import System.IO ( stdout )
import Web.Scotty ( get
Expand All @@ -54,6 +55,7 @@ opts :: [OptDescr (Config -> Config)]
opts =
[ Option [] ["help"] (NoArg (set cHelp True)) "Show usage info"
, Option [] ["port"] (ReqArg (set cPort . read) "PORT") "Port to run on"
, Option [] ["path"] (ReqArg (set cLogPath) "PATH") "Path to save log files to, default is 'logs'"
, Option []
["interval"]
(ReqArg (set cInterval . (1000000 *) . read) "INTERVAL (s)")
Expand All @@ -71,6 +73,7 @@ main = (recreateConfig . getOpt Permute opts <$> getArgs) >>= \case
(config , _ , _ ) -> do
upd <- newEmptyMVar -- putMVar when to update
viewRef <- createViewReference
createDirectoryIfMissing True (_cLogPath config)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this path relative?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe both?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both absolute and relative that is

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It handles both :D 🗡️

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AWESOME! I am now a much calmer human.


-- In the list there are three items running concurrently:
-- 1. Timer that sends a signal to the updater when it's time to update
Expand Down
1 change: 1 addition & 0 deletions mat-chalmers.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ executable mat-chalmers
, time >= 1.12.2 && < 1.13
, wai-extra >= 3.1.14 && < 4.0
, wai-middleware-static-embedded == 0.1.0.0
, directory >= 1.3.5.0 && < 1.4
, async >= 2.2.5 && <= 3.0
default-language: GHC2021

Expand Down
3 changes: 2 additions & 1 deletion src/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ data Config = Config
, _cNextDayHour :: !Int
, _cInterval :: !Int
, _cPort :: !Int
, _cLogPath :: !String
}
deriving (Eq, Show)

makeLenses ''Config

defaultConfig :: Config
defaultConfig = Config False 14 (1000000 * 60 * 30) 5007
defaultConfig = Config False 14 (1000000 * 60 * 30) 5007 "logs"

-- | Create a Config we can touch
--
Expand Down
14 changes: 7 additions & 7 deletions src/Model.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,22 @@ import Data.IORef ( IORef
, writeIORef
)
import Data.Foldable ( for_ )
import Data.Text.Lazy ( fromStrict
, pack
)
import Prettyprinter ( Doc
, pretty
)
import Data.Text.Lazy ( fromStrict )
import Prettyprinter ( Doc )
import Data.AffineSpace ( (.+^) )
import Data.Thyme ( _localDay
, _localTimeOfDay
, _todHour
, _zonedTimeToLocalTime
, getZonedTime
, getCurrentTime
)
import Lens.Micro.Platform ( (^.)
, (&)
, (%~)
, view
)
import Text.Printf ( printf )
import Network.HTTP.Req

import Config
Expand Down Expand Up @@ -112,7 +110,9 @@ update = do

for_ rest $ \r -> case menu r of
Left e ->
logMessage =<< timestamp (pretty $ name r <> ": " <> pack (show e))
asks _cLogPath >>= \path ->
liftIO getCurrentTime >>=
liftIO . flip writeFile (show e) . flip (printf "%s/%s%s.txt" path) (name r) . show
Right _ -> pure ()

return (View rest textday d)
Expand Down
13 changes: 6 additions & 7 deletions src/Model/Linsen.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# LANGUAGE FlexibleContexts, OverloadedStrings, LambdaCase #-}
{-# LANGUAGE FlexibleContexts, LambdaCase, MultiWayIf, OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}

module Model.Linsen
Expand Down Expand Up @@ -117,12 +117,11 @@ parse day =
[] -> fail "Failed to index into richtext"
(v:_) -> pure v)
>=> (.: "text")
>=> \s -> if
pure day ==
parseTime swedishTimeLocale "%A %d-%m-%Y" s &&
length v' >= 9
then pure v'
else pure mempty))
>=> \s ->
let sameDay = pure day == parseTime swedishTimeLocale "%A %d-%m-%Y" s
in if | sameDay && length v' >= 9 -> pure v'
| sameDay -> pure mempty
| otherwise -> fail "Unable to parse day"))
>=> menuParser
)
)
Expand Down
10 changes: 7 additions & 3 deletions test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Data.Thyme.Time.Core ( fromGregorian )
import Model.Karen ( parse )
import qualified Model.Linsen as L ( parse )
import Model.Types ( Menu(..)
, NoMenu( NoLunch )
, NoMenu( NoLunch, NMParseError )
)
import Model.Wijkanders ( getWijkanders
, hasDate
Expand All @@ -27,6 +27,10 @@ testFun = \case
either
(assertFailure . mappend "This is not expected, input was:\n" . show)
(@?= e)
Left (NMParseError e _) ->
either
((@?= e) . \(NMParseError e1 _) -> e1)
(assertFailure . mappend "This is not expected, input was:\n" . show)
Left e ->
either
(@?= e)
Expand Down Expand Up @@ -101,7 +105,7 @@ main = hspec $ do
"parses a blob of JSON without error, that has no lunch"
(do
s2 <- BL.readFile "test/linsen2.json" -- Test that has no lunch
testFun (Left NoLunch)
testFun (Left (NMParseError "Error in $: Unable to parse day" undefined))
(L.parse
(fromGregorian 2024 06 06)
(fromJust $ decode s2))
Expand All @@ -112,7 +116,7 @@ main = hspec $ do
"parses a blob of JSON without error, that has the wrong week"
(do
s3 <- BL.readFile "test/linsen3.json"
testFun (Left NoLunch)
testFun (Left (NMParseError "Error in $: Unable to parse day" undefined))
(L.parse
(fromGregorian 2024 06 26)
(fromJust $ decode s3))
Expand Down
Loading