2012-07-29 13:35:01 +00:00
|
|
|
{- git-annex assistant alerts
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE RankNTypes #-}
|
|
|
|
|
|
|
|
module Assistant.Alert where
|
|
|
|
|
2012-07-29 22:07:45 +00:00
|
|
|
import Common.Annex
|
|
|
|
import qualified Remote
|
|
|
|
|
2012-07-30 16:21:53 +00:00
|
|
|
import qualified Data.Map as M
|
2012-07-29 13:35:01 +00:00
|
|
|
import Yesod
|
|
|
|
|
|
|
|
type Widget = forall sub master. GWidget sub master ()
|
|
|
|
|
|
|
|
{- Different classes of alerts are displayed differently. -}
|
2012-07-29 23:05:51 +00:00
|
|
|
data AlertClass = Success | Message | Activity | Warning | Error
|
|
|
|
deriving (Eq, Ord)
|
2012-07-29 13:35:01 +00:00
|
|
|
|
2012-07-30 16:21:53 +00:00
|
|
|
data AlertPriority = Filler | Low | Medium | High | Pinned
|
|
|
|
deriving (Eq, Ord)
|
|
|
|
|
2012-07-30 06:07:02 +00:00
|
|
|
{- An alert can be a simple message, or an arbitrary Yesod Widget. -}
|
|
|
|
data AlertMessage = StringAlert String | WidgetAlert (Alert -> Widget)
|
2012-07-29 13:35:01 +00:00
|
|
|
|
2012-08-02 13:03:04 +00:00
|
|
|
{- An alert can have an name, which is used to combine it with other similar
|
|
|
|
- alerts. -}
|
|
|
|
data AlertName = AddFileAlert | DownloadFailedAlert | SanityCheckFixAlert
|
|
|
|
deriving (Eq)
|
|
|
|
|
|
|
|
{- The first alert is the new alert, the second is an old alert.
|
|
|
|
- Should return a modified version of the old alert. -}
|
|
|
|
type AlertCombiner = Maybe (Alert -> Alert -> Maybe Alert)
|
|
|
|
|
2012-07-29 13:35:01 +00:00
|
|
|
data Alert = Alert
|
|
|
|
{ alertClass :: AlertClass
|
2012-07-29 15:31:06 +00:00
|
|
|
, alertHeader :: Maybe String
|
2012-07-29 13:35:01 +00:00
|
|
|
, alertMessage :: AlertMessage
|
2012-07-29 15:31:06 +00:00
|
|
|
, alertBlockDisplay :: Bool
|
2012-07-29 23:41:17 +00:00
|
|
|
, alertClosable :: Bool
|
2012-07-29 23:05:51 +00:00
|
|
|
, alertPriority :: AlertPriority
|
2012-07-31 07:10:16 +00:00
|
|
|
, alertIcon :: Maybe String
|
2012-08-02 13:03:04 +00:00
|
|
|
, alertCombiner :: AlertCombiner
|
|
|
|
, alertName :: Maybe AlertName
|
2012-07-29 15:31:06 +00:00
|
|
|
}
|
|
|
|
|
2012-07-30 19:33:12 +00:00
|
|
|
type AlertPair = (AlertId, Alert)
|
|
|
|
|
|
|
|
type AlertMap = M.Map AlertId Alert
|
|
|
|
|
2012-07-29 23:05:51 +00:00
|
|
|
{- Higher AlertId indicates a more recent alert. -}
|
2012-07-30 18:08:22 +00:00
|
|
|
newtype AlertId = AlertId Integer
|
|
|
|
deriving (Read, Show, Eq, Ord)
|
|
|
|
|
|
|
|
{- Note: This first alert id is used for yesod's message. -}
|
|
|
|
firstAlertId :: AlertId
|
|
|
|
firstAlertId = AlertId 0
|
|
|
|
|
|
|
|
nextAlertId :: AlertId -> AlertId
|
|
|
|
nextAlertId (AlertId i) = AlertId $ succ i
|
2012-07-29 23:05:51 +00:00
|
|
|
|
2012-07-30 16:21:53 +00:00
|
|
|
{- This is as many alerts as it makes sense to display at a time.
|
|
|
|
- A display might be smaller ,or larger, the point is to not overwhelm the
|
|
|
|
- user with a ton of alerts. -}
|
|
|
|
displayAlerts :: Int
|
2012-07-30 19:33:12 +00:00
|
|
|
displayAlerts = 6
|
2012-07-30 16:21:53 +00:00
|
|
|
|
|
|
|
{- This is not a hard maximum, but there's no point in keeping a great
|
|
|
|
- many filler alerts in an AlertMap, so when there's more than this many,
|
|
|
|
- they start being pruned, down toward displayAlerts. -}
|
|
|
|
maxAlerts :: Int
|
|
|
|
maxAlerts = displayAlerts * 2
|
2012-07-29 23:05:51 +00:00
|
|
|
|
|
|
|
{- The desired order is the reverse of:
|
|
|
|
-
|
2012-07-29 23:41:17 +00:00
|
|
|
- - Pinned alerts
|
2012-07-29 23:05:51 +00:00
|
|
|
- - High priority alerts, newest first
|
|
|
|
- - Medium priority Activity, newest first (mostly used for Activity)
|
2012-07-30 06:07:02 +00:00
|
|
|
- - Low priority alerts, newest first
|
|
|
|
- - Filler priorty alerts, newest first
|
2012-07-29 23:05:51 +00:00
|
|
|
- - Ties are broken by the AlertClass, with Errors etc coming first.
|
|
|
|
-}
|
|
|
|
compareAlertPairs :: AlertPair -> AlertPair -> Ordering
|
|
|
|
compareAlertPairs
|
2012-07-30 19:33:12 +00:00
|
|
|
(aid, Alert { alertClass = aclass, alertPriority = aprio })
|
|
|
|
(bid, Alert { alertClass = bclass, alertPriority = bprio })
|
2012-07-29 23:05:51 +00:00
|
|
|
= compare aprio bprio
|
|
|
|
`thenOrd` compare aid bid
|
|
|
|
`thenOrd` compare aclass bclass
|
|
|
|
|
2012-07-30 16:21:53 +00:00
|
|
|
sortAlertPairs :: [AlertPair] -> [AlertPair]
|
|
|
|
sortAlertPairs = sortBy compareAlertPairs
|
|
|
|
|
2012-07-30 19:33:12 +00:00
|
|
|
{- Checks if two alerts display the same.
|
|
|
|
- Yesod Widgets cannot be compared, as they run code. -}
|
|
|
|
effectivelySameAlert :: Alert -> Alert -> Bool
|
|
|
|
effectivelySameAlert x y
|
|
|
|
| uncomparable x || uncomparable y = False
|
|
|
|
| otherwise = all id
|
|
|
|
[ alertClass x == alertClass y
|
|
|
|
, alertHeader x == alertHeader y
|
|
|
|
, extract (alertMessage x) == extract (alertMessage y)
|
|
|
|
, alertBlockDisplay x == alertBlockDisplay y
|
|
|
|
, alertClosable x == alertClosable y
|
|
|
|
, alertPriority x == alertPriority y
|
|
|
|
]
|
|
|
|
where
|
|
|
|
uncomparable (Alert { alertMessage = StringAlert _ }) = False
|
|
|
|
uncomparable _ = True
|
|
|
|
extract (StringAlert s) = s
|
|
|
|
extract _ = ""
|
|
|
|
|
2012-07-30 06:07:02 +00:00
|
|
|
makeAlertFiller :: Bool -> Alert -> Alert
|
|
|
|
makeAlertFiller success alert
|
2012-07-30 16:21:53 +00:00
|
|
|
| isFiller alert = alert
|
2012-07-30 06:07:02 +00:00
|
|
|
| otherwise = alert
|
|
|
|
{ alertClass = if c == Activity then c' else c
|
|
|
|
, alertPriority = Filler
|
2012-07-30 16:23:40 +00:00
|
|
|
, alertClosable = True
|
2012-07-31 07:10:16 +00:00
|
|
|
, alertIcon = Just $ if success then "ok" else "exclamation-sign"
|
2012-07-30 06:07:02 +00:00
|
|
|
}
|
|
|
|
where
|
|
|
|
c = alertClass alert
|
|
|
|
c'
|
|
|
|
| success = Success
|
|
|
|
| otherwise = Error
|
|
|
|
|
2012-07-30 16:21:53 +00:00
|
|
|
isFiller :: Alert -> Bool
|
|
|
|
isFiller alert = alertPriority alert == Filler
|
|
|
|
|
|
|
|
{- Converts a given alert into filler, manipulating it in the AlertMap.
|
2012-07-30 19:33:12 +00:00
|
|
|
-
|
2012-08-02 13:03:04 +00:00
|
|
|
- Any old filler that looks the same as the reference alert is removed,
|
|
|
|
- or, if the input alert has an alertCombine that combines it with
|
|
|
|
- old filler, the old filler is replaced with the result, and the
|
|
|
|
- input alert is removed.
|
2012-07-30 16:21:53 +00:00
|
|
|
-
|
|
|
|
- Old filler alerts are pruned once maxAlerts is reached.
|
|
|
|
-}
|
|
|
|
convertToFiller :: AlertId -> Bool -> AlertMap -> AlertMap
|
2012-07-30 19:33:12 +00:00
|
|
|
convertToFiller i success m = case M.lookup i m of
|
|
|
|
Nothing -> m
|
2012-08-02 13:03:04 +00:00
|
|
|
Just al ->
|
2012-07-30 19:33:12 +00:00
|
|
|
let al' = makeAlertFiller success al
|
2012-08-02 13:03:04 +00:00
|
|
|
in case alertCombiner al' of
|
|
|
|
Nothing -> updatePrune al'
|
|
|
|
Just combiner -> updateCombine combiner al'
|
2012-07-30 16:21:53 +00:00
|
|
|
where
|
2012-07-30 19:33:12 +00:00
|
|
|
pruneSame ref k al = k == i || not (effectivelySameAlert ref al)
|
|
|
|
pruneBloat m'
|
|
|
|
| bloat > 0 = M.fromList $ pruneold $ M.toList m'
|
|
|
|
| otherwise = m'
|
|
|
|
where
|
|
|
|
bloat = M.size m' - maxAlerts
|
|
|
|
pruneold l =
|
|
|
|
let (f, rest) = partition (\(_, al) -> isFiller al) l
|
|
|
|
in drop bloat f ++ rest
|
2012-08-02 13:03:04 +00:00
|
|
|
updatePrune al = pruneBloat $ M.filterWithKey (pruneSame al) $
|
|
|
|
M.insertWith' const i al m
|
|
|
|
updateCombine combiner al =
|
|
|
|
let combined = M.mapMaybe (combiner al) m
|
|
|
|
in if M.null combined
|
|
|
|
then updatePrune al
|
|
|
|
else M.delete i $ M.union combined m
|
2012-07-29 23:05:51 +00:00
|
|
|
|
2012-07-29 23:41:17 +00:00
|
|
|
baseActivityAlert :: Alert
|
|
|
|
baseActivityAlert = Alert
|
2012-07-29 15:31:06 +00:00
|
|
|
{ alertClass = Activity
|
2012-07-29 23:41:17 +00:00
|
|
|
, alertHeader = Nothing
|
|
|
|
, alertMessage = StringAlert ""
|
2012-07-29 15:31:06 +00:00
|
|
|
, alertBlockDisplay = False
|
2012-07-29 23:41:17 +00:00
|
|
|
, alertClosable = False
|
2012-07-29 23:05:51 +00:00
|
|
|
, alertPriority = Medium
|
2012-07-31 07:10:16 +00:00
|
|
|
, alertIcon = Just "refresh"
|
2012-08-02 13:03:04 +00:00
|
|
|
, alertCombiner = Nothing
|
|
|
|
, alertName = Nothing
|
2012-07-29 13:35:01 +00:00
|
|
|
}
|
2012-07-29 22:07:45 +00:00
|
|
|
|
2012-07-29 23:41:17 +00:00
|
|
|
activityAlert :: Maybe String -> String -> Alert
|
|
|
|
activityAlert header message = baseActivityAlert
|
|
|
|
{ alertHeader = header
|
|
|
|
, alertMessage = StringAlert message
|
|
|
|
}
|
|
|
|
|
2012-07-29 22:07:45 +00:00
|
|
|
startupScanAlert :: Alert
|
|
|
|
startupScanAlert = activityAlert Nothing "Performing startup scan"
|
|
|
|
|
|
|
|
pushAlert :: [Remote] -> Alert
|
|
|
|
pushAlert rs = activityAlert Nothing $
|
|
|
|
"Syncing with " ++ unwords (map Remote.name rs)
|
|
|
|
|
|
|
|
pushRetryAlert :: [Remote] -> Alert
|
|
|
|
pushRetryAlert rs = activityAlert (Just "Retrying sync") $
|
|
|
|
"with " ++ unwords (map Remote.name rs) ++ ", which failed earlier."
|
|
|
|
|
|
|
|
syncMountAlert :: FilePath -> [Remote] -> Alert
|
2012-07-29 23:41:17 +00:00
|
|
|
syncMountAlert dir rs = baseActivityAlert
|
|
|
|
{ alertHeader = Just $ "Syncing with " ++ unwords (map Remote.name rs)
|
2012-07-29 22:07:45 +00:00
|
|
|
, alertMessage = StringAlert $ unwords
|
2012-07-30 02:18:58 +00:00
|
|
|
["You plugged in"
|
2012-07-29 22:07:45 +00:00
|
|
|
, dir
|
|
|
|
, " -- let's get it in sync!"
|
|
|
|
]
|
|
|
|
, alertBlockDisplay = True
|
2012-07-29 23:05:51 +00:00
|
|
|
, alertPriority = Low
|
2012-07-29 22:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scanAlert :: Remote -> Alert
|
2012-07-29 23:41:17 +00:00
|
|
|
scanAlert r = baseActivityAlert
|
|
|
|
{ alertHeader = Just $ "Scanning " ++ Remote.name r
|
2012-07-29 22:07:45 +00:00
|
|
|
, alertMessage = StringAlert $ unwords
|
|
|
|
[ "Ensuring that ", Remote.name r
|
|
|
|
, "is fully in sync." ]
|
|
|
|
, alertBlockDisplay = True
|
2012-07-29 23:05:51 +00:00
|
|
|
, alertPriority = Low
|
2012-07-29 22:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sanityCheckAlert :: Alert
|
|
|
|
sanityCheckAlert = activityAlert (Just "Running daily sanity check")
|
2012-07-30 02:18:58 +00:00
|
|
|
"to make sure everything is ok."
|
2012-07-29 22:07:45 +00:00
|
|
|
|
|
|
|
sanityCheckFixAlert :: String -> Alert
|
|
|
|
sanityCheckFixAlert msg = Alert
|
|
|
|
{ alertClass = Warning
|
|
|
|
, alertHeader = Just "Fixed a problem"
|
2012-08-02 13:03:04 +00:00
|
|
|
, alertMessage = StringAlert $ unlines [ alerthead, msg, alertfoot ]
|
2012-07-29 22:07:45 +00:00
|
|
|
, alertBlockDisplay = True
|
2012-07-29 23:05:51 +00:00
|
|
|
, alertPriority = High
|
2012-07-29 23:41:17 +00:00
|
|
|
, alertClosable = True
|
2012-07-31 07:10:16 +00:00
|
|
|
, alertIcon = Just "exclamation-sign"
|
2012-08-02 13:03:04 +00:00
|
|
|
, alertName = Just SanityCheckFixAlert
|
|
|
|
, alertCombiner = messageCombiner combinemessage
|
|
|
|
}
|
|
|
|
where
|
|
|
|
alerthead = "The daily sanity check found and fixed a problem:"
|
|
|
|
alertfoot = "If these problems persist, consider filing a bug report."
|
|
|
|
combinemessage (StringAlert new) (StringAlert old) =
|
|
|
|
let newmsg = filter (/= alerthead) $
|
|
|
|
filter (/= alertfoot) $
|
|
|
|
lines old ++ lines new
|
|
|
|
in Just $ StringAlert $
|
|
|
|
unlines $ alerthead : newmsg ++ [alertfoot]
|
|
|
|
combinemessage _ _ = Nothing
|
|
|
|
|
|
|
|
addFileAlert :: FilePath -> Alert
|
|
|
|
addFileAlert file = (activityAlert (Just "Added") $ takeFileName file)
|
|
|
|
{ alertName = Just AddFileAlert
|
|
|
|
, alertCombiner = messageCombiner combinemessage
|
2012-07-29 22:07:45 +00:00
|
|
|
}
|
2012-08-02 13:03:04 +00:00
|
|
|
where
|
|
|
|
combinemessage (StringAlert new) (StringAlert old) =
|
|
|
|
Just $ StringAlert $
|
|
|
|
unlines $ take 10 $ new : lines old
|
|
|
|
combinemessage _ _ = Nothing
|
|
|
|
|
|
|
|
messageCombiner :: (AlertMessage -> AlertMessage -> Maybe AlertMessage) -> AlertCombiner
|
|
|
|
messageCombiner combinemessage = Just go
|
|
|
|
where
|
|
|
|
go new old
|
|
|
|
| alertClass new /= alertClass old = Nothing
|
|
|
|
| alertName new == alertName old =
|
|
|
|
case combinemessage (alertMessage new) (alertMessage old) of
|
|
|
|
Nothing -> Nothing
|
|
|
|
Just m -> Just $ old { alertMessage = m }
|
|
|
|
| otherwise = Nothing
|