Skip to content

Commit

Permalink
Merge pull request #552 from paczian/master
Browse files Browse the repository at this point in the history
added adminview query
  • Loading branch information
wgerlach authored Nov 3, 2017
2 parents 143ae0c + 5b3eb16 commit 1745bcc
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
42 changes: 41 additions & 1 deletion lib/controller/jobController.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ func (cr *JobController) Read(id string, cx *goweb.Context) {
// To do:
// - Iterate job queries
func (cr *JobController) ReadMany(cx *goweb.Context) {
LogRequest(cx.Request)
LogRequest(cx.Request)

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

// check if an adminview is being requested
if query.Has("adminview") {

// adminview requires a user
if u != nil {

// adminview requires the user to be an admin
if u.Admin {

// special is an attribute from the job document chosen via the cgi-param "special"
// this attribute can be a path in the document, separated by .
// special attributes do not have to be present in all job documents for this function to work
special := "info.userattr.bp_count"
if query.Has("special") {
special = query.Value("special")
}

// call the GetAdminView function, passing along the special attribute
results, err := core.GetAdminView(special)

// if there is an error, return it
if err != nil {
logger.Error("err " + err.Error())
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
return
}

// if there is no error, return the data
cx.RespondWithData(results)
return
} else {
cx.RespondWithErrorMessage("you need to be an administrator to access this function", http.StatusUnauthorized)
return
}
} else {
cx.RespondWithErrorMessage("you need to be logged in to access this function", http.StatusUnauthorized)
return
}
}

limit := conf.DEFAULT_PAGE_SIZE
offset := 0
order := "info.submittime"
Expand Down
27 changes: 27 additions & 0 deletions lib/core/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,33 @@ func dbFind(q bson.M, results *Jobs, options map[string]int) (count int, err err
return
}

// get a minimal subset of the job documents required for an admin overview
// for all completed jobs younger than a month and all running jobs
func dbAdminData(special string) (data []interface{}, err error) {
// get a DB connection
session := db.Connection.Session.Copy()

// close the connection when the function completes
defer session.Close()

// set the database and collection
c := session.DB(conf.MONGODB_DATABASE).C(conf.DB_COLL_JOBS)

// get the completed jobs that have a completed time not older than one month
var completedjobs = bson.M{ "state": "completed", "info.completedtime": bson.M{ "$gt": time.Now().AddDate(0, -1, 0) } }

// get all runnning jobs (those not deleted and not completed)
var runningjobs = bson.M{ "state": bson.M{ "$nin": []string{"completed","deleted"} } }

// select only those fields required for the output
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}

// return all data without iterating
err = c.Find(bson.M{ "$or": []bson.M{ completedjobs, runningjobs}}).Select(resultfields).All(&data)

return
}

func dbFindSort(q bson.M, results *Jobs, options map[string]int, sortby string, do_init bool) (count int, err error) {
if sortby == "" {
return 0, errors.New("sortby must be an nonempty string")
Expand Down
6 changes: 6 additions & 0 deletions lib/core/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,9 @@ func GetJobCount(q bson.M) (count int, err error) {
count, err = dbCount(q)
return
}

// patch the admin view data function from the job controller through to the db.go
func GetAdminView(special string) (data []interface{}, err error) {
data, err = dbAdminData(special)
return
}

0 comments on commit 1745bcc

Please sign in to comment.