Backup #1263

ID1263
Dateipfad/var/www/dev.campus.systemische-tools.de/src/Infrastructure/Persistence/KiProtokollRepository.php
Version6
Typ modified
Größe4.5 KB
Hash4e2193c21adca45efedb88195c96452c81cca181a7df8e80beee5f3de541996b
Datum2025-12-25 12:51:03
Geändert vonclaude-code-hook
GrundClaude Code Pre-Hook Backup vor Edit-Operation
Datei existiert Ja

Dateiinhalt

<?php

declare(strict_types=1);

namespace Infrastructure\Persistence;

// @responsibility: Persistenz für KI-Protokoll (Claude-Konversationen)

class KiProtokollRepository
{
    private \PDO $pdo;

    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }

    public function findById(int $id): ?array
    {
        $stmt = $this->pdo->prepare('SELECT * FROM protokoll WHERE id = ?');
        $stmt->execute([$id]);
        $result = $stmt->fetch(\PDO::FETCH_ASSOC);

        return $result !== false ? $result : null;
    }

    public function findLatest(int $limit = 20): array
    {
        $stmt = $this->pdo->prepare(
            'SELECT id, timestamp, client_name, request, status, duration_ms
             FROM protokoll ORDER BY id DESC LIMIT ?'
        );
        $stmt->bindValue(1, $limit, \PDO::PARAM_INT);
        $stmt->execute();

        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
    }

    public function findPaginated(
        ?string $search = null,
        ?string $status = null,
        ?string $model = null,
        int $limit = 50,
        int $offset = 0
    ): array {
        $sql = 'SELECT id, timestamp, client_name, model_name, status, tokens_total, duration_ms,
                       LEFT(request, 200) as request_preview
                FROM protokoll WHERE 1=1';
        $params = [];

        if ($search !== null && $search !== '') {
            $sql .= ' AND (request LIKE ? OR response LIKE ? OR client_name LIKE ?)';
            $params[] = '%' . $search . '%';
            $params[] = '%' . $search . '%';
            $params[] = '%' . $search . '%';
        }

        if ($status !== null && $status !== '') {
            $sql .= ' AND status = ?';
            $params[] = $status;
        }

        if ($model !== null && $model !== '') {
            $sql .= ' AND model_name = ?';
            $params[] = $model;
        }

        $sql .= ' ORDER BY timestamp DESC LIMIT ? OFFSET ?';

        $stmt = $this->pdo->prepare($sql);
        $paramIndex = 1;
        foreach ($params as $value) {
            $stmt->bindValue($paramIndex++, $value);
        }
        $stmt->bindValue($paramIndex++, $limit, \PDO::PARAM_INT);
        $stmt->bindValue($paramIndex, $offset, \PDO::PARAM_INT);
        $stmt->execute();

        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
    }

    public function countFiltered(?string $search = null, ?string $status = null, ?string $model = null): int
    {
        $sql = 'SELECT COUNT(*) FROM protokoll WHERE 1=1';
        $params = [];

        if ($search !== null && $search !== '') {
            $sql .= ' AND (request LIKE ? OR response LIKE ? OR client_name LIKE ?)';
            $params[] = '%' . $search . '%';
            $params[] = '%' . $search . '%';
            $params[] = '%' . $search . '%';
        }

        if ($status !== null && $status !== '') {
            $sql .= ' AND status = ?';
            $params[] = $status;
        }

        if ($model !== null && $model !== '') {
            $sql .= ' AND model_name = ?';
            $params[] = $model;
        }

        $stmt = $this->pdo->prepare($sql);
        $stmt->execute($params);

        return (int) $stmt->fetchColumn();
    }

    public function getStatistics(): array
    {
        $stmt = $this->pdo->query(
            'SELECT
                COUNT(*) as total,
                SUM(CASE WHEN status = "completed" THEN 1 ELSE 0 END) as completed,
                SUM(CASE WHEN status = "error" THEN 1 ELSE 0 END) as errors,
                COALESCE(SUM(tokens_total), 0) as tokens_total,
                COALESCE(SUM(duration_ms), 0) as duration_total
             FROM protokoll'
        );

        return $stmt->fetch(\PDO::FETCH_ASSOC);
    }

    public function getDistinctModels(): array
    {
        $stmt = $this->pdo->query(
            'SELECT DISTINCT model_name FROM protokoll WHERE model_name IS NOT NULL ORDER BY model_name'
        );

        return $stmt->fetchAll(\PDO::FETCH_COLUMN);
    }

    public function findPreviousId(int $id): ?int
    {
        $stmt = $this->pdo->prepare('SELECT id FROM protokoll WHERE id < ? ORDER BY id DESC LIMIT 1');
        $stmt->execute([$id]);
        $result = $stmt->fetchColumn();

        return $result !== false ? (int) $result : null;
    }

    public function findNextId(int $id): ?int
    {
        $stmt = $this->pdo->prepare('SELECT id FROM protokoll WHERE id > ? ORDER BY id ASC LIMIT 1');
        $stmt->execute([$id]);
        $result = $stmt->fetchColumn();

        return $result !== false ? (int) $result : null;
    }
}

Vollständig herunterladen

Aktionen

Herunterladen

Andere Versionen dieser Datei

ID Version Typ Größe Datum
2116 8 modified 7.0 KB 2025-12-29 09:09
2097 7 modified 4.6 KB 2025-12-29 08:42
1263 6 modified 4.5 KB 2025-12-25 12:51
1194 5 modified 4.5 KB 2025-12-25 10:33
1177 4 modified 4.6 KB 2025-12-25 10:32
782 3 modified 4.5 KB 2025-12-23 08:05
447 2 modified 778 B 2025-12-22 10:19
437 1 modified 1016 B 2025-12-22 10:11

← Zurück zur Übersicht