Heimdall-Apps/Immich/Immich.php
Yankel Scialom 5b0cda547c
Immich: display large numbers with suffixes (#839)
* Immich: display large numbers with suffixes

Because an app plate is quite small, displaying large numbers
can be problematic as they collide and cannot be distinguished.

This commit fixes this issue by displaying large numbers using
fewer characters and ISO suffixes, up to G (giga, 10^9).

Here is an example of the problematic behaviour:
+-----------------------+
| PHOTOS  VIDEOS  USAGE |
|   14398443 0.94GiB    |
+-----------------------+
The 14,398 photo count collides with the 443 video count.

This commit changes the display to:
+-----------------------+
| PHOTOS  VIDEOS  USAGE |
|   15k    443  0.94GiB |
+-----------------------+

* fixup! Immich: display large numbers with suffixes
2025-07-22 22:48:12 +02:00

73 lines
2.1 KiB
PHP

<?php
namespace App\SupportedApps\Immich;
class Immich extends \App\SupportedApps implements \App\EnhancedApps
{
public $config;
//protected $login_first = true; // Uncomment if api requests need to be authed first
//protected $method = 'POST'; // Uncomment if requests to the API should be set by POST
public function __construct()
{
//$this->jar = new \GuzzleHttp\Cookie\CookieJar; // Uncomment if cookies need to be set
}
public function test()
{
$attrs = [
"headers" => [
"Accept" => "application/json",
"x-api-key" => $this->config->api_key,
],
];
$test = parent::appTest($this->url("server/statistics"), $attrs);
echo $test->status;
}
private function formatLargeNumber($number)
{
$suffixes = [ "", "k", "M", "G" ];
$rank = 0;
while (abs($number) > 1000 && $rank < count($suffixes)) {
$number /= 1000;
$rank++;
}
$decimals = $number < 10 && $rank > 0 ? 1 : 0;
return number_format($number, $decimals) . $suffixes[$rank];
}
public function livestats()
{
$status = "inactive";
$attrs = [
"headers" => [
"Accept" => "application/json",
"x-api-key" => $this->config->api_key,
],
];
$res = parent::execute($this->url("server/statistics"), $attrs);
$details = json_decode($res->getBody());
$data = [];
if ($details) {
$status = "active";
$data["photos"] = $this->formatLargeNumber($details->photos);
$data["videos"] = $this->formatLargeNumber($details->videos);
$usageInGiB = number_format($details->usage / 1073741824, 2);
$data["usage"] = $usageInGiB . 'GiB';
}
return parent::getLiveStats($status, $data);
}
public function url($endpoint)
{
$api_url = parent::normaliseurl($this->config->url) .
"api/" .
$endpoint;
return $api_url;
}
}