From ab91ca49c934237da16dc31c5752a6fe624db56b Mon Sep 17 00:00:00 2001 From: Akhil Gupta Date: Wed, 10 Mar 2021 11:22:41 +0530 Subject: [PATCH] added size report --- client/index.html | 27 ++++++++++++++++++++++++--- client/settings.html | 19 +++++++++++++++++-- controllers/pages.go | 6 ++++-- db/dbfunctions.go | 21 ++++++++++++++++++++- db/podcast.go | 19 +++++++++++++++++++ main.go | 21 +++++++++++++++++++++ service/podcastService.go | 18 +++++++++++++----- 7 files changed, 118 insertions(+), 13 deletions(-) diff --git a/client/index.html b/client/index.html index 120b3fd..5e511e7 100644 --- a/client/index.html +++ b/client/index.html @@ -520,12 +520,33 @@ } this.podcasts.sort(compareFunction) }, + + getReadableSize(bytes){ + if(bytes<1024){ + return `${bytes} bytes`; + } + bytes=bytes/1024 + if(bytes<1024){ + return `${bytes.toFixed(2)} KB`; + } + bytes=bytes/1024 + if(bytes<1024){ + return `${bytes.toFixed(2)} MB`; + } + bytes=bytes/1024 + if(bytes<1024){ + return `${bytes.toFixed(2)} GB`; + } + bytes=bytes/1024 + return `${bytes.toFixed(2)} TB`; + + }, getEpisodeCountTooltip(podcast){ - var title=`${podcast.DownloadedEpisodesCount} episodes downloaded out of total ${podcast.AllEpisodesCount} episodes` + var title=`${podcast.DownloadedEpisodesCount} episodes (${this.getReadableSize(podcast.DownloadedEpisodesSize)}) downloaded out of total ${podcast.AllEpisodesCount} episodes (${this.getReadableSize(podcast.AllEpisodesSize)})` if(podcast.DownloadingEpisodesCount){ - title+= '\n'+podcast.DownloadingEpisodesCount+' episodes in the queue.' + title+= '\n'+podcast.DownloadingEpisodesCount+' episodes ('+ this.getReadableSize(podcast.DownloadingEpisodesSize)+') in the queue.' - } + } return title }, getFormattedLastEpisodeDate(podcast){ diff --git a/client/settings.html b/client/settings.html index d4fc67a..8c182f7 100644 --- a/client/settings.html +++ b/client/settings.html @@ -42,7 +42,7 @@
-
+

Settings

@@ -91,6 +91,21 @@
+
+
+

Disk Stats

+ + + + + + + + + +
Disk Used{{formatFileSize .diskStats.Downloaded}}
Pending Download{{ formatFileSize .diskStats.PendingDownload }}
+
+

@@ -113,7 +128,7 @@ Request here - Support development + Support the developer Support here diff --git a/controllers/pages.go b/controllers/pages.go index f22bb4a..a1750e6 100644 --- a/controllers/pages.go +++ b/controllers/pages.go @@ -183,9 +183,11 @@ func PlayerPage(c *gin.Context) { func SettingsPage(c *gin.Context) { setting := c.MustGet("setting").(*db.Setting) + diskStats, _ := db.GetPodcastEpisodeDiskStats() c.HTML(http.StatusOK, "settings.html", gin.H{ - "setting": setting, - "title": "Update your preferences", + "setting": setting, + "title": "Update your preferences", + "diskStats": diskStats, }) } diff --git a/db/dbfunctions.go b/db/dbfunctions.go index dfd9b91..b47c5cb 100644 --- a/db/dbfunctions.go +++ b/db/dbfunctions.go @@ -146,10 +146,29 @@ func GetAllPodcastItemsAlreadyDownloaded() (*[]PodcastItem, error) { func GetPodcastEpisodeStats() (*[]PodcastItemStatsModel, error) { var stats []PodcastItemStatsModel - result := DB.Model(&PodcastItem{}).Select("download_status,podcast_id, count(1) as count").Group("podcast_id,download_status").Find(&stats) + result := DB.Model(&PodcastItem{}).Select("download_status,podcast_id, count(1) as count,sum(file_size) as size").Group("podcast_id,download_status").Find(&stats) return &stats, result.Error } +func GetPodcastEpisodeDiskStats() (PodcastItemConsolidateDiskStatsModel, error) { + var stats []PodcastItemDiskStatsModel + result := DB.Model(&PodcastItem{}).Select("download_status,count(1) as count,sum(file_size) as size").Group("download_status").Find(&stats) + dict := make(map[DownloadStatus]int64) + for _, stat := range stats { + dict[stat.DownloadStatus] = stat.Size + } + + toReturn := PodcastItemConsolidateDiskStatsModel{ + Downloaded: dict[Downloaded], + Downloading: dict[Downloading], + Deleted: dict[Deleted], + NotDownloaded: dict[NotDownloaded], + PendingDownload: dict[NotDownloaded] + dict[Downloading], + } + + return toReturn, result.Error +} + func GetEpisodeNumber(podcastItemId, podcastId string) (int, error) { var id string var sequence int diff --git a/db/podcast.go b/db/podcast.go index 98fc5ec..ef84f15 100644 --- a/db/podcast.go +++ b/db/podcast.go @@ -26,6 +26,10 @@ type Podcast struct { DownloadedEpisodesCount int `gorm:"-"` DownloadingEpisodesCount int `gorm:"-"` AllEpisodesCount int `gorm:"-"` + + DownloadedEpisodesSize int64 `gorm:"-"` + DownloadingEpisodesSize int64 `gorm:"-"` + AllEpisodesSize int64 `gorm:"-"` } //PodcastItem is @@ -107,4 +111,19 @@ type PodcastItemStatsModel struct { PodcastID string DownloadStatus DownloadStatus Count int + Size int64 +} + +type PodcastItemDiskStatsModel struct { + DownloadStatus DownloadStatus + Count int + Size int64 +} + +type PodcastItemConsolidateDiskStatsModel struct { + Downloaded int64 + Downloading int64 + NotDownloaded int64 + Deleted int64 + PendingDownload int64 } diff --git a/main.go b/main.go index 2a7bb0f..ee28239 100644 --- a/main.go +++ b/main.go @@ -90,6 +90,27 @@ func main() { } return count }, + "formatFileSize": func(inputSize int64) string { + size := float64(inputSize) + const divisor float64 = 1024 + if size < divisor { + return fmt.Sprintf("%.0f bytes", size) + } + size = size / divisor + if size < divisor { + return fmt.Sprintf("%.2f KB", size) + } + size = size / divisor + if size < divisor { + return fmt.Sprintf("%.2f MB", size) + } + size = size / divisor + if size < divisor { + return fmt.Sprintf("%.2f GB", size) + } + size = size / divisor + return fmt.Sprintf("%.2f TB", size) + }, "formatDuration": func(total int) string { if total <= 0 { return "" diff --git a/service/podcastService.go b/service/podcastService.go index b878f2a..8c83c25 100644 --- a/service/podcastService.go +++ b/service/podcastService.go @@ -82,15 +82,23 @@ func GetAllPodcasts(sorting string) *[]db.Podcast { PodcastID string DownloadStatus db.DownloadStatus } - statsMap := make(map[Key]int) + countMap := make(map[Key]int) + sizeMap := make(map[Key]int64) for _, stat := range *stats { - statsMap[Key{stat.PodcastID, stat.DownloadStatus}] = stat.Count + countMap[Key{stat.PodcastID, stat.DownloadStatus}] = stat.Count + sizeMap[Key{stat.PodcastID, stat.DownloadStatus}] = stat.Size + } var toReturn []db.Podcast for _, podcast := range podcasts { - podcast.DownloadedEpisodesCount = statsMap[Key{podcast.ID, db.Downloaded}] - podcast.DownloadingEpisodesCount = statsMap[Key{podcast.ID, db.NotDownloaded}] - podcast.AllEpisodesCount = podcast.DownloadedEpisodesCount + podcast.DownloadingEpisodesCount + statsMap[Key{podcast.ID, db.Deleted}] + podcast.DownloadedEpisodesCount = countMap[Key{podcast.ID, db.Downloaded}] + podcast.DownloadingEpisodesCount = countMap[Key{podcast.ID, db.NotDownloaded}] + podcast.AllEpisodesCount = podcast.DownloadedEpisodesCount + podcast.DownloadingEpisodesCount + countMap[Key{podcast.ID, db.Deleted}] + + podcast.DownloadedEpisodesSize = sizeMap[Key{podcast.ID, db.Downloaded}] + podcast.DownloadingEpisodesSize = sizeMap[Key{podcast.ID, db.NotDownloaded}] + podcast.AllEpisodesSize = podcast.DownloadedEpisodesSize + podcast.DownloadingEpisodesSize + sizeMap[Key{podcast.ID, db.Deleted}] + toReturn = append(toReturn, podcast) } return &toReturn