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 2019/09/26 19:31:29 UTC

[GitHub] [trafficcontrol] rob05c commented on a change in pull request #3758: Rewrote deliveryservice_stats to Go

rob05c commented on a change in pull request #3758: Rewrote deliveryservice_stats to Go
URL: https://github.com/apache/trafficcontrol/pull/3758#discussion_r328788587
 
 

 ##########
 File path: lib/go-tc/traffic_stats.go
 ##########
 @@ -0,0 +1,148 @@
+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.
+ */
+
+import "strings"
+
+// TRAFFIC_STATS_VERSION was supposed to be the "API version", but actually the plugin (this route
+// used to be a plugin in Perl) always returned this static value
+const TRAFFIC_STATS_VERSION = "1.2"
+
+// TRAFFIC_STATS_SOURCE is the value of the "source" field in an API response. Perl always returned
+// source="TrafficStats", so we do too
+const TRAFFIC_STATS_SOURCE = "TrafficStats"
+
+// TrafficStatsDuration reflects all the possible durations that can be requested via the
+// deliveryservice_stats endpoint
+type TrafficStatsDuration string
+
+const (
+	InvalidDuration TrafficStatsDuration = ""
+	OneMinute       TrafficStatsDuration = "1m"
+	FiveMinutes     TrafficStatsDuration = "5m"
+	OneHour         TrafficStatsDuration = "1h"
+	SixHours        TrafficStatsDuration = "6h"
+	OneDay          TrafficStatsDuration = "1d"
+	OneWeek         TrafficStatsDuration = "1w"
+	OneMonth        TrafficStatsDuration = "4w"
+)
+
+// TrafficStatsDurationFromString converts the given string into the appropriate
+// TrafficStatsDuration if possible - returns the InvalidDuration constant if not.
+func TrafficStatsDurationFromString(v string) TrafficStatsDuration {
+	switch TrafficStatsDuration(strings.Trim(v, " ")) {
+	case OneMinute:
+		return OneMinute
+	case FiveMinutes:
+		return FiveMinutes
+	case OneHour:
+		return OneHour
+	case SixHours:
+		return SixHours
+	case OneDay:
+		return OneDay
+	case OneWeek:
+		return OneWeek
+	case OneMonth:
+		return OneMonth
+	}
+	return InvalidDuration
+}
+
+// Seconds returns the number of seconds to which a TrafficStatsDuration is equivalent.
+// For invalid objects, it returns -1 - otherwise it will always, of course be > 0.
+func (d TrafficStatsDuration) Seconds() int64 {
+	switch d {
 
 Review comment:
   I like functions, because they can't be accidentally changed. Someone could accidentally change a global variable, because Go doesn't allow const maps.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services