rawlinp commented on a change in pull request #4013: Add TO-Go /api/1.1/servers/status GET handler URL: https://github.com/apache/trafficcontrol/pull/4013#discussion_r338767825
########## File path: traffic_ops/traffic_ops_golang/server/status_count.go ########## @@ -0,0 +1,70 @@ +package server + +/* + * 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/traffic_ops/traffic_ops_golang/api" +) + +func GetServersStatusCountsHandler(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() + + statusCounts, err := getServersStatusCounts(inf.Tx.Tx, inf.Params["type"]) + if err != nil { + api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, errors.New("getting servers status counts: "+err.Error())) + return + } + api.WriteResp(w, r, statusCounts) +} + +func getServersStatusCounts(tx *sql.Tx, typeName string) (map[string]int, error) { + q := ` +SELECT status.name, count(server.id) +FROM server +JOIN status ON server.status = status.id +JOIN type ON server.type = type.id +WHERE type.name ~ $1 Review comment: to replicate the Perl version ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services