feat(admin): add instance wide dashboard with storage and bandwidth usage

* add DashboardCard component
* add instance wide podcasts and episodes numbers
* add app.storageLimit environment variable
* divide bytes by 1000 instead of 1024 in stats sql queries

closes #216
This commit is contained in:
Yassine Doghri 2022-07-06 15:29:15 +00:00
commit b1a6c02e56
57 changed files with 394 additions and 139 deletions

View file

@ -12,6 +12,8 @@ declare(strict_types=1);
namespace Modules\Analytics\Models;
use App\Entities\Media\BaseMedia;
use App\Models\MediaModel;
use CodeIgniter\Model;
use Modules\Analytics\Entities\AnalyticsPodcasts;
@ -93,7 +95,7 @@ class AnalyticsPodcastModel extends Model
public function getDataBandwidthByDay(int $podcastId): array
{
if (! ($found = cache("{$podcastId}_analytics_podcast_by_bandwidth"))) {
$found = $this->select('date as labels, round(bandwidth / 1048576, 1) as `values`')
$found = $this->select('date as labels, ROUND(bandwidth / 1000000, 2) as `values`')
->where([
'podcast_id' => $podcastId,
'date >' => date('Y-m-d', strtotime('-60 days')),
@ -235,4 +237,48 @@ class AnalyticsPodcastModel extends Model
return $found;
}
/**
* Gets total bandwidth data for instance
*
* @return AnalyticsPodcasts[]
*/
public function getDataTotalBandwidthByMonth(): array
{
if (! ($found = cache('analytics_total_bandwidth_by_month'))) {
$found = $this->select(
'DATE_FORMAT(updated_at,"%Y-%m") as labels, ROUND(sum(bandwidth) / 1000000, 2) as `values`'
)
->groupBy('labels')
->orderBy('labels', 'ASC')
->findAll();
cache()
->save('analytics_total_bandwidth_by_month', $found, 600);
}
return $found;
}
/**
* Get total storage
*
* @return BaseMedia[]
*/
public function getDataTotalStorageByMonth(): array
{
if (! ($found = cache('analytics_total_storage_by_month'))) {
$found = (new MediaModel())->select(
'DATE_FORMAT(uploaded_at,"%Y-%m") as labels, ROUND(sum(file_size) / 1000000, 2) as `values`'
)
->groupBy('labels')
->orderBy('labels', 'ASC')
->findAll();
cache()
->save('analytics_total_storage_by_month', $found, 600);
}
return $found;
}
}