Start to extract DB repositories

This commit is contained in:
Jan Eitzinger 2022-02-06 09:48:31 +01:00
parent 9167959875
commit 99865f4152
3 changed files with 50 additions and 8 deletions

View File

@ -17,6 +17,7 @@ import (
"github.com/ClusterCockpit/cc-backend/graph/model" "github.com/ClusterCockpit/cc-backend/graph/model"
"github.com/ClusterCockpit/cc-backend/log" "github.com/ClusterCockpit/cc-backend/log"
"github.com/ClusterCockpit/cc-backend/metricdata" "github.com/ClusterCockpit/cc-backend/metricdata"
"github.com/ClusterCockpit/cc-backend/repository"
"github.com/ClusterCockpit/cc-backend/schema" "github.com/ClusterCockpit/cc-backend/schema"
sq "github.com/Masterminds/squirrel" sq "github.com/Masterminds/squirrel"
"github.com/gorilla/mux" "github.com/gorilla/mux"
@ -24,6 +25,7 @@ import (
) )
type RestApi struct { type RestApi struct {
r *repository.JobRepository
DB *sqlx.DB DB *sqlx.DB
Resolver *graph.Resolver Resolver *graph.Resolver
AsyncArchiving bool AsyncArchiving bool
@ -51,7 +53,7 @@ func (api *RestApi) MountRoutes(r *mux.Router) {
} }
} }
type StartJobApiRespone struct { type StartJobApiResponse struct {
DBID int64 `json:"id"` DBID int64 `json:"id"`
} }
@ -153,14 +155,14 @@ func (api *RestApi) tagJob(rw http.ResponseWriter, r *http.Request) {
for _, tag := range req { for _, tag := range req {
var tagId int64 var tagId int64
if err := sq.Select("id").From("tag"). exists := false
Where("tag.tag_type = ?", tag.Type).Where("tag.tag_name = ?", tag.Name).
RunWith(api.DB).QueryRow().Scan(&tagId); err != nil { if exists, tagId = api.r.TagExists(tag.Type, tag.Name); exists {
http.Error(rw, fmt.Sprintf("the tag '%s:%s' does not exist", tag.Type, tag.Name), http.StatusNotFound) http.Error(rw, fmt.Sprintf("the tag '%s:%s' does not exist", tag.Type, tag.Name), http.StatusNotFound)
return return
} }
if _, err := api.DB.Exec(`INSERT INTO jobtag (job_id, tag_id) VALUES (?, ?)`, job.ID, tagId); err != nil { if err := api.r.AddTag(job.JobID, tagId); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError) http.Error(rw, err.Error(), http.StatusInternalServerError)
return return
} }
@ -204,8 +206,7 @@ func (api *RestApi) startJob(rw http.ResponseWriter, r *http.Request) {
} }
// Check if combination of (job_id, cluster_id, start_time) already exists: // Check if combination of (job_id, cluster_id, start_time) already exists:
rows, err := api.DB.Query(`SELECT job.id FROM job WHERE job.job_id = ? AND job.cluster = ? AND job.start_time = ?`, rows, err := api.r.JobExists(req.JobID, req.Cluster, req.StartTime)
req.JobID, req.Cluster, req.StartTime)
if err != nil { if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError) http.Error(rw, err.Error(), http.StatusInternalServerError)
return return
@ -250,7 +251,7 @@ func (api *RestApi) startJob(rw http.ResponseWriter, r *http.Request) {
log.Printf("new job (id: %d): cluster=%s, jobId=%d, user=%s, startTime=%d", id, req.Cluster, req.JobID, req.User, req.StartTime) log.Printf("new job (id: %d): cluster=%s, jobId=%d, user=%s, startTime=%d", id, req.Cluster, req.JobID, req.User, req.StartTime)
rw.Header().Add("Content-Type", "application/json") rw.Header().Add("Content-Type", "application/json")
rw.WriteHeader(http.StatusCreated) rw.WriteHeader(http.StatusCreated)
json.NewEncoder(rw).Encode(StartJobApiRespone{ json.NewEncoder(rw).Encode(StartJobApiResponse{
DBID: id, DBID: id,
}) })
} }

40
repository/job.go Normal file
View File

@ -0,0 +1,40 @@
package repository
import (
"database/sql"
sq "github.com/Masterminds/squirrel"
"github.com/jmoiron/sqlx"
)
type JobRepository struct {
DB *sqlx.DB
}
func (r *JobRepository) JobExists(jobId int64, cluster string, startTime int64) (rows *sql.Rows, err error) {
rows, err = r.DB.Query(`SELECT job.id FROM job WHERE job.job_id = ? AND job.cluster = ? AND job.start_time = ?`,
jobId, cluster, startTime)
return
}
func (r *JobRepository) IdExists(jobId int64) bool {
return true
}
func (r *JobRepository) AddTag(jobId int64, tagId int64) error {
_, err := r.DB.Exec(`INSERT INTO jobtag (job_id, tag_id) VALUES (?, ?)`, jobId, tagId)
return err
}
func (r *JobRepository) TagExists(tagType string, tagName string) (exists bool, tagId int64) {
exists = true
if err := sq.Select("id").From("tag").
Where("tag.tag_type = ?", tagType).Where("tag.tag_name = ?", tagName).
RunWith(r.DB).QueryRow().Scan(&tagId); err != nil {
exists = false
return exists, tagId
} else {
return exists, tagId
}
}

1
repository/user.go Normal file
View File

@ -0,0 +1 @@
package repository