admin: Bericht „Indexierung & Abrufe" — Rolle Erstquelle/Kopie, Quellenbefund, Crawler-Abrufe; Schlüsselpfad ab Projektwurzel
Der Bericht meldete auf Live „nicht konfiguriert", während der Lauf längst Daten holte: der relative Pfad der Schlüsseldatei war im Cron (Projektwurzel) lesbar, im Web-Request (public/) nicht. Relative Pfade werden jetzt ab base_path() aufgelöst. Die Verteilung trennt zusätzlich nach Rolle in der kanonischen Zuordnung (1c.8): bei einer Kopie mit Verweis ist „ausgeschlossen" der erwartete Befund. Neu: Quellenbefund über Erstquellen (welche Adresse Google führt, fremde Hosts), Abrufe durch Such- und KI-Dienste je Dienst, Tag und Meldung mit CSV. Herkunftsmarker als Join statt korreliertem EXISTS, Aggregate 15 Minuten gecacht. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
1c421c0377
commit
c26bdff681
8 changed files with 609 additions and 87 deletions
112
app/Services/PressRelease/CrawlerHitReport.php
Normal file
112
app/Services/PressRelease/CrawlerHitReport.php
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services\PressRelease;
|
||||
|
||||
use App\Models\PressReleaseCrawlerHit;
|
||||
use App\Support\CrawlerIdentifier;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
/**
|
||||
* Admin-Auswertung des Crawler-Abruf-Protokolls (Roadmap 1c.0): welche
|
||||
* Such- und KI-Dienste in welchem Umfang Meldungen geholt haben. Zählt
|
||||
* Abrufe, nicht Zitate — das Protokoll weiß nicht, was ein Dienst mit der
|
||||
* Seite gemacht hat.
|
||||
*/
|
||||
class CrawlerHitReport
|
||||
{
|
||||
/**
|
||||
* Je Dienst: Abrufe und betroffene Meldungen im Zeitraum, Abrufe seit
|
||||
* Beginn des Protokolls, letzter Abruf. Sortiert nach Abrufen im Zeitraum.
|
||||
*
|
||||
* @return list<array{service: string, label: string, kind: string, fetches: int, releases: int, fetches_total: int, last_at: Carbon}>
|
||||
*/
|
||||
public function byService(int $days = 30): array
|
||||
{
|
||||
$since = now()->subDays($days);
|
||||
|
||||
$recent = PressReleaseCrawlerHit::query()
|
||||
->where('created_at', '>=', $since)
|
||||
->selectRaw('service, count(*) as fetches, count(distinct press_release_id) as releases')
|
||||
->groupBy('service')
|
||||
->toBase()
|
||||
->get()
|
||||
->keyBy('service');
|
||||
|
||||
return PressReleaseCrawlerHit::query()
|
||||
->selectRaw('service, count(*) as fetches_total, max(created_at) as last_at')
|
||||
->groupBy('service')
|
||||
->toBase()
|
||||
->get()
|
||||
->map(fn (object $row): array => [
|
||||
'service' => (string) $row->service,
|
||||
'label' => CrawlerIdentifier::label((string) $row->service),
|
||||
'kind' => CrawlerIdentifier::kind((string) $row->service),
|
||||
'fetches' => (int) ($recent[$row->service]->fetches ?? 0),
|
||||
'releases' => (int) ($recent[$row->service]->releases ?? 0),
|
||||
'fetches_total' => (int) $row->fetches_total,
|
||||
'last_at' => Carbon::parse((string) $row->last_at),
|
||||
])
|
||||
->sortByDesc(fn (array $row): array => [$row['fetches'], $row['fetches_total']])
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Abrufe je Tag über die letzten N Tage, lückenlos, getrennt nach Suche
|
||||
* und KI (Art aus config/crawlers.php).
|
||||
*
|
||||
* @return list<array{date: Carbon, search: int, ai: int}>
|
||||
*/
|
||||
public function daily(int $days = 14): array
|
||||
{
|
||||
$start = now()->startOfDay()->subDays($days - 1);
|
||||
|
||||
$counts = PressReleaseCrawlerHit::query()
|
||||
->where('created_at', '>=', $start)
|
||||
->selectRaw('date(created_at) as day, service, count(*) as fetches')
|
||||
->groupBy('day', 'service')
|
||||
->toBase()
|
||||
->get()
|
||||
->groupBy('day');
|
||||
|
||||
$series = [];
|
||||
|
||||
for ($i = 0; $i < $days; $i++) {
|
||||
$day = $start->copy()->addDays($i);
|
||||
$row = ['date' => $day, 'search' => 0, 'ai' => 0];
|
||||
|
||||
foreach ($counts->get($day->toDateString(), collect()) as $entry) {
|
||||
$row[CrawlerIdentifier::kind((string) $entry->service) === 'search' ? 'search' : 'ai'] += (int) $entry->fetches;
|
||||
}
|
||||
|
||||
$series[] = $row;
|
||||
}
|
||||
|
||||
return $series;
|
||||
}
|
||||
|
||||
/**
|
||||
* Am häufigsten abgerufene Meldungen im Zeitraum.
|
||||
*
|
||||
* @return list<array{press_release_id: int, title: string, fetches: int, services: int}>
|
||||
*/
|
||||
public function topReleases(int $days = 30, int $limit = 15): array
|
||||
{
|
||||
return PressReleaseCrawlerHit::query()
|
||||
->join('press_releases', 'press_releases.id', '=', 'press_release_crawler_hits.press_release_id')
|
||||
->where('press_release_crawler_hits.created_at', '>=', now()->subDays($days))
|
||||
->selectRaw('press_release_crawler_hits.press_release_id, press_releases.title, count(*) as fetches, count(distinct service) as services')
|
||||
->groupBy('press_release_crawler_hits.press_release_id', 'press_releases.title')
|
||||
->orderByDesc('fetches')
|
||||
->limit($limit)
|
||||
->toBase()
|
||||
->get()
|
||||
->map(fn (object $row): array => [
|
||||
'press_release_id' => (int) $row->press_release_id,
|
||||
'title' => (string) $row->title,
|
||||
'fetches' => (int) $row->fetches,
|
||||
'services' => (int) $row->services,
|
||||
])
|
||||
->all();
|
||||
}
|
||||
}
|
||||
|
|
@ -10,6 +10,7 @@ use App\Scopes\PortalScope;
|
|||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
|
|
@ -23,73 +24,148 @@ class IndexStatusReport
|
|||
/**
|
||||
* Ausdruck, der eine Meldung der Herkunft „api" oder „direct" zuordnet:
|
||||
* Marker ist ein Status-Log-Eintrag mit Quelle „api" (wie beim
|
||||
* Mail-Versand — der Kontotyp ist nicht verlässlich).
|
||||
* Mail-Versand — der Kontotyp ist nicht verlässlich). Erwartet den
|
||||
* Join aus apiJoin(): als Join ein Bruchteil der Laufzeit eines
|
||||
* korrelierten EXISTS über den Bestand (0,3 s statt 2,6 s).
|
||||
*/
|
||||
private const string ORIGIN_SQL = "case when exists (select 1 from press_release_status_logs l where l.press_release_id = press_releases.id and l.source = 'api') then 'api' else 'direct' end";
|
||||
private const string ORIGIN_SQL = "case when api_origin.press_release_id is null then 'direct' else 'api' end";
|
||||
|
||||
private const int CACHE_SECONDS = 900;
|
||||
|
||||
/**
|
||||
* Anzahl der Meldungen je Portal, Herkunft und Befund — Stand der jeweils
|
||||
* jüngsten Prüfung. Meldungen ohne Prüfung erscheinen unter `unchecked`.
|
||||
* Rolle einer Meldung in der kanonischen Zuordnung (1c.8): Erstquelle
|
||||
* oder Kopie mit rel=canonical auf die andere Fassung. Für eine Kopie ist
|
||||
* „ausgeschlossen" (Google folgt dem Verweis) der erwartete Befund.
|
||||
*/
|
||||
private const string ROLE_SQL = "case when press_releases.canonical_press_release_id is null then 'source' else 'copy' end";
|
||||
|
||||
/** @var list<string> */
|
||||
public const array ORIGINS = ['direct', 'api'];
|
||||
|
||||
/** @var list<string> */
|
||||
public const array ROLES = ['source', 'copy'];
|
||||
|
||||
/**
|
||||
* Anzahl der Meldungen je Portal, Herkunft, Rolle und Befund — Stand der
|
||||
* jeweils jüngsten Prüfung. Meldungen ohne Prüfung erscheinen unter
|
||||
* `unchecked`.
|
||||
*
|
||||
* @return array<string, array<string, array<string, int>>> portal => origin => state|unchecked|total => count
|
||||
* @return array<string, array<string, array<string, array<string, int>>>> portal => origin => role => state|unchecked|total => count
|
||||
*/
|
||||
public function distribution(): array
|
||||
{
|
||||
return Cache::remember('index-status.distribution', self::CACHE_SECONDS, fn (): array => $this->computeDistribution());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array<string, array<string, array<string, int>>>>
|
||||
*/
|
||||
private function computeDistribution(): array
|
||||
{
|
||||
$rows = [];
|
||||
|
||||
foreach ([Portal::Presseecho, Portal::Businessportal24] as $portal) {
|
||||
// Herkunft je Meldung erst in einer abgeleiteten Tabelle bestimmen,
|
||||
// dann darüber gruppieren — MySQL (only_full_group_by) lehnt den
|
||||
// korrelierten Ausdruck direkt im GROUP BY ab.
|
||||
// Herkunft und Rolle je Meldung erst in einer abgeleiteten Tabelle
|
||||
// bestimmen, dann darüber gruppieren — MySQL (only_full_group_by)
|
||||
// lehnt den korrelierten Ausdruck direkt im GROUP BY ab.
|
||||
$visible = DB::query()
|
||||
->fromSub(
|
||||
PressRelease::query()
|
||||
$this->apiJoin(PressRelease::query()
|
||||
->withoutGlobalScope(PortalScope::class)
|
||||
->visibleInArchive()
|
||||
->forPortalValues([$portal->value, Portal::Both->value])
|
||||
->selectRaw('press_releases.id, '.self::ORIGIN_SQL.' as origin'),
|
||||
->forPortalValues([$portal->value, Portal::Both->value]))
|
||||
->selectRaw('press_releases.id, '.self::ORIGIN_SQL.' as origin, '.self::ROLE_SQL.' as role'),
|
||||
'r'
|
||||
)
|
||||
->selectRaw('origin, count(*) as total')
|
||||
->groupBy('origin')
|
||||
->selectRaw('origin, role, count(*) as total')
|
||||
->groupBy('origin', 'role')
|
||||
->get();
|
||||
|
||||
$checked = DB::query()
|
||||
->fromSub(
|
||||
$this->latestChecks($portal)
|
||||
$this->apiJoin($this->latestChecks($portal)
|
||||
->join('press_releases', 'press_releases.id', '=', 'press_release_index_checks.press_release_id')
|
||||
->whereNull('press_releases.deleted_at')
|
||||
->selectRaw('press_release_index_checks.id, press_release_index_checks.state, '.self::ORIGIN_SQL.' as origin'),
|
||||
->whereNull('press_releases.deleted_at'))
|
||||
->selectRaw('press_release_index_checks.id, press_release_index_checks.state, '.self::ORIGIN_SQL.' as origin, '.self::ROLE_SQL.' as role'),
|
||||
'c'
|
||||
)
|
||||
->selectRaw('origin, state, count(*) as total')
|
||||
->groupBy('origin', 'state')
|
||||
->selectRaw('origin, role, state, count(*) as total')
|
||||
->groupBy('origin', 'role', 'state')
|
||||
->get();
|
||||
|
||||
foreach (['direct', 'api'] as $origin) {
|
||||
$bucket = ['total' => 0, 'unchecked' => 0];
|
||||
foreach (self::ORIGINS as $origin) {
|
||||
foreach (self::ROLES as $role) {
|
||||
$bucket = ['total' => 0, 'unchecked' => 0];
|
||||
|
||||
foreach (IndexState::cases() as $state) {
|
||||
$bucket[$state->value] = 0;
|
||||
foreach (IndexState::cases() as $state) {
|
||||
$bucket[$state->value] = 0;
|
||||
}
|
||||
|
||||
$bucket['total'] = (int) ($visible->first(fn (object $row): bool => $row->origin === $origin && $row->role === $role)->total ?? 0);
|
||||
|
||||
$checkedTotal = 0;
|
||||
|
||||
foreach ($checked->filter(fn (object $row): bool => $row->origin === $origin && $row->role === $role) as $row) {
|
||||
$bucket[(string) $row->state] = (int) $row->total;
|
||||
$checkedTotal += (int) $row->total;
|
||||
}
|
||||
|
||||
$bucket['unchecked'] = max(0, $bucket['total'] - $checkedTotal);
|
||||
$rows[$portal->value][$origin][$role] = $bucket;
|
||||
}
|
||||
|
||||
$bucket['total'] = (int) ($visible->firstWhere('origin', $origin)->total ?? 0);
|
||||
|
||||
$checkedTotal = 0;
|
||||
|
||||
foreach ($checked->where('origin', $origin) as $row) {
|
||||
$bucket[(string) $row->state] = (int) $row->total;
|
||||
$checkedTotal += (int) $row->total;
|
||||
}
|
||||
|
||||
$bucket['unchecked'] = max(0, $bucket['total'] - $checkedTotal);
|
||||
$rows[$portal->value][$origin] = $bucket;
|
||||
}
|
||||
}
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* Quellenbefund (1c.9) über die jüngsten Prüfungen je Portal: welche
|
||||
* Adresse Google als maßgeblich führt — diese, die eigene Fassung auf dem
|
||||
* anderen Portal, ein fremdes Portal, oder keine Angabe. Nur Erstquellen
|
||||
* (1c.8) zählen: bei einer Kopie ist die Abweichung gewollt.
|
||||
*
|
||||
* @return array<string, array{self: int, own_other: int, foreign: int, unknown: int, total: int, foreign_hosts: array<string, int>}>
|
||||
*/
|
||||
public function sourceSummary(IndexStatusChecker $checker): array
|
||||
{
|
||||
return Cache::remember('index-status.sources', self::CACHE_SECONDS, fn (): array => $this->computeSourceSummary($checker));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array{self: int, own_other: int, foreign: int, unknown: int, total: int, foreign_hosts: array<string, int>}>
|
||||
*/
|
||||
private function computeSourceSummary(IndexStatusChecker $checker): array
|
||||
{
|
||||
$summary = [];
|
||||
|
||||
foreach ([Portal::Presseecho, Portal::Businessportal24] as $portal) {
|
||||
$bucket = ['self' => 0, 'own_other' => 0, 'foreign' => 0, 'unknown' => 0, 'total' => 0, 'foreign_hosts' => []];
|
||||
|
||||
$this->latestChecks($portal)
|
||||
->join('press_releases', 'press_releases.id', '=', 'press_release_index_checks.press_release_id')
|
||||
->whereNull('press_releases.deleted_at')
|
||||
->whereNull('press_releases.canonical_press_release_id')
|
||||
->select(['press_release_index_checks.id', 'press_release_index_checks.url', 'press_release_index_checks.google_canonical', 'press_release_index_checks.portal'])
|
||||
->lazyById(1000, 'press_release_index_checks.id', 'id')
|
||||
->each(function (PressReleaseIndexCheck $check) use (&$bucket, $checker): void {
|
||||
$verdict = $checker->sourceVerdict($check);
|
||||
$bucket[$verdict['kind']]++;
|
||||
$bucket['total']++;
|
||||
|
||||
if ($verdict['kind'] === 'foreign' && $verdict['host'] !== null) {
|
||||
$bucket['foreign_hosts'][$verdict['host']] = ($bucket['foreign_hosts'][$verdict['host']] ?? 0) + 1;
|
||||
}
|
||||
});
|
||||
|
||||
arsort($bucket['foreign_hosts']);
|
||||
$bucket['foreign_hosts'] = array_slice($bucket['foreign_hosts'], 0, 10, true);
|
||||
$summary[$portal->value] = $bucket;
|
||||
}
|
||||
|
||||
return $summary;
|
||||
}
|
||||
|
||||
/**
|
||||
* Jüngste Prüfungen, neueste zuerst.
|
||||
*
|
||||
|
|
@ -98,7 +174,7 @@ class IndexStatusReport
|
|||
public function latest(int $limit = 100): Collection
|
||||
{
|
||||
return PressReleaseIndexCheck::query()
|
||||
->with(['pressRelease' => fn ($query) => $query->withoutGlobalScopes()->select(['id', 'title', 'slug', 'published_at'])])
|
||||
->with(['pressRelease' => fn ($query) => $query->withoutGlobalScopes()->select(['id', 'title', 'slug', 'published_at', 'canonical_press_release_id'])])
|
||||
->orderByDesc('checked_at')
|
||||
->orderByDesc('id')
|
||||
->limit($limit)
|
||||
|
|
@ -112,6 +188,25 @@ class IndexStatusReport
|
|||
return $value ? Carbon::parse((string) $value) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Herkunftsmarker als Join: alle Meldungen, die je einen Status-Log-
|
||||
* Eintrag mit Quelle „api" haben.
|
||||
*
|
||||
* @template TModel of \Illuminate\Database\Eloquent\Model
|
||||
*
|
||||
* @param Builder<TModel> $query
|
||||
* @return Builder<TModel>
|
||||
*/
|
||||
private function apiJoin(Builder $query): Builder
|
||||
{
|
||||
$api = DB::table('press_release_status_logs')
|
||||
->select('press_release_id')
|
||||
->where('source', 'api')
|
||||
->distinct();
|
||||
|
||||
return $query->leftJoinSub($api, 'api_origin', 'api_origin.press_release_id', '=', 'press_releases.id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Jeweils jüngste Prüfung je Meldung auf einem Portal.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -22,9 +22,26 @@ class SearchConsoleClient
|
|||
|
||||
public function isConfigured(): bool
|
||||
{
|
||||
$file = (string) config('search_console.credentials');
|
||||
$file = $this->credentialsPath();
|
||||
|
||||
return (bool) config('search_console.enabled') && $file !== '' && is_readable($file);
|
||||
return (bool) config('search_console.enabled') && $file !== null && is_readable($file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pfad der Schlüsseldatei, relative Angaben ab Projektwurzel. Ohne das
|
||||
* hing die Lesbarkeit vom Arbeitsverzeichnis ab: im Cron (Projektwurzel)
|
||||
* lesbar, im Web-Request (public/) nicht — der Admin meldete „nicht
|
||||
* konfiguriert", während der Lauf längst Daten holte.
|
||||
*/
|
||||
private function credentialsPath(): ?string
|
||||
{
|
||||
$file = trim((string) config('search_console.credentials'));
|
||||
|
||||
if ($file === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return str_starts_with($file, '/') ? $file : base_path($file);
|
||||
}
|
||||
|
||||
public function propertyFor(string $portal): ?string
|
||||
|
|
@ -102,8 +119,8 @@ class SearchConsoleClient
|
|||
*/
|
||||
private function credentials(): array
|
||||
{
|
||||
$file = (string) config('search_console.credentials');
|
||||
$json = is_readable($file) ? json_decode((string) file_get_contents($file), true) : null;
|
||||
$file = $this->credentialsPath();
|
||||
$json = $file !== null && is_readable($file) ? json_decode((string) file_get_contents($file), true) : null;
|
||||
|
||||
if (! is_array($json) || empty($json['client_email']) || empty($json['private_key'])) {
|
||||
throw new RuntimeException('Search Console: Schlüsseldatei des Service-Kontos fehlt oder ist unvollständig.');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue