Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Query / Job.hs @ 3e02cd3c

History | View | Annotate | Download (5.6 kB)

1 a7e484c4 Iustin Pop
{-| Implementation of the Ganeti Query2 job queries.
2 a7e484c4 Iustin Pop
3 a7e484c4 Iustin Pop
 -}
4 a7e484c4 Iustin Pop
5 a7e484c4 Iustin Pop
{-
6 a7e484c4 Iustin Pop
7 a7e484c4 Iustin Pop
Copyright (C) 2012 Google Inc.
8 a7e484c4 Iustin Pop
9 a7e484c4 Iustin Pop
This program is free software; you can redistribute it and/or modify
10 a7e484c4 Iustin Pop
it under the terms of the GNU General Public License as published by
11 a7e484c4 Iustin Pop
the Free Software Foundation; either version 2 of the License, or
12 a7e484c4 Iustin Pop
(at your option) any later version.
13 a7e484c4 Iustin Pop
14 a7e484c4 Iustin Pop
This program is distributed in the hope that it will be useful, but
15 a7e484c4 Iustin Pop
WITHOUT ANY WARRANTY; without even the implied warranty of
16 a7e484c4 Iustin Pop
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 a7e484c4 Iustin Pop
General Public License for more details.
18 a7e484c4 Iustin Pop
19 a7e484c4 Iustin Pop
You should have received a copy of the GNU General Public License
20 a7e484c4 Iustin Pop
along with this program; if not, write to the Free Software
21 a7e484c4 Iustin Pop
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 a7e484c4 Iustin Pop
02110-1301, USA.
23 a7e484c4 Iustin Pop
24 a7e484c4 Iustin Pop
-}
25 a7e484c4 Iustin Pop
26 a7e484c4 Iustin Pop
module Ganeti.Query.Job
27 a7e484c4 Iustin Pop
  ( RuntimeData
28 a7e484c4 Iustin Pop
  , fieldsMap
29 a7e484c4 Iustin Pop
  , loadRuntimeData
30 a7e484c4 Iustin Pop
  , wantArchived
31 a7e484c4 Iustin Pop
  ) where
32 a7e484c4 Iustin Pop
33 a7e484c4 Iustin Pop
import qualified Data.Map as Map
34 a7e484c4 Iustin Pop
import qualified Text.JSON as J
35 a7e484c4 Iustin Pop
36 a7e484c4 Iustin Pop
import Ganeti.BasicTypes
37 a7e484c4 Iustin Pop
import qualified Ganeti.Constants as C
38 a7e484c4 Iustin Pop
import Ganeti.JQueue
39 a7e484c4 Iustin Pop
import Ganeti.Path
40 a7e484c4 Iustin Pop
import Ganeti.Query.Common
41 a7e484c4 Iustin Pop
import Ganeti.Query.Language
42 a7e484c4 Iustin Pop
import Ganeti.Query.Types
43 a7e484c4 Iustin Pop
import Ganeti.Types
44 a7e484c4 Iustin Pop
45 a7e484c4 Iustin Pop
-- | The runtime data for a job.
46 a7e484c4 Iustin Pop
type RuntimeData = Result (QueuedJob, Bool)
47 a7e484c4 Iustin Pop
48 a7e484c4 Iustin Pop
-- | Job priority explanation.
49 a7e484c4 Iustin Pop
jobPrioDoc :: String
50 a7e484c4 Iustin Pop
jobPrioDoc = "Current job priority (" ++ show C.opPrioLowest ++ " to " ++
51 a7e484c4 Iustin Pop
             show C.opPrioHighest ++ ")"
52 a7e484c4 Iustin Pop
53 a7e484c4 Iustin Pop
-- | Timestamp doc.
54 a7e484c4 Iustin Pop
tsDoc :: String -> String
55 a7e484c4 Iustin Pop
tsDoc = (++ " (tuple containing seconds and microseconds)")
56 a7e484c4 Iustin Pop
57 a7e484c4 Iustin Pop
-- | Wrapper for unavailable job.
58 a7e484c4 Iustin Pop
maybeJob :: (J.JSON a) =>
59 a7e484c4 Iustin Pop
            (QueuedJob -> a) -> RuntimeData -> JobId -> ResultEntry
