2024-04-11 23:04:30 +02:00
|
|
|
// Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
2022-07-29 06:29:21 +02:00
|
|
|
// All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2021-10-26 10:24:43 +02:00
|
|
|
package metricdata
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-03-17 16:15:35 +01:00
|
|
|
"encoding/json"
|
2021-11-26 10:32:36 +01:00
|
|
|
"fmt"
|
2022-01-17 13:33:35 +01:00
|
|
|
"time"
|
2021-10-26 10:24:43 +02:00
|
|
|
|
2022-06-21 17:52:36 +02:00
|
|
|
"github.com/ClusterCockpit/cc-backend/internal/config"
|
|
|
|
"github.com/ClusterCockpit/cc-backend/pkg/log"
|
|
|
|
"github.com/ClusterCockpit/cc-backend/pkg/schema"
|
2021-10-26 10:24:43 +02:00
|
|
|
)
|
|
|
|
|
2021-12-08 10:14:45 +01:00
|
|
|
type MetricDataRepository interface {
|
2021-12-09 16:25:48 +01:00
|
|
|
// Initialize this MetricDataRepository. One instance of
|
|
|
|
// this interface will only ever be responsible for one cluster.
|
2022-03-17 16:15:35 +01:00
|
|
|
Init(rawConfig json.RawMessage) error
|
2021-12-09 16:25:48 +01:00
|
|
|
|
|
|
|
// Return the JobData for the given job, only with the requested metrics.
|
2022-01-07 09:47:41 +01:00
|
|
|
LoadData(job *schema.Job, metrics []string, scopes []schema.MetricScope, ctx context.Context) (schema.JobData, error)
|
2021-12-09 16:25:48 +01:00
|
|
|
|
2022-01-12 13:03:01 +01:00
|
|
|
// Return a map of metrics to a map of nodes to the metric statistics of the job. node scope assumed for now.
|
2021-12-17 15:49:22 +01:00
|
|
|
LoadStats(job *schema.Job, metrics []string, ctx context.Context) (map[string]map[string]schema.MetricStatistics, error)
|
2021-12-09 16:25:48 +01:00
|
|
|
|
2022-01-31 15:16:05 +01:00
|
|
|
// Return a map of hosts to a map of metrics at the requested scopes for that node.
|
2022-03-24 14:34:42 +01:00
|
|
|
LoadNodeData(cluster string, metrics, nodes []string, scopes []schema.MetricScope, from, to time.Time, ctx context.Context) (map[string]map[string][]*schema.JobMetric, error)
|
2021-12-08 10:14:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var metricDataRepos map[string]MetricDataRepository = map[string]MetricDataRepository{}
|
2021-11-26 10:32:36 +01:00
|
|
|
|
2024-08-27 16:44:16 +02:00
|
|
|
func Init() error {
|
2022-09-05 17:46:38 +02:00
|
|
|
for _, cluster := range config.Keys.Clusters {
|
2021-12-08 10:14:45 +01:00
|
|
|
if cluster.MetricDataRepository != nil {
|
2022-03-17 16:15:35 +01:00
|
|
|
var kind struct {
|
|
|
|
Kind string `json:"kind"`
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(cluster.MetricDataRepository, &kind); err != nil {
|
2023-02-01 11:58:27 +01:00
|
|
|
log.Warn("Error while unmarshaling raw json MetricDataRepository")
|
2022-03-17 16:15:35 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-24 10:06:25 +01:00
|
|
|
var mdr MetricDataRepository
|
2022-03-17 16:15:35 +01:00
|
|
|
switch kind.Kind {
|
2021-12-08 10:14:45 +01:00
|
|
|
case "cc-metric-store":
|
2022-01-24 10:06:25 +01:00
|
|
|
mdr = &CCMetricStore{}
|
2022-03-15 18:35:27 +01:00
|
|
|
case "influxdb":
|
|
|
|
mdr = &InfluxDBv2DataRepository{}
|
2022-12-08 13:51:44 +01:00
|
|
|
case "prometheus":
|
|
|
|
mdr = &PrometheusDataRepository{}
|
2022-01-24 10:06:25 +01:00
|
|
|
case "test":
|
|
|
|
mdr = &TestMetricDataRepository{}
|
2021-12-08 10:14:45 +01:00
|
|
|
default:
|
2023-02-01 11:58:27 +01:00
|
|
|
return fmt.Errorf("METRICDATA/METRICDATA > Unknown MetricDataRepository %v for cluster %v", kind.Kind, cluster.Name)
|
2021-12-08 10:14:45 +01:00
|
|
|
}
|
2022-01-24 10:06:25 +01:00
|
|
|
|
2022-03-17 16:15:35 +01:00
|
|
|
if err := mdr.Init(cluster.MetricDataRepository); err != nil {
|
2023-02-01 11:58:27 +01:00
|
|
|
log.Errorf("Error initializing MetricDataRepository %v for cluster %v", kind.Kind, cluster.Name)
|
2022-01-24 10:06:25 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
metricDataRepos[cluster.Name] = mdr
|
2021-12-08 10:14:45 +01:00
|
|
|
}
|
2021-11-26 10:32:36 +01:00
|
|
|
}
|
2021-12-08 10:14:45 +01:00
|
|
|
return nil
|
2021-11-26 10:32:36 +01:00
|
|
|
}
|
|
|
|
|
2024-08-28 10:03:04 +02:00
|
|
|
func GetMetricDataRepo(cluster string) (MetricDataRepository, error) {
|
|
|
|
var err error
|
2024-08-27 16:44:16 +02:00
|
|
|
repo, ok := metricDataRepos[cluster]
|
2022-09-13 15:21:50 +02:00
|
|
|
|
2024-08-27 16:44:16 +02:00
|
|
|
if !ok {
|
2024-08-28 10:03:04 +02:00
|
|
|
err = fmt.Errorf("METRICDATA/METRICDATA > no metric data repository configured for '%s'", cluster)
|
2022-09-13 15:21:50 +02:00
|
|
|
}
|
2022-09-05 17:46:38 +02:00
|
|
|
|
2024-08-28 10:03:04 +02:00
|
|
|
return repo, err
|
2022-01-17 13:33:35 +01:00
|
|
|
}
|