2021-11-26 10:32:36 +01:00
|
|
|
package metricdata
|
|
|
|
|
|
|
|
import (
|
2022-01-07 09:47:41 +01:00
|
|
|
"bufio"
|
2021-11-26 10:32:36 +01:00
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2022-01-07 09:47:41 +01:00
|
|
|
"strconv"
|
2021-11-26 10:32:36 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ClusterCockpit/cc-jobarchive/config"
|
|
|
|
"github.com/ClusterCockpit/cc-jobarchive/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CCMetricStore struct {
|
|
|
|
jwt string
|
|
|
|
url string
|
|
|
|
client http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
type ApiRequestBody struct {
|
|
|
|
Metrics []string `json:"metrics"`
|
|
|
|
Selectors [][]string `json:"selectors"`
|
|
|
|
}
|
|
|
|
|
2022-01-12 13:03:01 +01:00
|
|
|
type ApiQuery struct {
|
|
|
|
Metric string `json:"metric"`
|
|
|
|
Hostname string `json:"hostname"`
|
|
|
|
Type *string `json:"type,omitempty"`
|
|
|
|
TypeIds []string `json:"type-ids,omitempty"`
|
|
|
|
SubType *string `json:"subtype,omitempty"`
|
|
|
|
SubTypeIds []string `json:"subtype-ids,omitempty"`
|
|
|
|
}
|
|
|
|
|
2021-11-26 10:32:36 +01:00
|
|
|
type ApiMetricData struct {
|
|
|
|
Error *string `json:"error"`
|
|
|
|
From int64 `json:"from"`
|
|
|
|
To int64 `json:"to"`
|
|
|
|
Data []schema.Float `json:"data"`
|
2022-01-07 09:47:41 +01:00
|
|
|
Avg schema.Float `json:"avg"`
|
|
|
|
Min schema.Float `json:"min"`
|
|
|
|
Max schema.Float `json:"max"`
|
2021-11-26 10:32:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type ApiStatsData struct {
|
|
|
|
Error *string `json:"error"`
|
|
|
|
From int64 `json:"from"`
|
|
|
|
To int64 `json:"to"`
|
|
|
|
Samples int `json:"samples"`
|
|
|
|
Avg schema.Float `json:"avg"`
|
|
|
|
Min schema.Float `json:"min"`
|
|
|
|
Max schema.Float `json:"max"`
|
|
|
|
}
|
|
|
|
|
2021-12-20 10:49:46 +01:00
|
|
|
func (ccms *CCMetricStore) Init(url, token string) error {
|
|
|
|
ccms.url = url
|
|
|
|
ccms.jwt = token
|
2022-01-12 13:03:01 +01:00
|
|
|
ccms.client = http.Client{
|
|
|
|
Timeout: 5 * time.Second,
|
|
|
|
}
|
2021-11-26 10:32:36 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-17 15:49:22 +01:00
|
|
|
func (ccms *CCMetricStore) doRequest(job *schema.Job, suffix string, metrics []string, ctx context.Context) (*http.Response, error) {
|
2021-11-26 10:38:38 +01:00
|
|
|
from, to := job.StartTime.Unix(), job.StartTime.Add(time.Duration(job.Duration)*time.Second).Unix()
|
2021-11-26 10:32:36 +01:00
|
|
|
reqBody := ApiRequestBody{}
|
|
|
|
reqBody.Metrics = metrics
|
2021-12-16 13:17:48 +01:00
|
|
|
for _, node := range job.Resources {
|
|
|
|
if node.Accelerators != nil || node.HWThreads != nil {
|
|
|
|
// TODO/FIXME:
|
|
|
|
return nil, errors.New("todo: cc-metric-store resources: Accelerator/HWThreads")
|
|
|
|
}
|
|
|
|
|
|
|
|
reqBody.Selectors = append(reqBody.Selectors, []string{job.Cluster, node.Hostname})
|
2021-11-26 10:32:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
reqBodyBytes, err := json.Marshal(reqBody)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-08 11:50:16 +01:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, fmt.Sprintf("%s/api/%d/%d/%s", ccms.url, from, to, suffix), bytes.NewReader(reqBodyBytes))
|
2021-11-26 10:32:36 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-09 16:26:59 +01:00
|
|
|
if ccms.jwt != "" {
|
|
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", ccms.jwt))
|
|
|
|
}
|
2021-12-08 11:50:16 +01:00
|
|
|
return ccms.client.Do(req)
|
|
|
|
}
|
|
|
|
|
2022-01-07 09:47:41 +01:00
|
|
|
func (ccms *CCMetricStore) LoadData(job *schema.Job, metrics []string, scopes []schema.MetricScope, ctx context.Context) (schema.JobData, error) {
|
|
|
|
type ApiQueryRequest struct {
|
|
|
|
Cluster string `json:"cluster"`
|
|
|
|
From int64 `json:"from"`
|
|
|
|
To int64 `json:"to"`
|
|
|
|
Queries []ApiQuery `json:"queries"`
|
2021-11-26 10:32:36 +01:00
|
|
|
}
|
|
|
|
|
2022-01-07 09:47:41 +01:00
|
|
|
type ApiQueryResponse struct {
|
|
|
|
ApiMetricData
|
|
|
|
Query *ApiQuery `json:"query"`
|
|
|
|
}
|
|
|
|
|
2022-01-17 13:33:35 +01:00
|
|
|
queries, assignedScope, err := ccms.buildQueries(job, metrics, scopes)
|
2022-01-12 13:03:01 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-07 09:47:41 +01:00
|
|
|
reqBody := ApiQueryRequest{
|
|
|
|
Cluster: job.Cluster,
|
|
|
|
From: job.StartTime.Unix(),
|
2022-01-10 16:13:40 +01:00
|
|
|
To: job.StartTime.Add(time.Duration(job.Duration) * time.Second).Unix(),
|
2022-01-12 13:03:01 +01:00
|
|
|
Queries: queries,
|
2022-01-07 09:47:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
if err := json.NewEncoder(buf).Encode(reqBody); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, ccms.url+"/api/query", buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if ccms.jwt != "" {
|
|
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", ccms.jwt))
|
|
|
|
}
|
|
|
|
res, err := ccms.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if res.StatusCode != http.StatusOK {
|
|
|
|
return nil, fmt.Errorf("cc-metric-store replied with: %s", res.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
var resBody []ApiQueryResponse
|
|
|
|
if err := json.NewDecoder(bufio.NewReader(res.Body)).Decode(&resBody); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:13:40 +01:00
|
|
|
// log.Printf("response: %#v", resBody)
|
|
|
|
|
2022-01-07 09:47:41 +01:00
|
|
|
var jobData schema.JobData = make(schema.JobData)
|
2022-01-17 13:33:35 +01:00
|
|
|
for i, res := range resBody {
|
2022-01-07 09:47:41 +01:00
|
|
|
metric := res.Query.Metric
|
2022-01-10 16:13:40 +01:00
|
|
|
if _, ok := jobData[metric]; !ok {
|
|
|
|
jobData[metric] = make(map[schema.MetricScope]*schema.JobMetric)
|
|
|
|
}
|
|
|
|
|
2022-01-07 09:47:41 +01:00
|
|
|
if res.Error != nil {
|
|
|
|
return nil, fmt.Errorf("cc-metric-store error while fetching %s: %s", metric, *res.Error)
|
|
|
|
}
|
|
|
|
|
2022-01-17 13:33:35 +01:00
|
|
|
scope := assignedScope[i]
|
2022-01-07 09:47:41 +01:00
|
|
|
mc := config.GetMetricConfig(job.Cluster, metric)
|
|
|
|
jobMetric, ok := jobData[metric][scope]
|
|
|
|
if !ok {
|
|
|
|
jobMetric = &schema.JobMetric{
|
|
|
|
Unit: mc.Unit,
|
|
|
|
Scope: scope,
|
|
|
|
Timestep: mc.Timestep,
|
|
|
|
Series: make([]schema.Series, 0),
|
|
|
|
}
|
|
|
|
jobData[metric][scope] = jobMetric
|
|
|
|
}
|
|
|
|
|
|
|
|
id := (*int)(nil)
|
|
|
|
if res.Query.Type != nil {
|
|
|
|
id = new(int)
|
|
|
|
*id, _ = strconv.Atoi(res.Query.TypeIds[0])
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:13:40 +01:00
|
|
|
if res.Avg.IsNaN() || res.Min.IsNaN() || res.Max.IsNaN() {
|
|
|
|
// TODO: use schema.Float instead of float64?
|
|
|
|
// This is done because regular float64 can not be JSONed when NaN.
|
|
|
|
res.Avg = schema.Float(0)
|
|
|
|
res.Min = schema.Float(0)
|
|
|
|
res.Max = schema.Float(0)
|
|
|
|
}
|
|
|
|
|
2022-01-07 09:47:41 +01:00
|
|
|
jobMetric.Series = append(jobMetric.Series, schema.Series{
|
|
|
|
Hostname: res.Query.Hostname,
|
|
|
|
Id: id,
|
|
|
|
Statistics: &schema.MetricStatistics{
|
|
|
|
Avg: float64(res.Avg),
|
|
|
|
Min: float64(res.Min),
|
|
|
|
Max: float64(res.Max),
|
|
|
|
},
|
|
|
|
Data: res.Data,
|
|
|
|
})
|
2021-11-26 10:32:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return jobData, nil
|
|
|
|
}
|
2021-12-08 11:50:16 +01:00
|
|
|
|
2022-01-12 13:03:01 +01:00
|
|
|
var (
|
2022-01-17 13:33:35 +01:00
|
|
|
hwthreadString = string("cpu") // TODO/FIXME: inconsistency between cc-metric-collector and ClusterCockpit
|
|
|
|
// coreString = string(schema.MetricScopeCore)
|
2022-01-12 13:03:01 +01:00
|
|
|
socketString = string(schema.MetricScopeSocket)
|
|
|
|
acceleratorString = string(schema.MetricScopeAccelerator)
|
|
|
|
)
|
|
|
|
|
2022-01-17 13:33:35 +01:00
|
|
|
func (ccms *CCMetricStore) buildQueries(job *schema.Job, metrics []string, scopes []schema.MetricScope) ([]ApiQuery, []schema.MetricScope, error) {
|
2022-01-12 13:03:01 +01:00
|
|
|
queries := make([]ApiQuery, 0, len(metrics)*len(scopes)*len(job.Resources))
|
|
|
|
topology := config.GetPartition(job.Cluster, job.Partition).Topology
|
2022-01-17 13:33:35 +01:00
|
|
|
assignedScope := []schema.MetricScope{}
|
2022-01-12 13:03:01 +01:00
|
|
|
|
|
|
|
for _, metric := range metrics {
|
|
|
|
mc := config.GetMetricConfig(job.Cluster, metric)
|
|
|
|
if mc == nil {
|
|
|
|
// return nil, fmt.Errorf("metric '%s' is not specified for cluster '%s'", metric, job.Cluster)
|
|
|
|
// log.Printf("metric '%s' is not specified for cluster '%s'", metric, job.Cluster)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-01-17 13:33:35 +01:00
|
|
|
// Avoid duplicates...
|
|
|
|
handledScopes := make([]schema.MetricScope, 0, 3)
|
2022-01-12 13:03:01 +01:00
|
|
|
|
2022-01-17 13:33:35 +01:00
|
|
|
scopesLoop:
|
|
|
|
for _, requestedScope := range scopes {
|
|
|
|
nativeScope := mc.Scope
|
|
|
|
scope := nativeScope.Max(requestedScope)
|
|
|
|
for _, s := range handledScopes {
|
|
|
|
if scope == s {
|
|
|
|
continue scopesLoop
|
|
|
|
}
|
2022-01-12 13:03:01 +01:00
|
|
|
}
|
2022-01-17 13:33:35 +01:00
|
|
|
handledScopes = append(handledScopes, scope)
|
2022-01-12 13:03:01 +01:00
|
|
|
|
2022-01-17 13:33:35 +01:00
|
|
|
for _, host := range job.Resources {
|
|
|
|
hwthreads := host.HWThreads
|
|
|
|
if hwthreads == nil {
|
|
|
|
hwthreads = topology.Node
|
|
|
|
}
|
2022-01-12 13:03:01 +01:00
|
|
|
|
2022-01-17 13:33:35 +01:00
|
|
|
// Accelerator -> Accelerator (Use "accelerator" scope if requested scope is lower than node)
|
|
|
|
if nativeScope == schema.MetricScopeAccelerator && scope.LT(schema.MetricScopeNode) {
|
|
|
|
for _, accel := range host.Accelerators {
|
|
|
|
queries = append(queries, ApiQuery{
|
|
|
|
Metric: metric,
|
|
|
|
Hostname: host.Hostname,
|
|
|
|
Type: &acceleratorString,
|
|
|
|
TypeIds: []string{strconv.Itoa(accel)},
|
|
|
|
})
|
|
|
|
assignedScope = append(assignedScope, schema.MetricScopeAccelerator)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2022-01-12 13:03:01 +01:00
|
|
|
|
2022-01-17 13:33:35 +01:00
|
|
|
// Accelerator -> Node
|
|
|
|
if nativeScope == schema.MetricScopeAccelerator && scope == schema.MetricScopeNode {
|
|
|
|
if len(host.Accelerators) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2022-01-12 13:03:01 +01:00
|
|
|
|
2022-01-17 13:33:35 +01:00
|
|
|
queries = append(queries, ApiQuery{
|
|
|
|
Metric: metric,
|
|
|
|
Hostname: host.Hostname,
|
|
|
|
Type: &acceleratorString,
|
|
|
|
TypeIds: toStringSlice(host.Accelerators),
|
|
|
|
})
|
|
|
|
assignedScope = append(assignedScope, schema.MetricScopeNode)
|
|
|
|
continue
|
|
|
|
}
|
2022-01-12 13:03:01 +01:00
|
|
|
|
2022-01-17 13:33:35 +01:00
|
|
|
// HWThread -> HWThead
|
|
|
|
if nativeScope == schema.MetricScopeHWThread && scope == schema.MetricScopeHWThread {
|
|
|
|
for _, hwthread := range hwthreads {
|
|
|
|
queries = append(queries, ApiQuery{
|
|
|
|
Metric: metric,
|
|
|
|
Hostname: host.Hostname,
|
|
|
|
Type: &hwthreadString,
|
|
|
|
TypeIds: []string{strconv.Itoa(hwthread)},
|
|
|
|
})
|
|
|
|
assignedScope = append(assignedScope, schema.MetricScopeHWThread)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2022-01-12 13:03:01 +01:00
|
|
|
|
2022-01-17 13:33:35 +01:00
|
|
|
// HWThread -> Core
|
|
|
|
if nativeScope == schema.MetricScopeHWThread && scope == schema.MetricScopeCore {
|
|
|
|
cores, _ := topology.GetCoresFromHWThreads(hwthreads)
|
|
|
|
for _, core := range cores {
|
|
|
|
queries = append(queries, ApiQuery{
|
|
|
|
Metric: metric,
|
|
|
|
Hostname: host.Hostname,
|
|
|
|
Type: &hwthreadString,
|
|
|
|
TypeIds: toStringSlice(topology.Core[core]),
|
|
|
|
})
|
|
|
|
assignedScope = append(assignedScope, schema.MetricScopeCore)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2022-01-12 13:03:01 +01:00
|
|
|
|
2022-01-17 13:33:35 +01:00
|
|
|
// HWThread -> Socket
|
|
|
|
if nativeScope == schema.MetricScopeHWThread && scope == schema.MetricScopeSocket {
|
|
|
|
sockets, _ := topology.GetSocketsFromHWThreads(hwthreads)
|
|
|
|
for _, socket := range sockets {
|
|
|
|
queries = append(queries, ApiQuery{
|
|
|
|
Metric: metric,
|
|
|
|
Hostname: host.Hostname,
|
|
|
|
Type: &hwthreadString,
|
|
|
|
TypeIds: toStringSlice(topology.Socket[socket]),
|
|
|
|
})
|
|
|
|
assignedScope = append(assignedScope, schema.MetricScopeSocket)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2022-01-12 13:03:01 +01:00
|
|
|
|
2022-01-17 13:33:35 +01:00
|
|
|
// HWThread -> Node
|
|
|
|
if nativeScope == schema.MetricScopeHWThread && scope == schema.MetricScopeNode {
|
|
|
|
queries = append(queries, ApiQuery{
|
|
|
|
Metric: metric,
|
|
|
|
Hostname: host.Hostname,
|
|
|
|
Type: &hwthreadString,
|
|
|
|
TypeIds: toStringSlice(hwthreads),
|
|
|
|
})
|
|
|
|
assignedScope = append(assignedScope, schema.MetricScopeNode)
|
|
|
|
continue
|
|
|
|
}
|
2022-01-12 13:03:01 +01:00
|
|
|
|
2022-01-17 13:33:35 +01:00
|
|
|
// Socket -> Socket
|
|
|
|
if nativeScope == schema.MetricScopeSocket && scope == schema.MetricScopeSocket {
|
|
|
|
sockets, _ := topology.GetSocketsFromHWThreads(hwthreads)
|
|
|
|
for _, socket := range sockets {
|
|
|
|
queries = append(queries, ApiQuery{
|
|
|
|
Metric: metric,
|
|
|
|
Hostname: host.Hostname,
|
|
|
|
Type: &acceleratorString,
|
|
|
|
TypeIds: []string{strconv.Itoa(socket)},
|
|
|
|
})
|
|
|
|
assignedScope = append(assignedScope, schema.MetricScopeSocket)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2022-01-12 13:03:01 +01:00
|
|
|
|
2022-01-17 13:33:35 +01:00
|
|
|
// Socket -> Node
|
|
|
|
if nativeScope == schema.MetricScopeSocket && scope == schema.MetricScopeNode {
|
|
|
|
sockets, _ := topology.GetSocketsFromHWThreads(hwthreads)
|
|
|
|
queries = append(queries, ApiQuery{
|
|
|
|
Metric: metric,
|
|
|
|
Hostname: host.Hostname,
|
|
|
|
Type: &socketString,
|
|
|
|
TypeIds: toStringSlice(sockets),
|
|
|
|
})
|
|
|
|
assignedScope = append(assignedScope, schema.MetricScopeNode)
|
|
|
|
continue
|
|
|
|
}
|
2022-01-12 13:03:01 +01:00
|
|
|
|
2022-01-17 13:33:35 +01:00
|
|
|
// Node -> Node
|
|
|
|
if nativeScope == schema.MetricScopeNode && scope == schema.MetricScopeNode {
|
2022-01-12 13:03:01 +01:00
|
|
|
queries = append(queries, ApiQuery{
|
|
|
|
Metric: metric,
|
2022-01-17 13:33:35 +01:00
|
|
|
Hostname: host.Hostname,
|
2022-01-12 13:03:01 +01:00
|
|
|
})
|
2022-01-17 13:33:35 +01:00
|
|
|
assignedScope = append(assignedScope, schema.MetricScopeNode)
|
|
|
|
continue
|
2022-01-12 13:03:01 +01:00
|
|
|
}
|
2022-01-17 13:33:35 +01:00
|
|
|
|
|
|
|
return nil, nil, fmt.Errorf("TODO: unhandled case: native-scope=%s, requested-scope=%s", nativeScope, requestedScope)
|
2022-01-12 13:03:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-17 13:33:35 +01:00
|
|
|
return queries, assignedScope, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toStringSlice(s []int) []string {
|
|
|
|
ret := make([]string, len(s))
|
|
|
|
for i, val := range s {
|
|
|
|
ret[i] = strconv.Itoa(val)
|
|
|
|
}
|
|
|
|
return ret
|
2022-01-12 13:03:01 +01:00
|
|
|
}
|
|
|
|
|
2021-12-17 15:49:22 +01:00
|
|
|
func (ccms *CCMetricStore) LoadStats(job *schema.Job, metrics []string, ctx context.Context) (map[string]map[string]schema.MetricStatistics, error) {
|
2021-12-08 11:50:16 +01:00
|
|
|
res, err := ccms.doRequest(job, "stats", metrics, ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-16 13:17:48 +01:00
|
|
|
resdata := make([]map[string]ApiStatsData, 0, len(job.Resources))
|
2021-12-08 11:50:16 +01:00
|
|
|
if err := json.NewDecoder(res.Body).Decode(&resdata); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
stats := map[string]map[string]schema.MetricStatistics{}
|
|
|
|
for _, metric := range metrics {
|
|
|
|
nodestats := map[string]schema.MetricStatistics{}
|
2021-12-16 13:17:48 +01:00
|
|
|
for i, node := range job.Resources {
|
|
|
|
if node.Accelerators != nil || node.HWThreads != nil {
|
|
|
|
// TODO/FIXME:
|
|
|
|
return nil, errors.New("todo: cc-metric-store resources: Accelerator/HWThreads")
|
|
|
|
}
|
|
|
|
|
2021-12-08 11:50:16 +01:00
|
|
|
data := resdata[i][metric]
|
|
|
|
if data.Error != nil {
|
|
|
|
return nil, errors.New(*data.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.Samples == 0 {
|
2021-12-16 13:17:48 +01:00
|
|
|
return nil, fmt.Errorf("no data for node '%s' and metric '%s'", node.Hostname, metric)
|
2021-12-08 11:50:16 +01:00
|
|
|
}
|
|
|
|
|
2021-12-16 13:17:48 +01:00
|
|
|
nodestats[node.Hostname] = schema.MetricStatistics{
|
2021-12-08 11:50:16 +01:00
|
|
|
Avg: float64(data.Avg),
|
|
|
|
Min: float64(data.Min),
|
|
|
|
Max: float64(data.Max),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stats[metric] = nodestats
|
|
|
|
}
|
|
|
|
|
|
|
|
return stats, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ccms *CCMetricStore) LoadNodeData(clusterId string, metrics, nodes []string, from, to int64, ctx context.Context) (map[string]map[string][]schema.Float, error) {
|
|
|
|
reqBody := ApiRequestBody{}
|
|
|
|
reqBody.Metrics = metrics
|
|
|
|
for _, node := range nodes {
|
|
|
|
reqBody.Selectors = append(reqBody.Selectors, []string{clusterId, node})
|
|
|
|
}
|
|
|
|
|
|
|
|
reqBodyBytes, err := json.Marshal(reqBody)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var req *http.Request
|
|
|
|
if nodes == nil {
|
2021-12-09 16:26:59 +01:00
|
|
|
req, err = http.NewRequestWithContext(ctx, http.MethodPost, fmt.Sprintf("%s/api/%s/%d/%d/all-nodes", ccms.url, clusterId, from, to), bytes.NewReader(reqBodyBytes))
|
2021-12-08 11:50:16 +01:00
|
|
|
} else {
|
|
|
|
req, err = http.NewRequestWithContext(ctx, http.MethodPost, fmt.Sprintf("%s/api/%d/%d/timeseries", ccms.url, from, to), bytes.NewReader(reqBodyBytes))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-09 16:26:59 +01:00
|
|
|
if ccms.jwt != "" {
|
|
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", ccms.jwt))
|
|
|
|
}
|
2021-12-08 11:50:16 +01:00
|
|
|
res, err := ccms.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
data := map[string]map[string][]schema.Float{}
|
|
|
|
if nodes == nil {
|
|
|
|
resdata := map[string]map[string]ApiMetricData{}
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&resdata); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for node, metrics := range resdata {
|
|
|
|
nodedata := map[string][]schema.Float{}
|
|
|
|
for metric, data := range metrics {
|
|
|
|
if data.Error != nil {
|
|
|
|
return nil, errors.New(*data.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
nodedata[metric] = data.Data
|
|
|
|
}
|
|
|
|
data[node] = nodedata
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resdata := make([]map[string]ApiMetricData, 0, len(nodes))
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&resdata); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, node := range nodes {
|
|
|
|
metricsData := map[string][]schema.Float{}
|
|
|
|
for metric, data := range resdata[i] {
|
|
|
|
if data.Error != nil {
|
|
|
|
return nil, errors.New(*data.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
metricsData[metric] = data.Data
|
|
|
|
}
|
|
|
|
|
|
|
|
data[node] = metricsData
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|