# HG changeset patch # User Sascha L. Teichmann # Date 1540380915 -7200 # Node ID 3de54d7b7d3030fd69a78aeaa9aa4cfd75ba051e # Parent a55f20dc8d8d8afeb008c9ec23c721f2fe5a1cbc Added endpoint to show the detail logs of an import. Path /api/import/[0-9]+ diff -r a55f20dc8d8d -r 3de54d7b7d30 pkg/controllers/importqueue.go --- a/pkg/controllers/importqueue.go Wed Oct 24 13:09:54 2018 +0200 +++ b/pkg/controllers/importqueue.go Wed Oct 24 13:35:15 2018 +0200 @@ -35,6 +35,15 @@ selectImportPagedSQL = selectImportsUnpagedSQL + ` LIMIT $1 OFFSET $2` + + selectImportLogsSQL = ` +SELECT + time, + kind::varchar, + msg +FROM waterway.import_logs +WHERE import_id = $1 +ORDER BY time` ) func listImports( @@ -90,6 +99,44 @@ Imports: imports, }, } - return } + +func importLogs( + _ interface{}, + req *http.Request, + conn *sql.Conn, +) (jr JSONResult, err error) { + id, _ := strconv.ParseInt(mux.Vars(req)["id"], 10, 64) + + var rows *sql.Rows + + rows, err = conn.QueryContext(req.Context(), selectImportLogsSQL, id) + if err != nil { + return + } + defer rows.Close() + + entries := make([]*models.ImportLogEntry, 0, 10) + + for rows.Next() { + var entry models.ImportLogEntry + if err = rows.Scan(&entry.Time, &entry.Kind, &entry.Message); err != nil { + return + } + entries = append(entries, &entry) + } + + if err = rows.Err(); err != nil { + return + } + + jr = JSONResult{ + Result: struct { + Entries []*models.ImportLogEntry `json:"entries"` + }{ + Entries: entries, + }, + } + return +} diff -r a55f20dc8d8d -r 3de54d7b7d30 pkg/controllers/routes.go --- a/pkg/controllers/routes.go Wed Oct 24 13:09:54 2018 +0200 +++ b/pkg/controllers/routes.go Wed Oct 24 13:35:15 2018 +0200 @@ -172,6 +172,11 @@ api.Handle("/imports", lsImports). Methods(http.MethodGet) + api.Handle("/imports/{id:[0-9]+}", waterwayAdmin(&JSONHandler{ + Handle: importLogs, + })). + Methods(http.MethodGet) + // Token handling: Login/Logout. api.HandleFunc("/login", login). Methods(http.MethodPost) diff -r a55f20dc8d8d -r 3de54d7b7d30 pkg/models/import.go --- a/pkg/models/import.go Wed Oct 24 13:09:54 2018 +0200 +++ b/pkg/models/import.go Wed Oct 24 13:35:15 2018 +0200 @@ -28,6 +28,12 @@ Kind string `json:"kind"` User string `json:"user"` } + + ImportLogEntry struct { + Time ImportTime `json:"time"` + Kind string `json:"kind"` + Message string `json:"message"` + } ) func (it ImportTime) MarshalJSON() ([]byte, error) {