|
|
@ -2,8 +2,8 @@ package namespace
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"caj-larsson/bog/util"
|
|
|
|
"caj-larsson/bog/util"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type Clock interface {
|
|
|
|
type Clock interface {
|
|
|
@ -11,17 +11,16 @@ type Clock interface {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type NamespaceService struct {
|
|
|
|
type NamespaceService struct {
|
|
|
|
repo Repository
|
|
|
|
repo Repository
|
|
|
|
outboxes []func(util.Event)
|
|
|
|
outboxes []func(util.Event)
|
|
|
|
logger util.Logger
|
|
|
|
logger util.Logger
|
|
|
|
clock Clock
|
|
|
|
clock Clock
|
|
|
|
default_ttl time.Duration
|
|
|
|
default_ttl time.Duration
|
|
|
|
default_quota_bytes int64
|
|
|
|
default_quota_bytes int64
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func NewNamespaceService(repo Repository, logger util.Logger, clock Clock, default_ttl time.Duration, default_quota_bytes int64) *NamespaceService {
|
|
|
|
func NewNamespaceService(repo Repository, logger util.Logger, clock Clock, default_ttl time.Duration, default_quota_bytes int64) *NamespaceService {
|
|
|
|
return &NamespaceService {
|
|
|
|
return &NamespaceService{
|
|
|
|
repo,
|
|
|
|
repo,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
logger,
|
|
|
|
logger,
|
|
|
@ -31,7 +30,6 @@ func NewNamespaceService(repo Repository, logger util.Logger, clock Clock, defau
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *NamespaceService) GetOrCreateNs(name string) *Namespace {
|
|
|
|
func (s *NamespaceService) GetOrCreateNs(name string) *Namespace {
|
|
|
|
ns, err := s.repo.GetByName(name)
|
|
|
|
ns, err := s.repo.GetByName(name)
|
|
|
|
|
|
|
|
|
|
|
@ -60,8 +58,8 @@ func (s *NamespaceService) GetOrCreateNs(name string) *Namespace {
|
|
|
|
return ns
|
|
|
|
return ns
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *NamespaceService) Wire(reg func(string, util.EventHandler), outbox func(ev util.Event)) {
|
|
|
|
func (s *NamespaceService) Wire(reg func(string, util.EventHandler), outbox func(ev util.Event)) { reg("FileUsed", s.handleFileUsed)
|
|
|
|
reg("FileUsed", s.handleFileUsed)
|
|
|
|
s.outboxes = append(s.outboxes, outbox)
|
|
|
|
s.outboxes = append(s.outboxes, outbox)
|
|
|
|
|
|
|
|
|
|
|
|
reg("FileUsed", s.handleFileUsed)
|
|
|
|
reg("FileUsed", s.handleFileUsed)
|
|
|
@ -69,8 +67,7 @@ func (s *NamespaceService) Wire(reg func(string, util.EventHandler), outbox func
|
|
|
|
reg("FileRecieved", s.handleFileRecieved)
|
|
|
|
reg("FileRecieved", s.handleFileRecieved)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *NamespaceService) All() []Namespace {
|
|
|
|
func (s *NamespaceService) All() ([]Namespace) {
|
|
|
|
|
|
|
|
nss, err := s.repo.All()
|
|
|
|
nss, err := s.repo.All()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
panic(err)
|
|
|
@ -79,22 +76,22 @@ func (s *NamespaceService) All() ([]Namespace) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *NamespaceService) handleFileUsed(payload interface{}) {
|
|
|
|
func (s *NamespaceService) handleFileUsed(payload interface{}) {
|
|
|
|
var payload_s = payload.(struct{
|
|
|
|
var payload_s = payload.(struct {
|
|
|
|
Name string
|
|
|
|
Name string
|
|
|
|
Size int64
|
|
|
|
Size int64
|
|
|
|
})
|
|
|
|
})
|
|
|
|
fmt.Printf("file used %v\n", payload_s )
|
|
|
|
fmt.Printf("file used %v\n", payload_s)
|
|
|
|
ns := s.GetOrCreateNs(payload_s.Name)
|
|
|
|
ns := s.GetOrCreateNs(payload_s.Name)
|
|
|
|
ns.FileQuota = ns.FileQuota.Add(payload_s.Size)
|
|
|
|
ns.FileQuota = ns.FileQuota.Add(payload_s.Size)
|
|
|
|
s.repo.Update(ns.ID, *ns)
|
|
|
|
s.repo.Update(ns.ID, *ns)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *NamespaceService) handleFileDeleted(payload interface{}) {
|
|
|
|
func (s *NamespaceService) handleFileDeleted(payload interface{}) {
|
|
|
|
var payload_s = payload.(struct{
|
|
|
|
var payload_s = payload.(struct {
|
|
|
|
Name string
|
|
|
|
Name string
|
|
|
|
Size int64
|
|
|
|
Size int64
|
|
|
|
})
|
|
|
|
})
|
|
|
|
fmt.Printf("file deleted %v\n", payload_s )
|
|
|
|
fmt.Printf("file deleted %v\n", payload_s)
|
|
|
|
ns := s.GetOrCreateNs(payload_s.Name)
|
|
|
|
ns := s.GetOrCreateNs(payload_s.Name)
|
|
|
|
ns.FileQuota = ns.FileQuota.Add(-payload_s.Size)
|
|
|
|
ns.FileQuota = ns.FileQuota.Add(-payload_s.Size)
|
|
|
|
fmt.Printf("file usage %v\n", ns.FileQuota)
|
|
|
|
fmt.Printf("file usage %v\n", ns.FileQuota)
|
|
|
@ -102,11 +99,11 @@ func (s *NamespaceService) handleFileDeleted(payload interface{}) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *NamespaceService) handleFileRecieved(payload interface{}) {
|
|
|
|
func (s *NamespaceService) handleFileRecieved(payload interface{}) {
|
|
|
|
var payload_s = payload.(struct{
|
|
|
|
var payload_s = payload.(struct {
|
|
|
|
Name string
|
|
|
|
Name string
|
|
|
|
Size int64
|
|
|
|
Size int64
|
|
|
|
})
|
|
|
|
})
|
|
|
|
fmt.Printf("file recieved %v\n", payload_s )
|
|
|
|
fmt.Printf("file recieved %v\n", payload_s)
|
|
|
|
ns := s.GetOrCreateNs(payload_s.Name)
|
|
|
|
ns := s.GetOrCreateNs(payload_s.Name)
|
|
|
|
ns.FileQuota = ns.FileQuota.Add(payload_s.Size)
|
|
|
|
ns.FileQuota = ns.FileQuota.Add(payload_s.Size)
|
|
|
|
fmt.Printf("file usage %v\n", ns.FileQuota)
|
|
|
|
fmt.Printf("file usage %v\n", ns.FileQuota)
|
|
|
|