ChrisHines commented on a change in pull request #4114: Rewrite current stats 
from Perl to Golang
URL: https://github.com/apache/trafficcontrol/pull/4114#discussion_r349183700
 
 

 ##########
 File path: traffic_ops/traffic_ops_golang/trafficstats/cdn.go
 ##########
 @@ -0,0 +1,157 @@
+package trafficstats
+
+/*
+ * 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 (
+       "errors"
+       "fmt"
+       "net/http"
+
+       influx "github.com/influxdata/influxdb/client/v2"
+       "github.com/lib/pq"
+
+       "github.com/apache/trafficcontrol/lib/go-tc"
+       "github.com/apache/trafficcontrol/lib/go-util"
+       "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
+)
+
+const (
+       cdnStatsQuery = `
+SELECT last(value) FROM "%s"."monthly"."%s"
+       WHERE cdn = $cdn`
+)
+
+// GetCurrentStats handler for getting current stats for CDNs
+func GetCurrentStats(w http.ResponseWriter, r *http.Request) {
+       inf, userErr, sysErr, errCode := api.NewInfo(r, nil, nil)
+       tx := inf.Tx.Tx
+
+       if userErr != nil || sysErr != nil {
+               api.HandleErr(w, r, tx, errCode, userErr, sysErr)
+               return
+       }
+       defer inf.Close()
+
+       client, err := inf.CreateInfluxClient()
+       if err != nil {
+               api.HandleErr(w, r, tx, http.StatusInternalServerError, nil, 
err)
+               return
+       } else if client == nil {
+               sysErr = errors.New("Traffic Stats is not configured, but DS 
stats were requested")
+               api.HandleErr(w, r, tx, http.StatusInternalServerError, nil, 
sysErr)
+               return
+       }
+       defer (*client).Close()
+
+       currentStats := []interface{}{}
+
+       // Get CDN names
+       cdns := []string{}
+       if err := tx.QueryRow(`SELECT ARRAY(SELECT name FROM 
cdn)`).Scan(pq.Array(&cdns)); err != nil {
+               api.HandleErr(w, r, tx, http.StatusInternalServerError, nil, 
errors.New("querying cdn names"))
+               return
+       }
+       totalStats := tc.TrafficStatsTotalStats{
+               CDN: "total",
+       }
+       // util func to add to total
+       addToTotal := func(v, t *float64) *float64 {
+               if v == nil {
+                       return t
+               }
+               if t == nil {
+                       t = util.FloatPtr(0.0)
+               }
+               *t += *v
+               return t
+       }
 
 Review comment:
   Consider reversing the order of the parameters so that they model the `+=` 
operator. Put the total first and the increment second as in `func(t, v 
*float64) *float64`.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to