You've already forked pilot-sdk
first commit
This commit is contained in:
42
src/Entity/Action.php
Normal file
42
src/Entity/Action.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace Bluesquare\Pilot\Entity;
|
||||
|
||||
abstract class Action extends Entity
|
||||
{
|
||||
public function action(
|
||||
$type = 'info',
|
||||
$title = null,
|
||||
$message = null,
|
||||
$button = null,
|
||||
$url = null,
|
||||
$files = [],
|
||||
) {
|
||||
return [
|
||||
'json' => [
|
||||
'type' => 'action',
|
||||
'action' => compact('type', 'title', 'message', 'button', 'url', 'files'),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function file(
|
||||
$contents,
|
||||
$name = '',
|
||||
$type = ''
|
||||
) {
|
||||
return [
|
||||
'file' => compact('contents', 'name', 'type'),
|
||||
];
|
||||
}
|
||||
|
||||
public function error($message)
|
||||
{
|
||||
return [
|
||||
'json' => [
|
||||
'type' => 'error',
|
||||
'error' => $message,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
16
src/Entity/Entity.php
Normal file
16
src/Entity/Entity.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Bluesquare\Pilot\Entity;
|
||||
|
||||
abstract class Entity
|
||||
{
|
||||
public function __construct(protected $slug = null)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function slug()
|
||||
{
|
||||
return $this->slug;
|
||||
}
|
||||
}
|
||||
116
src/Entity/Metric.php
Normal file
116
src/Entity/Metric.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
namespace Bluesquare\Pilot\Entity;
|
||||
|
||||
abstract class Metric extends Widget
|
||||
{
|
||||
/**
|
||||
* @var bool|int $cache Cache duration (in seconds) or FALSE to disable caching
|
||||
*/
|
||||
protected $cache = false;
|
||||
|
||||
protected $data = null;
|
||||
|
||||
abstract public function compute($entry = null);
|
||||
|
||||
public function handle($data)
|
||||
{
|
||||
$this->data = $data;
|
||||
|
||||
$cache_key = 'pilot-metric-'.$this->slug.'-'.json_encode($data);
|
||||
|
||||
if ($this->cache !== false && cache()->has($cache_key)) {
|
||||
$computed = cache()->get($cache_key);
|
||||
}
|
||||
else {
|
||||
$computed = $this->compute(
|
||||
$data['entry'] ?? null
|
||||
);
|
||||
|
||||
if ($this->cache !== false) {
|
||||
cache()->put($cache_key, $computed, $this->cache);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->output($computed);
|
||||
}
|
||||
|
||||
public function output($data)
|
||||
{
|
||||
return [
|
||||
'json' => [
|
||||
'metric' => $data
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
protected function resolveFilter($filters)
|
||||
{
|
||||
$filter = collect($filters)->firstWhere('value', $this->data['filter'] ?? null) ?? collect($filters)->first();
|
||||
|
||||
$computed = is_array($filter) ? $filter['callback']($this->data) : [];
|
||||
|
||||
return [
|
||||
'filter' => $filter['value'],
|
||||
'filters' => collect($filters)->map(function ($filter) {
|
||||
return [
|
||||
'label' => $filter['label'],
|
||||
'value' => $filter['value'],
|
||||
];
|
||||
})->values()->all(),
|
||||
...$computed
|
||||
];
|
||||
}
|
||||
|
||||
public function filter($label, $value, callable $callback)
|
||||
{
|
||||
return ['label' => $label, 'value' => $value, 'callback' => $callback];
|
||||
}
|
||||
|
||||
public function filters($filters)
|
||||
{
|
||||
return $this->resolveFilter($filters);
|
||||
}
|
||||
|
||||
public function progress($current, $target)
|
||||
{
|
||||
return $this->output([
|
||||
'type' => 'progress',
|
||||
'value' => $current,
|
||||
'target' => $target,
|
||||
]);
|
||||
}
|
||||
|
||||
public function trend($value, $previous = null)
|
||||
{
|
||||
return [
|
||||
'type' => 'trend',
|
||||
'value' => $value,
|
||||
'previous' => $previous,
|
||||
];
|
||||
}
|
||||
|
||||
public function partition($values)
|
||||
{
|
||||
return [
|
||||
'type' => 'partition',
|
||||
'values' => $values,
|
||||
];
|
||||
}
|
||||
|
||||
public function value($value)
|
||||
{
|
||||
return [
|
||||
'type' => 'value',
|
||||
'value' => $value,
|
||||
];
|
||||
}
|
||||
|
||||
public function monitor($available)
|
||||
{
|
||||
return [
|
||||
'type' => 'monitor',
|
||||
'value' => $available,
|
||||
];
|
||||
}
|
||||
}
|
||||
8
src/Entity/Widget.php
Normal file
8
src/Entity/Widget.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Bluesquare\Pilot\Entity;
|
||||
|
||||
abstract class Widget extends Entity
|
||||
{
|
||||
//
|
||||
}
|
||||
Reference in New Issue
Block a user