Skip to content

Commit 411be52

Browse files
authored
Merge pull request #146 from fleetbase/dev-v1.6.7
v1.6.7 ~ telemetry tweak patch
2 parents 5e267dc + 813ef2b commit 411be52

File tree

4 files changed

+55
-36
lines changed

4 files changed

+55
-36
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fleetbase/core-api",
3-
"version": "1.6.6",
3+
"version": "1.6.7",
44
"description": "Core Framework and Resources for Fleetbase API",
55
"keywords": [
66
"fleetbase",

src/Http/Controllers/Controller.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ public function hello(Request $request)
2121
{
2222
return response()->json(
2323
[
24-
'message' => 'Fleetbase API',
25-
'version' => config('fleetbase.api.version'),
24+
'message' => 'Fleetbase API',
25+
'version' => config('fleetbase.api.version'),
2626
'fleetbase' => config('fleetbase.version'),
27-
'ms' => microtime(true) - $request->attributes->get('request_start_time'),
27+
'ms' => microtime(true) - $request->attributes->get('request_start_time'),
2828
]
2929
);
3030
}

src/Support/Auth.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public static function getCompany(string|array $select = '*'): ?Company
184184
}
185185

186186
if (!$company) {
187-
$companyId = request()->or(['company', 'company_uuid']);
187+
$companyId = request()->input('company') ?? request()->input('company_uuid');
188188
if ($companyId) {
189189
$company = Company::select($select)->where(function ($query) use ($companyId) {
190190
$query->where('uuid', $companyId);

src/Support/Telemetry.php

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Fleetbase\Support;
44

5+
use Fleetbase\Models\Company;
56
use Illuminate\Support\Facades\Cache;
67
use Illuminate\Support\Facades\DB;
78
use Illuminate\Support\Facades\File;
@@ -44,38 +45,39 @@ public static function send(array $payload = []): bool
4445

4546
try {
4647
$ipinfo = self::getIpInfo();
48+
$tags = [
49+
'fleetbase.instance_id:' . self::getInstanceId(),
50+
'fleetbase.company:' . self::getCompanyName(),
51+
'fleetbase.version:' . self::getVersion(),
52+
'fleetbase.domain:' . request()->getHost(),
53+
'fleetbase.api:' . config('app.url'),
54+
'fleetbase.console:' . config('fleetbase.console.host'),
55+
'fleetbase.app_name:' . config('app.name'),
56+
'fleetbase.version:' . config('fleetbase.version'),
57+
'php.version:' . PHP_VERSION,
58+
'laravel.version:' . app()->version(),
59+
'env:' . app()->environment(),
60+
'timezone:' . data_get($ipinfo, 'time_zone.name', 'unknown'),
61+
'region:' . data_get($ipinfo, 'region', 'unknown'),
62+
'country:' . data_get($ipinfo, 'country_name', 'unknown'),
63+
'country_code:' . data_get($ipinfo, 'country_code', 'unknown'),
64+
'installation_type:' . self::getInstallationType(),
65+
'users.count:' . self::countUsers(),
66+
'companies.count:' . self::countCompanies(),
67+
'orders.count:' . self::countOrders(),
68+
'source.modified:' . (self::isSourceModified() ? 'true' : 'false'),
69+
'source.commit_hash:' . self::getCurrentCommitHash(),
70+
'source.main_hash:' . self::getOfficialRepoCommitHash(),
71+
];
4772

4873
$defaultPayload = [
4974
'title' => 'Fleetbase Instance Telemetry',
5075
'text' => 'Periodic instance telemetry from Fleetbase',
51-
'tags' => [
52-
'fleetbase.instance_id:' . self::getInstanceId(),
53-
'fleetbase.company:' . self::getCompanyName(),
54-
'fleetbase.version:' . self::getVersion(),
55-
'fleetbase.domain:' . request()->getHost(),
56-
'fleetbase.api:' . config('app.url'),
57-
'fleetbase.console:' . config('fleetbase.console.host'),
58-
'fleetbase.app_name:' . config('app.name'),
59-
'fleetbase.version:' . config('fleetbase.version'),
60-
'php.version:' . PHP_VERSION,
61-
'laravel.version:' . app()->version(),
62-
'env:' . app()->environment(),
63-
'timezone:' . data_get($ipinfo, 'time_zone.name', 'unknown'),
64-
'region:' . data_get($ipinfo, 'region', 'unknown'),
65-
'country:' . data_get($ipinfo, 'country_name', 'unknown'),
66-
'country_code:' . data_get($ipinfo, 'country_code', 'unknown'),
67-
'installation_type:' . self::getInstallationType(),
68-
'users.count:' . self::countUsers(),
69-
'companies.count:' . self::countCompanies(),
70-
'source.modified:' . (self::isSourceModified() ? 'true' : 'false'),
71-
'source.commit_hash:' . self::getCurrentCommitHash(),
72-
'source.main_hash:' . self::getOfficialRepoCommitHash(),
73-
],
76+
'tags' => $tags,
7477
'alert_type' => 'info',
7578
];
7679

7780
$response = Http::timeout(5)->post(self::$endpoint, array_merge($defaultPayload, $payload));
78-
7981
if ($response->successful()) {
8082
return true;
8183
}
@@ -100,14 +102,19 @@ public static function send(array $payload = []): bool
100102
*/
101103
public static function ping(): void
102104
{
103-
if (app()->environment('production')) {
104-
$lastPing = Cache::get('telemetry:last_ping');
105-
106-
if (!$lastPing || now()->diffInHours($lastPing) >= 24) {
107-
static::send();
108-
Cache::put('telemetry:last_ping', now(), now()->addHours(25));
105+
Cache::remember(
106+
'telemetry:last_ping',
107+
now()->addHours(24),
108+
function () {
109+
try {
110+
static::send();
111+
} catch (\Throwable $e) {
112+
Log::warning('[Telemetry] Send failed: ' . $e->getMessage());
113+
}
114+
115+
return now()->toDateTimeString();
109116
}
110-
}
117+
);
111118
}
112119

113120
/**
@@ -124,6 +131,10 @@ protected static function getInstanceId(): string
124131
protected static function getCompanyName(): string
125132
{
126133
$company = Auth::getCompany();
134+
if (!$company) {
135+
// default to first company
136+
$company = Company::first();
137+
}
127138

128139
return $company ? $company->name : 'unknown';
129140
}
@@ -184,6 +195,14 @@ public static function countCompanies(): int
184195
return DB::table('companies')->count();
185196
}
186197

198+
/**
199+
* Count the number of orders in the system.
200+
*/
201+
public static function countOrders(): int
202+
{
203+
return DB::table('orders')->count();
204+
}
205+
187206
/**
188207
* Check if the source code has been modified from the expected Git commit hash.
189208
*/

0 commit comments

Comments
 (0)