diff --git a/app/DataProviders/CachedHafas.php b/app/DataProviders/CachedHafas.php index 619367823..77a65f02a 100644 --- a/app/DataProviders/CachedHafas.php +++ b/app/DataProviders/CachedHafas.php @@ -4,6 +4,7 @@ use App\Enum\TravelType; use App\Helpers\CacheKey; +use App\Helpers\HCK; use App\Models\Station; use App\Models\Trip; use Carbon\Carbon; @@ -13,21 +14,30 @@ class CachedHafas extends Hafas implements DataProviderInterface { - public function fetchHafasTrip(string $tripID, string $lineName): Trip { $key = CacheKey::getHafasTripKey($tripID, $lineName); - return $this->remember($key, now()->addMinutes(15), function() use ($tripID, $lineName) { - return parent::fetchHafasTrip($tripID, $lineName); - }); + return $this->remember( + $key, + now()->addMinutes(15), + function() use ($tripID, $lineName) { + return parent::fetchHafasTrip($tripID, $lineName); + }, + HCK::TRIPS_SUCCESS + ); } public function getStations(string $query, int $results = 10): Collection { $key = CacheKey::getHafasStationsKey($query); - return $this->remember($key, now()->addMinutes(15), function() use ($query, $results) { - return parent::getStations($query, $results); - }); + return $this->remember( + $key, + now()->addMinutes(15), + function() use ($query, $results) { + return parent::getStations($query, $results); + }, + HCK::LOCATIONS_SUCCESS + ); } public function getDepartures(Station $station, Carbon $when, int $duration = 15, TravelType $type = null, bool $localtime = false): Collection { @@ -43,9 +53,14 @@ public function getDepartures(Station $station, Carbon $when, int $duration = 15 $key = CacheKey::getHafasDeparturesKey($station->id, $when, $localtime); - $departures = $this->remember($key, now()->addMinutes(15), function() use ($station, $when, $duration, $type, $localtime) { - return parent::getDepartures($station, $when, $duration, $type, $localtime); - }); + $departures = $this->remember( + $key, + now()->addMinutes(15), + function() use ($station, $when, $duration, $type, $localtime) { + return parent::getDepartures($station, $when, $duration, $type, $localtime); + }, + HCK::DEPARTURES_SUCCESS + ); // filter entries by when and duration return $departures->filter(function($departure) use ($filterWhen, $duration) { @@ -57,29 +72,39 @@ public function getDepartures(Station $station, Carbon $when, int $duration = 15 public function getStationByRilIdentifier(string $rilIdentifier): ?Station { $key = CacheKey::getHafasByRilIdentifierKey($rilIdentifier); - return $this->remember($key, now()->addMinutes(15), function() use ($rilIdentifier) { - return parent::getStationByRilIdentifier($rilIdentifier); - }); + return $this->remember( + $key, + now()->addMinutes(15), + function() use ($rilIdentifier) { + return parent::getStationByRilIdentifier($rilIdentifier); + }, + HCK::STATIONS_SUCCESS + ); } public function getStationsByFuzzyRilIdentifier(string $rilIdentifier): ?Collection { $key = CacheKey::getHafasStationsFuzzyKey($rilIdentifier); - return $this->remember($key, now()->addMinutes(15), function() use ($rilIdentifier) { - return parent::getStationsByFuzzyRilIdentifier($rilIdentifier); - }); + return $this->remember( + $key, + now()->addMinutes(15), + function() use ($rilIdentifier) { + return parent::getStationsByFuzzyRilIdentifier($rilIdentifier); + }, + HCK::STATIONS_SUCCESS + ); } - private function remember(string $key, Carbon $expires, callable $callback): mixed { + private function remember(string $key, Carbon $expires, callable $callback, ?string $ident = null): mixed { if (Cache::has($key)) { - CacheKey::increment('hafas_cache_hit'); + CacheKey::increment(CacheKey::getHafasCacheHitKey($ident)); return Cache::get($key); } try { $result = $callback(); + CacheKey::increment(CacheKey::getHafasCacheSetKey($ident)); Cache::put($key, $result, $expires); - CacheKey::increment('hafas_cache_set'); return $result; } catch (Throwable $e) { Cache::put($key, null, $expires); diff --git a/app/Helpers/CacheKey.php b/app/Helpers/CacheKey.php index e4f75295c..8c73b842b 100644 --- a/app/Helpers/CacheKey.php +++ b/app/Helpers/CacheKey.php @@ -27,11 +27,23 @@ class CacheKey private const string HAFAS_DEPARTURES = '_HafasDepartures_%d_%s_%s'; private const string HAFAFS_STATION_RIL = '_HafasStationRil'; private const string HAFAS_STATIONS_FUZZY = '_HafasStationsFuzzy'; + private const string HAFAS_CACHE_HIT = '_HafasCacheHit_%s'; + private const string HAFAS_CACHE_SET = '_HafasCacheSet_%s'; // formatting keys private const string FOR = '%s-for-%s'; private const string FROM_TO = '%s-from-%s-to-%s'; + public static function getHafasCacheHitKey(string $key): string { + $key = str_replace('monitoring-counter-', '', $key); + return sprintf(self::HAFAS_CACHE_HIT, $key); + } + + public static function getHafasCacheSetKey(string $key): string { + $key = str_replace('monitoring-counter-', '', $key); + return sprintf(self::HAFAS_CACHE_SET, $key); + } + public static function getHafasTripKey(string $tripId, string $lineName): string { $tripId = sha1($tripId); return sprintf(self::HAFAS_TRIP, $tripId, $lineName); diff --git a/app/Helpers/HCK.php b/app/Helpers/HCK.php index 80afc9e6f..951417fc6 100644 --- a/app/Helpers/HCK.php +++ b/app/Helpers/HCK.php @@ -33,6 +33,7 @@ public static function getFailures(): array { return [ self::DEPARTURES_FAILURE => 'Departures', self::TRIPS_FAILURE => 'Trips', + self::TRIPS_502 => 'Trips502', self::STOPS_FAILURE => 'Stops', self::STATIONS_FAILURE => 'Stations', self::LOCATIONS_FAILURE => 'Locations', diff --git a/app/Providers/PrometheusServiceProvider.php b/app/Providers/PrometheusServiceProvider.php index 0c78a9c19..be0ec1c67 100644 --- a/app/Providers/PrometheusServiceProvider.php +++ b/app/Providers/PrometheusServiceProvider.php @@ -115,6 +115,32 @@ public function register() { return $this->getHafasByType(HCK::getSuccesses()); }); + Prometheus::addGauge("hafas_cache_hits") + ->helpText("How many hafas requests have been served from cache?") + ->labels(["request_name"]) + ->value(function() { + $values = []; + foreach (HCK::getSuccesses() as $key => $name) { + $key = CacheKey::getHafasCacheHitKey($key); + $values[$name] = Cache::get($key, 0); + } + + return array_map(fn($value, $key) => [$value, [$key]], $values, array_keys($values)); + }); + + Prometheus::addGauge("hafas_cache_sets") + ->helpText("How many hafas requests have been stored in cache?") + ->labels(["request_name"]) + ->value(function() { + $values = []; + foreach (HCK::getSuccesses() as $key => $name) { + $key = CacheKey::getHafasCacheSetKey($key); + $values[$name] = Cache::get($key, 0); + } + + return array_map(fn($value, $key) => [$value, [$key]], $values, array_keys($values)); + }); + Prometheus::addGauge("completed_jobs_count") ->helpText("How many jobs are done? Old items from queue monitor table are deleted after 7 days.") ->labels(["job_name", "status", "queue"])