fix: persist tags in meta.json

This commit is contained in:
Lou Knauer 2022-02-23 14:18:55 +01:00
parent ef089a7f2b
commit f304698823
2 changed files with 44 additions and 33 deletions

View File

@ -42,28 +42,19 @@ func (r *mutationResolver) AddTagsToJob(ctx context.Context, job string, tagIds
return nil, err
}
tags := []*schema.Tag{}
for _, tagId := range tagIds {
tid, err := strconv.ParseInt(tagId, 10, 64)
if err != nil {
return nil, err
}
if err := r.Repo.AddTag(jid, tid); err != nil {
if tags, err = r.Repo.AddTag(jid, tid); err != nil {
return nil, err
}
}
j, err := r.Query().Job(ctx, job)
if err != nil {
return nil, err
}
j.Tags, err = r.Repo.GetTags(&jid)
if err != nil {
return nil, err
}
return j.Tags, metricdata.UpdateTags(j, j.Tags)
return tags, nil
}
func (r *mutationResolver) RemoveTagsFromJob(ctx context.Context, job string, tagIds []string) ([]*schema.Tag, error) {
@ -72,30 +63,19 @@ func (r *mutationResolver) RemoveTagsFromJob(ctx context.Context, job string, ta
return nil, err
}
tags := []*schema.Tag{}
for _, tagId := range tagIds {
tid, err := strconv.ParseInt(tagId, 10, 64)
if err != nil {
return nil, err
}
if err := r.Repo.RemoveTag(jid, tid); err != nil {
if tags, err = r.Repo.RemoveTag(jid, tid); err != nil {
return nil, err
}
}
dummyJob := schema.Job{}
dummyJob.ID = int64(jid)
tags, err := r.Job().Tags(ctx, &dummyJob)
if err != nil {
return nil, err
}
jobObj, err := r.Query().Job(ctx, job)
if err != nil {
return nil, err
}
return tags, metricdata.UpdateTags(jobObj, tags)
return tags, nil
}
func (r *mutationResolver) UpdateConfiguration(ctx context.Context, name string, value string) (*string, error) {

View File

@ -3,20 +3,47 @@ package repository
import (
"fmt"
"github.com/ClusterCockpit/cc-backend/metricdata"
"github.com/ClusterCockpit/cc-backend/schema"
sq "github.com/Masterminds/squirrel"
)
// Add the tag with id `tagId` to the job with the database id `jobId`.
func (r *JobRepository) AddTag(jobId int64, tagId int64) error {
_, err := r.stmtCache.Exec(`INSERT INTO jobtag (job_id, tag_id) VALUES ($1, $2)`, jobId, tagId)
return err
func (r *JobRepository) AddTag(job int64, tag int64) ([]*schema.Tag, error) {
if _, err := r.stmtCache.Exec(`INSERT INTO jobtag (job_id, tag_id) VALUES ($1, $2)`, job, tag); err != nil {
return nil, err
}
j, err := r.FindById(job)
if err != nil {
return nil, err
}
tags, err := r.GetTags(&job)
if err != nil {
return nil, err
}
return tags, metricdata.UpdateTags(j, tags)
}
// Removes a tag from a job
func (r *JobRepository) RemoveTag(job, tag int64) error {
_, err := r.stmtCache.Exec("DELETE FROM jobtag WHERE jobtag.job_id = $1 AND jobtag.tag_id = $2", job, tag)
return err
func (r *JobRepository) RemoveTag(job, tag int64) ([]*schema.Tag, error) {
if _, err := r.stmtCache.Exec("DELETE FROM jobtag WHERE jobtag.job_id = $1 AND jobtag.tag_id = $2", job, tag); err != nil {
return nil, err
}
j, err := r.FindById(job)
if err != nil {
return nil, err
}
tags, err := r.GetTags(&job)
if err != nil {
return nil, err
}
return tags, metricdata.UpdateTags(j, tags)
}
// CreateTag creates a new tag with the specified type and name and returns its database id.
@ -83,7 +110,11 @@ func (r *JobRepository) AddTagOrCreate(jobId int64, tagType string, tagName stri
}
}
return tagId, r.AddTag(jobId, tagId)
if _, err := r.AddTag(jobId, tagId); err != nil {
return 0, err
}
return tagId, nil
}
// TagId returns the database id of the tag with the specified type and name.