This is an automated email from the ASF dual-hosted git repository. zrhoffman pushed a commit to branch 6.0.x in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git
commit a8e0c2967c9f54d527ad8b5932f0d1f9bc1eae2d Author: dmohan001c <[email protected]> AuthorDate: Tue Sep 28 14:59:36 2021 -0600 Removed default limits values in logs API (#6235) (#6241) * removed limit default values * print statements added * removed the default limit values for the log * updated the docs for logs limit functionality * removed default values in logs only for V4 * removed default limits if the days query params are passed in V2 and V3 * added the docs for logs - limit query params * removed write alerts and renamed to Handle err (cherry picked from commit 2977937ed17a8a208f751ece446b53407820ca04) Co-authored-by: dmohan001c <[email protected]> --- docs/source/api/v2/logs.rst | 4 +- docs/source/api/v3/logs.rst | 4 +- docs/source/api/v4/logs.rst | 2 +- traffic_ops/traffic_ops_golang/logs/log.go | 79 +++++++++++++++++++++++- traffic_ops/traffic_ops_golang/routing/routes.go | 2 +- 5 files changed, 83 insertions(+), 8 deletions(-) diff --git a/docs/source/api/v2/logs.rst b/docs/source/api/v2/logs.rst index 442efa3..33da628 100644 --- a/docs/source/api/v2/logs.rst +++ b/docs/source/api/v2/logs.rst @@ -35,9 +35,9 @@ Request Structure +-----------+----------+-------------------------------------------------------------------------------------------------------------------------------------+ | Name | Required | Description | +===========+==========+=====================================================================================================================================+ - | days | no | An integer number of days of change logs to return | + | days | no | An integer number of days of change logs to return, by default there is no limit applied | +-----------+----------+-------------------------------------------------------------------------------------------------------------------------------------+ - | limit | no | The number of records to which to limit the response | + | limit | no | The number of records to which to limit the response, if there is no limit query params, it limits to 1000 | +-----------+----------+-------------------------------------------------------------------------------------------------------------------------------------+ | offset | no | The number of results to skip before beginning to return results. Must use in conjunction with limit | +-----------+----------+-------------------------------------------------------------------------------------------------------------------------------------+ diff --git a/docs/source/api/v3/logs.rst b/docs/source/api/v3/logs.rst index 2a1ddf5..6728733 100644 --- a/docs/source/api/v3/logs.rst +++ b/docs/source/api/v3/logs.rst @@ -35,9 +35,9 @@ Request Structure +-----------+----------+-------------------------------------------------------------------------------------------------------------------------------------+ | Name | Required | Description | +===========+==========+=====================================================================================================================================+ - | days | no | An integer number of days of change logs to return | + | days | no | An integer number of days of change logs to return, by default there is no limit applied | +-----------+----------+-------------------------------------------------------------------------------------------------------------------------------------+ - | limit | no | The number of records to which to limit the response | + | limit | no | The number of records to which to limit the response, if there is no limit query params, it limits to 1000 | +-----------+----------+-------------------------------------------------------------------------------------------------------------------------------------+ | offset | no | The number of results to skip before beginning to return results. Must use in conjunction with limit | +-----------+----------+-------------------------------------------------------------------------------------------------------------------------------------+ diff --git a/docs/source/api/v4/logs.rst b/docs/source/api/v4/logs.rst index 74ee027..ac7ce82 100644 --- a/docs/source/api/v4/logs.rst +++ b/docs/source/api/v4/logs.rst @@ -37,7 +37,7 @@ Request Structure +===========+==========+=====================================================================================================================================+ | days | no | An integer number of days of change logs to return | +-----------+----------+-------------------------------------------------------------------------------------------------------------------------------------+ - | limit | no | The number of records to which to limit the response | + | limit | no | The number of records to which to limit the response, by default there is no limit applied | +-----------+----------+-------------------------------------------------------------------------------------------------------------------------------------+ | offset | no | The number of results to skip before beginning to return results. Must use in conjunction with limit | +-----------+----------+-------------------------------------------------------------------------------------------------------------------------------------+ diff --git a/traffic_ops/traffic_ops_golang/logs/log.go b/traffic_ops/traffic_ops_golang/logs/log.go index c8c5569..25cbb9a 100644 --- a/traffic_ops/traffic_ops_golang/logs/log.go +++ b/traffic_ops/traffic_ops_golang/logs/log.go @@ -63,10 +63,32 @@ func Get(w http.ResponseWriter, r *http.Request) { if pLimit, ok := inf.IntParams["limit"]; ok { limit = pLimit } + setLastSeenCookie(w) + logs, count, err := getLog(inf, days, limit) + if err != nil { + api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, err, err) + return + } + api.WriteRespWithSummary(w, r, logs, count) +} + +// Get is the handler for GET requests to /logs V4.0. +func Getv40(w http.ResponseWriter, r *http.Request) { + inf, userErr, sysErr, errCode := api.NewInfo(r, nil, []string{"days", "limit"}) + if userErr != nil || sysErr != nil { + api.HandleErr(w, r, inf.Tx.Tx, errCode, userErr, sysErr) + return + } + defer inf.Close() + days := DefaultLogDays + if pDays, ok := inf.IntParams["days"]; ok { + days = pDays + } a := tc.Alerts{} setLastSeenCookie(w) - logs, count, err := getLog(inf, days, limit) + logs, count, err := getLogV40(inf, days) + if err != nil { a.AddNewAlert(tc.ErrorLevel, err.Error()) api.WriteAlerts(w, r, http.StatusInternalServerError, a) @@ -135,11 +157,64 @@ FROM "log" as l JOIN tm_user as u ON l.tm_user = u.id` const countQuery = `SELECT count(l.tm_user) FROM log as l` +func getLogV40(inf *api.APIInfo, days int) ([]tc.Log, uint64, error) { + var count = uint64(0) + var whereCount string + + queryParamsToQueryCols := map[string]dbhelpers.WhereColumnInfo{ + "username": {Column: "u.username", Checker: nil}, + } + where, _, pagination, queryValues, errs := + dbhelpers.BuildWhereAndOrderByAndPagination(inf.Params, queryParamsToQueryCols) + + if len(errs) > 0 { + return nil, 0, util.JoinErrs(errs) + } + + timeInterval := fmt.Sprintf("l.last_updated > now() - INTERVAL '%d' DAY", days) + if where != "" { + whereCount = ", tm_user as u\n" + where + " AND l.tm_user = u.id" + where = where + " AND " + timeInterval + } else { + whereCount = where + where = "\nWHERE " + timeInterval + } + queryCount := countQuery + whereCount + rowCount, err := inf.Tx.NamedQuery(queryCount, queryValues) + if err != nil { + return nil, count, fmt.Errorf("querying log count for a given user: %w", err) + } + defer rowCount.Close() + for rowCount.Next() { + if err = rowCount.Scan(&count); err != nil { + return nil, count, fmt.Errorf("scanning logs: %w", err) + } + } + + query := selectFromQuery + where + "\n ORDER BY last_updated DESC" + pagination + rows, err := inf.Tx.NamedQuery(query, queryValues) + if err != nil { + return nil, count, fmt.Errorf("querying logs: %w", err) + } + defer rows.Close() + ls := []tc.Log{} + for rows.Next() { + l := tc.Log{} + if err = rows.Scan(&l.ID, &l.Level, &l.Message, &l.User, &l.TicketNum, &l.LastUpdated); err != nil { + return nil, count, fmt.Errorf("scanning logs: %w", err) + } + ls = append(ls, l) + } + return ls, count, nil +} + func getLog(inf *api.APIInfo, days int, limit int) ([]tc.Log, uint64, error) { var count = uint64(0) var whereCount string if _, ok := inf.Params["limit"]; !ok { - inf.Params["limit"] = strconv.Itoa(DefaultLogLimit) + if _, ok := inf.Params["days"]; !ok { + inf.Params["limit"] = strconv.Itoa(DefaultLogLimit) + } } else { inf.Params["limit"] = strconv.Itoa(limit) } diff --git a/traffic_ops/traffic_ops_golang/routing/routes.go b/traffic_ops/traffic_ops_golang/routing/routes.go index 9ae855b..5a8170c 100644 --- a/traffic_ops/traffic_ops_golang/routing/routes.go +++ b/traffic_ops/traffic_ops_golang/routing/routes.go @@ -216,7 +216,7 @@ func Routes(d ServerData) ([]Route, []RawRoute, http.Handler, error) { {api.Version{Major: 4, Minor: 0}, http.MethodPost, `divisions/?$`, api.CreateHandler(&division.TODivision{}), auth.PrivLevelOperations, Authenticated, nil, 4537138003}, {api.Version{Major: 4, Minor: 0}, http.MethodDelete, `divisions/{id}$`, api.DeleteHandler(&division.TODivision{}), auth.PrivLevelOperations, Authenticated, nil, 43253822373}, - {api.Version{Major: 4, Minor: 0}, http.MethodGet, `logs/?$`, logs.Get, auth.PrivLevelReadOnly, Authenticated, nil, 4483405503}, + {api.Version{Major: 4, Minor: 0}, http.MethodGet, `logs/?$`, logs.Getv40, auth.PrivLevelReadOnly, Authenticated, nil, 4483405503}, {api.Version{Major: 4, Minor: 0}, http.MethodGet, `logs/newcount/?$`, logs.GetNewCount, auth.PrivLevelReadOnly, Authenticated, nil, 44058330123}, //Content invalidation jobs
