forked from UtrechtUniversity/yoda-ruleset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintake.py
827 lines (666 loc) · 28.4 KB
/
intake.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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
# -*- coding: utf-8 -*-
"""Functions for intake module."""
__copyright__ = 'Copyright (c) 2019-2021, Utrecht University'
__license__ = 'GPLv3, see LICENSE'
import fnmatch
import time
import genquery
import intake_dataset
import intake_lock
import intake_scan
from util import *
__all__ = ['api_intake_list_studies',
'api_intake_list_dm_studies',
'api_intake_count_total_files',
'api_intake_list_unrecognized_files',
'api_intake_list_datasets',
'api_intake_scan_for_datasets',
'api_intake_lock_dataset',
'api_intake_unlock_dataset',
'api_intake_dataset_get_details',
'api_intake_dataset_add_comment',
'api_intake_report_vault_dataset_counts_per_study',
'api_intake_report_vault_aggregated_info',
'api_intake_report_export_study_data',
'rule_intake_scan_for_datasets']
INTAKE_FILE_EXCLUSION_PATTERNS = ['*.abc', '*.PNG']
""" List of file patterns not to take into account within INTAKE module."""
@api.make()
def api_intake_list_studies(ctx):
"""Get list of all studies current user is involved in.
:param ctx: Combined type of a callback and rei struct
:returns: List of studies
"""
groups = []
user_name = user.name(ctx)
user_zone = user.zone(ctx)
iter = genquery.row_iterator(
"USER_GROUP_NAME",
"USER_NAME = '" + user_name + "' AND USER_ZONE = '" + user_zone + "'",
genquery.AS_LIST, ctx
)
for row in iter:
if row[0].startswith('grp-intake-'):
groups.append(row[0][11:])
groups.sort()
return groups
@api.make()
def api_intake_list_dm_studies(ctx):
"""Return list of studies current user is datamanager of.
:param ctx: Combined type of a callback and rei struct
:returns: List of dm studies
"""
datamanager_groups = []
user_name = user.name(ctx)
user_zone = user.zone(ctx)
iter = genquery.row_iterator(
"USER_GROUP_NAME",
"USER_NAME = '" + user_name + "' AND USER_ZONE = '" + user_zone + "'",
genquery.AS_LIST, ctx
)
for row in iter:
if row[0].startswith('grp-intake-'):
study = row[0][11:]
# Is a member of this study ... check whether member of corresponding datamanager group
iter2 = genquery.row_iterator(
"USER_NAME",
"USER_TYPE = 'rodsgroup' AND USER_NAME like 'datamanager-" + study + "'",
genquery.AS_LIST, ctx
)
for row2 in iter2:
datamanager_group = row2[0]
if user.is_member_of(ctx, datamanager_group):
datamanager_groups.append(study)
return datamanager_groups
@api.make()
def api_intake_count_total_files(ctx, coll):
"""Get the total count of all files in collection
.
:param ctx: Combined type of a callback and rei struct
:param coll: Collection from which to count all datasets
:returns: Total file count
"""
# Include coll name as equal names do occur and genquery delivers distinct results.
iter = genquery.row_iterator(
"COLL_NAME, DATA_NAME",
"COLL_NAME like '" + coll + "%'",
genquery.AS_LIST, ctx
)
count = 0
for row in iter:
exclusion_matched = any(fnmatch.fnmatch(row[1], p) for p in INTAKE_FILE_EXCLUSION_PATTERNS)
if not exclusion_matched:
count += 1
return count
@api.make()
def api_intake_list_unrecognized_files(ctx, coll):
"""Get list of all unrecognized files for given path including relevant metadata.
:param ctx: Combined type of a callback and rei struct
:param coll: Collection from which to list all unrecognized files
:returns: List of unrecognized files
"""
# check permissions
parts = coll.split('/')
group = parts[3]
datamanager_group = group.replace("-intake-", "-datamanager-", 1)
if user.is_member_of(ctx, group):
log.write(ctx, "IS GROUP MEMBER")
elif user.is_member_of(ctx, datamanager_group):
log.write(ctx, "IS DM")
else:
log.write(ctx, "NO PERMISSION")
return {}
# Include coll name as equal names do occur and genquery delivers distinct results.
iter = genquery.row_iterator(
"COLL_NAME, DATA_NAME, COLL_CREATE_TIME, DATA_OWNER_NAME",
"COLL_NAME like '" + coll + "%' AND META_DATA_ATTR_NAME = 'unrecognized'",
genquery.AS_LIST, ctx
)
files = []
for row in iter:
# Check whether object type is within exclusion pattern
exclusion_matched = any(fnmatch.fnmatch(row[1], p) for p in INTAKE_FILE_EXCLUSION_PATTERNS)
if not exclusion_matched:
# Error is hardcoded! (like in the original) and initialize attributes already as empty strings.
file_data = {"name": row[1],
"path": row[0],
"date": time.strftime('%Y-%m-%d', time.localtime(int(row[2]))),
"creator": row[3],
"error": 'Experiment type, wave or pseudocode is missing from path',
"experiment_type": '',
"pseudocode": '',
"wave": '',
"version": ''}
# per data object get relevent metadata (experiment type, version, wave, pseudocode) if present
iter2 = genquery.row_iterator(
"META_DATA_ATTR_NAME, META_DATA_ATTR_VALUE",
"COLL_NAME = '" + row[0] + "' AND DATA_NAME = '" + row[1] + "' AND META_DATA_ATTR_NAME in ('experiment_type', 'pseudocode', 'wave', 'version')",
genquery.AS_LIST, ctx
)
for row2 in iter2:
file_data[row2[0]] = row2[1]
files.append(file_data)
return files
@api.make()
def api_intake_list_datasets(ctx, coll):
"""Get list of datasets for given path.
A dataset is distinguished by attribute name 'dataset_toplevel' which can either reside on a collection or a data object.
That is why 2 seperate queries have to be performed.
:param ctx: Combined type of a callback and rei struct
:param coll: Collection from which to list all datasets
:returns: list of datasets
"""
datasets = []
# 1) Query for datasets distinguished by collections
iter = genquery.row_iterator(
"META_COLL_ATTR_VALUE, COLL_NAME",
"COLL_NAME like '" + coll + "%' AND META_COLL_ATTR_NAME = 'dataset_toplevel' ",
genquery.AS_LIST, ctx
)
for row in iter:
dataset = get_dataset_details(ctx, row[0], row[1])
datasets.append(dataset)
# 2) Query for datasets distinguished dataobjects
iter = genquery.row_iterator(
"META_DATA_ATTR_VALUE, COLL_NAME",
"COLL_NAME like '" + coll + "%' AND META_DATA_ATTR_NAME = 'dataset_toplevel' ",
genquery.AS_LIST, ctx
)
for row in iter:
dataset = get_dataset_details(ctx, row[0], row[1])
datasets.append(dataset)
return datasets
def get_dataset_details(ctx, dataset_id, path):
"""Get details of dataset based on dataset identifier.
:param ctx: Combined type of a callback and rei struct
:param dataset_id: Identifier of dataset
:param path: Path to dataset
:returns: Dict holding objects for the dataset
"""
# Inialise all attributes
dataset = {"dataset_id": dataset_id,
"path": path}
# Parse dataset_id to get WEPV-items individually
dataset_parts = dataset_id.split('\t')
dataset['wave'] = dataset_parts[0]
dataset['expType'] = dataset_parts[1]
dataset['experiment_type'] = dataset_parts[1]
dataset['pseudocode'] = dataset_parts[2]
dataset['version'] = dataset_parts[3]
dataset['datasetStatus'] = 'scanned'
dataset['datasetCreateName'] = '==UNKNOWN=='
dataset['datasetCreateDate'] = 0
dataset['datasetCreateDateFormatted'] = ''
dataset['datasetErrors'] = 0
dataset['datasetWarnings'] = 0
dataset['datasetComments'] = 0
dataset['objects'] = 0
dataset['objectErrors'] = 0
dataset['objectWarnings'] = 0
tl_info = get_dataset_toplevel_objects(ctx, path, dataset_id)
is_collection = tl_info['is_collection']
tl_objects = tl_info['objects']
if is_collection:
""" dataset is based on a collection """
tl_collection = tl_objects[0]
iter = genquery.row_iterator(
"COLL_NAME, COLL_OWNER_NAME, COLL_CREATE_TIME",
"COLL_NAME = '" + tl_collection + "' ",
genquery.AS_LIST, ctx
)
for row in iter:
dataset['datasetCreateName'] = row[1]
dataset['datasetCreateDate'] = int(row[2])
dataset['datasetCreateDateFormatted'] = time.strftime('%Y-%m-%d', time.localtime(int(row[2])))
dataset['datasetCreatedByWhen'] = row[1] + ':' + row[2]
iter = genquery.row_iterator(
"COLL_NAME, META_COLL_ATTR_NAME, count(META_COLL_ATTR_VALUE)",
"COLL_NAME = '" + tl_collection + "' ",
genquery.AS_LIST, ctx
)
for row in iter:
if row[1] == 'dataset_error':
dataset['datasetErrors'] += int(row[2])
if row[1] == 'dataset_warning':
dataset['datasetWarnings'] += int(row[2])
if row[1] == 'comment':
dataset['datasetComments'] += int(row[2])
if row[1] == 'to_vault_freeze':
dataset['datasetStatus'] = 'frozen'
if row[1] == 'to_vault_lock':
dataset['datasetStatus'] = 'locked'
iter = genquery.row_iterator(
"COLL_NAME, META_COLL_ATTR_NAME, META_COLL_ATTR_VALUE",
"COLL_NAME = '" + tl_collection + "' ",
genquery.AS_LIST, ctx
)
for row in iter:
if row[1] == 'object_count':
dataset['objects'] += int(row[2])
if row[1] == 'object_errors':
dataset['objectErrors'] += int(row[2])
if row[1] == 'object_warnings':
dataset['objectWarnings'] += int(row[2])
else:
# Dataset is based on a dataobject
# Step through all data objects as found in tl_objects
objects = 0
object_errors = 0
object_warnings = 0
for tl_object in tl_objects:
# split tl_object
tlo = pathutil.chop(tl_object)
parent = tlo[0]
base_name = tlo[1]
objects += 1
if objects == 1:
iter = genquery.row_iterator(
"DATA_OWNER_NAME, DATA_CREATE_TIME",
"COLL_NAME = '" + parent + "' and DATA_NAME = '" + base_name + "' ",
genquery.AS_LIST, ctx
)
for row in iter:
dataset['datasetCreateName'] = row[0]
dataset['datasetCreateDate'] = int(row[1])
dataset['datasetCreateDateFormatted'] = time.strftime('%Y-%m-%d', time.localtime(int(row[1])))
dataset['datasetCreatedByWhen'] = row[0] + ':' + row[1]
iter = genquery.row_iterator(
"META_DATA_ATTR_NAME, META_DATA_ATTR_VALUE",
"COLL_NAME = '" + parent + "' and DATA_NAME = '" + base_name + "' ",
genquery.AS_LIST, ctx
)
for row in iter:
if row[0] == 'error':
object_errors += 1
if row[0] == 'warning':
object_warnings += 1
if objects == 1:
# Only look at these items when objects==1 as they are added to each toplevel object present
if row[0] == 'dataset_error':
dataset['datasetErrors'] += 1
if row[0] == 'dataset_warning':
dataset['datasetWarnings'] += 1
if row[0] == 'comment':
dataset['datasetComments'] += 1
if row[0] == 'to_vault_freeze':
dataset['datasetStatus'] = 'frozen'
if row[0] == 'to_vault_lock':
dataset['datasetStatus'] = 'locked'
dataset['objects'] = objects
dataset['objectErrors'] = object_errors
dataset['objectWarnings'] = object_warnings
return dataset
def get_dataset_toplevel_objects(ctx, root, dataset_id):
"""Returns dict with toplevel object paths and whether is collection based dataset.
If is a collection - only one object is returned (collection path).
If not a collection- all objects are returned with full object path.
:param ctx: Combined type of a callback and rei struct
:param root: Path to a dataset
:param dataset_id: Identifier of the dataset
:returns: Dict holding objects for the dataset
"""
iter = genquery.row_iterator(
"COLL_NAME",
"COLL_NAME LIKE '" + root + "%' AND META_COLL_ATTR_NAME = 'dataset_toplevel' "
"AND META_COLL_ATTR_VALUE = '" + dataset_id + "'",
genquery.AS_LIST, ctx
)
for row in iter:
return {'is_collection': True,
'objects': [row[0]]}
# For dataobject situation gather all object path strings as a list
iter = genquery.row_iterator(
"DATA_NAME, COLL_NAME",
"COLL_NAME like '" + root + "%' AND META_DATA_ATTR_NAME = 'dataset_toplevel' "
"AND META_DATA_ATTR_VALUE = '" + dataset_id + "'",
genquery.AS_LIST, ctx
)
objects = []
for row in iter:
objects.append(row[1] + '/' + row[0])
return {'is_collection': False,
'objects': objects}
@api.make()
def api_intake_scan_for_datasets(ctx, coll):
"""The toplevel of a dataset can be determined by attribute 'dataset_toplevel'
and can either be a collection or a data_object.
:param ctx: Combined type of a callback and rei struct
:param coll: Collection to scan for datasets
:returns: indication correct
"""
if _intake_check_authorized_to_scan(ctx, coll):
_intake_scan_for_datasets(ctx, coll)
else:
return {}
return {"proc_status": "OK"}
@rule.make(inputs=[0], outputs=[1])
def rule_intake_scan_for_datasets(ctx, coll):
"""The toplevel of a dataset can be determined by attribute 'dataset_toplevel'
and can either be a collection or a data_object.
:param ctx: Combined type of a callback and rei struct
:param coll: Collection to scan for datasets
:returns: indication correct
"""
if _intake_check_authorized_to_scan(ctx, coll):
_intake_scan_for_datasets(ctx, coll, tl_datasets_log_target='stdout')
else:
return 1
return 0
def _intake_check_authorized_to_scan(ctx, coll):
"""Checks that user is authorized to scan intake group, either as
a data manager or as an intake group member.
:param ctx: Combined type of a callback and rei struct
:param coll: Collection to scan for datasets
:returns: boolean - whether user is authorized
"""
parts = coll.split('/')
group = parts[3]
datamanager_group = group.replace("-intake-", "-datamanager-", 1)
if (user.is_member_of(ctx, group) or user.is_member_of(ctx, datamanager_group)):
return True
else:
log.write(ctx, "No permissions to scan collection")
return False
def _intake_scan_for_datasets(ctx, coll, tl_datasets_log_target=''):
"""Internal function for actually running intake scan
:param ctx: Combined type of a callback and rei struct
:param coll: Collection to scan for datasets
:param tl_datasets_log_target: If in ['stdout', 'serverLog'] logging of toplevel datasets will take place to the specified target
"""
scope = {"wave": "",
"experiment_type": "",
"pseudocode": ""}
found_datasets = []
found_datasets = intake_scan.intake_scan_collection(ctx, coll, scope, False, found_datasets)
if tl_datasets_log_target in ['stdout', 'serverLog']:
for subscope in found_datasets:
try:
version = subscope['version']
except KeyError:
version = 'Raw'
ctx.writeLine(tl_datasets_log_target, ("Found dataset toplevel collection: "
+ "W<" + subscope['wave']
+ "> E<" + subscope['experiment_type']
+ "> P<" + subscope['pseudocode']
+ "> V<" + version
+ "> D<" + subscope['dataset_directory']
+ ">"))
intake_scan.intake_check_datasets(ctx, coll)
@api.make()
def api_intake_lock_dataset(ctx, path, dataset_ids):
"""Lock datasets as an indication it can be 'frozen' for it to progress to vault.
Lock = datamanager only
:param ctx: Combined type of a callback and rei struct
:param path: Collection for which to lock a specific dataset id
:param dataset_ids: Comma separated identifiers of datasets to be locked
:returns: indication correct
"""
# check permissions - datamanager only
parts = path.split('/')
group = parts[3]
datamanager_group = group.replace("-intake-", "-datamanager-", 1)
if not user.is_member_of(ctx, datamanager_group):
log.write(ctx, "No permissions to lock dataset")
return {"proc_status": "NOK"}
for dataset_id in dataset_ids.split(','):
intake_lock.intake_dataset_lock(ctx, path, dataset_id)
return {"proc_status": "OK"}
@api.make()
def api_intake_unlock_dataset(ctx, path, dataset_ids):
"""Unlock a dataset to remove the indication so it can be 'frozen' for it to progress to vault
Unlock = datamanager only
:param ctx: Combined type of a callback and rei struct
:param path: Collection for which to lock a specific dataset id
:param dataset_ids: Comma separated identifiers of datasets to be locked
:returns: indication correct
"""
# check permissions - datamanager only
parts = path.split('/')
group = parts[3]
datamanager_group = group.replace("-intake-", "-datamanager-", 1)
if not user.is_member_of(ctx, datamanager_group):
log.write(ctx, "No permissions to unlock dataset")
return {"proc_status": "NOK"}
for dataset_id in dataset_ids.split(','):
intake_lock.intake_dataset_unlock(ctx, path, dataset_id)
return {"proc_status": "OK"}
@api.make()
def api_intake_dataset_add_comment(ctx, study_id, dataset_id, comment):
"""Add a comment to a dataset.
:param ctx: Combined type of a callback and rei struct
:param study_id: Id of the study given dataset belongs to
:param dataset_id: Identifier of the dataset to add a comment to
:param comment: Comment as added by user
:returns: indication correct
"""
coll = '/' + user.zone(ctx) + '/home/grp-intake-' + study_id
log.write(ctx, 'INTAKE COLLECTION')
log.write(ctx, coll)
# check permissions - can be researcher or datamanager
parts = coll.split('/')
group = parts[3]
datamanager_group = group.replace("-intake-", "-datamanager-", 1)
if not (user.is_member_of(ctx, group) or user.is_member_of(ctx, datamanager_group)):
log.write(ctx, "No permissions to scan collection")
return {}
tl_info = get_dataset_toplevel_objects(ctx, coll, dataset_id)
is_collection = tl_info['is_collection']
tl_objects = tl_info['objects']
timestamp = int(time.time()) # int(datetime.timestamp(datetime.now()))
comment_data = user.name(ctx) + ':' + str(timestamp) + ':' + comment
log.write(ctx, comment_data)
for tl in tl_objects:
if is_collection:
avu.associate_to_coll(ctx, tl, 'comment', comment_data)
else:
avu.associate_to_data(ctx, tl, 'comment', comment_data)
return {'user': user.name(ctx), 'timestamp': time.strftime('%Y/%m/%d %H:%M:%S', time.localtime(timestamp)), 'comment': comment}
@api.make()
def api_intake_dataset_get_details(ctx, coll, dataset_id):
"""Get all details for a dataset (errors/warnings, scanned by who/when, comments, file tree).
1) Errors/warnings
2) Comments
3) Tree view of files within dataset.
:param ctx: Combined type of a callback and rei struct
:param coll: Collection to start from
:param dataset_id: Identifier of the dataset to get details for
:returns: dictionary with all dataset data
"""
# check permissions - can be researcher or datamanager
parts = coll.split('/')
group = parts[3]
datamanager_group = group.replace("-intake-", "-datamanager-", 1)
if not (user.is_member_of(ctx, group) or user.is_member_of(ctx, datamanager_group)):
log.write(ctx, "No permissions to scan collection")
return {}
tl_info = get_dataset_toplevel_objects(ctx, coll, dataset_id)
is_collection = tl_info['is_collection']
tl_objects = tl_info['objects']
scanned = ''
comments = []
dataset_warnings = []
dataset_errors = []
files = {}
for tl in tl_objects:
if is_collection:
coll = tl
# Dataset based on a collection
iter = genquery.row_iterator(
"META_COLL_ATTR_VALUE, META_COLL_ATTR_NAME, order_asc(META_COLL_MODIFY_TIME)",
"COLL_NAME = '{}' and META_COLL_ATTR_NAME in ('dataset_error', 'dataset_warning', 'comment')".format(coll),
genquery.AS_LIST, ctx
)
for row in iter:
if row[1] == 'dataset_error':
dataset_errors.append(row[0])
elif row[1] == 'dataset_warning':
dataset_warnings.append(row[0])
else:
comments.append(row[0])
# Scanned by/when
iter = genquery.row_iterator(
"META_DATA_ATTR_VALUE",
"META_DATA_ATTR_NAME = 'scanned' AND COLL_NAME = '{}'".format(coll),
genquery.AS_LIST, ctx
)
for row in iter:
scanned = row[0]
break
break
else:
# Dataset is based on a data object
parts = pathutil.chop(tl)
coll = parts[0]
file = parts[1]
iter = genquery.row_iterator(
"META_DATA_ATTR_VALUE, META_DATA_ATTR_NAME, order_asc(META_DATA_MODIFY_TIME)",
"COLL_NAME = '{}' AND DATA_NAME = '{}' and META_DATA_ATTR_NAME in ('dataset_error','dataset_warning','comment', 'scanned')".format(coll, file),
genquery.AS_LIST, ctx
)
for row in iter:
if row[1] == 'dataset_error':
dataset_errors.append(row[0])
elif row[1] == 'dataset_warning':
dataset_warnings.append(row[0])
elif row[1] == 'scanned':
scanned = row[0]
else:
comments.append(row[0])
# do it only once - all data is gathered in the first run
break
level = '0'
files = coll_objects(ctx, level, coll, dataset_id)
log.write(ctx, files)
if len(scanned.split(':')) != 2:
# Retrieve scannedby/when information in a different way
dataset = get_dataset_details(ctx, dataset_id, coll)
scanned = dataset['datasetCreatedByWhen']
return {"files": files,
# "is_collection": is_collection,
# "tlobj": tl_objects,
"scanned": scanned,
"comments": comments,
"dataset_warnings": dataset_warnings,
"dataset_errors": dataset_errors}
def coll_objects(ctx, level, coll, dataset_id):
"""Recursive function to pass entire folder/file structure in such that frontend
can do something useful with it including errors/warnings on object level
:param ctx: Combined type of a callback and rei struct
:param level: Level in hierarchy (tree)
:param coll: Collection to collect
:param dataset_id: id of the dataset involved
:returns: Tree of collections and files
"""
# First get the sub collections
counter = 0
files = {}
# COLLECTIONS
iter = genquery.row_iterator(
"COLL_NAME, COLL_ID",
"COLL_PARENT_NAME = '{}' AND META_COLL_ATTR_NAME = 'dataset_id' AND META_COLL_ATTR_VALUE = '{}'".format(coll, dataset_id),
genquery.AS_LIST, ctx
)
for row in iter:
# files(pathutil.basename(row[0]))
node = {}
node['name'] = pathutil.basename(row[0])
node['isFolder'] = True
node['parent_id'] = level
warnings = []
errors = []
# Per collection add errors/warnings from scan process
iter2 = genquery.row_iterator(
"META_COLL_ATTR_VALUE, META_COLL_ATTR_NAME",
"META_COLL_ATTR_NAME in ('warning', 'error') AND COLL_ID = '{}'".format(row[1]),
genquery.AS_LIST, ctx
)
for row2 in iter2:
if row[1] == 'error':
errors.append(row2[0])
else:
warnings.append(row2[0])
node['errors'] = errors
node['warnings'] = warnings
files[level + "." + str(counter)] = node
files.update(coll_objects(ctx, level + "." + str(counter), row[0], dataset_id))
counter += 1
# DATA OBJECTS
iter = genquery.row_iterator(
"DATA_NAME, DATA_ID",
"COLL_NAME = '{}' AND META_DATA_ATTR_NAME = 'dataset_id' AND META_DATA_ATTR_VALUE = '{}'".format(coll, dataset_id),
genquery.AS_LIST, ctx
)
for row in iter:
node = {}
node['name'] = row[0]
node['isFolder'] = False
node['parent_id'] = level
# Per data object add errors/warnings from scan process
iter2 = genquery.row_iterator(
"META_DATA_ATTR_VALUE, META_DATA_ATTR_NAME",
"META_DATA_ATTR_NAME in ('warning', 'error') AND DATA_ID = '{}'".format(row[1]),
genquery.AS_LIST, ctx
)
warnings = []
errors = []
for row2 in iter2:
if row2[1] == 'error':
errors.append(row2[0])
else:
warnings.append(row2[0])
node['errors'] = errors
node['warnings'] = warnings
files[level + "." + str(counter)] = node
counter += 1
return files
# Reporting / export functions
@api.make()
def api_intake_report_vault_dataset_counts_per_study(ctx, study_id):
"""Get the count of datasets wave/experimenttype.
In the vault a dataset is always located in a folder.
Therefore, looking at the folders only is enough.
:param ctx: Combined type of a callback and rei struct
:param study_id: Study id
:returns: Dictionary with relevant aggregated counts
"""
# check permissions - datamanager only
datamanager_group = "grp-datamanager-" + study_id
if not user.is_member_of(ctx, datamanager_group):
log.write(ctx, "No permissions for reporting functionality")
return {}
return intake_dataset.intake_youth_dataset_counts_per_study(ctx, study_id)
@api.make()
def api_intake_report_vault_aggregated_info(ctx, study_id):
"""Collects the following information for Raw, Processed datasets.
Including a totalisation of this all (Raw/processed is kept in VERSION).
-Total datasets
-Total files
-Total file size
-File size growth in a month
-Datasets growth in a month
-Pseudocodes (distinct)
:param ctx: Combined type of a callback and rei struct
:param study_id: Study id
:returns: Dictionary with data for analysis
"""
log.write(ctx, 'ERIN VAULT AGGREGATED INFO')
# check permissions - datamanager only
datamanager_group = "grp-datamanager-" + study_id
if not user.is_member_of(ctx, datamanager_group):
log.write(ctx, "No permissions for reporting functionality")
return {}
return intake_dataset.vault_aggregated_info(ctx, study_id)
@api.make()
def api_intake_report_export_study_data(ctx, study_id):
"""Find all datasets in the vault for $studyID.
Include file count and total file size as well as dataset meta data version, experiment type, pseudocode and wave
:param ctx: Combined type of a callback and rei struct
:param study_id: Study id to get a report from
:returns: Study report
"""
# check permissions - datamanager only
datamanager_group = "grp-datamanager-" + study_id
if not user.is_member_of(ctx, datamanager_group):
log.write(ctx, "No permissions to export data for this study")
return {}
return intake_dataset.intake_report_export_study_data(ctx, study_id)