git-annex/Assistant/Alert.hs

411 lines
12 KiB
Haskell
Raw Normal View History

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.
-}
2013-02-28 06:21:43 +00:00
{-# LANGUAGE OverloadedStrings #-}
2012-07-29 13:35:01 +00:00
module Assistant.Alert where
import Common.Annex
import qualified Remote
import Utility.Tense
2012-08-06 21:09:23 +00:00
import Logs.Transfer
import qualified Data.Text as T
import Data.Text (Text)
2012-07-30 16:21:53 +00:00
import qualified Data.Map as M
import Data.String
2012-07-29 13:35:01 +00:00
{- 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)
{- An alert can have an name, which is used to combine it with other similar
- alerts. -}
2012-09-08 19:07:44 +00:00
data AlertName
= FileAlert TenseChunk
| SanityCheckFixAlert
| WarningAlert String
| PairAlert String
| XMPPNeededAlert
| RemoteRemovalAlert String
| CloudRepoNeededAlert
| SyncAlert
deriving (Eq)
{- The first alert is the new alert, the second is an old alert.
- Should return a modified version of the old alert. -}
2012-08-06 19:41:42 +00:00
type AlertCombiner = Alert -> Alert -> Maybe Alert
2012-07-29 13:35:01 +00:00
data Alert = Alert
{ alertClass :: AlertClass
, alertHeader :: Maybe TenseText
2012-08-06 19:41:42 +00:00
, alertMessageRender :: [TenseChunk] -> TenseText
, alertData :: [TenseChunk]
, alertBlockDisplay :: Bool
, alertClosable :: Bool
2012-07-29 23:05:51 +00:00
, alertPriority :: AlertPriority
, alertIcon :: Maybe AlertIcon
2012-08-06 19:41:42 +00:00
, alertCombiner :: Maybe AlertCombiner
, alertName :: Maybe AlertName
, alertButton :: Maybe AlertButton
}
data AlertIcon = ActivityIcon | SuccessIcon | ErrorIcon | InfoIcon | TheCloud
{- When clicked, a button always redirects to a URL
- It may also run an IO action in the background, which is useful
- to make the button close or otherwise change the alert. -}
data AlertButton = AlertButton
{ buttonLabel :: Text
, buttonUrl :: Text
, buttonAction :: Maybe (AlertId -> IO ())
}
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. -}
newtype AlertId = AlertId Integer
2012-12-13 04:45:27 +00:00
deriving (Read, Show, Eq, Ord)
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
2012-07-30 16:21:53 +00:00
- user with a ton of alerts. -}
displayAlerts :: Int
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:
-
- - 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 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
(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
{- Renders an alert's header for display, if it has one. -}
renderAlertHeader :: Alert -> Maybe Text
renderAlertHeader alert = renderTense (alertTense alert) <$> alertHeader alert
{- Renders an alert's message for display. -}
renderAlertMessage :: Alert -> Text
2012-08-06 19:41:42 +00:00
renderAlertMessage alert = renderTense (alertTense alert) $
(alertMessageRender alert) (alertData alert)
2013-01-15 18:09:35 +00:00
showAlert :: Alert -> String
showAlert alert = T.unpack $ T.unwords $ catMaybes
[ renderAlertHeader alert
, Just $ renderAlertMessage alert
]
alertTense :: Alert -> Tense
alertTense alert
| alertClass alert == Activity = Present
| otherwise = Past
{- Checks if two alerts display the same. -}
effectivelySameAlert :: Alert -> Alert -> Bool
effectivelySameAlert x y = all id
[ alertClass x == alertClass y
, alertHeader x == alertHeader y
2012-08-06 19:41:42 +00:00
, alertData x == alertData y
, alertBlockDisplay x == alertBlockDisplay y
, alertClosable x == alertClosable y
, alertPriority x == alertPriority y
]
makeAlertFiller :: Bool -> Alert -> Alert
makeAlertFiller success alert
2012-07-30 16:21:53 +00:00
| isFiller alert = alert
| otherwise = alert
{ alertClass = if c == Activity then c' else c
, alertPriority = Filler
2012-07-30 16:23:40 +00:00
, alertClosable = True
, alertButton = Nothing
, alertIcon = Just $ if success then SuccessIcon else ErrorIcon
}
2012-10-31 06:34:03 +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
2012-08-06 21:09:23 +00:00
{- Updates the Alertmap, adding or updating an alert.
-
2012-08-06 21:09:23 +00:00
- Any old filler that looks the same as the alert is removed.
-
- Or, if the alert has an alertCombiner that combines it with
- an old alert, the old alert is replaced with the result, and the
- alert is removed.
2012-07-30 16:21:53 +00:00
-
- Old filler alerts are pruned once maxAlerts is reached.
-}
2012-08-06 21:09:23 +00:00
mergeAlert :: AlertId -> Alert -> AlertMap -> AlertMap
mergeAlert i al m = maybe updatePrune updateCombine (alertCombiner al)
2012-10-31 06:34:03 +00:00
where
pruneSame k al' = k == i || not (effectivelySameAlert al 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 (\(_, a) -> isFiller a) l
in drop bloat f ++ rest
updatePrune = pruneBloat $ M.filterWithKey pruneSame $
M.insertWith' const i al m
updateCombine combiner =
let combined = M.mapMaybe (combiner al) m
in if M.null combined
then updatePrune
else M.delete i $ M.union combined m
2012-07-29 23:05:51 +00:00
baseActivityAlert :: Alert
baseActivityAlert = Alert
{ alertClass = Activity
, alertHeader = Nothing
2012-08-06 19:41:42 +00:00
, alertMessageRender = tenseWords
, alertData = []
, alertBlockDisplay = False
, alertClosable = False
2012-07-29 23:05:51 +00:00
, alertPriority = Medium
, alertIcon = Just ActivityIcon
, alertCombiner = Nothing
, alertName = Nothing
, alertButton = Nothing
2012-07-29 13:35:01 +00:00
}
warningAlert :: String -> String -> Alert
warningAlert name msg = Alert
{ alertClass = Warning
, alertHeader = Just $ tenseWords ["warning"]
, alertMessageRender = tenseWords
, alertData = [UnTensed $ T.pack msg]
, alertBlockDisplay = True
, alertClosable = True
, alertPriority = High
, alertIcon = Just ErrorIcon
, alertCombiner = Just $ dataCombiner (++)
, alertName = Just $ WarningAlert name
, alertButton = Nothing
}
2012-08-06 19:41:42 +00:00
activityAlert :: Maybe TenseText -> [TenseChunk] -> Alert
activityAlert header dat = baseActivityAlert
{ alertHeader = header
2012-08-06 19:41:42 +00:00
, alertData = dat
}
startupScanAlert :: Alert
2012-09-13 04:57:52 +00:00
startupScanAlert = activityAlert Nothing
2012-08-06 19:41:42 +00:00
[Tensed "Performing" "Performed", "startup scan"]
2012-08-02 18:02:35 +00:00
commitAlert :: Alert
2012-09-13 04:57:52 +00:00
commitAlert = activityAlert Nothing
[Tensed "Committing" "Committed", "changes to git"]
showRemotes :: [Remote] -> TenseChunk
2013-03-18 23:11:46 +00:00
showRemotes = UnTensed . T.intercalate ", " . map (T.pack . Remote.name)
2012-08-02 18:02:35 +00:00
2012-08-06 21:09:23 +00:00
syncAlert :: [Remote] -> Alert
syncAlert rs = baseActivityAlert
{ alertName = Just SyncAlert
, alertHeader = Just $ tenseWords
2012-08-06 21:09:23 +00:00
[Tensed "Syncing" "Synced", "with", showRemotes rs]
2012-07-29 23:05:51 +00:00
, alertPriority = Low
2012-12-13 04:45:27 +00:00
}
syncResultAlert :: [Remote] -> [Remote] -> Alert
syncResultAlert succeeded failed = makeAlertFiller (not $ null succeeded) $
baseActivityAlert
{ alertName = Just SyncAlert
, alertHeader = Just $ tenseWords msg
}
where
msg
| null succeeded = ["Failed to sync with", showRemotes failed]
| null failed = ["Synced with", showRemotes succeeded]
| otherwise =
[ "Synced with", showRemotes succeeded
, "but not with", showRemotes failed
]
sanityCheckAlert :: Alert
sanityCheckAlert = activityAlert
(Just $ tenseWords [Tensed "Running" "Ran", "daily sanity check"])
2012-08-06 19:41:42 +00:00
["to make sure everything is ok."]
sanityCheckFixAlert :: String -> Alert
sanityCheckFixAlert msg = Alert
{ alertClass = Warning
, alertHeader = Just $ tenseWords ["Fixed a problem"]
2012-08-06 19:41:42 +00:00
, alertMessageRender = render
, alertData = [UnTensed $ T.pack msg]
, alertBlockDisplay = True
2012-07-29 23:05:51 +00:00
, alertPriority = High
, alertClosable = True
, alertIcon = Just ErrorIcon
, alertName = Just SanityCheckFixAlert
2012-08-06 19:41:42 +00:00
, alertCombiner = Just $ dataCombiner (++)
, alertButton = Nothing
}
2012-10-31 06:34:03 +00:00
where
render dta = tenseWords $ alerthead : dta ++ [alertfoot]
alerthead = "The daily sanity check found and fixed a problem:"
alertfoot = "If these problems persist, consider filing a bug report."
pairingAlert :: AlertButton -> Alert
pairingAlert button = baseActivityAlert
{ alertData = [ UnTensed "Pairing in progress" ]
, alertPriority = High
, alertButton = Just button
}
pairRequestReceivedAlert :: String -> AlertButton -> Alert
pairRequestReceivedAlert who button = Alert
2012-09-11 20:11:28 +00:00
{ alertClass = Message
, alertHeader = Nothing
, alertMessageRender = tenseWords
, alertData = [UnTensed $ T.pack $ who ++ " is sending a pair request."]
2012-09-11 20:11:28 +00:00
, alertBlockDisplay = False
2012-09-08 19:07:44 +00:00
, alertPriority = High
, alertClosable = True
, alertIcon = Just InfoIcon
, alertName = Just $ PairAlert who
, alertCombiner = Just $ dataCombiner $ \_old new -> new
, alertButton = Just button
}
2012-09-13 04:57:52 +00:00
pairRequestAcknowledgedAlert :: String -> Maybe AlertButton -> Alert
pairRequestAcknowledgedAlert who button = baseActivityAlert
{ alertData = ["Pairing with", UnTensed (T.pack who), Tensed "in progress" "complete"]
, alertPriority = High
, alertName = Just $ PairAlert who
, alertCombiner = Just $ dataCombiner $ \_old new -> new
2012-09-11 19:43:33 +00:00
, alertButton = button
2012-09-08 19:07:44 +00:00
}
xmppNeededAlert :: AlertButton -> Alert
xmppNeededAlert button = Alert
2012-11-11 00:52:46 +00:00
{ alertHeader = Just "Share with friends, and keep your devices in sync across the cloud."
, alertIcon = Just TheCloud
, alertPriority = High
, alertButton = Just button
, alertClosable = True
, alertClass = Message
, alertMessageRender = tenseWords
, alertBlockDisplay = True
, alertName = Just $ XMPPNeededAlert
, alertCombiner = Just $ dataCombiner $ \_old new -> new
, alertData = []
}
cloudRepoNeededAlert :: Maybe String -> AlertButton -> Alert
cloudRepoNeededAlert friendname button = Alert
{ alertHeader = Just $ fromString $ unwords
[ "Unable to download files from"
, (fromMaybe "your other devices" friendname) ++ "."
]
, alertIcon = Just ErrorIcon
, alertPriority = High
, alertButton = Just button
, alertClosable = True
, alertClass = Message
, alertMessageRender = tenseWords
, alertBlockDisplay = True
, alertName = Just $ CloudRepoNeededAlert
, alertCombiner = Just $ dataCombiner $ \_old new -> new
, alertData = []
}
remoteRemovalAlert :: String -> AlertButton -> Alert
remoteRemovalAlert desc button = Alert
{ alertHeader = Just $ fromString $
"The repository \"" ++ desc ++
"\" has been emptied, and can now be removed."
, alertIcon = Just InfoIcon
, alertPriority = High
, alertButton = Just button
, alertClosable = True
, alertClass = Message
, alertMessageRender = tenseWords
, alertBlockDisplay = True
, alertName = Just $ RemoteRemovalAlert desc
, alertCombiner = Just $ dataCombiner $ \_old new -> new
, alertData = []
}
2012-08-06 21:09:23 +00:00
fileAlert :: TenseChunk -> FilePath -> Alert
fileAlert msg file = (activityAlert Nothing [f])
{ alertName = Just $ FileAlert msg
2012-08-06 19:41:42 +00:00
, alertMessageRender = render
, alertCombiner = Just $ dataCombiner combiner
}
2012-10-31 06:34:03 +00:00
where
f = fromString $ shortFile $ takeFileName file
render fs = tenseWords $ msg : fs
combiner new old = take 10 $ new ++ old
2012-08-06 19:41:42 +00:00
2012-08-06 21:09:23 +00:00
addFileAlert :: FilePath -> Alert
addFileAlert = fileAlert (Tensed "Adding" "Added")
{- This is only used as a success alert after a transfer, not during it. -}
transferFileAlert :: Direction -> Bool -> FilePath -> Alert
transferFileAlert direction True
2012-08-06 21:09:23 +00:00
| direction == Upload = fileAlert "Uploaded"
| otherwise = fileAlert "Downloaded"
transferFileAlert direction False
| direction == Upload = fileAlert "Upload failed"
| otherwise = fileAlert "Download failed"
2012-08-06 21:09:23 +00:00
2012-08-06 19:41:42 +00:00
dataCombiner :: ([TenseChunk] -> [TenseChunk] -> [TenseChunk]) -> AlertCombiner
dataCombiner combiner new old
| alertClass new /= alertClass old = Nothing
| alertName new == alertName old =
Just $! old { alertData = alertData new `combiner` alertData old }
| otherwise = Nothing
2012-08-02 17:47:26 +00:00
shortFile :: FilePath -> String
shortFile f
| len < maxlen = f
| otherwise = take half f ++ ".." ++ drop (len - half) f
2012-10-31 06:34:03 +00:00
where
len = length f
maxlen = 20
half = (maxlen - 2) `div` 2
2012-08-02 17:47:26 +00:00