srijeet0406 commented on code in PR #7545:
URL: https://github.com/apache/trafficcontrol/pull/7545#discussion_r1229750378


##########
lib/go-tc/stats_summary.go:
##########
@@ -187,3 +187,148 @@ type StatsSummaryLastUpdatedAPIResponse struct {
        Response StatsSummaryLastUpdated `json:"response"`
        Alerts
 }
+
+// StatsSummaryV5 is an alias for the latest minor version for the major 
version 5.
+type StatsSummaryV5 StatsSummaryV50
+
+// StatsSummaryV50 is a summary of some kind of statistic for a CDN and/or
+// Delivery Service.
+type StatsSummaryV50 struct {
+       CDNName         *string    `json:"cdnName"  db:"cdn_name"`
+       DeliveryService *string    `json:"deliveryServiceName"  
db:"deliveryservice_name"`
+       StatName        *string    `json:"statName"  db:"stat_name"`
+       StatValue       *float64   `json:"statValue"  db:"stat_value"`
+       SummaryTime     time.Time  `json:"summaryTime"  db:"summary_time"`
+       StatDate        *time.Time `json:"statDate"  db:"stat_date"`
+}
+
+// Validate implements the
+// 
github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api.ParseValidator
+// interface.
+func (ss StatsSummaryV5) Validate(tx *sql.Tx) error {
+       errs := tovalidate.ToErrors(validation.Errors{
+               "statName":  validation.Validate(ss.StatName, 
validation.Required),
+               "statValue": validation.Validate(ss.StatValue, 
validation.Required),
+       })
+       return util.JoinErrs(errs)
+}
+
+// UnmarshalJSON implements the encoding/json.Unmarshaler interface with a
+// customized decoding to force the date format on StatDate.
+func (ss *StatsSummaryV5) UnmarshalJSON(data []byte) error {
+       type Alias StatsSummary
+       resp := struct {
+               SummaryTime string  `json:"summaryTime"`
+               StatDate    *string `json:"statDate"`
+               *Alias
+       }{
+               Alias: (*Alias)(ss),
+       }
+       err := json.Unmarshal(data, &resp)
+       if err != nil {
+               return err
+       }
+       if resp.StatDate != nil {
+               statDate, err := parseTimeV5(*resp.StatDate)
+               if err != nil {
+                       return errors.New("invalid timestamp given for 
statDate")

Review Comment:
   Could you add the error detail here?



##########
lib/go-tc/stats_summary.go:
##########
@@ -187,3 +187,148 @@ type StatsSummaryLastUpdatedAPIResponse struct {
        Response StatsSummaryLastUpdated `json:"response"`
        Alerts
 }
+
+// StatsSummaryV5 is an alias for the latest minor version for the major 
version 5.
+type StatsSummaryV5 StatsSummaryV50
+
+// StatsSummaryV50 is a summary of some kind of statistic for a CDN and/or
+// Delivery Service.
+type StatsSummaryV50 struct {
+       CDNName         *string    `json:"cdnName"  db:"cdn_name"`
+       DeliveryService *string    `json:"deliveryServiceName"  
db:"deliveryservice_name"`
+       StatName        *string    `json:"statName"  db:"stat_name"`
+       StatValue       *float64   `json:"statValue"  db:"stat_value"`
+       SummaryTime     time.Time  `json:"summaryTime"  db:"summary_time"`
+       StatDate        *time.Time `json:"statDate"  db:"stat_date"`
+}
+
+// Validate implements the
+// 
github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api.ParseValidator
+// interface.
+func (ss StatsSummaryV5) Validate(tx *sql.Tx) error {
+       errs := tovalidate.ToErrors(validation.Errors{
+               "statName":  validation.Validate(ss.StatName, 
validation.Required),
+               "statValue": validation.Validate(ss.StatValue, 
validation.Required),
+       })
+       return util.JoinErrs(errs)
+}
+
+// UnmarshalJSON implements the encoding/json.Unmarshaler interface with a
+// customized decoding to force the date format on StatDate.
+func (ss *StatsSummaryV5) UnmarshalJSON(data []byte) error {
+       type Alias StatsSummary
+       resp := struct {
+               SummaryTime string  `json:"summaryTime"`
+               StatDate    *string `json:"statDate"`
+               *Alias
+       }{
+               Alias: (*Alias)(ss),
+       }
+       err := json.Unmarshal(data, &resp)
+       if err != nil {
+               return err
+       }
+       if resp.StatDate != nil {
+               statDate, err := parseTimeV5(*resp.StatDate)
+               if err != nil {
+                       return errors.New("invalid timestamp given for 
statDate")
+               }
+               ss.StatDate = &statDate
+       }
+
+       ss.SummaryTime, err = parseTimeV5(resp.SummaryTime)
+       if err != nil {
+               return errors.New("invalid timestamp given for summaryTime")

Review Comment:
   Same comment here.



##########
lib/go-tc/stats_summary.go:
##########
@@ -187,3 +187,148 @@ type StatsSummaryLastUpdatedAPIResponse struct {
        Response StatsSummaryLastUpdated `json:"response"`
        Alerts
 }
+
+// StatsSummaryV5 is an alias for the latest minor version for the major 
version 5.
+type StatsSummaryV5 StatsSummaryV50
+
+// StatsSummaryV50 is a summary of some kind of statistic for a CDN and/or
+// Delivery Service.
+type StatsSummaryV50 struct {
+       CDNName         *string    `json:"cdnName"  db:"cdn_name"`
+       DeliveryService *string    `json:"deliveryServiceName"  
db:"deliveryservice_name"`
+       StatName        *string    `json:"statName"  db:"stat_name"`
+       StatValue       *float64   `json:"statValue"  db:"stat_value"`
+       SummaryTime     time.Time  `json:"summaryTime"  db:"summary_time"`
+       StatDate        *time.Time `json:"statDate"  db:"stat_date"`
+}
+
+// Validate implements the
+// 
github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api.ParseValidator
+// interface.
+func (ss StatsSummaryV5) Validate(tx *sql.Tx) error {
+       errs := tovalidate.ToErrors(validation.Errors{
+               "statName":  validation.Validate(ss.StatName, 
validation.Required),
+               "statValue": validation.Validate(ss.StatValue, 
validation.Required),
+       })
+       return util.JoinErrs(errs)
+}
+
+// UnmarshalJSON implements the encoding/json.Unmarshaler interface with a
+// customized decoding to force the date format on StatDate.
+func (ss *StatsSummaryV5) UnmarshalJSON(data []byte) error {
+       type Alias StatsSummary
+       resp := struct {
+               SummaryTime string  `json:"summaryTime"`
+               StatDate    *string `json:"statDate"`
+               *Alias
+       }{
+               Alias: (*Alias)(ss),
+       }
+       err := json.Unmarshal(data, &resp)
+       if err != nil {
+               return err
+       }
+       if resp.StatDate != nil {
+               statDate, err := parseTimeV5(*resp.StatDate)
+               if err != nil {
+                       return errors.New("invalid timestamp given for 
statDate")
+               }
+               ss.StatDate = &statDate
+       }
+
+       ss.SummaryTime, err = parseTimeV5(resp.SummaryTime)
+       if err != nil {
+               return errors.New("invalid timestamp given for summaryTime")
+       }
+       return nil
+}
+
+func parseTimeV5(ts string) (time.Time, error) {
+       rt, err := time.Parse(time.RFC3339, ts)
+       if err == nil {
+               return rt, err
+       }
+       return time.Parse(dateFormat, ts)
+}
+
+// MarshalJSON implements the encoding/json.Marshaler interface with a
+// customized encoding to force the date format on StatDate.
+func (ss StatsSummaryV5) MarshalJSON() ([]byte, error) {
+       type Alias StatsSummaryV5
+       resp := struct {
+               StatDate    *string `json:"statDate"`
+               SummaryTime string  `json:"summaryTime"`
+               Alias
+       }{
+               SummaryTime: ss.SummaryTime.Format(time.RFC3339),
+               Alias:       (Alias)(ss),
+       }
+       if ss.StatDate != nil {
+               resp.StatDate = util.Ptr(ss.StatDate.Format(dateFormat))

Review Comment:
   Shouldn't this also be RFC3339?



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to