aboutsummaryrefslogtreecommitdiff
path: root/server/src
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2016-02-04 23:00:38 +0000
committerGregor Kleen <gkleen@yggdrasil.li>2016-02-04 23:00:38 +0000
commit409e212bb4ef76784e6da1ef854e2cb85bebc0af (patch)
tree0e1a74c7bcc88321e98954a4cf2d6d96d7360b79 /server/src
parent04e26a5b118155c5dffb817b523bc8eada952b55 (diff)
downloadthermoprint-409e212bb4ef76784e6da1ef854e2cb85bebc0af.tar
thermoprint-409e212bb4ef76784e6da1ef854e2cb85bebc0af.tar.gz
thermoprint-409e212bb4ef76784e6da1ef854e2cb85bebc0af.tar.bz2
thermoprint-409e212bb4ef76784e6da1ef854e2cb85bebc0af.tar.xz
thermoprint-409e212bb4ef76784e6da1ef854e2cb85bebc0af.zip
More appropriate morphism for later use
Diffstat (limited to 'server/src')
-rw-r--r--server/src/Thermoprint/Server/Queue.hs40
1 files changed, 30 insertions, 10 deletions
diff --git a/server/src/Thermoprint/Server/Queue.hs b/server/src/Thermoprint/Server/Queue.hs
index 68dc7a9..17865b6 100644
--- a/server/src/Thermoprint/Server/Queue.hs
+++ b/server/src/Thermoprint/Server/Queue.hs
@@ -15,9 +15,12 @@ import qualified Thermoprint.API as API (JobStatus(..))
15 15
16import Thermoprint.Server.Database 16import Thermoprint.Server.Database
17 17
18import Data.Sequence (Seq, ViewL(..), viewl) 18import Data.Sequence (Seq, ViewL(..), viewl, (|>), (<|))
19import qualified Data.Sequence as Seq 19import qualified Data.Sequence as Seq
20 20
21import Data.Set (Set)
22import qualified Data.Set as Set
23
21import Data.Time 24import Data.Time
22import Data.Time.Clock 25import Data.Time.Clock
23 26
@@ -34,6 +37,7 @@ import Data.Default.Class
34import Control.Monad.Morph 37import Control.Monad.Morph
35import Control.Monad.Trans.Compose 38import Control.Monad.Trans.Compose
36 39
40import Data.Foldable
37import Data.Monoid 41import Data.Monoid
38 42
39-- | Zipper for 'Seq QueueEntry' with additional support for 'PrintingError' in the section after point 43-- | Zipper for 'Seq QueueEntry' with additional support for 'PrintingError' in the section after point
@@ -44,16 +48,32 @@ data Queue = Queue
44 } 48 }
45 deriving (Typeable, Generic, NFData) 49 deriving (Typeable, Generic, NFData)
46 50
47toSeq :: Queue -> Seq (Bool, QueueEntry, Maybe PrintingError) 51data QueueElem = Pending QueueEntry | Current QueueEntry | History QueueEntry (Maybe PrintingError)
48toSeq Queue{..} = fmap (\x -> (False, x, Nothing)) pending <> maybe Seq.empty (\c -> Seq.singleton (True, c, Nothing)) current <> fmap (\(x, p) -> (False, x, p)) history 52
53instance Eq QueueElem where
54 (Pending a) == (Pending b) = a == b
55 (Current a) == (Current b) = a == b
56 (History a _) == (History b _) = a == b
57 _ == _ = False
58
59instance Ord QueueElem where
60 (Pending _) <= _ = True
61 (Current _) <= (Pending _) = False
62 (Current _) <= _ = True
63 (History _ _) <= (History _ _) = True
64 (History _ _) <= _ = False
49 65
50fromSeq :: Seq (Bool, QueueEntry, Maybe PrintingError) -> Queue 66toSeq :: Queue -> Set (Int, QueueElem)
51fromSeq s = Queue pending' current' history' 67toSeq Queue{..} = fromSeq $ fmap Pending pending <> maybe Seq.empty (Seq.singleton . Current) current <> fmap (uncurry History) history
52 where 68 where
53 (fmap (\(_, x, _) -> x) -> pending', pending'') = Seq.breakl (\(c, _, _) -> c) s 69 fromSeq = Set.fromAscList . toList . Seq.mapWithIndex ((,))
54 (current', history') = case viewl pending'' of 70
55 EmptyL -> (Nothing, Seq.empty) 71fromSeq :: Set (Int, QueueElem) -> Queue
56 (_, a, _) :< as -> (Just a, fmap (\(_, x, p) -> (x, p)) as) 72fromSeq = foldr' (insert . snd) def
73 where
74 insert (Pending e) q@(Queue{..}) = q { pending = pending |> e }
75 insert (Current e) q = q { current = Just e }
76 insert (History e p) q@(Queue{..}) = q { history = history |> (e, p) }
57 77
58class HasQueue a where 78class HasQueue a where
59 extractQueue :: a -> TVar Queue 79 extractQueue :: a -> TVar Queue
@@ -72,7 +92,7 @@ data QueueEntry = QueueEntry
72 { jobId :: JobId 92 { jobId :: JobId
73 , created :: UTCTime 93 , created :: UTCTime
74 } 94 }
75 deriving (Typeable, Generic, NFData) 95 deriving (Typeable, Generic, NFData, Eq)
76 96
77-- | A queue manager periodically modifies a 'Queue', e.g. for cleanup of old jobs 97-- | A queue manager periodically modifies a 'Queue', e.g. for cleanup of old jobs
78type QueueManager t = ComposeT (StateT Queue) t STM DiffTime 98type QueueManager t = ComposeT (StateT Queue) t STM DiffTime