This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support units in Data Explorer and Custom Charting tiles (#939)
* Add `MetricsUnitsClient` Signed-off-by: Arthur Pitman <[email protected]> * Add `ConvertUnitMetricsProcessingDecorator` Signed-off-by: Arthur Pitman <[email protected]> * Remove unneeded Data Explorer tile fields Signed-off-by: Arthur Pitman <[email protected]> * Rename several dashboard types Signed-off-by: Arthur Pitman <[email protected]> * Collect multiple Data Explorer configuration problems into a single error Signed-off-by: Arthur Pitman <[email protected]> * Enable units support in Data Explorer and Custom Charting Signed-off-by: Arthur Pitman <[email protected]> * Improve handling of multiple visualization rules for a single query Signed-off-by: Arthur Pitman <[email protected]> Signed-off-by: Arthur Pitman <[email protected]>
- Loading branch information
1 parent
d52df15
commit e32dd8a
Showing
43 changed files
with
3,224 additions
and
224 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package dynatrace | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/url" | ||
"strconv" | ||
) | ||
|
||
// MetricsUnitsPath is the base endpoint for Metrics Units API | ||
const MetricsUnitsPath = "/api/v2/units" | ||
|
||
// UnitConversionResult is the result of a conversion. | ||
type UnitConversionResult struct { | ||
UnitID string `json:"unitId"` | ||
ResultValue float64 `json:"resultValue"` | ||
} | ||
|
||
const ( | ||
valueKey = "value" | ||
targetUnitKey = "targetUnit" | ||
) | ||
|
||
// MetricsUnitsClientConvertRequest encapsulates the request for the MetricsUnitsClient's Convert method. | ||
type MetricsUnitsClientConvertRequest struct { | ||
sourceUnitID string | ||
value float64 | ||
targetUnitID string | ||
} | ||
|
||
// NewMetricsUnitsClientConvertRequest creates a new MetricsUnitsClientConvertRequest. | ||
func NewMetricsUnitsClientConvertRequest(sourceUnitID string, value float64, targetUnitID string) MetricsUnitsClientConvertRequest { | ||
return MetricsUnitsClientConvertRequest{ | ||
sourceUnitID: sourceUnitID, | ||
value: value, | ||
targetUnitID: targetUnitID, | ||
} | ||
} | ||
|
||
// RequestString encodes MetricsUnitsClientConvertRequest into a request string. | ||
func (q *MetricsUnitsClientConvertRequest) RequestString() string { | ||
queryParameters := newQueryParameters() | ||
queryParameters.add(valueKey, strconv.FormatFloat(q.value, 'f', -1, 64)) | ||
queryParameters.add(targetUnitKey, q.targetUnitID) | ||
|
||
return MetricsUnitsPath + "/" + url.PathEscape(q.sourceUnitID) + "/convert?" + queryParameters.encode() | ||
} | ||
|
||
// MetricsUnitsClientInterface defines functions for the Dynatrace Metrics Units endpoint. | ||
type MetricsUnitsClientInterface interface { | ||
// Convert converts a value between the specified units. | ||
Convert(ctx context.Context, request MetricsUnitsClientConvertRequest) (float64, error) | ||
} | ||
|
||
// MetricsUnitsClient is a client for interacting with Dynatrace Metrics Units endpoint. | ||
type MetricsUnitsClient struct { | ||
client ClientInterface | ||
} | ||
|
||
// NewMetricsUnitsClient creates a new MetricsUnitsClient | ||
func NewMetricsUnitsClient(client ClientInterface) *MetricsUnitsClient { | ||
return &MetricsUnitsClient{ | ||
client: client, | ||
} | ||
} | ||
|
||
// Convert converts a value between the specified units. | ||
func (c *MetricsUnitsClient) Convert(ctx context.Context, request MetricsUnitsClientConvertRequest) (float64, error) { | ||
body, err := c.client.Get(ctx, request.RequestString()) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
var result UnitConversionResult | ||
err = json.Unmarshal(body, &result) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return result.ResultValue, nil | ||
} |
This file contains 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.