aboutsummaryrefslogtreecommitdiff
path: root/quotas.go
diff options
context:
space:
mode:
authorQuentin Dufour <quentin@deuxfleurs.fr>2023-09-25 22:00:46 +0200
committerQuentin Dufour <quentin@deuxfleurs.fr>2023-09-25 22:00:46 +0200
commit08287375736a0a57c03b4bd6d9c222dc5567db6e (patch)
treeb6426ca7127cc65484034542790b75773e5282f5 /quotas.go
parentbc368943a4c0853718b8a53b1caadc297412ef32 (diff)
downloadguichet-08287375736a0a57c03b4bd6d9c222dc5567db6e.tar.gz
guichet-08287375736a0a57c03b4bd6d9c222dc5567db6e.zip
patch and delete with quota are now implemented
Diffstat (limited to 'quotas.go')
-rw-r--r--quotas.go71
1 files changed, 71 insertions, 0 deletions
diff --git a/quotas.go b/quotas.go
index 3eec9b8..9a2e426 100644
--- a/quotas.go
+++ b/quotas.go
@@ -77,3 +77,74 @@ func (q *UserQuota) DefaultWebsiteQuota() *garage.UpdateBucketRequestQuotas {
return qr
}
+
+func (q *UserQuota) WebsiteSizeAdjust(sz int64) int64 {
+ if sz < q.WebsiteSizeDefault {
+ return q.WebsiteSizeDefault
+ } else if sz > q.WebsiteSizeBursted {
+ return q.WebsiteSizeBursted
+ } else {
+ return sz
+ }
+}
+
+func (q *UserQuota) WebsiteObjectAdjust(objs int64) int64 {
+ if objs > q.WebsiteObjects || objs <= 0 {
+ return q.WebsiteObjects
+ } else {
+ return objs
+ }
+}
+
+func (q *UserQuota) WebsiteSizeBurstedPretty() string {
+ return prettyValue(q.WebsiteSizeBursted)
+}
+
+// --- A quota stat we can use
+type QuotaStat struct {
+ Current int64
+ Max int64
+ Ratio float64
+ Burstable bool
+}
+func NewQuotaStat(current, max int64, burstable bool) QuotaStat {
+ return QuotaStat {
+ Current: current,
+ Max: max,
+ Ratio: float64(current) / float64(max),
+ Burstable: burstable,
+ }
+}
+func (q *QuotaStat) IsFull() bool {
+ return q.Current >= q.Max
+}
+func (q *QuotaStat) Percent() int64 {
+ return int64(q.Ratio * 100)
+}
+
+func (q *QuotaStat) PrettyCurrent() string {
+ return prettyValue(q.Current)
+}
+func (q *QuotaStat) PrettyMax() string {
+ return prettyValue(q.Max)
+}
+
+func prettyValue(v int64) string {
+ if v < 1024 {
+ return fmt.Sprintf("%d octets", v)
+ }
+ v = v / 1024
+ if v < 1024 {
+ return fmt.Sprintf("%d kio", v)
+ }
+ v = v / 1024
+ if v < 1024 {
+ return fmt.Sprintf("%d Mio", v)
+ }
+ v = v / 1024
+ if v < 1024 {
+ return fmt.Sprintf("%d Gio", v)
+ }
+ v = v / 1024
+ return fmt.Sprintf("%d Tio", v)
+}