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-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-29 15:31:06 +00:00
|
|
|
{- An alert can be a simple message, or an arbitrary Yesod Widget -}
|
2012-07-29 13:35:01 +00:00
|
|
|
data AlertMessage = StringAlert String | WidgetAlert Widget
|
|
|
|
|
|
|
|
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-29 15:31:06 +00:00
|
|
|
}
|
|
|
|
|
2012-07-29 23:05:51 +00:00
|
|
|
{- Higher AlertId indicates a more recent alert. -}
|
|
|
|
type AlertId = Integer
|
|
|
|
|
|
|
|
type AlertPair = (AlertId, Alert)
|
|
|
|
|
2012-07-29 23:41:17 +00:00
|
|
|
data AlertPriority = Low | Medium | High | Pinned
|
2012-07-29 23:05:51 +00:00
|
|
|
deriving (Eq, Ord)
|
|
|
|
|
|
|
|
{- 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)
|
|
|
|
- - Low priority alwerts, newest first
|
|
|
|
- - Ties are broken by the AlertClass, with Errors etc coming first.
|
|
|
|
-}
|
|
|
|
compareAlertPairs :: AlertPair -> AlertPair -> Ordering
|
|
|
|
compareAlertPairs
|
|
|
|
(aid, Alert {alertClass = aclass, alertPriority = aprio})
|
|
|
|
(bid, Alert {alertClass = bclass, alertPriority = bprio})
|
|
|
|
= compare aprio bprio
|
|
|
|
`thenOrd` compare aid bid
|
|
|
|
`thenOrd` compare aclass bclass
|
|
|
|
|
|
|
|
sortAlertPairs :: [AlertPair] -> [AlertPair]
|
|
|
|
sortAlertPairs = reverse . sortBy compareAlertPairs
|
|
|
|
|
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-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"
|
|
|
|
|
2012-07-29 23:05:51 +00:00
|
|
|
runningAlert :: Alert
|
2012-07-29 23:41:17 +00:00
|
|
|
runningAlert = baseActivityAlert
|
2012-07-29 23:05:51 +00:00
|
|
|
{ alertClass = Success
|
2012-07-29 23:41:17 +00:00
|
|
|
, alertMessage = StringAlert "Running"
|
|
|
|
, alertPriority = Pinned
|
2012-07-29 23:05:51 +00:00
|
|
|
}
|
|
|
|
|
2012-07-29 22:07:45 +00:00
|
|
|
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"
|
|
|
|
, alertMessage = StringAlert $ unwords
|
|
|
|
[ "The daily sanity check found and fixed a problem:"
|
|
|
|
, msg
|
|
|
|
, "If these problems persist, consider filing a bug report."
|
|
|
|
]
|
|
|
|
, alertBlockDisplay = True
|
2012-07-29 23:05:51 +00:00
|
|
|
, alertPriority = High
|
2012-07-29 23:41:17 +00:00
|
|
|
, alertClosable = True
|
2012-07-29 22:07:45 +00:00
|
|
|
}
|