You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by mi...@apache.org on 2019/08/05 15:41:47 UTC

[trafficcontrol] branch master updated: Add TO Go Logs (#2356)

This is an automated email from the ASF dual-hosted git repository.

mitchell852 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new c9c1dde  Add TO Go Logs (#2356)
c9c1dde is described below

commit c9c1dded5a10bf004fd9bbefc7a1844f34baa235
Author: Robert Butts <ro...@users.noreply.github.com>
AuthorDate: Mon Aug 5 09:41:42 2019 -0600

    Add TO Go Logs (#2356)
    
    * Add TO Go Logs
    
    * Add TO Go logs/newcount
    
    * adds a couple of tests for the logs endpoint
    
    * Fix GoDoc comments, gofmt
---
 lib/go-tc/log.go                                 |  37 +++++++
 traffic_ops/client/log.go                        |  61 +++++++++++
 traffic_ops/testing/api/v14/logs_test.go         |  44 ++++++++
 traffic_ops/traffic_ops_golang/logs/log.go       | 132 +++++++++++++++++++++++
 traffic_ops/traffic_ops_golang/routing/routes.go |   5 +
 5 files changed, 279 insertions(+)

diff --git a/lib/go-tc/log.go b/lib/go-tc/log.go
new file mode 100644
index 0000000..7ba8a3e
--- /dev/null
+++ b/lib/go-tc/log.go
@@ -0,0 +1,37 @@
+package tc
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+type LogsResponse struct {
+	Response []Log `json:"response"`
+}
+
+type Log struct {
+	ID           *int       `json:"id"`
+	LastUpdated  *Time      `json:"lastUpdated"`
+	Level        *string    `json:"level"`
+	Message      *string    `json:"message"`
+	TicketNum    *int       `json:"ticketNum"`
+	User         *string    `json:"user"`
+}
+
+type NewLogCountResp struct {
+	NewLogCount uint64 `json:"newLogcount"`
+}
diff --git a/traffic_ops/client/log.go b/traffic_ops/client/log.go
new file mode 100644
index 0000000..f895510
--- /dev/null
+++ b/traffic_ops/client/log.go
@@ -0,0 +1,61 @@
+/*
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+package client
+
+import (
+	"encoding/json"
+	"fmt"
+	"net/http"
+
+	"github.com/apache/trafficcontrol/lib/go-tc"
+)
+
+const (
+	API_v14_Logs = "/api/1.4/logs"
+)
+
+// GetLogsByQueryParams gets a list of logs filtered by query params.
+func (to *Session) GetLogsByQueryParams(queryParams string) ([]tc.Log, ReqInf, error) {
+	URI := API_v14_Logs + queryParams
+	resp, remoteAddr, err := to.request(http.MethodGet, URI, nil)
+	reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
+	if err != nil {
+		return nil, reqInf, err
+	}
+	defer resp.Body.Close()
+
+	var data tc.LogsResponse
+	if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
+		return nil, reqInf, err
+	}
+
+	return data.Response, reqInf, nil
+}
+
+// GetLogs gets a list of logs.
+func (to *Session) GetLogs() ([]tc.Log, ReqInf, error) {
+	return to.GetLogsByQueryParams("")
+}
+
+// GetLogsByLimit gets a list of logs limited to a certain number of logs.
+func (to *Session) GetLogsByLimit(limit int) ([]tc.Log, ReqInf, error) {
+	return to.GetLogsByQueryParams(fmt.Sprintf("?limit=%d", limit))
+}
+
+// GetLogsByDays gets a list of logs limited to a certain number of days.
+func (to *Session) GetLogsByDays(days int) ([]tc.Log, ReqInf, error) {
+	return to.GetLogsByQueryParams(fmt.Sprintf("?days=%d", days))
+}
diff --git a/traffic_ops/testing/api/v14/logs_test.go b/traffic_ops/testing/api/v14/logs_test.go
new file mode 100644
index 0000000..959062d
--- /dev/null
+++ b/traffic_ops/testing/api/v14/logs_test.go
@@ -0,0 +1,44 @@
+package v14
+
+/*
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+import (
+	"testing"
+)
+
+func TestLogs(t *testing.T) {
+	WithObjs(t, []TCObj{}, func() {
+		GetTestLogs(t)
+		GetTestLogsByLimit(t)
+	})
+}
+
+func GetTestLogs(t *testing.T) {
+	_, _, err := TOSession.GetLogs()
+	if err != nil {
+		t.Fatalf("error getting logs: " + err.Error())
+	}
+}
+
+func GetTestLogsByLimit(t *testing.T) {
+	toLogs, _, err := TOSession.GetLogsByLimit(10)
+	if err != nil {
+		t.Fatalf("error getting logs: " + err.Error())
+	}
+	if len(toLogs) != 10 {
+		t.Fatalf("GET logs by limit: incorrect number of logs returned\n")
+	}
+}
diff --git a/traffic_ops/traffic_ops_golang/logs/log.go b/traffic_ops/traffic_ops_golang/logs/log.go
new file mode 100644
index 0000000..cac5af1
--- /dev/null
+++ b/traffic_ops/traffic_ops_golang/logs/log.go
@@ -0,0 +1,132 @@
+package logs
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import (
+	"database/sql"
+	"errors"
+	"net/http"
+	"time"
+
+	"github.com/apache/trafficcontrol/lib/go-tc"
+	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
+)
+
+const DefaultLogLimit = 1000
+const DefaultLogLimitForDays = 1000000
+const DefaultLogDays = 30
+
+func Get(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()
+
+	limit := DefaultLogLimit
+	days := DefaultLogDays
+	if pDays, ok := inf.IntParams["days"]; ok {
+		days = pDays
+		limit = DefaultLogLimitForDays
+	}
+	if pLimit, ok := inf.IntParams["limit"]; ok {
+		limit = pLimit
+	}
+
+	setLastSeenCookie(w)
+	api.RespWriter(w, r, inf.Tx.Tx)(getLog(inf.Tx.Tx, days, limit))
+}
+
+func GetNewCount(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()
+
+	lastSeen, ok := getLastSeenCookie(r)
+	if !ok {
+		setLastSeenCookie(w) // only set the cookie if it didn't exist; emulates old Perl behavior
+		api.WriteResp(w, r, tc.NewLogCountResp{NewLogCount: 0})
+		return
+	}
+	newCount, err := getLogCountSince(inf.Tx.Tx, lastSeen)
+	if err != nil {
+		api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, errors.New("getting log new count: "+err.Error()))
+		return
+	}
+	api.WriteResp(w, r, tc.NewLogCountResp{NewLogCount: newCount})
+}
+
+const LastSeenLogCookieName = "last_seen_log"
+
+func setLastSeenCookie(w http.ResponseWriter) {
+	http.SetCookie(w, &http.Cookie{
+		Name:    LastSeenLogCookieName,
+		Value:   time.Now().Format(time.RFC3339Nano),
+		Expires: time.Now().Add(time.Hour * 24 * 7),
+		Path:    "/",
+	})
+}
+
+// getLastSeenCookie returns the time of the last log seen cookie, or whether the cookie didn't exist or there was an error parsing it. Errors are not logged or returned, only that the cookie could not be retrieved.
+func getLastSeenCookie(r *http.Request) (time.Time, bool) {
+	cookie, err := r.Cookie(LastSeenLogCookieName)
+	if err != nil {
+		return time.Time{}, false
+	}
+	lastSeen, err := time.Parse(time.RFC3339Nano, cookie.Value)
+	if err != nil {
+		return time.Time{}, false
+	}
+	return lastSeen, true
+}
+
+func getLog(tx *sql.Tx, days int, limit int) ([]tc.Log, error) {
+	rows, err := tx.Query(`
+SELECT l.id, l.level, l.message, u.username as user, l.ticketnum, l.last_updated
+FROM "log" as l JOIN tm_user as u ON l.tm_user = u.id
+WHERE l.last_updated > now() - ($1 || ' DAY')::INTERVAL
+ORDER BY l.last_updated DESC
+LIMIT $2
+`, days, limit)
+	if err != nil {
+		return nil, errors.New("querying logs: " + err.Error())
+	}
+	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, errors.New("scanning logs: " + err.Error())
+		}
+		ls = append(ls, l)
+	}
+	return ls, nil
+}
+
+func getLogCountSince(tx *sql.Tx, since time.Time) (uint64, error) {
+	count := uint64(0)
+	if err := tx.QueryRow(`SELECT count(*) from log where last_updated > $1`, since).Scan(&count); err != nil {
+		return 0, errors.New("querying log last seen count: " + err.Error())
+	}
+	return count, nil
+}
diff --git a/traffic_ops/traffic_ops_golang/routing/routes.go b/traffic_ops/traffic_ops_golang/routing/routes.go
index 3cfd7fc..5feac7b 100644
--- a/traffic_ops/traffic_ops_golang/routing/routes.go
+++ b/traffic_ops/traffic_ops_golang/routing/routes.go
@@ -54,6 +54,7 @@ import (
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/federations"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/hwinfo"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/login"
+	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/logs"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/origin"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/parameter"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/physlocation"
@@ -155,6 +156,10 @@ func Routes(d ServerData) ([]Route, []RawRoute, http.Handler, error) {
 		{1.1, http.MethodDelete, `divisions/{id}$`, api.DeleteHandler(&division.TODivision{}), auth.PrivLevelOperations, Authenticated, nil},
 		{1.1, http.MethodGet, `divisions/name/{name}/?(\.json)?$`, api.ReadHandler(&division.TODivision{}), auth.PrivLevelReadOnly, Authenticated, nil},
 
+		{1.1, http.MethodGet, `logs/?(\.json)?$`, logs.Get, auth.PrivLevelReadOnly, Authenticated, nil},
+		{1.1, http.MethodGet, `logs/{days}/days/?(\.json)?$`, logs.Get, auth.PrivLevelReadOnly, Authenticated, nil},
+		{1.1, http.MethodGet, `logs/newcount/?(\.json)?$`, logs.GetNewCount, auth.PrivLevelReadOnly, Authenticated, nil},
+
 		//HWInfo
 		{1.1, http.MethodGet, `hwinfo-wip/?(\.json)?$`, hwinfo.Get, auth.PrivLevelReadOnly, Authenticated, nil},