Skip to content
Snippets Groups Projects
Wowa.php 3.49 KiB
<?php

namespace App;

use App\Logging\LogTraining;
use Illuminate\Database\Eloquent\Model;
use Monolog\Logger;
use Psr\Log\LoggerInterface;

class Wowa extends Model
{
    const STATE_CREATED = "CREATED";
    const STATE_QUEUED = "QUEUED";
    const STATE_COLLECTION = "DATA_COLLECTION";
    const STATE_RUNNING = "RUNNING";
    const STATE_SUCCESS = "SUCCESS";
    const STATE_FAILED = "FAILED";

    public static $status_classes = [
        self::STATE_SUCCESS => "btn-success",
        self::STATE_FAILED => "btn-danger",
        self::STATE_RUNNING => "btn-warning",
        self::STATE_COLLECTION => "btn-warning"
    ];


    protected $casts = [
        'w_weights' => 'array',
        'p_weights' => 'array',
    ];
    //Method to display status badge
    private function getStatusBadgeClass()
    {
        $status = $this->status;
        if (!isset(self::$status_classes[$status])) {
            return "btn-primary";
        }

        return self::$status_classes[$status];
    }

    //Display text in the status badge
    public function statusBadge()
    {
        if ($this->status == self::STATE_RUNNING || $this->status == self::STATE_QUEUED) {
            return "<span class='btn btn-badge btn-sm "
                . $this->getStatusBadgeClass() . "'>" . $this->status
                . " : " . $this->getProgression() . "</span>";
        } else {
            return "<span class='btn btn-badge btn-sm "
                . $this->getStatusBadgeClass() . "'>" . $this->status . "</span>";
        }
    }

    public function weightsP()
    {
        return $this->w_weights->toArray();
    }

    //Method to get the path for the log file. Name linked to the Wowa id in sqlite DB
    public function getLogPathOnDisk(): string
    {
        $log_dir = storage_path("app/training");
        if (! is_dir($log_dir)) {
            mkdir($log_dir);
        }
        return $log_dir . sprintf("/wowa-%'.09d", $this->id) . ".log";
    }
    //Method to get the progression
    //Read the last line of the log file, get the progression value
    public function getProgression() : string
    {
        $line = '';
        if (!file_exists($this->getLogPathOnDisk())) {
            return "Calculation in Progress...";
        }
        $f = fopen($this->getLogPathOnDisk(), 'r');
        $cursor = -1;

        fseek($f, $cursor, SEEK_END);
        $char = fgetc($f);

        while ($char === "\n" || $char === "\r") {
            fseek($f, $cursor--, SEEK_END);
            $char = fgetc($f);
        }

        while ($char !== false && $char !== "\n" && $char !== "\r") {
            $line = $char . $line;
            fseek($f, $cursor--, SEEK_END);
            $char = fgetc($f);
        }

        $generation = intval($this->getStringBetween($line, ': ', '|'));
        if ($this->status === Wowa::STATE_COLLECTION) {
            return $generation;
        }
        return (($generation +1) / $this->generation_number) * 100;
    }

    //Method to return the value between : and | (progression)
    private function getStringBetween($string, $start, $end)
    {
        $string = ' ' . $string;
        $ini = strpos($string, $start);
        if ($ini == 0) {
            return '';
        }
        $ini += strlen($start);
        $len = strpos($string, $end, $ini) - $ini;
        return substr($string, $ini, $len);
    }

    //Create a Logger instance. The log will be stored in a log file. File path is given by getLogPathOnDisk method.
    public function logger() : LoggerInterface
    {
        return \Log::channel('custom');
    }
}