forked from cylc/cylc-uiserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.py
566 lines (499 loc) · 17.8 KB
/
schema.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# Copyright (C) NIWA & British Crown (Met Office) & Contributors.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""GraphQL Schema.
This is a strict superset of the Cylc Flow schema that includes
extra functionality specific to the UIS.
"""
from functools import partial
from typing import TYPE_CHECKING, Any, List, Optional
import graphene
from graphene.types.generic import GenericScalar
from cylc.flow.id import Tokens
from cylc.flow.network.schema import (
CyclePoint,
GenericResponse,
ID,
SortArgs,
Task,
Mutations,
Queries,
process_resolver_info,
STRIP_NULL_DEFAULT,
Subscriptions,
WorkflowID,
_mut_field,
sstrip,
get_nodes_all
)
from cylc.uiserver.resolvers import (
Resolvers,
list_log_files,
stream_log,
)
if TYPE_CHECKING:
from graphql import ResolveInfo
async def mutator(
root: Optional[Any],
info: 'ResolveInfo',
*,
command: str,
workflows: Optional[List[str]] = None,
**kwargs: Any
):
"""Call the resolver method that act on the workflow service
via the internal command queue."""
if workflows is None:
workflows = []
parsed_workflows = [Tokens(w_id) for w_id in workflows]
if kwargs.get('args', False):
kwargs.update(kwargs.get('args', {}))
kwargs.pop('args')
resolvers: 'Resolvers' = (
info.context.get('resolvers') # type: ignore[union-attr]
)
res = await resolvers.service(info, command, parsed_workflows, kwargs)
return GenericResponse(result=res)
class RunMode(graphene.Enum):
"""The mode to run a workflow in."""
Live = 'live'
Dummy = 'dummy'
DummyLocal = 'dummy-local'
Simulation = 'simulation'
@property
def description(self):
if self == RunMode.Live:
return 'The normal default run mode'
if self == RunMode.Dummy:
return 'Replaces all *-script items with nothing'
if self == RunMode.DummyLocal:
return (
'Replaces all *-script items with nothing and sets'
' platform = localhost for all tasks.'
)
if self == RunMode.Simulation:
return 'Simulates job submission, does not run anything at all.'
class CylcVersion(graphene.String):
"""A Cylc version identifier e.g. 8.0.0"""
class Play(graphene.Mutation):
class Meta:
description = sstrip('''
Start, resume or restart a workflow run.
Valid for: stopped workflows.
''')
# Note we have the "resume" mutation for paused workflows.
resolver = partial(mutator, command='play')
class Arguments:
workflows = graphene.List(WorkflowID, required=True)
cylc_version = CylcVersion(
description=sstrip('''
Set the Cylc version that the workflow starts with.
''')
)
initial_cycle_point = CyclePoint(
description=sstrip('''
Set the initial cycle point.
Required if not defined in flow.cylc.
''')
)
start_cycle_point = CyclePoint(
description=sstrip('''
Set the start cycle point, which may be after the initial cycle
point.
If the specified start point is not in the sequence, the next
on-sequence point will be used.
(Not to be confused with the initial cycle point).
This replaces the Cylc 7 --warm option.
''')
)
final_cycle_point = CyclePoint(
description=sstrip('''
Set the final cycle point. This command line option overrides
the workflow config option `[scheduling]final cycle point`.
''')
)
stop_cycle_point = CyclePoint(
description=sstrip('''
Set the stop cycle point. Shut down after all tasks have PASSED
this cycle point. (Not to be confused with the final cycle
point.) This command line option overrides the workflow config
option `[scheduling]stop after cycle point`.
''')
)
pause = graphene.Boolean(
description=sstrip('''
Pause workflow immediately on starting.
''')
)
hold_cycle_point = CyclePoint(
description=sstrip('''
Hold all tasks after this cycle point.
''')
)
mode = RunMode(
default_value=RunMode.Live.name # type: ignore[attr-defined]
)
host = graphene.String(
description=sstrip('''
Specify the host on which to start-up the workflow. If not
specified, a host will be selected using the
`[scheduler]run hosts` global config.
''')
)
main_loop = graphene.List(
graphene.String,
description=sstrip('''
Specify an additional plugin to run in the main loop. These
are used in combination with those specified in
`[scheduler][main loop]plugins`. Can be used multiple times.
''')
)
upgrade = graphene.Boolean(
default_value=False,
description=sstrip('''
Allow the workflow to be restarted with a newer
version of Cylc.
''')
)
abort_if_any_task_fails = graphene.Boolean(
default_value=False,
description=sstrip('''
If set workflow will abort with status 1 if any task fails.
''')
)
debug = graphene.Boolean(
default_value=False,
description=sstrip('''
Output developer information and show exception tracebacks.
''')
)
no_timestamp = graphene.Boolean(
default_value=False,
description=sstrip('''
Don't timestamp logged messages.
''')
)
set = graphene.List( # noqa: A003 (graphql field name)
graphene.String,
description=sstrip('''
Set the value of a Jinja2 template variable in the workflow
definition. Values should be valid Python literals so strings
must be quoted e.g. `STR="string"`, `INT=43`, `BOOL=True`.
This option can be used multiple times on the command line.
NOTE: these settings persist across workflow restarts, but can
be set again on the `cylc play` command line if they need to be
overridden.
''')
)
set_file = graphene.String(
description=sstrip('''
Set the value of Jinja2 template variables in the workflow
definition from a file containing NAME=VALUE pairs (one per
line). As with "set" values should be valid Python literals so
strings must be quoted e.g. `STR='string'`. NOTE: these
settings persist across workflow restarts, but can be set again
on the `cylc play` command line if they need to be overridden.
''')
)
result = GenericScalar()
class Clean(graphene.Mutation):
class Meta:
description = sstrip('''
Clean a workflow from the run directory.
Valid for: stopped workflows.
''')
resolver = partial(mutator, command='clean')
class Arguments:
workflows = graphene.List(WorkflowID, required=True)
rm = graphene.String(
default_value='',
description=sstrip('''
Only clean the specified subdirectories or files in
the run directory, rather than the whole run.
A colon separated list that accepts globs,
e.g. ``.service/db:log:share:work/2020*``.
''')
)
local_only = graphene.Boolean(
default_value=False,
description=sstrip('''
Only clean on the local filesystem (not remote hosts).
''')
)
remote_only = graphene.Boolean(
default_value=False,
description=sstrip('''
Only clean on remote hosts (not the local filesystem).
''')
)
result = GenericScalar()
class Scan(graphene.Mutation):
class Meta:
description = sstrip('''
Scan the filesystem for file changes.
Valid for: stopped workflows.
''')
resolver = partial(mutator, command='scan')
result = GenericScalar()
async def get_jobs(root, info, **kwargs):
if kwargs['live']:
return await get_nodes_all(root, info, **kwargs)
_, field_ids = process_resolver_info(root, info, kwargs)
if hasattr(kwargs, 'id'):
kwargs['ids'] = [kwargs.get('id')]
if field_ids:
if isinstance(field_ids, str):
field_ids = [field_ids]
elif isinstance(field_ids, dict):
field_ids = list(field_ids)
kwargs['ids'] = field_ids
elif field_ids == []:
return []
for arg in ('ids', 'exids'):
# live objects can be represented by a universal ID
kwargs[arg] = [Tokens(n_id, relative=True) for n_id in kwargs[arg]]
kwargs['workflows'] = [
Tokens(w_id) for w_id in kwargs['workflows']]
kwargs['exworkflows'] = [
Tokens(w_id) for w_id in kwargs['exworkflows']]
return await list_jobs(kwargs)
async def list_jobs(args):
if not args['workflows']:
raise Exception('At least one workflow must be provided.')
from cylc.flow.rundb import CylcWorkflowDAO
from cylc.flow.pathutil import get_workflow_run_dir
from cylc.flow.workflow_files import WorkflowFiles
jobs = []
for workflow in args['workflows']:
db_file = get_workflow_run_dir(
workflow['workflow'],
WorkflowFiles.LogDir.DIRNAME,
"db"
)
with CylcWorkflowDAO(db_file, is_public=True) as dao:
conn = dao.connect()
jobs.extend(make_query(conn, workflow))
return jobs
def make_query(conn, workflow):
# TODO: support all arguments including states
# https://github.com/cylc/cylc-uiserver/issues/440
tasks = []
for row in conn.execute('''
SELECT
name,
cycle,
submit_num,
submit_status,
time_run,
time_run_exit,
job_id,
platform_name,
time_submit,
-- Calculate Queue time stats
MIN(queue_time) AS min_queue_time,
AVG(queue_time) AS mean_queue_time,
MAX(queue_time) AS max_queue_time,
AVG(queue_time * queue_time) AS mean_squares_queue_time,
MAX(CASE WHEN queue_time_quartile = 1 THEN queue_time END)
queue_quartile_1,
MAX(CASE WHEN queue_time_quartile = 2 THEN queue_time END)
queue_quartile_2,
MAX(CASE WHEN queue_time_quartile = 3 THEN queue_time END)
queue_quartile_3,
-- Calculate Run time stats
MIN(run_time) AS min_run_time,
AVG(run_time) AS mean_run_time,
MAX(run_time) AS max_run_time,
AVG(run_time * run_time) AS mean_squares_run_time,
MAX(CASE WHEN run_time_quartile = 1 THEN run_time END) run_quartile_1,
MAX(CASE WHEN run_time_quartile = 2 THEN run_time END) run_quartile_2,
MAX(CASE WHEN run_time_quartile = 3 THEN run_time END) run_quartile_3,
-- Calculate Total time stats
MIN(total_time) AS min_total_time,
AVG(total_time) AS mean_total_time,
MAX(total_time) AS max_total_time,
AVG(total_time * total_time) AS mean_squares_total_time,
MAX(CASE WHEN total_time_quartile = 1 THEN total_time END)
total_quartile_1,
MAX(CASE WHEN total_time_quartile = 2 THEN total_time END)
total_quartile_2,
MAX(CASE WHEN total_time_quartile = 3 THEN total_time END)
total_quartile_3,
COUNT(*) AS n
FROM
(SELECT
*,
NTILE (4) OVER (PARTITION BY name ORDER BY queue_time)
queue_time_quartile,
NTILE (4) OVER (PARTITION BY name ORDER BY run_time)
run_time_quartile,
NTILE (4) OVER (PARTITION BY name ORDER BY total_time)
total_time_quartile
FROM
(SELECT
*,
STRFTIME('%s', time_run_exit) -
STRFTIME('%s', time_submit) AS total_time,
STRFTIME('%s', time_run_exit) -
STRFTIME('%s', time_run) AS run_time,
STRFTIME('%s', time_run) -
STRFTIME('%s', time_submit) AS queue_time
FROM
task_jobs
WHERE
run_status = 0))
GROUP BY
name;
'''):
tasks.append({
'id': workflow.duplicate(
cycle=row[1],
task=row[0],
job=row[2]
),
'name': row[0],
'cycle_point': row[1],
'submit_num': row[2],
'state': row[3],
'started_time': row[4],
'finished_time': row[5],
'job_ID': row[6],
'platform': row[7],
'submitted_time': row[8],
# Queue time stats
'min_queue_time': row[9],
'mean_queue_time': row[10],
'max_queue_time': row[11],
'std_dev_queue_time': (row[12] - row[10]**2)**0.5,
'queue_quartiles': [row[13],
row[13] if row[14] is None else row[14],
row[13] if row[15] is None else row[15]],
# Run time stats
'min_run_time': row[16],
'mean_run_time': row[17],
'max_run_time': row[18],
'std_dev_run_time': (row[19] - row[17]**2)**0.5,
'run_quartiles': [row[20],
row[20] if row[21] is None else row[21],
row[20] if row[22] is None else row[22]],
# Total
'min_total_time': row[23],
'mean_total_time': row[24],
'max_total_time': row[25],
'std_dev_total_time': (row[26] - row[24] ** 2) ** 0.5,
'total_quartiles': [row[27],
row[27] if row[28] is None else row[28],
row[27] if row[29] is None else row[29]],
'count': row[30]
})
return tasks
class UISTask(Task):
platform = graphene.String()
min_total_time = graphene.Int()
mean_total_time = graphene.Int()
max_total_time = graphene.Int()
std_dev_total_time = graphene.Int()
queue_quartiles = graphene.List(
graphene.Int,
description=sstrip('''
List containing the first, second,
third and forth quartile queue times.'''))
min_queue_time = graphene.Int()
mean_queue_time = graphene.Int()
max_queue_time = graphene.Int()
std_dev_queue_time = graphene.Int()
run_quartiles = graphene.List(
graphene.Int,
description=sstrip('''
List containing the first, second,
third and forth quartile run times.'''))
min_run_time = graphene.Int()
mean_run_time = graphene.Int()
max_run_time = graphene.Int()
std_dev_run_time = graphene.Int()
total_quartiles = graphene.List(
graphene.Int,
description=sstrip('''
List containing the first, second,
third and forth quartile total times.'''))
count = graphene.Int()
class UISQueries(Queries):
class LogFiles(graphene.ObjectType):
# Example GraphiQL query:
# {
# logFiles(workflowID: "<workflow_id>", task: "<task_id>") {
# files
# }
# }
files = graphene.List(graphene.String)
log_files = graphene.Field(
LogFiles,
description='List available job logs',
id=graphene.Argument(
graphene.ID,
description='workflow//[cycle/task]',
required=True,
),
resolver=list_log_files
)
tasks = graphene.List(
UISTask,
description=Task._meta.description,
live=graphene.Boolean(default_value=True),
strip_null=STRIP_NULL_DEFAULT,
resolver=get_jobs,
workflows=graphene.List(ID, default_value=[]),
exworkflows=graphene.List(ID, default_value=[]),
ids=graphene.List(ID, default_value=[]),
exids=graphene.List(ID, default_value=[]),
mindepth=graphene.Int(default_value=-1),
maxdepth=graphene.Int(default_value=-1),
sort=SortArgs(default_value=None),
)
class UISSubscriptions(Subscriptions):
# Example graphiql workflow log subscription:
# subscription {
# logs(workflow: "foo") {
# lines
# }
# }
class Logs(graphene.ObjectType):
lines = graphene.List(graphene.String)
connected = graphene.Boolean()
path = graphene.String()
error = graphene.String()
logs = graphene.Field(
Logs,
description='Workflow & job logs',
id=graphene.Argument(
graphene.ID,
description='workflow//[cycle/task[/job]]',
required=True,
),
file=graphene.Argument(
graphene.String,
required=False,
description='File name of job log to fetch, e.g. job.out'
),
resolver=stream_log
)
class UISMutations(Mutations):
play = _mut_field(Play)
clean = _mut_field(Clean)
scan = _mut_field(Scan)
schema = graphene.Schema(
query=UISQueries,
subscription=UISSubscriptions,
mutation=UISMutations
)