|
| 1 | +# Copyright 2021-2023 VMware, Inc. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +""" |
| 4 | +Aggregate the necessary job info into one event to be forwarded. |
| 5 | +""" |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +import fnmatch |
| 9 | +import logging |
| 10 | +from typing import TYPE_CHECKING |
| 11 | +from typing import AsyncIterator |
| 12 | +from typing import Type |
| 13 | + |
| 14 | +from saf.collect.event_bus import EventBusCollectedEvent |
| 15 | +from saf.models import CollectedEvent |
| 16 | +from saf.models import PipelineRunContext |
| 17 | +from saf.models import ProcessConfigBase |
| 18 | + |
| 19 | +if TYPE_CHECKING: |
| 20 | + from datetime import datetime |
| 21 | + from datetime import timedelta |
| 22 | + |
| 23 | +log = logging.getLogger(__name__) |
| 24 | + |
| 25 | + |
| 26 | +class StateAggregateConfig(ProcessConfigBase): |
| 27 | + """ |
| 28 | + Job aggregate collector configuration. |
| 29 | + """ |
| 30 | + |
| 31 | + |
| 32 | +def get_config_schema() -> Type[StateAggregateConfig]: |
| 33 | + """ |
| 34 | + Get the job aggregate collect plugin configuration schema. |
| 35 | + """ |
| 36 | + return StateAggregateConfig |
| 37 | + |
| 38 | + |
| 39 | +class StateAggregateCollectedEvent(CollectedEvent): |
| 40 | + """ |
| 41 | + A collected event with aggregated state run information. |
| 42 | + """ |
| 43 | + |
| 44 | + start_time: datetime |
| 45 | + end_time: datetime |
| 46 | + duration: timedelta |
| 47 | + minion_id: str |
| 48 | + |
| 49 | + |
| 50 | +async def process( |
| 51 | + *, |
| 52 | + ctx: PipelineRunContext[StateAggregateConfig], |
| 53 | + event: CollectedEvent, |
| 54 | +) -> AsyncIterator[CollectedEvent]: |
| 55 | + """ |
| 56 | + Aggregate received events, otherwise store in cache. |
| 57 | + """ |
| 58 | + if isinstance(event, EventBusCollectedEvent): |
| 59 | + salt_event = event.salt_event |
| 60 | + tag = salt_event.tag |
| 61 | + data = salt_event.data |
| 62 | + if fnmatch.fnmatch(tag, "salt/job/*/new"): |
| 63 | + # We will probably want to make this condition configurable |
| 64 | + if TYPE_CHECKING: |
| 65 | + assert isinstance(salt_event.data, dict) |
| 66 | + if data.get("fun") == "state.apply": |
| 67 | + jid = tag.split("/")[2] |
| 68 | + if "watched_jids" not in ctx.cache: |
| 69 | + ctx.cache["watched_jids"] = {} |
| 70 | + # We are going to want a TTL at some point for the watched jids |
| 71 | + ctx.cache["watched_jids"][jid] = salt_event |
| 72 | + elif fnmatch.fnmatch(tag, "salt/job/*/ret/*"): |
| 73 | + split_tag = tag.split("/") |
| 74 | + jid = split_tag[2] |
| 75 | + if "watched_jids" not in ctx.cache: |
| 76 | + ctx.cache["watched_jids"] = {} |
| 77 | + if jid in ctx.cache["watched_jids"]: |
| 78 | + job_start_event = ctx.cache["watched_jids"][jid] |
| 79 | + minion_id = split_tag[-1] |
| 80 | + start_time = job_start_event.stamp |
| 81 | + end_time = salt_event.stamp |
| 82 | + duration = end_time - start_time |
| 83 | + yield StateAggregateCollectedEvent.construct( |
| 84 | + data=data, |
| 85 | + start_time=start_time, |
| 86 | + end_time=end_time, |
| 87 | + duration=duration, |
| 88 | + minion_id=minion_id, |
| 89 | + ) |
0 commit comments