60 a7e484c4 Iustin Pop
maybeJob _ (Bad _) _      = rsUnavail
61 a7e484c4 Iustin Pop
maybeJob f (Ok (v, _))  _ = rsNormal $ f v
62 a7e484c4 Iustin Pop
63 d45a824b Iustin Pop
-- | Wrapper for optional fields that should become unavailable.
64 d45a824b Iustin Pop
maybeJobOpt :: (J.JSON a) =>
65 d45a824b Iustin Pop
            (QueuedJob -> Maybe a) -> RuntimeData -> JobId -> ResultEntry
66 d45a824b Iustin Pop
maybeJobOpt _ (Bad _) _      = rsUnavail
67 d45a824b Iustin Pop
maybeJobOpt f (Ok (v, _))  _ = case f v of
68 d45a824b Iustin Pop
                                 Nothing -> rsUnavail
69 d45a824b Iustin Pop
                                 Just w -> rsNormal w
70 d45a824b Iustin Pop
71 a7e484c4 Iustin Pop
-- | Simple helper for a job getter.
72 a7e484c4 Iustin Pop
jobGetter :: (J.JSON a) => (QueuedJob -> a) -> FieldGetter JobId RuntimeData
73 a7e484c4 Iustin Pop
jobGetter = FieldRuntime . maybeJob
74 a7e484c4 Iustin Pop
75 a7e484c4 Iustin Pop
-- | Simple helper for a per-opcode getter.
76 a7e484c4 Iustin Pop
opsGetter :: (J.JSON a) => (QueuedOpCode -> a) -> FieldGetter JobId RuntimeData
77 a7e484c4 Iustin Pop
opsGetter f = FieldRuntime $ maybeJob (map f . qjOps)
78 a7e484c4 Iustin Pop
79 d45a824b Iustin Pop
-- | Simple helper for a per-opcode optional field getter.
80 d45a824b Iustin Pop
opsOptGetter :: (J.JSON a) =>
81 d45a824b Iustin Pop
                (QueuedOpCode -> Maybe a) -> FieldGetter JobId RuntimeData
82 d45a824b Iustin Pop
opsOptGetter f =
83 d45a824b Iustin Pop
  FieldRuntime $ maybeJob (map (\qo -> case f qo of
84 d45a824b Iustin Pop
                                         Nothing -> J.JSNull
85 d45a824b Iustin Pop
                                         Just a -> J.showJSON a) . qjOps)
