|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
| 3 | +import math |
3 | 4 | from dataclasses import dataclass
|
4 |
| -from typing import TYPE_CHECKING |
| 5 | +from datetime import datetime, timezone |
| 6 | +from decimal import Decimal |
| 7 | +from typing import TYPE_CHECKING, Union |
| 8 | + |
| 9 | +from pydantic import TypeAdapter |
| 10 | + |
| 11 | +from apify._models import ActorRun, PricingModel |
| 12 | +from apify.log import logger |
| 13 | +from apify.storages import Dataset |
5 | 14 |
|
6 | 15 | if TYPE_CHECKING:
|
7 | 16 | from apify_client import ApifyClientAsync
|
8 | 17 |
|
9 | 18 | from apify._configuration import Configuration
|
10 | 19 |
|
11 | 20 |
|
| 21 | +run_validator: TypeAdapter[ActorRun | None] = TypeAdapter(Union[ActorRun, None]) |
| 22 | + |
| 23 | + |
12 | 24 | class ChargingManager:
|
| 25 | + LOCAL_CHARGING_LOG_DATASET_NAME = 'charging_log' |
| 26 | + |
13 | 27 | def __init__(self, configuration: Configuration, client: ApifyClientAsync) -> None:
|
14 |
| - pass |
| 28 | + self._max_total_charge_usd = configuration.max_total_charge_usd or Decimal('inf') |
| 29 | + self._is_at_home = configuration.is_at_home |
| 30 | + self._actor_run_id = configuration.actor_run_id |
| 31 | + self._purge_charging_log_dataset = configuration.purge_on_start |
| 32 | + self._pricing_model: PricingModel | None = None |
| 33 | + |
| 34 | + if configuration.test_pay_per_event: |
| 35 | + if self._is_at_home: |
| 36 | + raise ValueError( |
| 37 | + 'Using the ACTOR_TEST_PAY_PER_EVENT environment variable is only supported ' |
| 38 | + 'in a local development environment' |
| 39 | + ) |
| 40 | + |
| 41 | + self._pricing_model = 'PAY_PER_EVENT' |
| 42 | + |
| 43 | + self._client = client |
| 44 | + self._charging_log_dataset: Dataset | None = None |
| 45 | + |
| 46 | + self._charging_state: dict[str, ChargingStateItem] | None = None |
| 47 | + self._pricing_info: dict[str, PricingInfoItem] = {} |
| 48 | + |
| 49 | + self._not_ppe_warning_printed = False |
15 | 50 |
|
16 | 51 | async def init(self) -> None:
|
17 |
| - pass |
| 52 | + self._charging_state = {} |
| 53 | + |
| 54 | + if self._is_at_home: |
| 55 | + if self._actor_run_id is None: |
| 56 | + raise RuntimeError('Actor run ID not found even though the Actor is running on Apify') |
| 57 | + |
| 58 | + run = run_validator.validate_python(await self._client.run(self._actor_run_id).get()) |
| 59 | + if run is None: |
| 60 | + raise RuntimeError('Actor run not found') |
| 61 | + |
| 62 | + if run.pricing_info is not None: |
| 63 | + self._pricing_model = run.pricing_info.pricing_model |
| 64 | + |
| 65 | + if run.pricing_info.pricing_model == 'PAY_PER_EVENT': |
| 66 | + for event_name, event_pricing in run.pricing_info.pricing_per_event.actor_charge_events.items(): |
| 67 | + self._pricing_info[event_name] = PricingInfoItem( |
| 68 | + price=event_pricing.event_price_usd, |
| 69 | + title=event_pricing.event_title, |
| 70 | + ) |
| 71 | + |
| 72 | + self._max_total_charge_usd = run.options.max_total_charge_usd or self._max_total_charge_usd |
| 73 | + |
| 74 | + for event_name, count in (run.charged_event_counts or {}).items(): |
| 75 | + price = self._pricing_info.get(event_name, PricingInfoItem(Decimal(), title='')).price |
| 76 | + self._charging_state[event_name] = ChargingStateItem( |
| 77 | + charge_count=count, |
| 78 | + total_charged_amount=count * price, |
| 79 | + ) |
| 80 | + |
| 81 | + if not self._is_at_home and self._pricing_model == 'PAY_PER_EVENT': |
| 82 | + if self._purge_charging_log_dataset: |
| 83 | + dataset = await Dataset.open(name=self.LOCAL_CHARGING_LOG_DATASET_NAME) |
| 84 | + await dataset.drop() |
| 85 | + |
| 86 | + self._charging_log_dataset = await Dataset.open(name=self.LOCAL_CHARGING_LOG_DATASET_NAME) |
18 | 87 |
|
19 | 88 | async def charge(self, event_name: str, count: int = 1) -> ChargeResult:
|
20 |
| - pass |
| 89 | + if self._charging_state is None: |
| 90 | + raise RuntimeError('Charging manager is not initialized') |
| 91 | + |
| 92 | + def calculate_chargeable() -> dict[str, int | None]: |
| 93 | + return { |
| 94 | + event_name: self.calculate_max_event_charge_count_within_limit(event_name) |
| 95 | + for event_name in self._pricing_info |
| 96 | + } |
| 97 | + |
| 98 | + if self._pricing_model != 'PAY_PER_EVENT': |
| 99 | + if not self._not_ppe_warning_printed: |
| 100 | + logger.warning( |
| 101 | + 'Ignored attempt to charge for an event - the Actor does not use the pay-per-event pricing' |
| 102 | + ) |
| 103 | + self._not_ppe_warning_printed = True |
| 104 | + |
| 105 | + return ChargeResult( |
| 106 | + event_charge_limit_reached=False, |
| 107 | + charged_count=0, |
| 108 | + chargeable_within_limit=calculate_chargeable(), |
| 109 | + ) |
| 110 | + |
| 111 | + # START OF CRITICAL SECTION - no awaits here |
| 112 | + charged_count = min(count, self.calculate_max_event_charge_count_within_limit(event_name) or count) |
| 113 | + |
| 114 | + if charged_count == 0: |
| 115 | + return ChargeResult( |
| 116 | + event_charge_limit_reached=True, |
| 117 | + charged_count=0, |
| 118 | + chargeable_within_limit=calculate_chargeable(), |
| 119 | + ) |
21 | 120 |
|
22 |
| - def calculate_max_event_charge_within_limit(self, event_name: str) -> int: |
23 |
| - pass |
| 121 | + pricing_info = self._pricing_info.get( |
| 122 | + event_name, |
| 123 | + PricingInfoItem( |
| 124 | + price=Decimal() |
| 125 | + if self._is_at_home |
| 126 | + else Decimal( |
| 127 | + '1' |
| 128 | + ), # Use a nonzero price for local development so that the maximum budget can be reached, |
| 129 | + title=f"Unknown event '{event_name}'", |
| 130 | + ), |
| 131 | + ) |
| 132 | + |
| 133 | + self._charging_state.setdefault(event_name, ChargingStateItem(0, Decimal())) |
| 134 | + self._charging_state[event_name].charge_count += charged_count |
| 135 | + self._charging_state[event_name].total_charged_amount += charged_count * pricing_info.price |
| 136 | + |
| 137 | + # END OF CRITICAL SECTION |
| 138 | + if self._is_at_home: |
| 139 | + if self._actor_run_id is None: |
| 140 | + raise RuntimeError('Actor run ID not configured') |
| 141 | + |
| 142 | + if event_name is self._pricing_info: |
| 143 | + await self._client.run(self._actor_run_id).charge(event_name, charged_count) |
| 144 | + else: |
| 145 | + logger.warning(f"Attempting to charge for an unknown event '{event_name}'") |
| 146 | + |
| 147 | + if self._charging_log_dataset: |
| 148 | + await self._charging_log_dataset.push_data( |
| 149 | + { |
| 150 | + 'event_name': event_name, |
| 151 | + 'event_title': pricing_info.title, |
| 152 | + 'event_price_usd': round(pricing_info.price, 3), |
| 153 | + 'charged_count': charged_count, |
| 154 | + 'timestamp': datetime.now(timezone.utc).isoformat(), |
| 155 | + } |
| 156 | + ) |
| 157 | + |
| 158 | + if charged_count < count: |
| 159 | + subject = 'instance' if count == 1 else 'instances' |
| 160 | + logger.info( |
| 161 | + f"Charging {count} ${subject} of '{event_name}' event would exceed max_total_charge_usd " |
| 162 | + '- only {charged_count} events were charged' |
| 163 | + ) |
| 164 | + |
| 165 | + max_charge_count = self.calculate_max_event_charge_count_within_limit(event_name) |
| 166 | + |
| 167 | + return ChargeResult( |
| 168 | + event_charge_limit_reached=max_charge_count is not None and max_charge_count <= 0, |
| 169 | + charged_count=charged_count, |
| 170 | + chargeable_within_limit=calculate_chargeable(), |
| 171 | + ) |
| 172 | + |
| 173 | + def calculate_total_charged_amount(self) -> Decimal: |
| 174 | + if self._charging_state is None: |
| 175 | + raise RuntimeError('Charging manager is not initialized') |
| 176 | + |
| 177 | + return sum( |
| 178 | + (item.total_charged_amount for item in self._charging_state.values()), |
| 179 | + start=Decimal(), |
| 180 | + ) |
| 181 | + |
| 182 | + def calculate_max_event_charge_count_within_limit(self, event_name: str) -> int | None: |
| 183 | + if self._charging_state is None: |
| 184 | + raise RuntimeError('Charging manager is not initialized') |
| 185 | + |
| 186 | + pricing_info = self._pricing_info.get(event_name) |
| 187 | + |
| 188 | + if pricing_info is not None: |
| 189 | + price = pricing_info.price |
| 190 | + elif not self._is_at_home: |
| 191 | + price = Decimal('1') # Use a nonzero price for local development so that the maximum budget can be reached |
| 192 | + else: |
| 193 | + price = Decimal() |
| 194 | + |
| 195 | + if not price: |
| 196 | + return None |
| 197 | + |
| 198 | + return math.floor((self._max_total_charge_usd - self.calculate_total_charged_amount()) / price) |
| 199 | + |
| 200 | + def get_pricing_info(self) -> ActorPricingInfo: |
| 201 | + if self._charging_state is None: |
| 202 | + raise RuntimeError('Charging manager is not initialized') |
| 203 | + |
| 204 | + return ActorPricingInfo( |
| 205 | + pricing_model=self._pricing_model, |
| 206 | + is_pay_per_event=self._pricing_model == 'PAY_PER_EVENT', |
| 207 | + max_total_charge_usd=self._max_total_charge_usd |
| 208 | + if self._max_total_charge_usd is not None |
| 209 | + else Decimal('inf'), |
| 210 | + per_event_prices={ |
| 211 | + event_name: pricing_info.price for event_name, pricing_info in self._pricing_info.items() |
| 212 | + }, |
| 213 | + ) |
24 | 214 |
|
25 | 215 |
|
26 | 216 | @dataclass(frozen=True)
|
27 | 217 | class ChargeResult:
|
28 | 218 | event_charge_limit_reached: bool
|
29 | 219 | charged_count: int
|
30 |
| - chargeable_within_limit: int |
| 220 | + chargeable_within_limit: dict[str, int | None] |
| 221 | + |
| 222 | + |
| 223 | +@dataclass |
| 224 | +class ChargingStateItem: |
| 225 | + charge_count: int |
| 226 | + total_charged_amount: Decimal |
| 227 | + |
| 228 | + |
| 229 | +@dataclass |
| 230 | +class PricingInfoItem: |
| 231 | + price: Decimal |
| 232 | + title: str |
| 233 | + |
| 234 | + |
| 235 | +@dataclass |
| 236 | +class ActorPricingInfo: |
| 237 | + pricing_model: PricingModel | None |
| 238 | + max_total_charge_usd: Decimal |
| 239 | + is_pay_per_event: bool |
| 240 | + per_event_prices: dict[str, Decimal] |
0 commit comments