Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Query / Job.hs @ 88772d17

History | View | Annotate | Download (5.3 kB)

1
{-| Implementation of the Ganeti Query2 job queries.
2

    
3
 -}
4

    
5
{-
6

    
7
Copyright (C) 2012 Google Inc.
8

    
9
This program is free software; you can redistribute it and/or modify
10
it under the terms of the GNU General Public License as published by
11
the Free Software Foundation; either version 2 of the License, or
12
(at your option) any later version.
13

    
14
This program is distributed in the hope that it will be useful, but
15
WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
General Public License for more details.
18

    
19
You should have received a copy of the GNU General Public License
20
along with this program; if not, write to the Free Software
21
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22
02110-1301, USA.
23

    
24
-}
25

    
26
module Ganeti.Query.Job
27
  ( RuntimeData
28
  , fieldsMap
29
  , wantArchived
30
  ) where
31

    
32
import qualified Text.JSON as J
33

    
34
import Ganeti.BasicTypes
35
import qualified Ganeti.Constants as C
36
import Ganeti.JQueue
37
import Ganeti.Query.Common
38
import Ganeti.Query.Language
39
import Ganeti.Query.Types
40
import Ganeti.Types
41

    
42
-- | The runtime data for a job.
43
type RuntimeData = Result (QueuedJob, Bool)
44

    
45
-- | Job priority explanation.
46
jobPrioDoc :: String
47
jobPrioDoc = "Current job priority (" ++ show C.opPrioLowest ++ " to " ++
48
             show C.opPrioHighest ++ ")"
49

    
50
-- | Timestamp doc.
51
tsDoc :: String -> String
52
tsDoc = (++ " (tuple containing seconds and microseconds)")
53

    
54
-- | Wrapper for unavailable job.
55
maybeJob :: (J.JSON a) =>
56
            (QueuedJob -> a) -> RuntimeData -> JobId -> ResultEntry
57
maybeJob _ (Bad _) _      = rsUnavail
58
maybeJob f (Ok (v, _))  _ = rsNormal $ f v
59

    
60
-- | Wrapper for optional fields that should become unavailable.
61
maybeJobOpt :: (J.JSON a) =>
62
            (QueuedJob -> Maybe a) -> RuntimeData -> JobId -> ResultEntry
63
maybeJobOpt _ (Bad _) _      = rsUnavail
64
maybeJobOpt f (Ok (v, _))  _ = case f v of
65
                                 Nothing -> rsUnavail
66
                                 Just w -> rsNormal w
67

    
68
-- | Simple helper for a job getter.
69
jobGetter :: (J.JSON a) => (QueuedJob -> a) -> FieldGetter JobId RuntimeData
70
jobGetter = FieldRuntime . maybeJob
71

    
72
-- | Simple helper for a per-opcode getter.
73
opsGetter :: (J.JSON a) => (QueuedOpCode -> a) -> FieldGetter JobId RuntimeData
74
opsGetter f = FieldRuntime $ maybeJob (map f . qjOps)
75

    
76
-- | Simple helper for a per-opcode optional field getter.
77
opsOptGetter :: (J.JSON a) =>
78
                (QueuedOpCode -> Maybe a) -> FieldGetter JobId RuntimeData
79
opsOptGetter f =
80
  FieldRuntime $ maybeJob (map (\qo -> case f qo of
81
                                         Nothing -> J.JSNull
82
                                         Just a -> J.showJSON a) . qjOps)
83

    
84
-- | Archived field name.
85
archivedField :: String
86
archivedField = "archived"
87

    
88
-- | Check whether we should look at archived jobs as well.
89
wantArchived :: [FilterField] -> Bool
90
wantArchived = (archivedField `elem`)
91

    
92
-- | List of all node fields. FIXME: QFF_JOB_ID on the id field.
93
jobFields :: FieldList JobId RuntimeData
94
jobFields =
95
  [ (FieldDefinition "id" "ID" QFTNumber "Job ID", FieldSimple rsNormal,
96
     QffNormal)
97
  , (FieldDefinition "status" "Status" QFTText "Job status",
98
     jobGetter calcJobStatus, QffNormal)
99
  , (FieldDefinition "priority" "Priority" QFTNumber jobPrioDoc,
100
     jobGetter calcJobPriority, QffNormal)
101
  , (FieldDefinition archivedField "Archived" QFTBool
102
       "Whether job is archived",
103
     FieldRuntime (\jinfo _ -> case jinfo of
104
                                 Ok (_, archive) -> rsNormal archive
105
                                 _ -> rsUnavail), QffNormal)
106
  , (FieldDefinition "ops" "OpCodes" QFTOther "List of all opcodes",
107
     opsGetter qoInput, QffNormal)
108
  , (FieldDefinition "opresult" "OpCode_result" QFTOther
109
       "List of opcodes results", opsGetter qoResult, QffNormal)
110
  , (FieldDefinition "opstatus" "OpCode_status" QFTOther
111
       "List of opcodes status", opsGetter qoStatus, QffNormal)
112
  , (FieldDefinition "oplog" "OpCode_log" QFTOther
113
       "List of opcode output logs", opsGetter qoLog, QffNormal)
114
  , (FieldDefinition "opstart" "OpCode_start" QFTOther
115
       "List of opcode start timestamps (before acquiring locks)",
116
     opsOptGetter qoStartTimestamp, QffNormal)
117
  , (FieldDefinition "opexec" "OpCode_exec" QFTOther
118
       "List of opcode execution start timestamps (after acquiring locks)",
119
     opsOptGetter qoExecTimestamp, QffNormal)
120
  , (FieldDefinition "opend" "OpCode_end" QFTOther
121
       "List of opcode execution end timestamps",
122
     opsOptGetter qoEndTimestamp, QffNormal)
123
  , (FieldDefinition "oppriority" "OpCode_prio" QFTOther
124
       "List of opcode priorities", opsGetter qoPriority, QffNormal)
125
  , (FieldDefinition "summary" "Summary" QFTOther
126
       "List of per-opcode summaries",
127
     opsGetter (extractOpSummary . qoInput), QffNormal)
128
  , (FieldDefinition "received_ts" "Received" QFTOther
129
       (tsDoc "Timestamp of when job was received"),
130
     FieldRuntime (maybeJobOpt qjReceivedTimestamp), QffTimestamp)
131
  , (FieldDefinition "start_ts" "Start" QFTOther
132
       (tsDoc "Timestamp of job start"),
133
     FieldRuntime (maybeJobOpt qjStartTimestamp), QffTimestamp)
134
  , (FieldDefinition "end_ts" "End" QFTOther
135
       (tsDoc "Timestamp of job end"),
136
     FieldRuntime (maybeJobOpt qjEndTimestamp), QffTimestamp)
137
  ]
138

    
139
-- | The node fields map.
140
fieldsMap :: FieldMap JobId RuntimeData
141
fieldsMap = fieldListToFieldMap jobFields