package database import ( "math/rand" "time" "git.phc.dm.unipi.it/phc/cluster-dashboard/backend/executor" "git.phc.dm.unipi.it/phc/cluster-dashboard/backend/model" ) // simpleDB è una implementazione di [database.Database] che tiene giusto una cache in memoria e // quando il server viene riavviato perde tutte le statistiche che ha accumulato. Più avanti si // potrebbe pensare di scrivere queste informazioni in un file o usare un vero database. type simpleDB struct { Executor executor.Service // lastUpdate tiene traccia di quando abbiamo aggiornato l'ultima volta tutti i dati lastUpdate *time.Time // Nodes is a map from hostname to node info nodes map[string]*model.Node // Jobs is a map from job id to job info jobs map[string]*model.Job // The following are maps from hostname to a list of sampled temperatures, used memory, used storage space and network upload and download rate. temperatureSamples map[string][]model.Sample[float64] memorySamples map[string][]model.Sample[int64] storageSamples map[string][]model.Sample[int64] networkUploadSamples map[string][]model.Sample[int64] networkDownloadSamples map[string][]model.Sample[int64] } func NewSimpleDatabase(ex executor.Service) Database { return &simpleDB{Executor: ex} } func (s *simpleDB) GetNode(hostname string) (*model.Node, error) { return &model.Node{ Hostname: hostname, StartTime: time.Now().Add(-1 * time.Hour), }, nil } func (s *simpleDB) GetJob(id string) (*model.Job, error) { return &model.Job{ Id: id, Name: "example-job", Status: "active", Nodes: []string{"node-1", "node-2"}, Resources: []string{}, }, nil } func (s *simpleDB) AllNodes() ([]*model.Node, error) { return []*model.Node{ { Hostname: "node-1", StartTime: time.Now().Add(-1 * time.Hour), }, { Hostname: "node-2", StartTime: time.Now().Add(-1 * time.Hour), }, { Hostname: "node-3", StartTime: time.Now().Add(-1 * time.Hour), }, }, nil } func (s *simpleDB) AllJobs() ([]*model.Job, error) { return []*model.Job{ { Id: "job-1", Name: "example-job", Status: "active", Nodes: []string{"node-1", "node-2"}, Resources: []string{}, }, { Id: "job-2", Name: "example-job", Status: "active", Nodes: []string{"node-1", "node-2"}, Resources: []string{}, }, { Id: "job-3", Name: "example-job", Status: "active", Nodes: []string{"node-1", "node-2"}, Resources: []string{}, }, { Id: "job-4", Name: "example-job", Status: "active", Nodes: []string{"node-1", "node-2"}, Resources: []string{}, }, { Id: "job-5", Name: "example-job", Status: "active", Nodes: []string{"node-1", "node-2"}, Resources: []string{}, }, }, nil } func (s *simpleDB) QueryTemperatureSamples(from, to time.Time) ([]model.Sample[float64], error) { return generateFakeFloat64Samples(100), nil } func (s *simpleDB) QueryMemorySamples(from, to time.Time) ([]model.Sample[int64], error) { return generateFakeInt64Samples(100), nil } func (s *simpleDB) QueryStorageSamples(from, to time.Time) ([]model.Sample[int64], error) { return generateFakeInt64Samples(100), nil } func (s *simpleDB) QueryNetworkUploadSamples(from, to time.Time) ([]model.Sample[int64], error) { return generateFakeInt64Samples(100), nil } func (s *simpleDB) QueryNetworkDownloadSamples(from, to time.Time) ([]model.Sample[int64], error) { return generateFakeInt64Samples(100), nil } func generateFakeFloat64Samples(amount int) []model.Sample[float64] { fakeSamples := make([]model.Sample[float64], amount) for i := range fakeSamples { fakeSamples[i] = model.Sample[float64]{ Timestamp: time.Now().Add(time.Duration(i-amount) * time.Second), Value: rand.Float64(), } } return fakeSamples } func generateFakeInt64Samples(amount int) []model.Sample[int64] { fakeSamples := make([]model.Sample[int64], amount) for i := range fakeSamples { fakeSamples[i] = model.Sample[int64]{ Timestamp: time.Now().Add(time.Duration(i-amount) * time.Second), Value: rand.Int63n(1000), } } return fakeSamples }