22
33namespace Fleetbase \Support ;
44
5+ use Fleetbase \Models \Company ;
56use Illuminate \Support \Facades \Cache ;
67use Illuminate \Support \Facades \DB ;
78use 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