86 d45a824b Iustin Pop
87 a7e484c4 Iustin Pop
-- | Archived field name.
88 a7e484c4 Iustin Pop
archivedField :: String
89 a7e484c4 Iustin Pop
archivedField = "archived"
90 a7e484c4 Iustin Pop
91 a7e484c4 Iustin Pop
-- | Check whether we should look at archived jobs as well.
92 a7e484c4 Iustin Pop
wantArchived :: [FilterField] -> Bool
93 a7e484c4 Iustin Pop
wantArchived = (archivedField `elem`)
94 a7e484c4 Iustin Pop
95 a7e484c4 Iustin Pop
-- | List of all node fields. FIXME: QFF_JOB_ID on the id field.
96 a7e484c4 Iustin Pop
jobFields :: FieldList JobId RuntimeData
97 a7e484c4 Iustin Pop
jobFields =
98 a7e484c4 Iustin Pop
  [ (FieldDefinition "id" "ID" QFTNumber "Job ID", FieldSimple rsNormal,
99 a7e484c4 Iustin Pop
     QffNormal)
100 a7e484c4 Iustin Pop
  , (FieldDefinition "status" "Status" QFTText "Job status",
101 a7e484c4 Iustin Pop
     jobGetter calcJobStatus, QffNormal)
102 a7e484c4 Iustin Pop
  , (FieldDefinition "priority" "Priority" QFTNumber jobPrioDoc,
103 a7e484c4 Iustin Pop
     jobGetter calcJobPriority, QffNormal)
104 a7e484c4 Iustin Pop
  , (FieldDefinition archivedField "Archived" QFTBool
105 a7e484c4 Iustin Pop
       "Whether job is archived",
106 a7e484c4 Iustin Pop
     FieldRuntime (\jinfo _ -> case jinfo of
107 a7e484c4 Iustin Pop
                                 Ok (_, archive) -> rsNormal archive
108 a7e484c4 Iustin Pop
                                 _ -> rsUnavail), QffNormal)
109 a7e484c4 Iustin Pop
  , (FieldDefinition "ops" "OpCodes" QFTOther "List of all opcodes",
110 a7e484c4 Iustin Pop
     opsGetter qoInput, QffNormal)
111 a7e484c4 Iustin Pop
  , (FieldDefinition "opresult" "OpCode_result" QFTOther
112 a7e484c4 Iustin Pop
       "List of opcodes results", opsGetter qoResult, QffNormal)
113 a7e484c4 Iustin Pop
  , (FieldDefinition "opstatus" "OpCode_status" QFTOther
114 a7e484c4 Iustin Pop
       "List of opcodes status", opsGetter qoStatus, QffNormal)
115 a7e484c4 Iustin Pop
  , (FieldDefinition "oplog" "OpCode_log" QFTOther
116 a7e484c4 Iustin Pop
       "List of opcode output logs", opsGetter qoLog, QffNormal)
117 a7e484c4 Iustin Pop
  , (FieldDefinition "opstart" "OpCode_start" QFTOther
118 a7e484c4 Iustin Pop
       "List of opcode start timestamps (before acquiring locks)",
119 d45a824b Iustin Pop
     opsOptGetter qoStartTimestamp, QffNormal)
120 a7e484c4 Iustin Pop
  , (FieldDefinition "opexec" "OpCode_exec" QFTOther
121 a7e484c4 Iustin Pop
       "List of opcode execution start timestamps (after acquiring locks)",
122 d45a824b Iustin Pop
     opsOptGetter qoExecTimestamp, QffNormal)
123 a7e484c4 Iustin Pop
  , (FieldDefinition "opend" "OpCode_end" QFTOther
124 a7e484c4 Iustin Pop
       "List of opcode execution end timestamps",
125 d45a824b Iustin Pop
     opsOptGetter qoEndTimestamp, QffNormal)
126 a7e484c4 Iustin Pop
  , (FieldDefinition "oppriority" "OpCode_prio" QFTOther
127 a7e484c4 Iustin Pop
       "List of opcode priorities", opsGetter qoPriority, QffNormal)
128 a7e484c4 Iustin Pop
  , (FieldDefinition "summary" "Summary" QFTOther
129 a7e484c4 Iustin Pop
       "List of per-opcode summaries",
130 d45a824b Iustin Pop
     opsGetter (extractOpSummary . qoInput), QffNormal)
131 a7e484c4 Iustin Pop
  , (FieldDefinition "received_ts" "Received" QFTOther
132 a7e484c4 Iustin Pop
       (tsDoc "Timestamp of when job was received"),
133 d45a824b Iustin Pop
     FieldRuntime (maybeJobOpt qjReceivedTimestamp), QffTimestamp)
134 a7e484c4 Iustin Pop
  , (FieldDefinition "start_ts" "Start" QFTOther
135 a7e484c4 Iustin Pop
       (tsDoc "Timestamp of job start"),
136 d45a824b Iustin Pop
     FieldRuntime (maybeJobOpt qjStartTimestamp), QffTimestamp)
137 a7e484c4 Iustin Pop
  , (FieldDefinition "end_ts" "End" QFTOther
138 a7e484c4 Iustin Pop
       (tsDoc "Timestamp of job end"),
139 d45a824b Iustin Pop
     FieldRuntime (maybeJobOpt qjEndTimestamp), QffTimestamp)
140 a7e484c4 Iustin Pop
  ]
141 a7e484c4 Iustin Pop
142 a7e484c4 Iustin Pop
-- | The node fields map.
143 a7e484c4 Iustin Pop
fieldsMap :: FieldMap JobId RuntimeData
144 a7e484c4 Iustin Pop
fieldsMap =
145 a7e484c4 Iustin Pop
  Map.fromList $ map (\v@(f, _, _) -> (fdefName f, v)) jobFields
146 a7e484c4 Iustin Pop
147 a7e484c4 Iustin Pop
-- | Load the given jobs from disk.
148 a7e484c4 Iustin Pop
loadRuntimeData :: [JobId] -> Bool -> IO [RuntimeData]
149 a7e484c4 Iustin Pop
loadRuntimeData ids archived = do
150 a7e484c4 Iustin Pop
  qdir <- queueDir
151 a7e484c4 Iustin Pop
  mapM (loadJobFromDisk qdir archived) ids