Skip to content
Snippets Groups Projects
MarkController.php 3.59 KiB
<?php

namespace App\Http\Controllers;

use Cylab\Mark\Client;


class MarkController extends Controller
{
    private $server;

    public function __construct() {
        $this->server = new Client();
    }

    public function status() {
        $status = $this->server->status();
        $status_history = $this->server->history();

        $history_memory = $this->extractPoints($status_history, "memory_used");
        $history_load = $this->extractPoints($status_history, "load");

        $history_jobs_executed = $this->extractPoints($status_history, "executor_jobs_executed");
        $history_jobs_execution_rate = $this->computeExecutionRate($history_jobs_executed);

        return view("app.status", [
            "status" => $status,
            "history_memory" => $history_memory,
            "history_load" => $history_load,
            "history_jobs_execution_rate" => $history_jobs_execution_rate]);
    }

    public function pause() {
        $this->server->pause();
        return redirect(action('MarkController@status'));
    }

    public function resume() {
        $this->server->resume();
        return redirect(action('MarkController@status'));
    }

    public function reload() {
        $this->server->reload();
        return redirect(action('MarkController@status'));
    }

    public function ranking(string $label) {
        $detectors = $this->server->activation();
        $evidences = $this->server->findEvidence($label);
        return view(
                "app.ranking", [
                    "label" => $label,
                    "evidences" => $evidences,
                    "detectors" => $detectors]);
    }

    public function rankingHome() {
        $detectors = $this->server->activation();
        $label = $detectors[0]['label'];
        return redirect('/app/ranking/' . $label);

    }

    public function extractPoints(array $evidences, string $field) : array
    {
        $points = [];
        foreach ($evidences as $evidence) {
            $points[] = new \App\TimePoint(
                    $evidence["time"],
                    $evidence[$field]);
        }
        return $points;
    }

    public function evidence(string $id)
    {
        $time_window = 3600; // in seconds

        $ev = $this->server->findEvidenceById($id);
        $since = $ev["time"] - $time_window * 1000;

        $references = [];
        foreach ($ev["references"] as $id) {
            $references[] = $this->server->findEvidenceById($id);
        }

        $history = $this->server->findEvidenceSince($ev["label"], $ev["subject"], $since);

        return view(
                'app.evidence',[
                    "evidence" => $ev,
                    "history" => $history,
                    "history_points" => $this->extractPoints($history, "score"),
                    "references" => $references]);
    }

    /**
     * Compute the number of jobs executed per minute.
     *
     * @param array $history_jobs_executed
     * @return array
     */
    private function computeExecutionRate(array $history_jobs_executed) : array {
        $points = [];
        $last_point_time = $history_jobs_executed[0]->t;
        $last_point_value = $history_jobs_executed[0]->y;

        foreach ($history_jobs_executed as $point) {
            // this point is less than a minute after the last computed point
            if (($point->t - $last_point_time) < 60000) {
                continue;
            }

            $points[] = new \App\TimePoint($point->t, $point->y - $last_point_value);

            $last_point_time = $point->t;
            $last_point_value = $point->y;
        }

        return $points;

    }

}