rob05c commented on a change in pull request #2306: Add TO Go cdns/capacity URL: https://github.com/apache/trafficcontrol/pull/2306#discussion_r316785557
########## File path: traffic_ops/traffic_ops_golang/cdn/capacity.go ########## @@ -0,0 +1,325 @@ +package cdn + +/* + * 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" + "encoding/json" + "errors" + "net/http" + "net/url" + "strconv" + "strings" + "time" + + "github.com/apache/trafficcontrol/lib/go-tc" + "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api" +) + +func GetCapacity(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() + + api.RespWriter(w, r, inf.Tx.Tx)(getCapacity(inf.Tx.Tx)) +} + +const MonitorProxyParameter = "tm.traffic_mon_fwd_proxy" +const MonitorRequestTimeout = time.Second * 10 +const MonitorOnlineStatus = "ONLINE" + +// CRStates contains the Monitor CRStates members needed for health. It is NOT the full object served by the Monitor, but only the data required by this endpoint. +type CRStates struct { + Caches map[tc.CacheName]Available `json:"caches"` +} + +type Available struct { + IsAvailable bool `json:"isAvailable"` +} + +// CRConfig contains the Monitor CRConfig members needed for health. It is NOT the full object served by the Monitor, but only the data required by this endpoint. +type CRConfig struct { + ContentServers map[tc.CacheName]CRConfigServer `json:"contentServers"` +} + +type CRConfigServer struct { + CacheGroup tc.CacheGroupName `json:"locationId"` + Status tc.CacheStatus `json:"status"` + Type tc.CacheType `json:"type"` + Profile string `json:"profile"` +} + +func getCapacity(tx *sql.Tx) (CapacityResp, error) { + monitors, err := getCDNMonitorFQDNs(tx) + if err != nil { + return CapacityResp{}, errors.New("getting monitors: " + err.Error()) + } + return getMonitorsCapacity(tx, monitors) +} + +type CapacityResp struct { + AvailablePercent float64 `json:"availablePercent"` + UnavailablePercent float64 `json:"unavailablePercent"` + UtilizedPercent float64 `json:utilizedPercent"` + MaintenancePercent float64 `json:maintenancePercent"` +} + +type CapData struct { + Available float64 + Unavailable float64 + Utilized float64 + Maintenance float64 + Capacity float64 +} + +func getMonitorsCapacity(tx *sql.Tx, monitors map[tc.CDNName]string) (CapacityResp, error) { + monitorForwardProxy, monitorForwardProxyExists, err := getGlobalParam(tx, MonitorProxyParameter) + if err != nil { + return CapacityResp{}, errors.New("getting global monitor proxy parameter: " + err.Error()) + } + client := &http.Client{Timeout: MonitorRequestTimeout} + if monitorForwardProxyExists { + proxyURI, err := url.Parse(monitorForwardProxy) + if err != nil { + return CapacityResp{}, errors.New("monitor forward proxy '" + monitorForwardProxy + "' in parameter '" + MonitorProxyParameter + "' not a URI: " + err.Error()) + } + client = &http.Client{Timeout: MonitorRequestTimeout, Transport: &http.Transport{Proxy: http.ProxyURL(proxyURI)}} + } + + thresholds, err := getEdgeProfileHealthThresholdBandwidth(tx) + if err != nil { + return CapacityResp{}, errors.New("getting profile thresholds: " + err.Error()) + } + + cap := CapData{} + for cdn, monitorFQDN := range monitors { + crStates, err := getCRStates(monitorFQDN, client) + // TODO on err, try another online monitor Review comment: I think this was written to behave exactly like Perl. But that does seem like a pretty safe change, I can go ahead and make it now if you want ---------------------------------------------------------------- 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
