|
| 1 | +# docs: docs/reference/actions.md |
| 2 | +import dataclasses |
| 3 | + |
| 4 | +from finecode_extension_api import code_action, textstyler |
| 5 | + |
| 6 | + |
| 7 | +@dataclasses.dataclass |
| 8 | +class CleanServicesLogsRunPayload(code_action.RunActionPayload): |
| 9 | + service_ids: list[str] | None = None |
| 10 | + """Services whose logs should be deleted. |
| 11 | + None = discover and clean all available services (requires a discovery handler). |
| 12 | + Empty list = explicit no-op.""" |
| 13 | + |
| 14 | + |
| 15 | +class CleanServicesLogsRunContext( |
| 16 | + code_action.RunActionContext[CleanServicesLogsRunPayload] |
| 17 | +): |
| 18 | + def __init__( |
| 19 | + self, |
| 20 | + run_id: int, |
| 21 | + initial_payload: CleanServicesLogsRunPayload, |
| 22 | + meta: code_action.RunActionMeta, |
| 23 | + info_provider: code_action.RunContextInfoProvider, |
| 24 | + progress_sender: code_action.ProgressSender = code_action._NOOP_PROGRESS_SENDER, |
| 25 | + ) -> None: |
| 26 | + super().__init__( |
| 27 | + run_id=run_id, |
| 28 | + initial_payload=initial_payload, |
| 29 | + meta=meta, |
| 30 | + info_provider=info_provider, |
| 31 | + progress_sender=progress_sender, |
| 32 | + ) |
| 33 | + self.service_ids: list[str] | None = None |
| 34 | + """None = discovery has not run. [] = explicit no-op.""" |
| 35 | + |
| 36 | + async def init(self) -> None: |
| 37 | + if self.initial_payload.service_ids is not None: |
| 38 | + self.service_ids = list(self.initial_payload.service_ids) |
| 39 | + |
| 40 | + |
| 41 | +@dataclasses.dataclass |
| 42 | +class CleanServicesLogsRunResult(code_action.RunActionResult): |
| 43 | + services_cleaned: list[str] = dataclasses.field(default_factory=list) |
| 44 | + errors: list[str] = dataclasses.field(default_factory=list) |
| 45 | + |
| 46 | + def update(self, other: code_action.RunActionResult) -> None: |
| 47 | + if not isinstance(other, CleanServicesLogsRunResult): |
| 48 | + return |
| 49 | + self.services_cleaned += other.services_cleaned |
| 50 | + self.errors += other.errors |
| 51 | + |
| 52 | + def to_text(self) -> str | textstyler.StyledText: |
| 53 | + if self.errors: |
| 54 | + return "\n".join(self.errors) |
| 55 | + return f"Cleaned {len(self.services_cleaned)} service(s)." |
| 56 | + |
| 57 | + @property |
| 58 | + def return_code(self) -> code_action.RunReturnCode: |
| 59 | + return ( |
| 60 | + code_action.RunReturnCode.ERROR |
| 61 | + if self.errors |
| 62 | + else code_action.RunReturnCode.SUCCESS |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +class CleanServicesLogsAction( |
| 67 | + code_action.Action[ |
| 68 | + CleanServicesLogsRunPayload, |
| 69 | + CleanServicesLogsRunContext, |
| 70 | + CleanServicesLogsRunResult, |
| 71 | + ] |
| 72 | +): |
| 73 | + """Delete logs for multiple observability services. |
| 74 | +
|
| 75 | + When service_ids is None, a discovery handler must run first to populate |
| 76 | + run_context.service_ids from list_observability_services. No-op when |
| 77 | + service_ids is an empty list. |
| 78 | + """ |
| 79 | + |
| 80 | + DESCRIPTION = "Delete logs for multiple observability services." |
| 81 | + PAYLOAD_TYPE = CleanServicesLogsRunPayload |
| 82 | + RUN_CONTEXT_TYPE = CleanServicesLogsRunContext |
| 83 | + RESULT_TYPE = CleanServicesLogsRunResult |
0 commit comments