bito-code-review[bot] commented on code in PR #44140:
URL: https://github.com/apache/superset/pull/44140#discussion_r3977956409


##########
superset-frontend/plugins/plugin-chart-echarts/src/BigNumber/BigNumberYoyMom/timeRange.ts:
##########
@@ -0,0 +1,290 @@
+/**
+ * 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.
+ */
+/**
+ * Expands open-ended time range strings into fully-enclosed
+ * "start : end" timestamps so the backend's Time Comparison validation
+ * (superset/models/helpers.py) accepts the query.
+ *
+ * The backend (superset/utils/date_parser.py) already resolves "Last ...",
+ * "Next ...", "Current ..." and "previous calendar ..." ranges into enclosed
+ * bounds on its own, so those are passed through untouched. Ranges such as
+ * "Previous week", "This month", "Yesterday" or "month to date" resolve to an
+ * open-ended range (only an "until" bound) on the backend, which fails the
+ * enclosed-range check that is required whenever a time comparison
+ * (time_offsets) is present. Those are expanded here using the same calendar
+ * semantics the backend applies to their capitalized counterparts.
+ */
+
+const DATE_SEPARATOR = ' : ';
+
+// Ranges the backend resolves to enclosed bounds on its own.
+const BACKEND_ENCLOSED =
+  /^(Last|Next|Current)( \d+)? 
(second|minute|hour|day|week|month|quarter|year)s?$|^previous calendar 
(week|month|quarter|year)$/;
+
+const SECOND = 1000;
+const MINUTE = 60 * SECOND;
+const HOUR = 60 * MINUTE;
+const DAY = 24 * HOUR;
+const WEEK = 7 * DAY;
+
+const pad = (n: number): string => String(n).padStart(2, '0');
+
+const formatDateTime = (d: Date): string =>
+  `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(
+    d.getHours(),
+  )}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
+
+const truncateToDay = (d: Date): Date =>
+  new Date(d.getFullYear(), d.getMonth(), d.getDate());
+
+const startOfIsoWeek = (d: Date): Date => {
+  const day = truncateToDay(d);
+  const offset = (day.getDay() + 6) % 7; // Monday = 0
+  return new Date(day.getFullYear(), day.getMonth(), day.getDate() - offset);
+};
+
+const addMonths = (d: Date, months: number): Date =>
+  new Date(d.getFullYear(), d.getMonth() + months, d.getDate());

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Month-end date overflow</b></div>
   <div id="fix">
   
   `addMonths` overflows when the source day exceeds the target month's length: 
`Mar 31 - 1 month` yields `Mar 3`, not `Feb 28`. This is reachable via 
lowercase `last/next N months/quarters` in `toEnclosedTimeRange` (base = 
`today`, day can be 31), producing a wrong time range. Note 
`shiftPointToOffset` (lines 198-205) already clamps correctly, so the two paths 
diverge. Clamp to the target month's last day.
   </div>
   
   
   <details>
   <summary>
   <b>Code suggestion</b>
   </summary>
   <blockquote>Check the AI-generated fix before applying</blockquote>
   <div id="code">
   
   
   ````suggestion
   const addMonths = (d: Date, months: number): Date => {
     const target = new Date(d.getFullYear(), d.getMonth() + months, 1);
     const lastDay = new Date(target.getFullYear(), target.getMonth() + 1, 
0).getDate();
     return new Date(target.getFullYear(), target.getMonth(), 
Math.min(d.getDate(), lastDay));
   };
   ````
   
   </div>
   </details>
   
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #ba0d8a</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to