clean up of the codebase
This commit is contained in:
1 parent
7822529a38
commit
0ac61b4ece
52 files changed
+2545
-2274
No files matched your search
@@ -0,0 +1,74 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Size struct {
|
||||
MemoryMB int `toml:"memory_mb"`
|
||||
CPUs float64 `toml:"cpus"`
|
||||
}
|
||||
|
||||
func (s Size) Add(o Size) Size { return Size{s.MemoryMB + o.MemoryMB, s.CPUs + o.CPUs} }
|
||||
func (s Size) Sub(o Size) Size { return Size{s.MemoryMB - o.MemoryMB, s.CPUs - o.CPUs} }
|
||||
func (s Size) Fits(in Size) bool {
|
||||
return s.MemoryMB <= in.MemoryMB && s.CPUs <= in.CPUs+1e-9
|
||||
}
|
||||
func (s Size) String() string {
|
||||
mem := fmt.Sprintf("%d MB", s.MemoryMB)
|
||||
if s.MemoryMB >= 1024 && s.MemoryMB%1024 == 0 {
|
||||
mem = fmt.Sprintf("%d GB", s.MemoryMB/1024)
|
||||
}
|
||||
cpu := strconv.FormatFloat(s.CPUs, 'f', -1, 64) + " CPUs"
|
||||
if s.CPUs == 1 {
|
||||
cpu = "1 CPU"
|
||||
}
|
||||
return mem + " RAM, " + cpu
|
||||
}
|
||||
|
||||
type OwnerKind string
|
||||
|
||||
const (
|
||||
OwnerUser OwnerKind = "user"
|
||||
OwnerChannel OwnerKind = "channel"
|
||||
)
|
||||
|
||||
type Owner struct {
|
||||
ID string `json:"id"`
|
||||
Kind OwnerKind `json:"kind"`
|
||||
Machine string `json:"machine"`
|
||||
Secret string `json:"secret"`
|
||||
Size Size `json:"size"`
|
||||
LastActive time.Time `json:"last_active"`
|
||||
|
||||
Node string `json:"node,omitempty"`
|
||||
Drive string `json:"drive,omitempty"`
|
||||
StorageMinMB int64 `json:"storage_min_mb"`
|
||||
StorageMaxMB int64 `json:"storage_max_mb"`
|
||||
|
||||
// 0 means the config default.
|
||||
IdleTimeout time.Duration `json:"idle_timeout,omitempty"`
|
||||
}
|
||||
|
||||
const Indefinite time.Duration = -1
|
||||
|
||||
type Session struct {
|
||||
Channel string `json:"channel"`
|
||||
ThreadTS string `json:"thread_ts"`
|
||||
OwnerID string `json:"owner_id"`
|
||||
ACPID string `json:"acp_id"`
|
||||
Title string `json:"title"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
func MachineName(id string, kind OwnerKind) string {
|
||||
if kind == OwnerChannel {
|
||||
return "c-" + strings.ToLower(id)
|
||||
}
|
||||
return "u-" + strings.ToLower(id)
|
||||
}
|
||||
|
||||
func IsOurs(name string) bool { return strings.HasPrefix(name, "u-") || strings.HasPrefix(name, "c-") }
|
||||
Reference in new issue
Block a user