forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResult.php
More file actions
117 lines (104 loc) · 2.96 KB
/
Result.php
File metadata and controls
117 lines (104 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace App\Models;
use App\Events\ResultCreated;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Result extends Model
{
use HasFactory;
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'ping',
'download',
'upload',
'server_id',
'server_host',
'server_name',
'url',
'comments',
'scheduled',
'successful',
'data',
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'scheduled' => 'boolean',
'successful' => 'boolean',
'data' => 'array',
'created_at' => 'datetime',
];
/**
* Event mapping for the model.
*
* @var array
*/
protected $dispatchesEvents = [
'created' => ResultCreated::class,
];
/**
* The tag attributes to be passed to influxdb
*/
public function formatTagsForInfluxDB2(): array
{
return [
'server_id' => (int) $this->server_id,
'server_host' => $this->server_host,
'server_name' => $this->server_name,
];
}
/**
* The attributes to be passed to influxdb
*/
public function formatForInfluxDB2()
{
$data = json_decode($this->data, true);
return [
'id' => (int) $this->id,
'ping' => (float) $this->ping,
'download' => (int) $this->download,
'upload' => (int) $this->upload,
'download_bits' => (int) $this->download * 8,
'upload_bits' => (int) $this->upload * 8,
'ping_jitter' => (float) $data['ping']['jitter'] ?? null,
'download_jitter' => (float) $data['download']['latency']['jitter'] ?? null,
'upload_jitter' => (float) $data['upload']['latency']['jitter'] ?? null,
'server_id' => (int) $this->server_id,
'server_host' => $this->server_host,
'server_name' => $this->server_name,
'scheduled' => $this->scheduled,
'packet_loss' => array_key_exists('packetLoss', $data) ? (float) $data['packetLoss'] : null, // optional, because apparently the cli doesn't always have this metric
];
}
public function getJitterData(): array
{
$data = json_decode($this->data, true);
return [
'download' => $data['download']['latency']['jitter'] ?? null,
'upload' => $data['upload']['latency']['jitter'] ?? null,
'ping' => $data['ping']['jitter'] ?? null,
];
}
/**
* Return the previous test result.
*/
public function previous(): ?self
{
return static::orderByDesc('id')
->where('id', '<', $this->id)
->first();
}
}