Matthias Andreas Benkard | 262110d | 2021-08-24 06:35:55 +0200 | [diff] [blame] | 1 | {-# LANGUAGE PatternSynonyms #-} |
| 2 | {-# LANGUAGE TypeApplications #-} |
| 3 | {-# LANGUAGE UndecidableInstances #-} |
| 4 | |
| 5 | module Mulkup.Main where |
| 6 | |
| 7 | import Colog (Message, richMessageAction, simpleMessageAction) |
| 8 | import Colog.Polysemy.Effect (Log, runLogAction) |
| 9 | import qualified Data.List.NonEmpty as NonEmpty |
| 10 | import qualified Data.Set as Set |
| 11 | import Data.Time |
| 12 | import Mulkup.Bupstash |
| 13 | import Mulkup.Config |
| 14 | import Mulkup.Flags |
| 15 | import Mulkup.Logging |
| 16 | import Mulkup.Prelude |
| 17 | import Optics |
| 18 | import Options.Applicative |
| 19 | ( execParser, |
| 20 | fullDesc, |
| 21 | helper, |
| 22 | info, |
| 23 | ) |
| 24 | import Polysemy (Member, Sem) |
| 25 | import Polysemy.Error |
| 26 | import Polysemy.Final |
| 27 | import Polysemy.Reader (Reader, asks, runReader) |
| 28 | |
| 29 | main :: IO () |
| 30 | main = do |
| 31 | flags <- execParser $ info (flagParser <**> helper) fullDesc |
| 32 | let messageAction = |
| 33 | if verbose flags |
| 34 | then richMessageAction |
| 35 | else simpleMessageAction |
| 36 | |
| 37 | config <- readConfig "./config.dhall" |
| 38 | |
| 39 | result <- |
| 40 | main' |
| 41 | & runBupstash |
| 42 | & runLogAction @IO messageAction |
| 43 | & runReader (config :: MulkupConfig) |
| 44 | & errorToIOFinal @Text |
| 45 | & embedToFinal @IO |
| 46 | & runFinal @IO |
| 47 | |
| 48 | case result of |
| 49 | Left err -> do |
| 50 | error err |
| 51 | Right () -> |
| 52 | return () |
| 53 | |
| 54 | main' :: (Member (Log Message) r, Member (Reader MulkupConfig) r, Member Bupstash r) => Sem r () |
| 55 | main' = do |
| 56 | stashes <- asks @MulkupConfig (^. #stashes) |
| 57 | forM_ stashes $ \stash -> do |
| 58 | let labels = [("name", stash ^. #name)] |
| 59 | |
| 60 | currentItems <- bupList (BupFilter labels Nothing) |
| 61 | |
| 62 | let tiers = |
| 63 | [ (utctHour . bupItemUTCTime, #hourly), |
| 64 | (utctJulianDay . bupItemUTCTime, #daily), |
| 65 | (utctWeek . bupItemUTCTime, #weekly), |
| 66 | (utctMonth . bupItemUTCTime, #monthly) |
| 67 | ] |
| 68 | |
| 69 | let keepIds = |
| 70 | Set.unions $ |
| 71 | map |
| 72 | ( \(discriminator, cfgLens) -> |
| 73 | let tierCfg = stash ^. #tiers ^. cfgLens |
| 74 | in tierKeepIds discriminator (tierCfg ^. #keep) currentItems |
| 75 | ) |
| 76 | tiers |
| 77 | |
| 78 | let currentIds = Set.fromList (map (^. #id) currentItems) |
| 79 | let rmIds = Set.difference currentIds keepIds |
| 80 | |
| 81 | logInfo (show labels <> " Keeping: " <> show (Set.toList keepIds)) |
| 82 | logInfo (show labels <> " Removing: " <> show (Set.toList rmIds)) |
| 83 | bupRemove (Set.toList rmIds) |
| 84 | |
| 85 | logInfo (show labels <> " Creating backup.") |
| 86 | bupPut |
| 87 | (stash ^. #baseDir) |
| 88 | (stash ^. #exclusions) |
| 89 | labels |
| 90 | |
| 91 | tierKeepIds :: (BupItem -> Integer) -> Natural -> [BupItem] -> Set Text |
| 92 | tierKeepIds discriminator keep items = |
| 93 | fromList $ |
| 94 | take (fromIntegral keep) $ |
| 95 | map (^. #id) $ |
| 96 | reverse $ |
| 97 | sortWith bupItemUTCTime $ |
| 98 | map (head . NonEmpty.sortWith bupItemUTCTime) $ |
| 99 | elems $ |
| 100 | (groupBy discriminator items :: HashMap Integer (NonEmpty BupItem)) |
| 101 | |
| 102 | utctHour :: UTCTime -> Integer |
| 103 | utctHour (UTCTime (ModifiedJulianDay julianDay) tdiff) = (julianDay * 24) + (diffTimeToPicoseconds tdiff `div` (10 ^ (12 :: Integer)) `div` 3600) |
| 104 | |
| 105 | utctJulianDay :: UTCTime -> Integer |
| 106 | utctJulianDay (UTCTime (ModifiedJulianDay julianDay) _) = julianDay |
| 107 | |
| 108 | utctWeek :: UTCTime -> Integer |
| 109 | utctWeek (UTCTime day _) = |
| 110 | julianDay + fromIntegral (dayOfWeekDiff (dayOfWeek day) Sunday) |
| 111 | where |
| 112 | (ModifiedJulianDay julianDay) = day |
| 113 | dayOfWeekDiff a b = mod (fromEnum a - fromEnum b) 7 |
| 114 | |
| 115 | utctMonth :: UTCTime -> Integer |
| 116 | utctMonth (UTCTime day _) = fromIntegral m |
| 117 | where |
| 118 | (_, m, _) = toGregorian day |