Skip to content

Instantly share code, notes, and snippets.

@robshep
Last active February 15, 2025 20:39
Show Gist options
  • Save robshep/26773ea38669abfad0329f930eabbf36 to your computer and use it in GitHub Desktop.
Save robshep/26773ea38669abfad0329f930eabbf36 to your computer and use it in GitHub Desktop.
Superset auto range cal heatmap WIP-1 patch
Subject: [PATCH] WIP auto range for CalHeatmap
---
Index: superset/viz.py
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/superset/viz.py b/superset/viz.py
--- a/superset/viz.py (revision 6264ff516532f0359d914bd72356f2007925109b)
+++ b/superset/viz.py (date 1739648516672)
@@ -44,7 +44,7 @@
from geopy.point import Point
from pandas.tseries.frequencies import to_offset
-from superset import app
+from superset import app, constants
from superset.common.db_query_status import QueryStatus
from superset.constants import NULL_STRING
from superset.errors import ErrorLevel, SupersetError, SupersetErrorType
@@ -752,6 +752,15 @@
credits = "<a href=https://github.com/wa0x6e/cal-heatmap>cal-heatmap</a>"
is_timeseries = True
+ def find_earliest_latest(self, data):
+ metric_mins = []
+ metric_maxs = []
+ for metric, timestamps in data.items():
+ int_timestamps = list(map(float, timestamps.keys()))
+ metric_mins.append(min(int_timestamps))
+ metric_maxs.append(max(int_timestamps))
+ return min(metric_mins), max(metric_maxs)
+
@deprecated(deprecated_in="3.0")
def get_data(self, df: pd.DataFrame) -> VizData: # pylint: disable=too-many-locals
if df.empty:
@@ -769,21 +778,32 @@
values[str(v / 10**9)] = query_obj.get(metric)
data[metric] = values
- try:
- start, end = get_since_until(
- relative_start=relative_start,
- relative_end=relative_end,
- time_range=form_data.get("time_range"),
- since=form_data.get("since"),
- until=form_data.get("until"),
- )
- except ValueError as ex:
- raise QueryObjectValidationError(str(ex)) from ex
- if not start or not end:
- raise QueryObjectValidationError(
- "Please provide both time bounds (Since and Until)"
- )
- domain = form_data.get("domain_granularity")
+ domain = form_data.get("domain_granularity")
+
+ if(form_data.get("time_range") == constants.NO_TIME_RANGE):
+ min_ts, max_ts = self.find_earliest_latest(data)
+ if domain == 'month':
+ start_dt = datetime.utcfromtimestamp(min_ts) # Convert to datetime
+ start = datetime(start_dt.year, start_dt.month, 1)
+ end_dt = datetime.utcfromtimestamp(max_ts) # Convert to datetime
+ end = datetime(end_dt.year, end_dt.month, 1)
+
+ else:
+ try:
+ start, end = get_since_until(
+ relative_start=relative_start,
+ relative_end=relative_end,
+ time_range=form_data.get("time_range"),
+ since=form_data.get("since"),
+ until=form_data.get("until"),
+ )
+ except ValueError as ex:
+ raise QueryObjectValidationError(str(ex)) from ex
+ if not start or not end:
+ raise QueryObjectValidationError(
+ "Please provide both time bounds (Since and Until)"
+ )
+
diff_delta = rdelta.relativedelta(end, start)
diff_secs = (end - start).total_seconds()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment