Files
slacker-bot/slackerbot/internal/model/model.go
T

87 lines
2.2 KiB
Go

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"`
// Empty means the channel's default.
Listen ListenMode `json:"listen,omitempty"`
}
// Whether the bot reads every reply in a channel thread, or only ones that
// @mention it. DMs always read everything.
type ListenMode string
const (
ListenMentions ListenMode = "only-mentions"
ListenAll ListenMode = "all-messages"
)
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-") }