Backup #1816

ID1816
Dateipfad/var/www/dev.campus.systemische-tools.de/src/Domain/ValueObject/ExecutionMetrics.php
Version2
Typ modified
Größe2.6 KB
Hasha017ef0995c91058485de9afd0e724f40b74af651d42d8339d2cd0bbe4dae51a
Datum2025-12-27 15:39:18
Geändert vonclaude-code-hook
GrundClaude Code Pre-Hook Backup vor Edit-Operation
Datei existiert Ja

Dateiinhalt

<?php

declare(strict_types=1);

namespace Domain\ValueObject;

// @responsibility: Immutables Value Object für Ausführungs-Metriken (Tokens + Kosten + Dauer)

use Domain\Constants;

final class ExecutionMetrics
{
    private function __construct(
        private TokenCount $tokens,
        private ?float $costUsd,
        private ?int $durationMs
    ) {
    }

    public static function create(TokenCount $tokens, ?float $costUsd = null, ?int $durationMs = null): self
    {
        return new self($tokens, $costUsd, $durationMs);
    }

    public static function zero(): self
    {
        return new self(TokenCount::zero(), null, null);
    }

    public static function fromEstimation(string $request, string $response): self
    {
        $inputTokens = max(1, (int) (strlen($request) / 4));
        $outputTokens = max(1, (int) (strlen($response) / 4));

        return new self(TokenCount::create($inputTokens, $outputTokens), null, null);
    }

    public function tokens(): TokenCount
    {
        return $this->tokens;
    }

    public function costUsd(): ?float
    {
        return $this->costUsd;
    }

    public function durationMs(): ?int
    {
        return $this->durationMs;
    }

    public function withCost(float $costUsd): self
    {
        return new self($this->tokens, $costUsd, $this->durationMs);
    }

    public function withDuration(int $durationMs): self
    {
        return new self($this->tokens, $this->costUsd, $durationMs);
    }

    public function withTokens(TokenCount $tokens): self
    {
        return new self($tokens, $this->costUsd, $this->durationMs);
    }

    public function hasCost(): bool
    {
        return $this->costUsd !== null;
    }

    public function hasDuration(): bool
    {
        return $this->durationMs !== null;
    }

    public function format(): string
    {
        $parts = [$this->tokens->format()];

        if ($this->costUsd !== null) {
            $parts[] = sprintf('$%.4f', $this->costUsd);
        }

        if ($this->durationMs !== null) {
            if ($this->durationMs < 1000) {
                $parts[] = "{$this->durationMs}ms";
            } else {
                $parts[] = sprintf('%.2fs', $this->durationMs / 1000);
            }
        }

        return implode(' | ', $parts);
    }

    public function toArray(): array
    {
        return [
            'tokens_input' => $this->tokens->input(),
            'tokens_output' => $this->tokens->output(),
            'tokens_total' => $this->tokens->total(),
            'cost_usd' => $this->costUsd,
            'duration_ms' => $this->durationMs,
        ];
    }
}

Vollständig herunterladen

Aktionen

Herunterladen

Andere Versionen dieser Datei

ID Version Typ Größe Datum
1816 2 modified 2.6 KB 2025-12-27 15:39
1815 1 modified 2.6 KB 2025-12-27 15:39

← Zurück zur Übersicht