-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmultigrid_control.py
More file actions
779 lines (744 loc) · 31.6 KB
/
multigrid_control.py
File metadata and controls
779 lines (744 loc) · 31.6 KB
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
import logging
import subprocess
import threading
import time
from dataclasses import dataclass, field
from datetime import datetime
from functools import partial
from pathlib import Path
from typing import Dict, List, Optional
from urllib.parse import urlparse
from murfey.client.analyser import Analyser
from murfey.client.context import ensure_dcg_exists
from murfey.client.contexts.spa import SPAContext
from murfey.client.contexts.tomo import TomographyContext
from murfey.client.destinations import determine_default_destination
from murfey.client.instance_environment import MurfeyInstanceEnvironment
from murfey.client.rsync import RSyncer, RSyncerUpdate, TransferResult
from murfey.client.watchdir import DirWatcher
from murfey.util import posix_path
from murfey.util.client import (
capture_delete,
capture_get,
capture_post,
get_machine_config_client,
)
log = logging.getLogger("murfey.client.multigrid_control")
@dataclass
class MultigridController:
sources: list[Path]
visit: str
instrument_name: str
session_id: int
murfey_url: str = "http://localhost:8000"
rsync_url: str = ""
rsync_module: str = "data"
finalising: bool = False
dormant: bool = False
multigrid_watcher_active: bool = True
do_transfer: bool = True
dummy_dc: bool = False
force_mdoc_metadata: bool = True
rsync_restarts: List[str] = field(default_factory=lambda: [])
rsync_processes: Dict[Path, RSyncer] = field(default_factory=lambda: {})
analysers: Dict[Path, Analyser] = field(default_factory=lambda: {})
data_collection_parameters: dict = field(default_factory=lambda: {})
token: str = ""
serialem: bool = False
acquisition_uuid: Optional[str] = None
_machine_config: dict = field(default_factory=lambda: {})
visit_end_time: Optional[datetime] = None
def __post_init__(self):
machine_data = capture_get(
base_url=self.murfey_url,
router_name="session_control.router",
function_name="machine_info_by_instrument",
token=self.token,
instrument_name=self.instrument_name,
).json()
self.rsync_url = machine_data.get("rsync_url", "")
self.rsync_module = machine_data.get("rsync_module", "data")
self._environment = MurfeyInstanceEnvironment(
url=urlparse(self.murfey_url, allow_fragments=False),
client_id=0,
murfey_session=self.session_id,
software_versions=machine_data.get("software_versions", {}),
visit=self.visit,
dose_per_frame=self.data_collection_parameters.get("dose_per_frame"),
gain_ref=self.data_collection_parameters.get("gain_ref"),
symmetry=self.data_collection_parameters.get("symmetry"),
eer_fractionation=self.data_collection_parameters.get("eer_fractionation"),
instrument_name=self.instrument_name,
acquisition_uuid=self.acquisition_uuid,
)
self._machine_config = get_machine_config_client(
str(self._environment.url.geturl()),
self.token,
instrument_name=self._environment.instrument_name,
)
self._data_suffixes = (".mrc", ".tiff", ".tif", ".eer")
self._data_substrings = [
s
for val in self._machine_config["data_required_substrings"].values()
for ds in val.values()
for s in ds
]
self.rsync_processes = self.rsync_processes or {}
self.analysers = self.analysers or {}
# Calculate the time offset between the client and the server
current_time = datetime.now()
server_timestamp = capture_get(
base_url=self.murfey_url,
router_name="session_control.router",
function_name="get_current_timestamp",
token=self.token,
).json()["timestamp"]
self.server_time_offset = current_time - datetime.fromtimestamp(
server_timestamp
)
# Store the visit end time in the current device's equivalent time
if self.visit_end_time:
self.visit_end_time += self.server_time_offset
def _multigrid_watcher_finalised(self):
self.multigrid_watcher_active = False
def is_ready_for_dormancy(self):
"""
When the multigrid watcher is no longer active, sends a request to safely stop
the analyser and file watcher threads, then checks to see that those threads
and the RSyncer processes associated with the current session have all been
safely stopped
"""
log.debug(
f"Starting dormancy check for MultigridController for session {self.session_id}"
)
if not self.multigrid_watcher_active:
for a in self.analysers.values():
if a.is_safe_to_stop():
a.stop()
for w in self._environment.watchers.values():
if w.is_safe_to_stop():
w.stop()
rsyncers_finalised = all(
r._finalised for r in self.rsync_processes.values()
)
analysers_alive = any(a.thread.is_alive() for a in self.analysers.values())
watchers_alive = any(
w.thread.is_alive() for w in self._environment.watchers.values()
)
log.debug(
"Dormancy check: \n"
f" rsyncers: {rsyncers_finalised} \n"
f" analysers: {not analysers_alive} \n"
f" watchers: {not watchers_alive}"
)
return rsyncers_finalised and not analysers_alive and not watchers_alive
log.debug(f"Multigrid watcher for session {self.session_id} is still active")
return False
def clean_up_once_dormant(self, running_threads: list[threading.Thread]):
"""
A function run in a separate thread that runs the post-session cleanup logic
once all threads associated with this current session are halted, and marks
the controller as being fully dormant after doing so.
"""
for thread in running_threads:
thread.join()
log.debug(f"RSyncer cleanup thread {thread.ident} has stopped safely")
while not self.is_ready_for_dormancy():
time.sleep(10)
# Once all threads are stopped, remove session from the database
log.debug(
f"Submitting request to remove session {self.session_id} from database"
)
response = capture_delete(
base_url=str(self._environment.url.geturl()),
router_name="session_control.router",
function_name="remove_session",
token=self.token,
session_id=self.session_id,
)
success = response.status_code == 200 if response else False
if not success:
log.warning(f"Could not delete database data for {self.session_id}")
# Mark as dormant
self.dormant = True
def abandon(self):
for a in self.analysers.values():
a.request_stop()
for w in self._environment.watchers.values():
w.request_stop()
for p in self.rsync_processes.values():
p.request_stop()
def finalise(self):
self.finalising = True
for a in self.analysers.values():
a.request_stop()
log.debug(f"Stop request sent to analyser {a}")
for w in self._environment.watchers.values():
w.request_stop()
log.debug(f"Stop request sent to watcher {w}")
rsync_finaliser_threads = []
for p in self.rsync_processes.keys():
# Collect the running rsyncer finaliser threads to pass to the dormancy checker
rsync_finaliser_threads.append(self._finalise_rsyncer(p))
log.debug(f"Finalised rsyncer {p}")
# Run the session cleanup function in a separate thread
cleanup_upon_dormancy_thread = threading.Thread(
target=self.clean_up_once_dormant,
args=[rsync_finaliser_threads],
daemon=True,
)
cleanup_upon_dormancy_thread.start()
def update_visit_time(self, new_end_time: datetime):
# Convert the received server timestamp into the local equivalent
self.visit_end_time = new_end_time + self.server_time_offset
for rp in self.rsync_processes.values():
rp._end_time = self.visit_end_time
def _start_rsyncer_multigrid(
self,
source: Path,
extra_directory: str = "",
use_suggested_path: bool = True,
destination_overrides: Optional[Dict[Path, str]] = None,
remove_files: bool = False,
analyse: bool = True,
tag: str = "",
limited: bool = False,
):
log.info(f"Starting multigrid rsyncer: {source}")
log.debug(f"Analysis of {source} is {('enabled' if analyse else 'disabled')}")
destination_overrides = destination_overrides or {}
machine_data = capture_get(
base_url=str(self._environment.url.geturl()),
router_name="session_control.router",
function_name="machine_info_by_instrument",
token=self.token,
instrument_name=self.instrument_name,
).json()
if destination_overrides.get(source):
destination = (
destination_overrides[source]
if str(source) in self.rsync_restarts
else destination_overrides[source] + f"/{extra_directory}"
)
else:
for k, v in destination_overrides.items():
if Path(v).name in source.parts:
destination = str(k / extra_directory)
break
else:
self._environment.default_destinations[source] = (
f"{datetime.now().year}"
)
destination = determine_default_destination(
self._environment.visit,
source,
self._environment.default_destinations[source],
self._environment,
self.token,
touch=True,
extra_directory=extra_directory,
use_suggested_path=use_suggested_path,
)
self._environment.sources.append(source)
self._start_rsyncer(
source,
destination,
analyse=analyse,
remove_files=remove_files,
tag=tag,
limited=limited,
transfer=machine_data.get("data_transfer_enabled", True),
restarted=str(source) in self.rsync_restarts,
)
def _rsyncer_stopped(self, source: Path, explicit_stop: bool = False):
if explicit_stop:
capture_delete(
base_url=self.murfey_url,
router_name="session_control.router",
function_name="delete_rsyncer",
token=self.token,
session_id=self.session_id,
data={"path": str(source)},
)
else:
capture_post(
base_url=self.murfey_url,
router_name="session_control.router",
function_name="register_stopped_rsyncer",
token=self.token,
instrument_name=self._environment.instrument_name,
session_id=self.session_id,
data={"path": str(source)},
)
def _finalise_rsyncer(self, source: Path):
"""
Starts a new Rsyncer thread that cleans up the directories, and returns that
thread to be managed by a central thread.
"""
finalise_thread = threading.Thread(
name=f"Controller finaliser thread ({source})",
target=self.rsync_processes[source].finalise,
kwargs={"thread": False},
daemon=True,
)
finalise_thread.start()
log.debug(f"Started RSync cleanup for {str(source)}")
return finalise_thread
def _restart_rsyncer(self, source: Path):
self.rsync_processes[source].restart()
capture_post(
base_url=self.murfey_url,
router_name="session_control.router",
function_name="register_restarted_rsyncer",
token=self.token,
instrument_name=self._environment.instrument_name,
session_id=self.session_id,
data={"path": str(source)},
)
def _request_watcher_stop(self, source: Path):
self._environment.watchers[source]._stopping = True
self._environment.watchers[source]._halt_thread = True
def _start_rsyncer(
self,
source: Path,
destination: str,
visit_path: str = "",
analyse: bool = True,
remove_files: bool = False,
tag: str = "",
limited: bool = False,
transfer: bool = True,
restarted: bool = False,
):
log.info(f"starting rsyncer: {source}")
if transfer:
# Always make sure the destination directory exists
capture_post(
base_url=self.murfey_url,
router_name="file_io_instrument.router",
function_name="make_rsyncer_destination",
token=self.token,
instrument_name=self._environment.instrument_name,
session_id=self.session_id,
data={"destination": destination},
)
if self._environment:
self._environment.default_destinations[source] = destination
if self._environment.gain_ref and visit_path:
# Set up rsync command
rsync_cmd = [
"rsync",
f"{posix_path(self._environment.gain_ref)!r}", # '!r' will print strings in ''
f"{self._environment.url.hostname}::{self.rsync_module}/{visit_path}/processing",
]
# Wrap in bash shell
cmd = [
"bash",
"-c",
" ".join(rsync_cmd),
]
# Run rsync subprocess
gain_rsync = subprocess.run(cmd)
if gain_rsync.returncode:
log.warning(
f"Gain reference file {posix_path(self._environment.gain_ref)!r} was not successfully transferred to {visit_path}/processing"
)
if transfer:
self.rsync_processes[source] = RSyncer(
source,
basepath_remote=Path(destination),
rsync_module=self.rsync_module,
server_url=(
urlparse(self.rsync_url)
if self.rsync_url
else self._environment.url
),
stop_callback=self._rsyncer_stopped,
do_transfer=self.do_transfer,
remove_files=remove_files,
substrings_blacklist=self._machine_config.get(
"substrings_blacklist", {"directories": [], "files": []}
),
end_time=self.visit_end_time,
)
def rsync_result(update: RSyncerUpdate):
if not update.base_path:
raise ValueError("No base path from rsyncer update")
if not self.rsync_processes.get(update.base_path):
raise ValueError("TUI rsync process does not exist")
if update.outcome is TransferResult.SUCCESS:
# log.info(
# f"File {str(update.file_path)!r} successfully transferred ({update.file_size} bytes)"
# )
pass
else:
log.warning(f"Failed to transfer file {str(update.file_path)!r}")
self.rsync_processes[update.base_path].enqueue(update.file_path)
self.rsync_processes[source].subscribe(rsync_result)
self.rsync_processes[source].subscribe(
partial(
self._increment_transferred_files_prometheus,
destination=destination,
source=str(source),
)
)
self.rsync_processes[source].subscribe(
partial(
self._increment_transferred_files,
destination=destination,
source=str(source),
),
secondary=True,
)
if restarted:
capture_post(
base_url=self.murfey_url,
router_name="session_control.router",
function_name="register_restarted_rsyncer",
token=self.token,
instrument_name=self._environment.instrument_name,
session_id=self.session_id,
data={"path": str(source)},
)
else:
rsyncer_data = {
"source": str(source),
"destination": destination,
"session_id": self.session_id,
"transferring": self.do_transfer,
"tag": tag,
}
capture_post(
base_url=self.murfey_url,
router_name="session_control.router",
function_name="register_rsyncer",
token=self.token,
instrument_name=self._environment.instrument_name,
session_id=self._environment.murfey_session,
data=rsyncer_data,
)
self._environment.watchers[source] = DirWatcher(
source,
settling_time=30,
substrings_blacklist=self._machine_config.get(
"substrings_blacklist", {"directories": [], "files": []}
),
)
if not self.analysers.get(source) and analyse:
log.info(f"Starting analyser for {source}")
self.analysers[source] = Analyser(
source,
self.token,
environment=self._environment if not self.dummy_dc else None,
force_mdoc_metadata=self.force_mdoc_metadata,
limited=limited,
serialem=self.serialem,
)
self.analysers[source].subscribe(self._start_dc)
self.analysers[source].start()
if transfer:
self.rsync_processes[source].subscribe(self.analysers[source].enqueue)
if transfer:
self.rsync_processes[source].start()
if self._environment:
if self._environment.watchers.get(source):
if transfer:
self._environment.watchers[source].subscribe(
self.rsync_processes[source].enqueue
)
else:
# the watcher and rsyncer don't notify with the same object so conversion required here
def _rsync_update_converter(p: Path) -> None:
self.analysers[source].enqueue(
RSyncerUpdate(
file_path=p,
file_size=0,
outcome=TransferResult.SUCCESS,
transfer_total=0,
queue_size=0,
base_path=source,
)
)
return None
self._environment.watchers[source].subscribe(
_rsync_update_converter
)
self._environment.watchers[source].subscribe(
partial(
self._increment_file_count,
destination=destination,
source=str(source),
),
secondary=True,
)
self._environment.watchers[source].start()
def _start_dc(self, metadata_json):
if self.dummy_dc:
return
# Safely convert all entries into strings, but leave None as-is
metadata_json = {
k: str(v) if v is not None else None for k, v in metadata_json.items()
}
self._environment.dose_per_frame = metadata_json.get("dose_per_frame")
self._environment.gain_ref = metadata_json.get("gain_ref")
self._environment.symmetry = metadata_json.get("symmetry")
self._environment.eer_fractionation = metadata_json.get("eer_fractionation")
source = Path(metadata_json["source"])
context = self.analysers[source]._context
if context:
context.data_collection_parameters = {
k: None if v == "None" else v for k, v in metadata_json.items()
}
if isinstance(context, TomographyContext):
source = Path(metadata_json["source"])
context.register_tomography_data_collections(
file_extension=metadata_json["file_extension"],
image_directory=str(self._environment.default_destinations[source]),
environment=self._environment,
)
log.info("Registering tomography processing parameters")
if context.data_collection_parameters.get("num_eer_frames"):
eer_response = capture_post(
base_url=str(self._environment.url.geturl()),
router_name="file_io_instrument.router",
function_name="write_eer_fractionation_file",
token=self.token,
instrument_name=self._environment.instrument_name,
visit_name=self._environment.visit,
session_id=self._environment.murfey_session,
data={
"num_frames": context.data_collection_parameters[
"num_eer_frames"
],
"fractionation": self._environment.eer_fractionation,
"dose_per_frame": self._environment.dose_per_frame,
"fractionation_file_name": "eer_fractionation_tomo.txt",
},
)
eer_fractionation_file = eer_response.json()["eer_fractionation_file"]
metadata_json.update({"eer_fractionation_file": eer_fractionation_file})
capture_post(
base_url=str(self._environment.url.geturl()),
router_name="workflow.tomo_router",
function_name="register_tomo_proc_params",
token=self.token,
instrument_name=self._environment.instrument_name,
session_id=self._environment.murfey_session,
data=metadata_json,
)
capture_post(
base_url=str(self._environment.url.geturl()),
router_name="workflow.tomo_router",
function_name="flush_tomography_processing",
token=self.token,
instrument_name=self._environment.instrument_name,
visit_name=self._environment.visit,
session_id=self._environment.murfey_session,
data={"rsync_source": str(source)},
)
log.info("Tomography processing flushed")
elif isinstance(context, SPAContext):
if self._environment.visit in source.parts:
metadata_source = source
else:
metadata_source_as_str = (
"/".join(source.parts[:-2])
+ f"/{self._environment.visit}/"
+ "/".join(source.parts[-2:])
)
metadata_source = Path(metadata_source_as_str.replace("//", "/"))
try:
ensure_dcg_exists(
collection_type="spa",
metadata_source=metadata_source,
environment=self._environment,
machine_config=self._machine_config,
token=self.token,
)
except Exception as e:
log.error(f"Failed to register data collection group: {e}")
data = {
"voltage": metadata_json["voltage"],
"pixel_size_on_image": metadata_json["pixel_size_on_image"],
"experiment_type": metadata_json["experiment_type"],
"image_size_x": metadata_json["image_size_x"],
"image_size_y": metadata_json["image_size_y"],
"file_extension": metadata_json["file_extension"],
"acquisition_software": metadata_json["acquisition_software"],
"image_directory": str(self._environment.default_destinations[source]),
"tag": str(source),
"source": str(source),
"magnification": metadata_json["magnification"],
"total_exposed_dose": metadata_json.get("total_exposed_dose"),
"c2aperture": metadata_json.get("c2aperture"),
"exposure_time": metadata_json.get("exposure_time"),
"slit_width": metadata_json.get("slit_width"),
"phase_plate": metadata_json.get("phase_plate", False),
}
capture_post(
base_url=str(self._environment.url.geturl()),
router_name="workflow.router",
function_name="start_dc",
token=self.token,
instrument_name=self._environment.instrument_name,
visit_name=self._environment.visit,
session_id=self.session_id,
data=data,
)
for recipe in (
"em-spa-preprocess",
"em-spa-extract",
"em-spa-class2d",
"em-spa-class3d",
"em-spa-refine",
):
capture_post(
base_url=str(self._environment.url.geturl()),
router_name="workflow.router",
function_name="register_proc",
token=self.token,
instrument_name=self._environment.instrument_name,
visit_name=self._environment.visit,
session_id=self.session_id,
data={
"tag": str(source),
"source": str(source),
"recipe": recipe,
},
)
log.info(f"Posting SPA processing parameters: {metadata_json}")
response = capture_post(
base_url=str(self._environment.url.geturl()),
router_name="workflow.spa_router",
function_name="register_spa_proc_params",
token=self.token,
instrument_name=self._environment.instrument_name,
session_id=self.session_id,
data={
**{k: None if v == "None" else v for k, v in metadata_json.items()},
"tag": str(source),
},
)
if response and not str(response.status_code).startswith("2"):
log.warning(f"{response.reason}")
capture_post(
base_url=str(self._environment.url.geturl()),
router_name="workflow.spa_router",
function_name="flush_spa_processing",
token=self.token,
instrument_name=self._environment.instrument_name,
visit_name=self._environment.visit,
session_id=self.session_id,
data={"tag": str(source)},
)
def _increment_file_count(
self, observed_files: List[Path], source: str, destination: str
):
num_data_files = len(
[
f
for f in observed_files
if f.suffix in self._data_suffixes
and any(substring in f.name for substring in self._data_substrings)
]
)
data = {
"source": source,
"destination": destination,
"session_id": self.session_id,
"increment_count": len(observed_files),
"increment_data_count": num_data_files,
}
capture_post(
base_url=str(self._environment.url.geturl()),
router_name="prometheus.router",
function_name="increment_rsync_file_count",
token=self.token,
instrument_name=self._environment.instrument_name,
visit_name=self._environment.visit,
data=data,
)
# Prometheus can handle higher traffic so update for every transferred file rather
# than batching as we do for the Murfey database updates in _increment_transferred_files
def _increment_transferred_files_prometheus(
self, update: RSyncerUpdate, source: str, destination: str
):
if update.outcome is TransferResult.SUCCESS:
data_files = (
[update]
if update.file_path.suffix in self._data_suffixes
and any(
substring in update.file_path.name
for substring in self._data_substrings
)
else []
)
data = {
"source": source,
"destination": destination,
"session_id": self.session_id,
"increment_count": 1,
"bytes": update.file_size,
"increment_data_count": len(data_files),
"data_bytes": sum(f.file_size for f in data_files),
}
capture_post(
base_url=str(self._environment.url.geturl()),
router_name="prometheus.router",
function_name="increment_rsync_transferred_files_prometheus",
token=self.token,
instrument_name=self._environment.instrument_name,
visit_name=self._environment.visit,
data=data,
)
def _increment_transferred_files(
self,
updates: List[RSyncerUpdate],
num_skipped_files: int,
source: str,
destination: str,
):
capture_post(
base_url=str(self._environment.url.geturl()),
router_name="prometheus.router",
function_name="increment_rsync_skipped_files_prometheus",
token=self.token,
instrument_name=self._environment.instrument_name,
visit_name=self._environment.visit,
data={
"source": source,
"session_id": self.session_id,
"increment_count": num_skipped_files,
},
)
checked_updates = [
update for update in updates if update.outcome is TransferResult.SUCCESS
]
if not checked_updates:
return
data_files = [
u
for u in updates
if u.file_path.suffix in self._data_suffixes
and any(
substring in u.file_path.name for substring in self._data_substrings
)
]
data = {
"source": source,
"destination": destination,
"session_id": self.session_id,
"increment_count": len(checked_updates),
"bytes": sum(f.file_size for f in checked_updates),
"increment_data_count": len(data_files),
"data_bytes": sum(f.file_size for f in data_files),
}
capture_post(
base_url=str(self._environment.url.geturl()),
router_name="prometheus.router",
function_name="increment_rsync_transferred_files",
token=self.token,
instrument_name=self._environment.instrument_name,
visit_name=self._environment.visit,
data=data,
)