This repository was archived by the owner on Nov 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 356
Fixes Stats Summary V5 apis to respond with RFC3339 date/time Format #7545
Merged
srijeet0406
merged 23 commits into
apache:master
from
jagan-parthiban:improve/rfc3339-stats-summary
Jul 5, 2023
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
58013db
Fixes https://github.com/apache/trafficcontrol/issues/7544 Updated V5…
jagan-parthiban cf68b85
Fixes https://github.com/apache/trafficcontrol/issues/7544 Updated In…
jagan-parthiban 002d80f
Fixes https://github.com/apache/trafficcontrol/issues/7544 Added Unit…
jagan-parthiban db8a54b
CHANGELOG.md Update
jagan-parthiban f1c71ea
Documentation Update
jagan-parthiban 09f8924
Fixed PR review comments
jagan-parthiban 4869b8e
Use SOH timestamp to calculate bandwidth in TM (#7539)
rimashah25 e79e87a
Fixed PR review comments for https://github.com/apache/trafficcontrol…
jagan-parthiban 7a008a9
Fixed CHANGELOG.md for https://github.com/apache/trafficcontrol/issue…
jagan-parthiban 04f6f74
Fixed CHANGELOG.md for https://github.com/apache/trafficcontrol/issue…
jagan-parthiban f87d693
Fixed CHANGELOG.md for https://github.com/apache/trafficcontrol/issue…
jagan-parthiban bb287cc
Fixed PR review comments for https://github.com/apache/trafficcontrol…
jagan-parthiban 9f8fe05
Fixed PR Review comments
jagan-parthiban 6974d89
Fixed CHANGELOG.md
jagan-parthiban 02c4a53
Update stats_summary lib
jagan-parthiban a7e490a
Updated statessummary file
jagan-parthiban 1676dc6
Updated unit test cases
jagan-parthiban 33088b7
Temp changes to check GHA
jagan-parthiban 789d98b
Temp changes to check GHA
jagan-parthiban 200b082
revert Temp changes to check GHA
jagan-parthiban d057485
Updated CHANGELOG.md
jagan-parthiban 9f3a6dd
Updated GHA failing test case for testing.
jagan-parthiban 2c7b7d4
Updated GHA failing test case.
jagan-parthiban File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ import ( | |
| "database/sql" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/apache/trafficcontrol/lib/go-tc/tovalidate" | ||
|
|
@@ -187,3 +188,149 @@ 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"` | ||
|
jagan-parthiban marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // 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 StatsSummaryV5 | ||
| 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 fmt.Errorf("invalid timestamp given for statDate: %v", err) | ||
| } | ||
| ss.StatDate = &statDate | ||
| } | ||
|
|
||
| ss.SummaryTime, err = parseTimeV5(resp.SummaryTime) | ||
| if err != nil { | ||
| return fmt.Errorf("invalid timestamp given for summaryTime: %v", err) | ||
| } | ||
| 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)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this also be RFC3339?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. RFC3339 will return date and time. but the expectation is only the date. So as used in the v4 function, in v5 aswell we are using dateFormat
|
||
| } | ||
| return json.Marshal(&resp) | ||
| } | ||
|
|
||
| // StatsSummaryResponseV5 is an alias for the latest minor version for the major version 5. | ||
| type StatsSummaryResponseV5 StatsSummaryResponseV50 | ||
|
|
||
| // StatsSummaryResponseV50 is the structure of a response from Traffic Ops to | ||
| // GET requests made to its /stats_summary V5 API endpoint. | ||
| type StatsSummaryResponseV50 struct { | ||
| Response []StatsSummaryV5 `json:"response"` | ||
| Alerts | ||
| } | ||
|
|
||
| // StatsSummaryLastUpdatedV5 is an alias for the latest minor version for the major version 5. | ||
| type StatsSummaryLastUpdatedV5 StatsSummaryLastUpdatedV50 | ||
|
|
||
| // StatsSummaryLastUpdatedV50 is the type of the `response` property of a response | ||
| // from Traffic Ops to a GET request made to its /stats_summary endpoint when | ||
| // the 'lastSummaryDate' query string parameter is passed as 'true'. | ||
| type StatsSummaryLastUpdatedV50 struct { | ||
| SummaryTime *time.Time `json:"summaryTime" db:"summary_time"` | ||
| } | ||
|
|
||
| // MarshalJSON implements the encoding/json.Marshaler interface with a | ||
| // customized encoding to force the date format on SummaryTime. | ||
| func (ss StatsSummaryLastUpdatedV5) MarshalJSON() ([]byte, error) { | ||
| resp := struct { | ||
| SummaryTime *string `json:"summaryTime"` | ||
| }{} | ||
| if ss.SummaryTime != nil { | ||
| resp.SummaryTime = util.Ptr(ss.SummaryTime.Format(time.RFC3339)) | ||
| } | ||
| return json.Marshal(&resp) | ||
| } | ||
|
|
||
| // UnmarshalJSON implements the encoding/json.Unmarshaler interface with a | ||
| // customized decoding to force the SummaryTime format. | ||
| func (ss *StatsSummaryLastUpdatedV5) UnmarshalJSON(data []byte) error { | ||
| resp := struct { | ||
| SummaryTime *string `json:"summaryTime"` | ||
| }{} | ||
| err := json.Unmarshal(data, &resp) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if resp.SummaryTime != nil { | ||
| var summaryTime time.Time | ||
| summaryTime, err = time.Parse(time.RFC3339, *resp.SummaryTime) | ||
| if err == nil { | ||
| ss.SummaryTime = &summaryTime | ||
| return nil | ||
| } | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // StatsSummaryLastUpdatedAPIResponseV5 is an alias for the latest minor version for the major version 5. | ||
| type StatsSummaryLastUpdatedAPIResponseV5 StatsSummaryLastUpdatedAPIResponseV50 | ||
|
|
||
| // StatsSummaryLastUpdatedAPIResponseV50 is the type of a response from Traffic | ||
| // Ops to a request to its /stats_summary endpoint with the 'lastSummaryDate' | ||
| // query string parameter set to 'true'. | ||
| type StatsSummaryLastUpdatedAPIResponseV50 struct { | ||
| Response StatsSummaryLastUpdatedV5 `json:"response"` | ||
| Alerts | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.