Skip to content

Commit 9f94f50

Browse files
authored
Beacon Telemetry (#20)
1 parent 9a3066e commit 9f94f50

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

config/cachet.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,6 @@
9797
'major_outage' => 25.0,
9898

9999
'beacon' => env('CACHET_BEACON', true),
100+
101+
'docker' => env('CACHET_DOCKER', false),
100102
];

src/Jobs/SendBeaconJob.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
namespace Cachet\Jobs;
44

5+
use Cachet\Cachet;
6+
use Cachet\Events\Beacon\BeaconSent;
7+
use Cachet\Models\Component;
8+
use Cachet\Models\Incident;
9+
use Cachet\Models\Metric;
10+
use Cachet\Models\Schedule;
11+
use Cachet\Settings\AppSettings;
12+
use Illuminate\Support\Facades\Http;
13+
514
class SendBeaconJob
615
{
716
/**
@@ -11,6 +20,23 @@ class SendBeaconJob
1120
*/
1221
public function handle()
1322
{
14-
//
23+
if (! config('cachet.beacon')) {
24+
return;
25+
}
26+
27+
$request = Http::asJson()->post('https://cachethq.io/beacon', [
28+
'install_id' => app(AppSettings::class)->install_id,
29+
'version' => Cachet::version(),
30+
'docker' => config('cachet.docker'),
31+
'database' => config('database.default'),
32+
'data' => [
33+
'components' => Component::query()->count(),
34+
'incidents' => Incident::query()->count(),
35+
'metrics' => Metric::query()->count(),
36+
'schedules' => Schedule::query()->count(),
37+
],
38+
]);
39+
40+
BeaconSent::dispatchIf($request->successful());
1541
}
1642
}

tests/Unit/Jobs/SendBeaconJobTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
use Cachet\Cachet;
4+
use Cachet\Jobs\SendBeaconJob;
5+
use Cachet\Models\Component;
6+
use Cachet\Models\Incident;
7+
use Cachet\Models\Metric;
8+
use Cachet\Models\Schedule;
9+
use Illuminate\Http\Client\Request;
10+
use Illuminate\Support\Facades\Http;
11+
12+
it('will not send the beacon if disabled', function () {
13+
Http::fake();
14+
15+
config(['cachet.beacon' => false]);
16+
17+
dispatch(new SendBeaconJob());
18+
19+
Http::assertNothingSent();
20+
});
21+
22+
it('sends telemetry data', function () {
23+
Http::fake([
24+
'https://cachethq.io/beacon' => Http::response([]),
25+
]);
26+
27+
config(['cachet.beacon' => true]);
28+
29+
Component::factory()->count(1)->create();
30+
Incident::factory()->count(2)->create();
31+
Metric::factory()->count(3)->create();
32+
Schedule::factory()->count(4)->create();
33+
34+
dispatch(new SendBeaconJob());
35+
36+
Http::assertSent(function (Request $request) {
37+
return $request['version'] === Cachet::version() &&
38+
$request['docker'] === false &&
39+
$request['data']['components'] === 1 &&
40+
$request['data']['incidents'] === 2 &&
41+
$request['data']['metrics'] === 3 &&
42+
$request['data']['schedules'] === 4;
43+
});
44+
Http::assertSentCount(1);
45+
});

0 commit comments

Comments
 (0)