Add Tag view including required changes.

This commit is contained in:
Jan Eitzinger
2022-02-10 18:48:58 +01:00
parent 27fd3c4e5a
commit 14693ad32b
6 changed files with 170 additions and 2 deletions

View File

@@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"errors"
"fmt"
"strconv"
"github.com/ClusterCockpit/cc-backend/auth"
@@ -123,6 +124,42 @@ func (r *JobRepository) CreateTag(tagType string, tagName string) (tagId int64,
return res.LastInsertId()
}
func (r *JobRepository) GetTags() (tags []schema.Tag, counts map[string]int, err error) {
tags = make([]schema.Tag, 0, 100)
xrows, err := r.DB.Queryx("SELECT * FROM tag")
for xrows.Next() {
var t schema.Tag
err = xrows.StructScan(&t)
tags = append(tags, t)
}
q := sq.Select("t.tag_name, count(jt.tag_id)").
From("tag t").
LeftJoin("jobtag jt ON t.id = jt.tag_id").
GroupBy("t.tag_name")
qs, _, err := q.ToSql()
rows, err := r.DB.Query(qs)
if err != nil {
fmt.Println(err)
}
counts = make(map[string]int)
for rows.Next() {
var tagName string
var count int
err = rows.Scan(&tagName, &count)
if err != nil {
fmt.Println(err)
}
counts[tagName] = count
}
err = rows.Err()
return
}
// AddTagOrCreate adds the tag with the specified type and name to the job with the database id `jobId`.
// If such a tag does not yet exist, it is created.
func (r *JobRepository) AddTagOrCreate(jobId int64, tagType string, tagName string) (tagId int64, err error) {

78
repository/job_test.go Normal file
View File

@@ -0,0 +1,78 @@
package repository
import (
"fmt"
"testing"
"github.com/jmoiron/sqlx"
_ "github.com/go-sql-driver/mysql"
_ "github.com/mattn/go-sqlite3"
)
var db *sqlx.DB
func init() {
var err error
if db != nil {
panic("prefer using sub-tests (`t.Run`) or implement `cleanup` before calling setup twice.")
}
db, err = sqlx.Open("sqlite3", "../var/test.db")
if err != nil {
fmt.Println(err)
}
}
func setup(t *testing.T) *JobRepository {
return &JobRepository{
DB: db,
}
}
func TestFind(t *testing.T) {
r := setup(t)
job, err := r.Find(1001789, "emmy", 1540853248)
if err != nil {
t.Fatal(err)
}
// fmt.Printf("%+v", job)
if job.ID != 1245 {
t.Errorf("wrong summary for diagnostic 3\ngot: %d \nwant: 1245", job.JobID)
}
}
func TestFindById(t *testing.T) {
r := setup(t)
job, err := r.FindById(1245)
if err != nil {
t.Fatal(err)
}
// fmt.Printf("%+v", job)
if job.JobID != 1001789 {
t.Errorf("wrong summary for diagnostic 3\ngot: %d \nwant: 1001789", job.JobID)
}
}
func TestGetTags(t *testing.T) {
r := setup(t)
tags, _, err := r.GetTags()
if err != nil {
t.Fatal(err)
}
fmt.Printf("TAGS %+v \n", tags)
// fmt.Printf("COUNTS %+v \n", counts)
t.Errorf("wrong summary for diagnostic 3\ngot: %d \nwant: 23", 28)
// if counts["load-imbalance"] != 23 {
// t.Errorf("wrong summary for diagnostic 3\ngot: %d \nwant: 23", counts["load-imbalance"])
// }
}