Skip to content

Commit

Permalink
fix: use most recent report rather than one from 14 days ago
Browse files Browse the repository at this point in the history
  • Loading branch information
jimeh committed Oct 29, 2023
1 parent 11bb8dd commit 89ddadc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
28 changes: 21 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c *QuotaCollector) Describe(ch chan<- *prometheus.Desc) {
}

func (c *QuotaCollector) Collect(ch chan<- prometheus.Metric) {
totalQuota, usedQuota, _, err := c.fetchQuotaStats()
_, totalQuota, usedQuota, _, err := c.fetchQuotaStats()
if err != nil {
slog.Error(
"Failed to fetch quota stats",
Expand All @@ -68,11 +68,23 @@ func (c *QuotaCollector) Collect(ch chan<- prometheus.Metric) {
)
}

func (c *QuotaCollector) fetchQuotaStats() (float64, float64, float64, error) {
fourteenDaysAgo := time.Now().AddDate(0, 0, -14).Format("2006-01-02")
resp, err := c.client.CustomerUsageReports.Get(fourteenDaysAgo).Do()
func (c *QuotaCollector) fetchQuotaStats() (
time.Time, float64, float64, float64, error,
) {
var t time.Time
var resp *admin.UsageReports
var err error

for i := -1; i > -6; i-- {
t = time.Now().AddDate(0, 0, i).UTC().Truncate(24 * time.Hour)
date := t.Format("2006-01-02")
resp, err = c.client.CustomerUsageReports.Get(date).Do()
if err == nil {
break
}
}
if err != nil {
return 0, 0, 0, err
return time.Time{}, 0, 0, 0, err
}

var totalQuota float64
Expand All @@ -89,7 +101,7 @@ func (c *QuotaCollector) fetchQuotaStats() (float64, float64, float64, error) {

percentageUsed := (usedQuota / totalQuota) * 100

return totalQuota, usedQuota, percentageUsed, nil
return t, totalQuota, usedQuota, percentageUsed, nil
}

func getClient(config *oauth2.Config) *http.Client {
Expand Down Expand Up @@ -140,14 +152,15 @@ func saveToken(file string, token *oauth2.Token) {
}

type QuotaStats struct {
Date string // in YYYY-MM-DD
TotalQuota string // in TB
UsedQuota string // in TB
PercentageUsed float64 // in percentage
}

func statsPageHanderFunc(collector *QuotaCollector) http.HandlerFunc {
return func(w http.ResponseWriter, _ *http.Request) {
total, used, percentage, err := collector.fetchQuotaStats()
t, total, used, percentage, err := collector.fetchQuotaStats()
if err != nil {
slog.Error(
"Failed to fetch quota stats",
Expand All @@ -161,6 +174,7 @@ func statsPageHanderFunc(collector *QuotaCollector) http.HandlerFunc {
}

stats := QuotaStats{
Date: t.Format("2006-01-02"),
TotalQuota: strconv.FormatFloat(total/1048576, 'f', 3, 64),
UsedQuota: strconv.FormatFloat(used/1048576, 'f', 3, 64),
PercentageUsed: percentage,
Expand Down
9 changes: 6 additions & 3 deletions templates/stats.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@

<body class="bg-transparent text-gray-300 h-screen flex items-center justify-center">
<div class="p-4 rounded-lg w-full max-w-screen-lg mx-auto">
<h1 class="text-lg mb-2 border-b border-gray-700 pb-2 text-gray-100">Workspace Storage</h1>
<div class="flex items-center mt-2 mb-2">
<h1 class="text-lg mb-0 text-gray-100">
Workspace Storage
<span class="text-gray-400 text-sm">({{.Date}})</span>
</h1>
<div class="flex items-center mb-1">
<div class="h-2 bg-gray-700 rounded-full flex-grow mr-2">
<div class="h-full bg-blue-600 rounded-full" style="width:{{printf " %.2f" .PercentageUsed}}%;"></div>
</div>
<span class="text-green-500">{{printf "%.2f" .PercentageUsed}}%</span>
</div>
<div class="flex justify-between mt-2">
<div class="flex justify-between">
<span class="text-red-600">{{.UsedQuota}} TB</span>
<span class="text-blue-600">{{.TotalQuota}} TB</span>
</div>
Expand Down

0 comments on commit 89ddadc

Please sign in to comment.