remove old filler that is effectively the same as new filler

This commit is contained in:
Joey Hess 2012-07-30 15:33:12 -04:00
parent f4484949ef
commit 5469bd6e42

View file

@ -36,6 +36,10 @@ data Alert = Alert
, alertPriority :: AlertPriority , alertPriority :: AlertPriority
} }
type AlertPair = (AlertId, Alert)
type AlertMap = M.Map AlertId Alert
{- Higher AlertId indicates a more recent alert. -} {- Higher AlertId indicates a more recent alert. -}
newtype AlertId = AlertId Integer newtype AlertId = AlertId Integer
deriving (Read, Show, Eq, Ord) deriving (Read, Show, Eq, Ord)
@ -47,15 +51,11 @@ firstAlertId = AlertId 0
nextAlertId :: AlertId -> AlertId nextAlertId :: AlertId -> AlertId
nextAlertId (AlertId i) = AlertId $ succ i nextAlertId (AlertId i) = AlertId $ succ i
type AlertPair = (AlertId, Alert)
type AlertMap = M.Map AlertId Alert
{- This is as many alerts as it makes sense to display at a time. {- 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 - A display might be smaller ,or larger, the point is to not overwhelm the
- user with a ton of alerts. -} - user with a ton of alerts. -}
displayAlerts :: Int displayAlerts :: Int
displayAlerts = 10 displayAlerts = 6
{- This is not a hard maximum, but there's no point in keeping a great {- 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, - many filler alerts in an AlertMap, so when there's more than this many,
@ -74,8 +74,8 @@ maxAlerts = displayAlerts * 2
-} -}
compareAlertPairs :: AlertPair -> AlertPair -> Ordering compareAlertPairs :: AlertPair -> AlertPair -> Ordering
compareAlertPairs compareAlertPairs
(aid, Alert {alertClass = aclass, alertPriority = aprio}) (aid, Alert { alertClass = aclass, alertPriority = aprio })
(bid, Alert {alertClass = bclass, alertPriority = bprio}) (bid, Alert { alertClass = bclass, alertPriority = bprio })
= compare aprio bprio = compare aprio bprio
`thenOrd` compare aid bid `thenOrd` compare aid bid
`thenOrd` compare aclass bclass `thenOrd` compare aclass bclass
@ -83,6 +83,25 @@ compareAlertPairs
sortAlertPairs :: [AlertPair] -> [AlertPair] sortAlertPairs :: [AlertPair] -> [AlertPair]
sortAlertPairs = sortBy compareAlertPairs sortAlertPairs = sortBy compareAlertPairs
{- 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 _ = ""
makeAlertFiller :: Bool -> Alert -> Alert makeAlertFiller :: Bool -> Alert -> Alert
makeAlertFiller success alert makeAlertFiller success alert
| isFiller alert = alert | isFiller alert = alert
@ -113,19 +132,28 @@ isFiller :: Alert -> Bool
isFiller alert = alertPriority alert == Filler isFiller alert = alertPriority alert == Filler
{- Converts a given alert into filler, manipulating it in the AlertMap. {- Converts a given alert into filler, manipulating it in the AlertMap.
-
- Any old filler that looks the same as the reference alert is removed.
- -
- Old filler alerts are pruned once maxAlerts is reached. - Old filler alerts are pruned once maxAlerts is reached.
-} -}
convertToFiller :: AlertId -> Bool -> AlertMap -> AlertMap convertToFiller :: AlertId -> Bool -> AlertMap -> AlertMap
convertToFiller i success m convertToFiller i success m = case M.lookup i m of
| bloat > 0 = M.fromList $ prune $ M.toList m' Nothing -> m
| otherwise = m' Just al ->
let al' = makeAlertFiller success al
in pruneBloat $ M.filterWithKey (pruneSame al') $
M.insertWith' const i al' m
where where
bloat = M.size m - maxAlerts pruneSame ref k al = k == i || not (effectivelySameAlert ref al)
m' = M.adjust (\al -> makeAlertFiller success al) i m pruneBloat m'
prune l = | bloat > 0 = M.fromList $ pruneold $ M.toList m'
let (f, rest) = partition (\(_, al) -> isFiller al) l | otherwise = m'
in drop bloat f ++ rest where
bloat = M.size m' - maxAlerts
pruneold l =
let (f, rest) = partition (\(_, al) -> isFiller al) l
in drop bloat f ++ rest
baseActivityAlert :: Alert baseActivityAlert :: Alert
baseActivityAlert = Alert baseActivityAlert = Alert