Backup #1221

ID1221
Dateipfad/var/www/dev.campus.systemische-tools.de/src/Domain/DTO/SessionStatsDTO.php
Version1
Typ modified
Größe2.9 KB
Hashd1e7ff2f8f713e0a33424ecfc93da52f1ff586239f27258faed56b2984cf3c7b
Datum2025-12-25 10:49:55
Geändert vonclaude-code-hook
GrundClaude Code Pre-Hook Backup vor Write-Operation
Datei existiert Ja

Dateiinhalt

<?php

declare(strict_types=1);

namespace Domain\DTO;

// @responsibility: DTO für Chat-Session-Statistiken

final readonly class SessionStatsDTO
{
    /**
     * @param array<string> $collections
     */
    public function __construct(
        public int $id,
        public string $uuid,
        public ?string $title,
        public string $model,
        public array $collections,
        public int $contextLimit,
        public int $messageCount,
        public int $totalTokensInput,
        public int $totalTokensOutput,
        public float $totalCostUsd,
        public \DateTimeImmutable $createdAt,
        public \DateTimeImmutable $lastActivity,
        public ?int $authorProfileId = null,
    ) {}

    /**
     * Create from database row with aggregated stats.
     *
     * @param array<string, mixed> $row
     */
    public static function fromDatabaseRow(array $row): self
    {
        return new self(
            id: (int) $row['id'],
            uuid: (string) $row['uuid'],
            title: $row['title'] ?? null,
            model: (string) ($row['model'] ?? 'unknown'),
            collections: self::parseCollections($row['collections'] ?? ''),
            contextLimit: (int) ($row['context_limit'] ?? 3),
            messageCount: (int) ($row['message_count'] ?? 0),
            totalTokensInput: (int) ($row['total_tokens_input'] ?? 0),
            totalTokensOutput: (int) ($row['total_tokens_output'] ?? 0),
            totalCostUsd: (float) ($row['total_cost_usd'] ?? 0.0),
            createdAt: new \DateTimeImmutable($row['created_at'] ?? 'now'),
            lastActivity: new \DateTimeImmutable($row['last_activity'] ?? $row['created_at'] ?? 'now'),
            authorProfileId: isset($row['author_profile_id']) ? (int) $row['author_profile_id'] : null,
        );
    }

    /**
     * Parse comma-separated collections or JSON array.
     *
     * @return array<string>
     */
    private static function parseCollections(string|array $collections): array
    {
        if (is_array($collections)) {
            return $collections;
        }

        if ($collections === '') {
            return [];
        }

        $decoded = json_decode($collections, true);
        if (is_array($decoded)) {
            return $decoded;
        }

        return array_filter(array_map('trim', explode(',', $collections)));
    }

    /**
     * Get total tokens used.
     */
    public function totalTokens(): int
    {
        return $this->totalTokensInput + $this->totalTokensOutput;
    }

    /**
     * Get display title (with fallback).
     */
    public function displayTitle(): string
    {
        return $this->title ?? 'Chat vom ' . $this->createdAt->format('d.m.Y H:i');
    }

    /**
     * Check if session is active (has recent activity).
     */
    public function isRecentlyActive(int $hoursThreshold = 24): bool
    {
        $threshold = new \DateTimeImmutable("-{$hoursThreshold} hours");
        return $this->lastActivity > $threshold;
    }
}

Vollständig herunterladen

Aktionen

Herunterladen

Andere Versionen dieser Datei

ID Version Typ Größe Datum
1890 4 modified 4.0 KB 2025-12-28 01:01
1886 3 modified 4.0 KB 2025-12-28 01:00
1226 2 modified 3.9 KB 2025-12-25 10:50
1221 1 modified 2.9 KB 2025-12-25 10:49

← Zurück zur Übersicht