Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Query / Job.hs @ c92b4671

History | View | Annotate | Download (5.3 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
  , wantArchived
30 a7e484c4 Iustin Pop
  ) where
31 a7e484c4 Iustin Pop
32 a7e484c4 Iustin Pop
import qualified Text.JSON as J
33 a7e484c4 Iustin Pop
34 a7e484c4 Iustin Pop
import Ganeti.BasicTypes
35 a7e484c4 Iustin Pop
import qualified Ganeti.Constants as C
36 a7e484c4 Iustin Pop
import Ganeti.JQueue
37 a7e484c4 Iustin Pop
import Ganeti.Query.Common
38 a7e484c4 Iustin Pop
import Ganeti.Query.Language
39 a7e484c4 Iustin Pop
import Ganeti.Query.Types
40 a7e484c4 Iustin Pop
import Ganeti.Types
41 a7e484c4 Iustin Pop
42 a7e484c4 Iustin Pop
-- | The runtime data for a job.
43 a7e484c4 Iustin Pop
type RuntimeData = Result (QueuedJob, Bool)
44 a7e484c4 Iustin Pop
45 a7e484c4 Iustin Pop
-- | Job priority explanation.
46 a7e484c4 Iustin Pop
jobPrioDoc :: String
47 a7e484c4 Iustin Pop
jobPrioDoc = "Current job priority (" ++ show C.opPrioLowest ++ " to " ++
48 a7e484c4 Iustin Pop
             show C.opPrioHighest ++ ")"
49 a7e484c4 Iustin Pop
50 a7e484c4 Iustin Pop
-- | Timestamp doc.
51 a7e484c4 Iustin Pop
tsDoc :: String -> String
52 a7e484c4 Iustin Pop
tsDoc = (++ " (tuple containing seconds and microseconds)")
53 a7e484c4 Iustin Pop
54 a7e484c4 Iustin Pop
-- | Wrapper for unavailable job.
55 a7e484c4 Iustin Pop
maybeJob :: (J.JSON a) =>
56 a7e484c4 Iustin Pop
            (QueuedJob -> a) -> RuntimeData -> JobId -> ResultEntry
57 a7e484c4 Iustin Pop
maybeJob _ (Bad _) _      = rsUnavail
58 a7e484c4 Iustin Pop
maybeJob f (Ok (v, _))  _ = rsNormal $ f v
59 a7e484c4 Iustin Pop
60 d45a824b Iustin Pop
-- | Wrapper for optional fields that should become unavailable.
61 d45a824b Iustin Pop
maybeJobOpt :: (J.JSON a) =>
62 d45a824b Iustin Pop
            (QueuedJob -> Maybe a) -> RuntimeData -> JobId -> ResultEntry
63 d45a824b Iustin Pop
maybeJobOpt _ (Bad _) _      = rsUnavail
64 d45a824b Iustin Pop
maybeJobOpt f (Ok (v, _))  _ = case f v of
65 d45a824b Iustin Pop
                                 Nothing -> rsUnavail
66 d45a824b Iustin Pop
                                 Just w -> rsNormal w
67 d45a824b Iustin Pop
68 a7e484c4 Iustin Pop
-- | Simple helper for a job getter.
69 a7e484c4 Iustin Pop
jobGetter :: (J.JSON a) => (QueuedJob -> a) -> FieldGetter JobId RuntimeData
70 a7e484c4 Iustin Pop
jobGetter = FieldRuntime . maybeJob
71 a7e484c4 Iustin Pop
72 a7e484c4 Iustin Pop
-- | Simple helper for a per-opcode getter.
73 a7e484c4 Iustin Pop
opsGetter :: (J.JSON a) => (QueuedOpCode -> a) -> FieldGetter JobId RuntimeData
74 a7e484c4 Iustin Pop
opsGetter f = FieldRuntime $ maybeJob (map f . qjOps)
75 a7e484c4 Iustin Pop
76 d45a824b Iustin Pop
-- | Simple helper for a per-opcode optional field getter.
77 d45a824b Iustin Pop
opsOptGetter :: (J.JSON a) =>
78 d45a824b Iustin Pop
                (QueuedOpCode -> Maybe a) -> FieldGetter JobId RuntimeData
79 d45a824b Iustin Pop
opsOptGetter f =
80 d45a824b Iustin Pop
  FieldRuntime $ maybeJob (map (\qo -> case f qo of
81 d45a824b Iustin Pop
                                         Nothing -> J.JSNull
82 d45a824b Iustin Pop
                                         Just a -> J.showJSON a) . qjOps)
