2020-12-04 23:41:26 +01:00
|
|
|
package db
|
2016-04-16 21:42:57 +02:00
|
|
|
|
2020-12-01 20:06:49 +01:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
2016-04-16 21:42:57 +02:00
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// Event represents information generated by ansible or api action captured to the database during execution
|
2016-04-16 21:42:57 +02:00
|
|
|
type Event struct {
|
2021-08-20 08:28:50 +02:00
|
|
|
UserID *int `db:"user_id" json:"user_id"`
|
2016-04-16 21:42:57 +02:00
|
|
|
ProjectID *int `db:"project_id" json:"project_id"`
|
|
|
|
ObjectID *int `db:"object_id" json:"object_id"`
|
|
|
|
ObjectType *string `db:"object_type" json:"object_type"`
|
|
|
|
Description *string `db:"description" json:"description"`
|
|
|
|
Created time.Time `db:"created" json:"created"`
|
|
|
|
|
|
|
|
ObjectName string `db:"-" json:"object_name"`
|
|
|
|
ProjectName *string `db:"project_name" json:"project_name"`
|
2021-08-25 06:27:16 +02:00
|
|
|
Username *string `db:"-" json:"username"`
|
2016-04-16 21:42:57 +02:00
|
|
|
}
|
2021-10-12 12:25:43 +02:00
|
|
|
|
|
|
|
func FillEvents(d Store, events []Event) (err error) {
|
|
|
|
usernames := make(map[int]string)
|
|
|
|
|
|
|
|
for i, evt := range events {
|
|
|
|
var objName string
|
|
|
|
objName, err = getEventObjectName(d, evt)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if objName != "" {
|
|
|
|
events[i].ObjectName = objName
|
|
|
|
}
|
|
|
|
|
|
|
|
if evt.UserID == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var username string
|
|
|
|
|
|
|
|
username, ok := usernames[*evt.UserID]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
username, err = getEventUsername(d, evt)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if username == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
usernames[*evt.UserID] = username
|
|
|
|
}
|
|
|
|
|
|
|
|
events[i].Username = &username
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEventObjectName(d Store, evt Event) (string, error) {
|
|
|
|
if evt.ObjectID == nil || evt.ObjectType == nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
switch *evt.ObjectType {
|
|
|
|
case "task":
|
|
|
|
task, err := d.GetTask(*evt.ProjectID, *evt.ObjectID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return task.Playbook, nil
|
|
|
|
default:
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEventUsername(d Store, evt Event) (username string, err error) {
|
|
|
|
if evt.UserID == nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := d.GetUser(*evt.UserID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return user.Username, nil
|
|
|
|
}
|