Add rest-api for starting/stoping jobs

This commit is contained in:
Lou Knauer
2021-11-26 10:34:29 +01:00
parent 4ef6d47830
commit 8e5e278ea4
4 changed files with 241 additions and 11 deletions

View File

@@ -2,13 +2,18 @@ package metricdata
import (
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
"math"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"github.com/ClusterCockpit/cc-jobarchive/config"
"github.com/ClusterCockpit/cc-jobarchive/graph/model"
"github.com/ClusterCockpit/cc-jobarchive/schema"
)
@@ -23,8 +28,13 @@ func getPath(job *model.Job, file string) (string, error) {
return "", err
}
lvl1, lvl2 := id/1000, id%1000
return filepath.Join(JobArchivePath, job.ClusterID, fmt.Sprintf("%d", lvl1), fmt.Sprintf("%03d", lvl2), file), nil
lvl1, lvl2 := fmt.Sprintf("%d", id/1000), fmt.Sprintf("%03d", id%1000)
legacyPath := filepath.Join(JobArchivePath, job.ClusterID, lvl1, lvl2, file)
if _, err := os.Stat(legacyPath); errors.Is(err, os.ErrNotExist) {
return filepath.Join(JobArchivePath, job.ClusterID, lvl1, lvl2, strconv.FormatInt(job.StartTime.Unix(), 10), file), nil
}
return legacyPath, nil
}
// Assuming job is completed/archived, return the jobs metric data.
@@ -123,3 +133,99 @@ func loadAveragesFromArchive(job *model.Job, metrics []string, data [][]schema.F
return nil
}
// Writes a running job to the job-archive
func ArchiveJob(job *model.Job, ctx context.Context) error {
if job.State != model.JobStateRunning {
return errors.New("cannot archive job that is not running")
}
allMetrics := make([]string, 0)
metricConfigs := config.GetClusterConfig(job.ClusterID).MetricConfig
for _, mc := range metricConfigs {
allMetrics = append(allMetrics, mc.Name)
}
jobData, err := LoadData(job, allMetrics, ctx)
if err != nil {
return err
}
tags := []struct {
Name string `json:"name"`
Type string `json:"type"`
}{}
for _, tag := range job.Tags {
tags = append(tags, struct {
Name string `json:"name"`
Type string `json:"type"`
}{
Name: tag.TagName,
Type: tag.TagType,
})
}
metaData := &schema.JobMeta{
JobId: job.JobID,
UserId: job.UserID,
ClusterId: job.ClusterID,
NumNodes: job.NumNodes,
JobState: job.State.String(),
StartTime: job.StartTime.Unix(),
Duration: int64(job.Duration),
Nodes: job.Nodes,
Tags: tags,
Statistics: make(map[string]*schema.JobMetaStatistics),
}
for metric, data := range jobData {
avg, min, max := 0.0, math.MaxFloat32, -math.MaxFloat32
for _, nodedata := range data.Series {
avg += nodedata.Statistics.Avg
min = math.Min(min, nodedata.Statistics.Min)
max = math.Max(max, nodedata.Statistics.Max)
}
metaData.Statistics[metric] = &schema.JobMetaStatistics{
Unit: config.GetMetricConfig(job.ClusterID, metric).Unit,
Avg: avg / float64(job.NumNodes),
Min: min,
Max: max,
}
}
dirPath, err := getPath(job, "")
if err != nil {
return err
}
if err := os.MkdirAll(dirPath, 0777); err != nil {
return err
}
f, err := os.Create(path.Join(dirPath, "meta.json"))
if err != nil {
return err
}
defer f.Close()
writer := bufio.NewWriter(f)
if err := json.NewEncoder(writer).Encode(metaData); err != nil {
return err
}
if err := writer.Flush(); err != nil {
return err
}
f, err = os.Create(path.Join(dirPath, "data.json"))
if err != nil {
return err
}
writer = bufio.NewWriter(f)
if err := json.NewEncoder(writer).Encode(metaData); err != nil {
return err
}
if err := writer.Flush(); err != nil {
return err
}
return f.Close()
}