Skip to content

Commit 1745bcc

Browse files
authored
Merge pull request #552 from paczian/master
added adminview query
2 parents 143ae0c + 5b3eb16 commit 1745bcc

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

lib/controller/jobController.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ func (cr *JobController) Read(id string, cx *goweb.Context) {
400400
// To do:
401401
// - Iterate job queries
402402
func (cr *JobController) ReadMany(cx *goweb.Context) {
403-
LogRequest(cx.Request)
403+
LogRequest(cx.Request)
404404

405405
// Try to authenticate user.
406406
u, err := request.Authenticate(cx.Request)
@@ -432,6 +432,46 @@ func (cr *JobController) ReadMany(cx *goweb.Context) {
432432
}
433433
}
434434

435+
// check if an adminview is being requested
436+
if query.Has("adminview") {
437+
438+
// adminview requires a user
439+
if u != nil {
440+
441+
// adminview requires the user to be an admin
442+
if u.Admin {
443+
444+
// special is an attribute from the job document chosen via the cgi-param "special"
445+
// this attribute can be a path in the document, separated by .
446+
// special attributes do not have to be present in all job documents for this function to work
447+
special := "info.userattr.bp_count"
448+
if query.Has("special") {
449+
special = query.Value("special")
450+
}
451+
452+
// call the GetAdminView function, passing along the special attribute
453+
results, err := core.GetAdminView(special)
454+
455+
// if there is an error, return it
456+
if err != nil {
457+
logger.Error("err " + err.Error())
458+
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
459+
return
460+
}
461+
462+
// if there is no error, return the data
463+
cx.RespondWithData(results)
464+
return
465+
} else {
466+
cx.RespondWithErrorMessage("you need to be an administrator to access this function", http.StatusUnauthorized)
467+
return
468+
}
469+
} else {
470+
cx.RespondWithErrorMessage("you need to be logged in to access this function", http.StatusUnauthorized)
471+
return
472+
}
473+
}
474+
435475
limit := conf.DEFAULT_PAGE_SIZE
436476
offset := 0
437477
order := "info.submittime"

lib/core/db.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,33 @@ func dbFind(q bson.M, results *Jobs, options map[string]int) (count int, err err
136136
return
137137
}
138138

139+
// get a minimal subset of the job documents required for an admin overview
140+
// for all completed jobs younger than a month and all running jobs
141+
func dbAdminData(special string) (data []interface{}, err error) {
142+
// get a DB connection
143+
session := db.Connection.Session.Copy()
144+
145+
// close the connection when the function completes
146+
defer session.Close()
147+
148+
// set the database and collection
149+
c := session.DB(conf.MONGODB_DATABASE).C(conf.DB_COLL_JOBS)
150+
151+
// get the completed jobs that have a completed time not older than one month
152+
var completedjobs = bson.M{ "state": "completed", "info.completedtime": bson.M{ "$gt": time.Now().AddDate(0, -1, 0) } }
153+
154+
// get all runnning jobs (those not deleted and not completed)
155+
var runningjobs = bson.M{ "state": bson.M{ "$nin": []string{"completed","deleted"} } }
156+
157+
// select only those fields required for the output
158+
var resultfields = bson.M{"_id":0,"state":1,"info.name":1,"info.submittime":1,"info.startedtime":1,"info.completedtime":1,"info.pipeline":1,"tasks.createdDate":1,"tasks.startedDate":1,"tasks.completedDate":1,"tasks.state":1,"tasks.inputs.size":1,"tasks.outputs.size":1,special: 1}
159+
160+
// return all data without iterating
161+
err = c.Find(bson.M{ "$or": []bson.M{ completedjobs, runningjobs}}).Select(resultfields).All(&data)
162+
163+
return
164+
}
165+
139166
func dbFindSort(q bson.M, results *Jobs, options map[string]int, sortby string, do_init bool) (count int, err error) {
140167
if sortby == "" {
141168
return 0, errors.New("sortby must be an nonempty string")

lib/core/jobs.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,9 @@ func GetJobCount(q bson.M) (count int, err error) {
8686
count, err = dbCount(q)
8787
return
8888
}
89+
90+
// patch the admin view data function from the job controller through to the db.go
91+
func GetAdminView(special string) (data []interface{}, err error) {
92+
data, err = dbAdminData(special)
93+
return
94+
}

0 commit comments

Comments
 (0)