83 d45a824b Iustin Pop
84 a7e484c4 Iustin Pop
-- | Archived field name.
85 a7e484c4 Iustin Pop
archivedField :: String
86 a7e484c4 Iustin Pop
archivedField = "archived"
87 a7e484c4 Iustin Pop
88 a7e484c4 Iustin Pop
-- | Check whether we should look at archived jobs as well.
89 a7e484c4 Iustin Pop
wantArchived :: [FilterField] -> Bool
90 a7e484c4 Iustin Pop
wantArchived = (archivedField `elem`)
91 a7e484c4 Iustin Pop
92 a7e484c4 Iustin Pop
-- | List of all node fields. FIXME: QFF_JOB_ID on the id field.
93 a7e484c4 Iustin Pop
jobFields :: FieldList JobId RuntimeData
94 a7e484c4 Iustin Pop
jobFields =
95 a7e484c4 Iustin Pop
  [ (FieldDefinition "id" "ID" QFTNumber "Job ID", FieldSimple rsNormal,
96 a7e484c4 Iustin Pop
     QffNormal)
97 a7e484c4 Iustin Pop
  , (FieldDefinition "status" "Status" QFTText "Job status",
98 a7e484c4 Iustin Pop
     jobGetter calcJobStatus, QffNormal)
99 a7e484c4 Iustin Pop
  , (FieldDefinition "priority" "Priority" QFTNumber jobPrioDoc,
100 a7e484c4 Iustin Pop
     jobGetter calcJobPriority, QffNormal)
101 a7e484c4 Iustin Pop
  , (FieldDefinition archivedField "Archived" QFTBool
102 a7e484c4 Iustin Pop
       "Whether job is archived",
103 a7e484c4 Iustin Pop
     FieldRuntime (\jinfo _ -> case jinfo of
104 a7e484c4 Iustin Pop
                                 Ok (_, archive) -> rsNormal archive
105 a7e484c4 Iustin Pop
                                 _ -> rsUnavail), QffNormal)
106 a7e484c4 Iustin Pop
  , (FieldDefinition "ops" "OpCodes" QFTOther "List of all opcodes",
107 a7e484c4 Iustin Pop
     opsGetter qoInput, QffNormal)
108 a7e484c4 Iustin Pop
  , (FieldDefinition "opresult" "OpCode_result" QFTOther
109 a7e484c4 Iustin Pop
       "List of opcodes results", opsGetter qoResult, QffNormal)
110 a7e484c4 Iustin Pop
  , (FieldDefinition "opstatus" "OpCode_status" QFTOther
111 a7e484c4 Iustin Pop
       "List of opcodes status", opsGetter qoStatus, QffNormal)
112 a7e484c4 Iustin Pop
  , (FieldDefinition "oplog" "OpCode_log" QFTOther
113 a7e484c4 Iustin Pop
       "List of opcode output logs", opsGetter qoLog, QffNormal)
114 a7e484c4 Iustin Pop
  , (FieldDefinition "opstart" "OpCode_start" QFTOther
115 a7e484c4 Iustin Pop
       "List of opcode start timestamps (before acquiring locks)",
116 d45a824b Iustin Pop
     opsOptGetter qoStartTimestamp, QffNormal)
117 a7e484c4 Iustin Pop
  , (FieldDefinition "opexec" "OpCode_exec" QFTOther
118 a7e484c4 Iustin Pop
       "List of opcode execution start timestamps (after acquiring locks)",
119 d45a824b Iustin Pop
     opsOptGetter qoExecTimestamp, QffNormal)
120 a7e484c4 Iustin Pop
  , (FieldDefinition "opend" "OpCode_end" QFTOther
121 a7e484c4 Iustin Pop
       "List of opcode execution end timestamps",
122 d45a824b Iustin Pop
     opsOptGetter qoEndTimestamp, QffNormal)
123 a7e484c4 Iustin Pop
  , (FieldDefinition "oppriority" "OpCode_prio" QFTOther
124 a7e484c4 Iustin Pop
       "List of opcode priorities", opsGetter qoPriority, QffNormal)
125 a7e484c4 Iustin Pop
  , (FieldDefinition "summary" "Summary" QFTOther
126 a7e484c4 Iustin Pop
       "List of per-opcode summaries",
127 d45a824b Iustin Pop
     opsGetter (extractOpSummary . qoInput), QffNormal)
128 a7e484c4 Iustin Pop
  , (FieldDefinition "received_ts" "Received" QFTOther
129 a7e484c4 Iustin Pop
       (tsDoc "Timestamp of when job was received"),
130 d45a824b Iustin Pop
     FieldRuntime (maybeJobOpt qjReceivedTimestamp), QffTimestamp)
131 a7e484c4 Iustin Pop
  , (FieldDefinition "start_ts" "Start" QFTOther
132 a7e484c4 Iustin Pop
       (tsDoc "Timestamp of job start"),
133 d45a824b Iustin Pop
     FieldRuntime (maybeJobOpt qjStartTimestamp), QffTimestamp)
134 a7e484c4 Iustin Pop
  , (FieldDefinition "end_ts" "End" QFTOther
135 a7e484c4 Iustin Pop
       (tsDoc "Timestamp of job end"),
136 d45a824b Iustin Pop
     FieldRuntime (maybeJobOpt qjEndTimestamp), QffTimestamp)
137 a7e484c4 Iustin Pop
  ]
138 a7e484c4 Iustin Pop
139 a7e484c4 Iustin Pop
-- | The node fields map.
140 a7e484c4 Iustin Pop
fieldsMap :: FieldMap JobId RuntimeData
141 c92b4671 Klaus Aehlig
fieldsMap = fieldListToFieldMap jobFields