Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / Query / Job.hs @ a7e484c4

History | View | Annotate | Download (4.9 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
  , loadRuntimeData
30
  , wantArchived
31
  ) where
32

    
33
import qualified Data.Map as Map
34
import qualified Text.JSON as J
35

    
36
import Ganeti.BasicTypes
37
import qualified Ganeti.Constants as C
38
import Ganeti.JQueue
39
import Ganeti.OpCodes (opSummary, metaOpCode)
40
import Ganeti.Path
41
import Ganeti.Query.Common
42
import Ganeti.Query.Language
43
import Ganeti.Query.Types
44
import Ganeti.Types
45

    
46
-- | The runtime data for a job.
47
type RuntimeData = Result (QueuedJob, Bool)
48

    
49
-- | Job priority explanation.
50
jobPrioDoc :: String
51
jobPrioDoc = "Current job priority (" ++ show C.opPrioLowest ++ " to " ++
52
             show C.opPrioHighest ++ ")"
53

    
54
-- | Timestamp doc.
55
tsDoc :: String -> String
56
tsDoc = (++ " (tuple containing seconds and microseconds)")
57

    
58
-- | Wrapper for unavailable job.
59
maybeJob :: (J.JSON a) =>
60
            (QueuedJob -> a) -> RuntimeData -> JobId -> ResultEntry
61
maybeJob _ (Bad _) _      = rsUnavail
62
maybeJob f (Ok (v, _))  _ = rsNormal $ f v
63

    
64
-- | Simple helper for a job getter.
65
jobGetter :: (J.JSON a) => (QueuedJob -> a) -> FieldGetter JobId RuntimeData
66
jobGetter = FieldRuntime . maybeJob
67

    
68
-- | Simple helper for a per-opcode getter.
69
opsGetter :: (J.JSON a) => (QueuedOpCode -> a) -> FieldGetter JobId RuntimeData
70
opsGetter f = FieldRuntime $ maybeJob (map f . qjOps)
71

    
72
-- | Archived field name.
73
archivedField :: String
74
archivedField = "archived"
75

    
76
-- | Check whether we should look at archived jobs as well.
77
wantArchived :: [FilterField] -> Bool
78
wantArchived = (archivedField `elem`)
79

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

    
127
-- | The node fields map.
128
fieldsMap :: FieldMap JobId RuntimeData
129
fieldsMap =
130
  Map.fromList $ map (\v@(f, _, _) -> (fdefName f, v)) jobFields
131

    
132
-- | Load the given jobs from disk.
133
loadRuntimeData :: [JobId] -> Bool -> IO [RuntimeData]
134
loadRuntimeData ids archived = do
135
  qdir <- queueDir
136
  mapM (loadJobFromDisk qdir archived) ids