Repository: cdklabs/cdk-monitoring-constructs (language: python)

Issue:
Add Ability to Override Period to Time Range on Bar and Pie Charts for monitorCustom()
### Feature scope

Custom

### Describe your suggested feature

Currently when you pass a `CustomMetricGroup` to `monitorCustom()` you can specify `graphWidgetType` including PIE and BAR as options.

By default, PIE and BAR graphs in CloudWatch display information for only the most recent metric period, rather than the time range of the dashboard [[ref]](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_cloudwatch.GraphWidgetProps.html#setperiodtotimerange). The `GraphWidget` CDK construct allows one to set a flag, `setPeriodToTimeRange` to override this behavior, however there is no way to do so with the graphs generated using this package.


Discussion / hints:
Hi, is there any ETA for completing this feature? We took a dependency on this library and now we have a business usecase for this feature. Its hard to move out or create a new dashboard just for this purpose. If it is not too difficult, can we please add this feature?

Fix patch:
diff --git a/API.md b/API.md
index c1c25d45..7f9d0583 100644
--- a/API.md
+++ b/API.md
@@ -12155,6 +12155,7 @@ const customMetricGroup: CustomMetricGroup = { ... }
 | <code><a href="#cdk-monitoring-constructs.CustomMetricGroup.property.graphWidgetAxis">graphWidgetAxis</a></code> | <code>aws-cdk-lib.aws_cloudwatch.YAxisProps</code> | optional axis. |
 | <code><a href="#cdk-monitoring-constructs.CustomMetricGroup.property.graphWidgetLegend">graphWidgetLegend</a></code> | <code>aws-cdk-lib.aws_cloudwatch.LegendPosition</code> | graph widget legend. |
 | <code><a href="#cdk-monitoring-constructs.CustomMetricGroup.property.graphWidgetRightAxis">graphWidgetRightAxis</a></code> | <code>aws-cdk-lib.aws_cloudwatch.YAxisProps</code> | optional right axis. |
+| <code><a href="#cdk-monitoring-constructs.CustomMetricGroup.property.graphWidgetSetPeriodToTimeRange">graphWidgetSetPeriodToTimeRange</a></code> | <code>boolean</code> | *No description.* |
 | <code><a href="#cdk-monitoring-constructs.CustomMetricGroup.property.graphWidgetType">graphWidgetType</a></code> | <code><a href="#cdk-monitoring-constructs.GraphWidgetType">GraphWidgetType</a></code> | type of the widget. |
 | <code><a href="#cdk-monitoring-constructs.CustomMetricGroup.property.horizontalAnnotations">horizontalAnnotations</a></code> | <code>aws-cdk-lib.aws_cloudwatch.HorizontalAnnotation[]</code> | optional custom horizontal annotations which will be displayed over the metrics on the left axis (if there are any alarms, any existing annotations will be merged together). |
 | <code><a href="#cdk-monitoring-constructs.CustomMetricGroup.property.horizontalRightAnnotations">horizontalRightAnnotations</a></code> | <code>aws-cdk-lib.aws_cloudwatch.HorizontalAnnotation[]</code> | optional custom horizontal annotations which will be displayed over the metrics on the right axis (if there are any alarms, any existing annotations will be merged together). |
@@ -12238,6 +12239,18 @@ optional right axis.
 
 ---
 
+##### `graphWidgetSetPeriodToTimeRange`<sup>Optional</sup> <a name="graphWidgetSetPeriodToTimeRange" id="cdk-monitoring-constructs.CustomMetricGroup.property.graphWidgetSetPeriodToTimeRange"></a>
+
+```typescript
+public readonly graphWidgetSetPeriodToTimeRange: boolean;
+```
+
+- *Type:* boolean
+
+> [{GraphWidgetProps.setPeriodToTimeRange}]({GraphWidgetProps.setPeriodToTimeRange})
+
+---
+
 ##### `graphWidgetType`<sup>Optional</sup> <a name="graphWidgetType" id="cdk-monitoring-constructs.CustomMetricGroup.property.graphWidgetType"></a>
 
 ```typescript
diff --git a/lib/monitoring/custom/CustomMonitoring.ts b/lib/monitoring/custom/CustomMonitoring.ts
index 60b929da..2776b70d 100644
--- a/lib/monitoring/custom/CustomMonitoring.ts
+++ b/lib/monitoring/custom/CustomMonitoring.ts
@@ -169,6 +169,10 @@ export interface CustomMetricGroup {
    * @default BOTTOM
    */
   readonly graphWidgetLegend?: LegendPosition;
+  /**
+   * @see {GraphWidgetProps.setPeriodToTimeRange}
+   */
+  readonly graphWidgetSetPeriodToTimeRange?: boolean;
   /**
    * @deprecated use addToSummaryDashboard. addToSummaryDashboard will take precedence over important.
    * @see addToSummaryDashboard
@@ -409,6 +413,8 @@ export class CustomMonitoring extends Monitoring {
         leftYAxis: annotatedGroup.metricGroup.graphWidgetAxis,
         rightYAxis: annotatedGroup.metricGroup.graphWidgetRightAxis,
         legendPosition: annotatedGroup.metricGroup.graphWidgetLegend,
+        setPeriodToTimeRange:
+          annotatedGroup.metricGroup.graphWidgetSetPeriodToTimeRange,
       };
 
       const widget = useAnomalyDetectionWidget


Test patch:
diff --git a/test/monitoring/custom/CustomMonitoring.test.ts b/test/monitoring/custom/CustomMonitoring.test.ts
index 67731c43..c5635993 100644
--- a/test/monitoring/custom/CustomMonitoring.test.ts
+++ b/test/monitoring/custom/CustomMonitoring.test.ts
@@ -107,6 +107,7 @@ test("snapshot test", () => {
       {
         title: "DummyGroup3",
         graphWidgetType: GraphWidgetType.PIE,
+        graphWidgetSetPeriodToTimeRange: true,
         metrics: [
           // regular metric
           new Metric({ metricName: "DummyMetric20", namespace, dimensionsMap }),
diff --git a/test/monitoring/custom/__snapshots__/CustomMonitoring.test.ts.snap b/test/monitoring/custom/__snapshots__/CustomMonitoring.test.ts.snap
index 2bb865d4..9a844402 100644
--- a/test/monitoring/custom/__snapshots__/CustomMonitoring.test.ts.snap
+++ b/test/monitoring/custom/__snapshots__/CustomMonitoring.test.ts.snap
@@ -660,7 +660,7 @@ Object {
               Object {
                 "Ref": "AWS::Region",
               },
-              "\\",\\"metrics\\":[[\\"DummyCustomNamespace\\",\\"DummyMetric20\\",\\"CustomDimension\\",\\"CustomDimensionValue\\"],[\\"DummyCustomNamespace\\",\\"DummyMetric21\\",\\"CustomDimension\\",\\"CustomDimensionValue\\"],[\\"DummyCustomNamespace\\",\\"DummyMetric22\\",\\"CustomDimension\\",\\"CustomDimensionValue\\",{\\"yAxis\\":\\"right\\"}]],\\"annotations\\":{\\"horizontal\\":[{\\"label\\":\\"DummyAnnotation1\\",\\"value\\":30,\\"fill\\":\\"above\\",\\"yAxis\\":\\"left\\"},{\\"label\\":\\"DummyAnnotation2\\",\\"value\\":20,\\"fill\\":\\"below\\",\\"yAxis\\":\\"left\\"},{\\"label\\":\\"DummyAnnotation3\\",\\"value\\":20,\\"fill\\":\\"below\\",\\"yAxis\\":\\"right\\"},{\\"label\\":\\"DummyMetric22 > 10 for 3 datapoints within 15 minutes\\",\\"value\\":10,\\"yAxis\\":\\"right\\"},{\\"label\\":\\"DummyMetric22 > 50 for 3 datapoints within 15 minutes\\",\\"value\\":50,\\"yAxis\\":\\"right\\"}]},\\"yAxis\\":{\\"right\\":{\\"label\\":\\"CustomValue\\"}},\\"legend\\":{\\"position\\":\\"right\\"}}},{\\"type\\":\\"metric\\",\\"width\\":6,\\"height\\":100,\\"x\\":18,\\"y\\":3,\\"properties\\":{\\"view\\":\\"singleValue\\",\\"title\\":\\"DummyGroup4\\",\\"region\\":\\"",
+              "\\",\\"metrics\\":[[\\"DummyCustomNamespace\\",\\"DummyMetric20\\",\\"CustomDimension\\",\\"CustomDimensionValue\\"],[\\"DummyCustomNamespace\\",\\"DummyMetric21\\",\\"CustomDimension\\",\\"CustomDimensionValue\\"],[\\"DummyCustomNamespace\\",\\"DummyMetric22\\",\\"CustomDimension\\",\\"CustomDimensionValue\\",{\\"yAxis\\":\\"right\\"}]],\\"annotations\\":{\\"horizontal\\":[{\\"label\\":\\"DummyAnnotation1\\",\\"value\\":30,\\"fill\\":\\"above\\",\\"yAxis\\":\\"left\\"},{\\"label\\":\\"DummyAnnotation2\\",\\"value\\":20,\\"fill\\":\\"below\\",\\"yAxis\\":\\"left\\"},{\\"label\\":\\"DummyAnnotation3\\",\\"value\\":20,\\"fill\\":\\"below\\",\\"yAxis\\":\\"right\\"},{\\"label\\":\\"DummyMetric22 > 10 for 3 datapoints within 15 minutes\\",\\"value\\":10,\\"yAxis\\":\\"right\\"},{\\"label\\":\\"DummyMetric22 > 50 for 3 datapoints within 15 minutes\\",\\"value\\":50,\\"yAxis\\":\\"right\\"}]},\\"yAxis\\":{\\"right\\":{\\"label\\":\\"CustomValue\\"}},\\"legend\\":{\\"position\\":\\"right\\"},\\"setPeriodToTimeRange\\":true}},{\\"type\\":\\"metric\\",\\"width\\":6,\\"height\\":100,\\"x\\":18,\\"y\\":3,\\"properties\\":{\\"view\\":\\"singleValue\\",\\"title\\":\\"DummyGroup4\\",\\"region\\":\\"",
               Object {
                 "Ref": "AWS::Region",
               },