2022-09-05 17:46:38 +02:00
|
|
|
// Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
|
|
|
|
// All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package archive
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2022-09-13 07:37:48 +02:00
|
|
|
"time"
|
2022-09-05 17:46:38 +02:00
|
|
|
|
|
|
|
"github.com/ClusterCockpit/cc-backend/pkg/schema"
|
|
|
|
)
|
|
|
|
|
2022-09-13 07:37:48 +02:00
|
|
|
func DecodeJobData(r io.Reader, k string) (schema.JobData, error) {
|
|
|
|
data := cache.Get(k, func() (value interface{}, ttl time.Duration, size int) {
|
|
|
|
var d schema.JobData
|
|
|
|
if err := json.NewDecoder(r).Decode(&d); err != nil {
|
|
|
|
return err, 0, 1000
|
|
|
|
}
|
2022-09-05 17:46:38 +02:00
|
|
|
|
2022-09-13 07:37:48 +02:00
|
|
|
return d, 1 * time.Hour, d.Size()
|
|
|
|
})
|
|
|
|
|
|
|
|
if err, ok := data.(error); ok {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-05 17:46:38 +02:00
|
|
|
|
2022-09-13 07:37:48 +02:00
|
|
|
return data.(schema.JobData), nil
|
2022-09-05 17:46:38 +02:00
|
|
|
}
|
|
|
|
|
2022-09-13 07:37:48 +02:00
|
|
|
func DecodeJobMeta(r io.Reader) (*schema.JobMeta, error) {
|
2022-09-05 17:46:38 +02:00
|
|
|
var d schema.JobMeta
|
|
|
|
if err := json.NewDecoder(r).Decode(&d); err != nil {
|
2022-09-13 07:37:48 +02:00
|
|
|
return &d, err
|
2022-09-05 17:46:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sanitize parameters
|
|
|
|
|
2022-09-13 07:37:48 +02:00
|
|
|
return &d, nil
|
2022-09-05 17:46:38 +02:00
|
|
|
}
|
|
|
|
|
2022-09-13 07:37:48 +02:00
|
|
|
func DecodeCluster(r io.Reader) (*schema.Cluster, error) {
|
2022-09-07 12:24:45 +02:00
|
|
|
var c schema.Cluster
|
2022-09-05 17:46:38 +02:00
|
|
|
if err := json.NewDecoder(r).Decode(&c); err != nil {
|
2022-09-13 07:37:48 +02:00
|
|
|
return &c, err
|
2022-09-05 17:46:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sanitize parameters
|
|
|
|
|
2022-09-13 07:37:48 +02:00
|
|
|
return &c, nil
|
2022-09-05 17:46:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func EncodeJobData(w io.Writer, d *schema.JobData) error {
|
|
|
|
// Sanitize parameters
|
|
|
|
if err := json.NewEncoder(w).Encode(d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func EncodeJobMeta(w io.Writer, d *schema.JobMeta) error {
|
|
|
|
// Sanitize parameters
|
|
|
|
if err := json.NewEncoder(w).Encode(d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|