You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2018/09/28 21:32:52 UTC

[GitHub] rawlinp commented on a change in pull request #2824: Add Traffic Ops Golang /steering endpoint

rawlinp commented on a change in pull request #2824: Add Traffic Ops Golang /steering endpoint
URL: https://github.com/apache/trafficcontrol/pull/2824#discussion_r221377145
 
 

 ##########
 File path: traffic_ops/traffic_ops_golang/steering/steering.go
 ##########
 @@ -0,0 +1,233 @@
+package steering
+
+/*
+ * 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"
+
+	"github.com/apache/trafficcontrol/lib/go-tc"
+	"github.com/apache/trafficcontrol/lib/go-util"
+	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
+
+	"github.com/lib/pq"
+)
+
+func Get(w http.ResponseWriter, r *http.Request) {
+	inf, userErr, sysErr, errCode := api.NewInfo(r, nil, nil)
+	if userErr != nil || sysErr != nil {
+		api.HandleErr(w, r, inf.Tx.Tx, errCode, userErr, sysErr)
+		return
+	}
+	defer inf.Close()
+
+	steering, err := findSteering(inf.Tx.Tx)
+	if err != nil {
+		api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, errors.New("steering.Get finding: "+err.Error()))
+		return
+	}
+	api.WriteResp(w, r, steering)
+}
+
+func findSteering(tx *sql.Tx) ([]tc.Steering, error) {
+	steeringData, err := getSteeringData(tx)
+	if err != nil {
+		return nil, err
+	}
+	targetIDs := steeringDataTargetIDs(steeringData)
+	steeringFilters, err := getSteeringFilters(tx, targetIDs)
+	if err != nil {
+		return nil, err
+	}
+	primaryOriginCoords, err := getPrimaryOriginCoords(tx, targetIDs)
+	if err != nil {
+		return nil, err
+	}
+
+	steerings := map[tc.DeliveryServiceName]tc.Steering{}
+
+	for _, data := range steeringData {
+		if _, ok := steerings[data.DeliveryService]; !ok {
+			steerings[data.DeliveryService] = tc.Steering{
+				DeliveryService: data.DeliveryService,
+				ClientSteering:  data.DSType == tc.DSTypeClientSteering,
+				Filters:         []tc.SteeringFilter{},         // Initialize, so JSON produces `[]` not `null` if there are no filters.
+				Targets:         []tc.SteeringSteeringTarget{}, // Initialize, so JSON produces `[]` not `null` if there are no targets.
+			}
+		}
+		steering := steerings[data.DeliveryService]
+
+		if filters, ok := steeringFilters[data.TargetID]; ok {
+			steering.Filters = append(steering.Filters, filters...)
+		}
+
+		target := tc.SteeringSteeringTarget{DeliveryService: data.TargetName}
+		switch data.Type {
+		case tc.SteeringTypeOrder:
+			target.Order = uint64(data.Value)
+		case tc.SteeringTypeWeight:
+			target.Weight = uint64(data.Value)
+		case tc.SteeringTypeGeoOrder:
+			target.GeoOrder = util.IntPtr(data.Value)
+			target.Latitude = util.FloatPtr(primaryOriginCoords[data.TargetID].Lat)
+			target.Longitude = util.FloatPtr(primaryOriginCoords[data.TargetID].Lon)
+		case tc.SteeringTypeGeoWeight:
+			target.Weight = uint64(data.Value)
+			target.GeoOrder = util.IntPtr(0)
+			target.Latitude = util.FloatPtr(primaryOriginCoords[data.TargetID].Lat)
+			target.Longitude = util.FloatPtr(primaryOriginCoords[data.TargetID].Lon)
+		}
+		steering.Targets = append(steering.Targets, target)
+		steerings[data.DeliveryService] = steering
+	}
+
+	arr := []tc.Steering{}
+	for _, steering := range steerings {
+		arr = append(arr, steering)
+	}
+	return arr, nil
+}
+
+type SteeringData struct {
+	DeliveryService tc.DeliveryServiceName
+	SteeringID      int
+	TargetName      tc.DeliveryServiceName
+	TargetID        int
+	Value           int
+	Type            tc.SteeringType
+	DSType          tc.DSType
+}
+
+func steeringDataTargetIDs(data []SteeringData) []int {
+	ids := []int{}
+	for _, d := range data {
+		ids = append(ids, d.TargetID)
+	}
+	return ids
+}
+
+func getSteeringData(tx *sql.Tx) ([]SteeringData, error) {
+	qry := `
+SELECT
+  ds.xml_id as steering_xml_id,
+  ds.id as steering_id,
+  t.xml_id as target_xml_id,
+  t.id as target_id,
+  st.value,
+  tp.name as steering_type,
+  dt.name as ds_type
+FROM
+  steering_target st
+  JOIN deliveryservice ds on ds.id = st.deliveryservice
+  JOIN deliveryservice t on t.id = st.target
+  JOIN type tp on tp.id = st.type
+  JOIN type dt on dt.id = ds.type
+ORDER BY
+  steering_xml_id,
+  target_xml_id
+`
+	rows, err := tx.Query(qry)
+	if err != nil {
+		return nil, errors.New("querying steering: " + err.Error())
+	}
+	defer rows.Close()
+	data := []SteeringData{}
+	for rows.Next() {
+		sd := SteeringData{}
+		if err := rows.Scan(&sd.DeliveryService, &sd.SteeringID, &sd.TargetName, &sd.TargetID, &sd.Value, &sd.Type, &sd.DSType); err != nil {
+			return nil, errors.New("scanning profile name parameters: " + err.Error())
 
 Review comment:
   wrong error message

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


With regards,
Apache Git Services