def __init__( self, granularity: str, groupby: List[str] = None, metrics: List[Metric] = None, filters: List[str] = None, time_range: Optional[str] = None, time_shift: Optional[str] = None, is_timeseries: bool = False, row_limit: int = app.config.get('ROW_LIMIT'), limit: int = 0, timeseries_limit_metric: Optional[Metric] = None, order_desc: bool = True, extras: Optional[Dict] = None, ): self.granularity = granularity self.from_dttm, self.to_dttm = utils.get_since_until( time_range, time_shift) self.is_timeseries = is_timeseries self.groupby = groupby or [] self.metrics = metrics or [] self.filter = filters or [] self.row_limit = row_limit self.timeseries_limit = int(limit) self.timeseries_limit_metric = timeseries_limit_metric self.order_desc = order_desc self.prequeries = [] self.is_prequery = False self.extras = extras
def __init__( self, granularity: str, metrics: List[Union[Dict, str]], groupby: Optional[List[str]] = None, filters: Optional[List[str]] = None, time_range: Optional[str] = None, time_shift: Optional[str] = None, is_timeseries: bool = False, timeseries_limit: int = 0, row_limit: int = app.config["ROW_LIMIT"], timeseries_limit_metric: Optional[Dict] = None, order_desc: bool = True, extras: Optional[Dict] = None, columns: Optional[List[str]] = None, orderby: Optional[List[List]] = None, relative_start: str = app.config["DEFAULT_RELATIVE_START_TIME"], relative_end: str = app.config["DEFAULT_RELATIVE_END_TIME"], ): self.granularity = granularity self.from_dttm, self.to_dttm = utils.get_since_until( relative_start=relative_start, relative_end=relative_end, time_range=time_range, time_shift=time_shift, ) self.is_timeseries = is_timeseries self.time_range = time_range self.time_shift = utils.parse_human_timedelta(time_shift) self.groupby = groupby or [] # Temporal solution for backward compatability issue due the new format of # non-ad-hoc metric which needs to adhere to superset-ui per # https://git.io/Jvm7P. self.metrics = [ metric if "expressionType" in metric else metric["label"] # type: ignore for metric in metrics ] self.row_limit = row_limit self.filter = filters or [] self.timeseries_limit = timeseries_limit self.timeseries_limit_metric = timeseries_limit_metric self.order_desc = order_desc self.extras = extras or {} if app.config[ "SIP_15_ENABLED"] and "time_range_endpoints" not in self.extras: self.extras["time_range_endpoints"] = get_time_range_endpoints( form_data={}) self.columns = columns or [] self.orderby = orderby or []
def test_get_since_until(self): form_data = {} result = get_since_until(form_data) expected = None, datetime(2016, 11, 7) self.assertEqual(result, expected) form_data = {'time_range': ' : now'} result = get_since_until(form_data) expected = None, datetime(2016, 11, 7) self.assertEqual(result, expected) form_data = {'time_range': 'yesterday : tomorrow'} result = get_since_until(form_data) expected = datetime(2016, 11, 6), datetime(2016, 11, 8) self.assertEqual(result, expected) form_data = {'time_range': '2018-01-01T00:00:00 : 2018-12-31T23:59:59'} result = get_since_until(form_data) expected = datetime(2018, 1, 1), datetime(2018, 12, 31, 23, 59, 59) self.assertEqual(result, expected) form_data = {'time_range': 'Last year'} result = get_since_until(form_data) expected = datetime(2015, 11, 7), datetime(2016, 11, 7) self.assertEqual(result, expected) form_data = {'time_range': 'Last 5 months'} result = get_since_until(form_data) expected = datetime(2016, 6, 7), datetime(2016, 11, 7) self.assertEqual(result, expected) form_data = {'time_range': 'Next 5 months'} result = get_since_until(form_data) expected = datetime(2016, 11, 7), datetime(2017, 4, 7) self.assertEqual(result, expected) form_data = {'since': '5 days'} result = get_since_until(form_data) expected = datetime(2016, 11, 2), datetime(2016, 11, 7) self.assertEqual(result, expected) form_data = {'since': '5 days ago', 'until': 'tomorrow'} result = get_since_until(form_data) expected = datetime(2016, 11, 2), datetime(2016, 11, 8) self.assertEqual(result, expected)
def __init__( self, granularity: str, metrics: List[Union[Dict, str]], groupby: List[str] = None, filters: List[str] = None, time_range: Optional[str] = None, time_shift: Optional[str] = None, is_timeseries: bool = False, timeseries_limit: int = 0, row_limit: int = app.config.get("ROW_LIMIT"), timeseries_limit_metric: Optional[Dict] = None, order_desc: bool = True, extras: Optional[Dict] = None, prequeries: Optional[List[Dict]] = None, is_prequery: bool = False, columns: List[str] = None, orderby: List[List] = None, relative_start: str = app.config.get("DEFAULT_RELATIVE_START_TIME", "today"), relative_end: str = app.config.get("DEFAULT_RELATIVE_END_TIME", "today"), ): self.granularity = granularity self.from_dttm, self.to_dttm = utils.get_since_until( relative_start=relative_start, relative_end=relative_end, time_range=time_range, time_shift=time_shift, ) self.is_timeseries = is_timeseries self.time_range = time_range self.time_shift = utils.parse_human_timedelta(time_shift) self.groupby = groupby if groupby is not None else [] # Temporal solution for backward compatability issue # due the new format of non-ad-hoc metric. self.metrics = [ metric if "expressionType" in metric else metric["label"] # noqa: T484 for metric in metrics ] self.row_limit = row_limit self.filter = filters if filters is not None else [] self.timeseries_limit = timeseries_limit self.timeseries_limit_metric = timeseries_limit_metric self.order_desc = order_desc self.prequeries = prequeries if prequeries is not None else [] self.is_prequery = is_prequery self.extras = extras if extras is not None else {} self.columns = columns if columns is not None else [] self.orderby = orderby if orderby is not None else []
def time_range(self, **kwargs: Any) -> FlaskResponse: """Get actually time range from human readable string or datetime expression""" time_range = kwargs["rison"] try: since, until = get_since_until(time_range) result = { "since": since.isoformat() if since else "", "until": until.isoformat() if until else "", "timeRange": time_range, } return self.json_response({"result": result}) except ValueError as error: error_msg = {"message": f"Unexpected time range: {error}"} return self.json_response(error_msg, 400)
def __init__( self, granularity: str, metrics: List[Union[Dict, str]], groupby: List[str] = None, filters: List[str] = None, time_range: Optional[str] = None, time_shift: Optional[str] = None, is_timeseries: bool = False, timeseries_limit: int = 0, row_limit: int = app.config.get('ROW_LIMIT'), timeseries_limit_metric: Optional[Dict] = None, order_desc: bool = True, extras: Optional[Dict] = None, prequeries: Optional[Dict] = None, is_prequery: bool = False, columns: List[str] = None, orderby: List[List] = None, ): self.granularity = granularity self.from_dttm, self.to_dttm = utils.get_since_until( time_range, time_shift) self.is_timeseries = is_timeseries self.time_range = time_range self.time_shift = utils.parse_human_timedelta(time_shift) self.groupby = groupby if groupby is not None else [] # Temporal solution for backward compatability issue # due the new format of non-ad-hoc metric. self.metrics = [ metric if 'expressionType' in metric else metric['label'] for metric in metrics ] self.row_limit = row_limit self.filter = filters if filters is not None else [] self.timeseries_limit = timeseries_limit self.timeseries_limit_metric = timeseries_limit_metric self.order_desc = order_desc self.prequeries = prequeries self.is_prequery = is_prequery self.extras = extras if extras is not None else {} self.columns = columns if columns is not None else [] self.orderby = orderby if orderby is not None else []
def __init__( self, granularity: str, metrics: List[Union[Dict, str]], groupby: List[str] = None, filters: List[str] = None, time_range: Optional[str] = None, time_shift: Optional[str] = None, is_timeseries: bool = False, timeseries_limit: int = 0, row_limit: int = app.config.get('ROW_LIMIT'), timeseries_limit_metric: Optional[Dict] = None, order_desc: bool = True, extras: Optional[Dict] = None, prequeries: Optional[List[Dict]] = None, is_prequery: bool = False, columns: List[str] = None, orderby: List[List] = None, ): self.granularity = granularity self.from_dttm, self.to_dttm = utils.get_since_until(time_range, time_shift) self.is_timeseries = is_timeseries self.time_range = time_range self.time_shift = utils.parse_human_timedelta(time_shift) self.groupby = groupby if groupby is not None else [] # Temporal solution for backward compatability issue # due the new format of non-ad-hoc metric. self.metrics = [ metric if 'expressionType' in metric else metric['label'] # noqa: T484 for metric in metrics ] self.row_limit = row_limit self.filter = filters if filters is not None else [] self.timeseries_limit = timeseries_limit self.timeseries_limit_metric = timeseries_limit_metric self.order_desc = order_desc self.prequeries = prequeries if prequeries is not None else [] self.is_prequery = is_prequery self.extras = extras if extras is not None else {} self.columns = columns if columns is not None else [] self.orderby = orderby if orderby is not None else []
def __init__( self, granularity: str, metrics: List[Union[Dict, str]], groupby: Optional[List[str]] = None, filters: Optional[List[str]] = None, time_range: Optional[str] = None, time_shift: Optional[str] = None, is_timeseries: bool = False, timeseries_limit: int = 0, row_limit: int = app.config["ROW_LIMIT"], timeseries_limit_metric: Optional[Dict] = None, order_desc: bool = True, extras: Optional[Dict] = None, columns: Optional[List[str]] = None, orderby: Optional[List[List]] = None, relative_start: str = app.config["DEFAULT_RELATIVE_START_TIME"], relative_end: str = app.config["DEFAULT_RELATIVE_END_TIME"], ): self.granularity = granularity self.from_dttm, self.to_dttm = utils.get_since_until( relative_start=relative_start, relative_end=relative_end, time_range=time_range, time_shift=time_shift, ) self.is_timeseries = is_timeseries self.time_range = time_range self.time_shift = utils.parse_human_timedelta(time_shift) self.groupby = groupby or [] self.metrics = [utils.get_metric_name(metric) for metric in metrics] self.row_limit = row_limit self.filter = filters or [] self.timeseries_limit = timeseries_limit self.timeseries_limit_metric = timeseries_limit_metric self.order_desc = order_desc self.extras = extras or {} self.columns = columns or [] self.orderby = orderby or []
def test_get_since_until(self): result = get_since_until() expected = None, datetime(2016, 11, 7) self.assertEqual(result, expected) result = get_since_until(" : now") expected = None, datetime(2016, 11, 7, 9, 30, 10) self.assertEqual(result, expected) result = get_since_until("yesterday : tomorrow") expected = datetime(2016, 11, 6), datetime(2016, 11, 8) self.assertEqual(result, expected) result = get_since_until("2018-01-01T00:00:00 : 2018-12-31T23:59:59") expected = datetime(2018, 1, 1), datetime(2018, 12, 31, 23, 59, 59) self.assertEqual(result, expected) result = get_since_until("Last year") expected = datetime(2015, 11, 7), datetime(2016, 11, 7) self.assertEqual(result, expected) result = get_since_until("Last quarter") expected = datetime(2016, 8, 7), datetime(2016, 11, 7) self.assertEqual(result, expected) result = get_since_until("Last 5 months") expected = datetime(2016, 6, 7), datetime(2016, 11, 7) self.assertEqual(result, expected) result = get_since_until("Next 5 months") expected = datetime(2016, 11, 7), datetime(2017, 4, 7) self.assertEqual(result, expected) result = get_since_until(since="5 days") expected = datetime(2016, 11, 2), datetime(2016, 11, 7) self.assertEqual(result, expected) result = get_since_until(since="5 days ago", until="tomorrow") expected = datetime(2016, 11, 2), datetime(2016, 11, 8) self.assertEqual(result, expected) result = get_since_until(time_range="yesterday : tomorrow", time_shift="1 day") expected = datetime(2016, 11, 5), datetime(2016, 11, 7) self.assertEqual(result, expected) result = get_since_until(time_range="5 days : now") expected = datetime(2016, 11, 2), datetime(2016, 11, 7, 9, 30, 10) self.assertEqual(result, expected) result = get_since_until("Last week", relative_end="now") expected = datetime(2016, 10, 31), datetime(2016, 11, 7, 9, 30, 10) self.assertEqual(result, expected) result = get_since_until("Last week", relative_start="now") expected = datetime(2016, 10, 31, 9, 30, 10), datetime(2016, 11, 7) self.assertEqual(result, expected) result = get_since_until("Last week", relative_start="now", relative_end="now") expected = datetime(2016, 10, 31, 9, 30, 10), datetime(2016, 11, 7, 9, 30, 10) self.assertEqual(result, expected) with self.assertRaises(ValueError): get_since_until(time_range="tomorrow : yesterday")
def test_get_since_until(self): result = get_since_until() expected = None, datetime(2016, 11, 7) self.assertEqual(result, expected) result = get_since_until(' : now') expected = None, datetime(2016, 11, 7) self.assertEqual(result, expected) result = get_since_until('yesterday : tomorrow') expected = datetime(2016, 11, 6), datetime(2016, 11, 8) self.assertEqual(result, expected) result = get_since_until('2018-01-01T00:00:00 : 2018-12-31T23:59:59') expected = datetime(2018, 1, 1), datetime(2018, 12, 31, 23, 59, 59) self.assertEqual(result, expected) result = get_since_until('Last year') expected = datetime(2015, 11, 7), datetime(2016, 11, 7) self.assertEqual(result, expected) result = get_since_until('Last 5 months') expected = datetime(2016, 6, 7), datetime(2016, 11, 7) self.assertEqual(result, expected) result = get_since_until('Next 5 months') expected = datetime(2016, 11, 7), datetime(2017, 4, 7) self.assertEqual(result, expected) result = get_since_until(since='5 days') expected = datetime(2016, 11, 2), datetime(2016, 11, 7) self.assertEqual(result, expected) result = get_since_until(since='5 days ago', until='tomorrow') expected = datetime(2016, 11, 2), datetime(2016, 11, 8) self.assertEqual(result, expected) result = get_since_until(time_range='yesterday : tomorrow', time_shift='1 day') expected = datetime(2016, 11, 5), datetime(2016, 11, 7) self.assertEqual(result, expected) result = get_since_until(time_range='5 days : now') expected = datetime(2016, 11, 2), datetime(2016, 11, 7) self.assertEqual(result, expected) with self.assertRaises(ValueError): get_since_until(time_range='tomorrow : yesterday')
def __init__( self, annotation_layers: Optional[List[Dict[str, Any]]] = None, applied_time_extras: Optional[Dict[str, str]] = None, granularity: Optional[str] = None, metrics: Optional[List[Union[Dict[str, Any], str]]] = None, groupby: Optional[List[str]] = None, filters: Optional[List[Dict[str, Any]]] = None, time_range: Optional[str] = None, time_shift: Optional[str] = None, is_timeseries: Optional[bool] = None, timeseries_limit: int = 0, row_limit: Optional[int] = None, row_offset: Optional[int] = None, timeseries_limit_metric: Optional[Metric] = None, order_desc: bool = True, extras: Optional[Dict[str, Any]] = None, columns: Optional[List[str]] = None, orderby: Optional[List[List[str]]] = None, post_processing: Optional[List[Optional[Dict[str, Any]]]] = None, **kwargs: Any, ): annotation_layers = annotation_layers or [] metrics = metrics or [] extras = extras or {} is_sip_38 = is_feature_enabled("SIP_38_VIZ_REARCHITECTURE") self.annotation_layers = [ layer for layer in annotation_layers # formula annotations don't affect the payload, hence can be dropped if layer["annotationType"] != "FORMULA" ] self.applied_time_extras = applied_time_extras or {} self.granularity = granularity self.from_dttm, self.to_dttm = get_since_until( relative_start=extras.get("relative_start", config["DEFAULT_RELATIVE_START_TIME"]), relative_end=extras.get("relative_end", config["DEFAULT_RELATIVE_END_TIME"]), time_range=time_range, time_shift=time_shift, ) # is_timeseries is True if time column is in groupby self.is_timeseries = (is_timeseries if is_timeseries is not None else (DTTM_ALIAS in groupby if groupby else False)) self.time_range = time_range self.time_shift = parse_human_timedelta(time_shift) self.post_processing = [ post_proc for post_proc in post_processing or [] if post_proc ] if not is_sip_38: self.groupby = groupby or [] # Support metric reference/definition in the format of # 1. 'metric_name' - name of predefined metric # 2. { label: 'label_name' } - legacy format for a predefined metric # 3. { expressionType: 'SIMPLE' | 'SQL', ... } - adhoc metric self.metrics = [ metric if isinstance(metric, str) or "expressionType" in metric else metric["label"] # type: ignore for metric in metrics ] self.row_limit = row_limit or config["ROW_LIMIT"] self.row_offset = row_offset or 0 self.filter = filters or [] self.timeseries_limit = timeseries_limit self.timeseries_limit_metric = timeseries_limit_metric self.order_desc = order_desc self.extras = extras if config[ "SIP_15_ENABLED"] and "time_range_endpoints" not in self.extras: self.extras["time_range_endpoints"] = get_time_range_endpoints( form_data={}) self.columns = columns or [] if is_sip_38 and groupby: self.columns += groupby logger.warning( "The field `groupby` is deprecated. Viz plugins should " "pass all selectables via the `columns` field") self.orderby = orderby or [] # rename deprecated fields for field in DEPRECATED_FIELDS: if field.old_name in kwargs: logger.warning( "The field `%s` is deprecated, please use `%s` instead.", field.old_name, field.new_name, ) value = kwargs[field.old_name] if value: if hasattr(self, field.new_name): logger.warning( "The field `%s` is already populated, " "replacing value with contents from `%s`.", field.new_name, field.old_name, ) setattr(self, field.new_name, value) # move deprecated extras fields to extras for field in DEPRECATED_EXTRAS_FIELDS: if field.old_name in kwargs: logger.warning( "The field `%s` is deprecated and should " "be passed to `extras` via the `%s` property.", field.old_name, field.new_name, ) value = kwargs[field.old_name] if value: if hasattr(self.extras, field.new_name): logger.warning( "The field `%s` is already populated in " "`extras`, replacing value with contents " "from `%s`.", field.new_name, field.old_name, ) self.extras[field.new_name] = value
def __init__( self, granularity: Optional[str] = None, metrics: Optional[List[Union[Dict[str, Any], str]]] = None, groupby: Optional[List[str]] = None, filters: Optional[List[Dict[str, Any]]] = None, time_range: Optional[str] = None, time_shift: Optional[str] = None, is_timeseries: bool = False, timeseries_limit: int = 0, row_limit: int = app.config["ROW_LIMIT"], timeseries_limit_metric: Optional[Metric] = None, order_desc: bool = True, extras: Optional[Dict[str, Any]] = None, columns: Optional[List[str]] = None, orderby: Optional[List[List[str]]] = None, post_processing: Optional[List[Dict[str, Any]]] = None, **kwargs: Any, ): metrics = metrics or [] extras = extras or {} is_sip_38 = is_feature_enabled("SIP_38_VIZ_REARCHITECTURE") self.granularity = granularity self.from_dttm, self.to_dttm = utils.get_since_until( relative_start=extras.get( "relative_start", app.config["DEFAULT_RELATIVE_START_TIME"]), relative_end=extras.get("relative_end", app.config["DEFAULT_RELATIVE_END_TIME"]), time_range=time_range, time_shift=time_shift, ) self.is_timeseries = is_timeseries self.time_range = time_range self.time_shift = utils.parse_human_timedelta(time_shift) self.post_processing = post_processing or [] if not is_sip_38: self.groupby = groupby or [] # Temporary solution for backward compatibility issue due the new format of # non-ad-hoc metric which needs to adhere to superset-ui per # https://git.io/Jvm7P. self.metrics = [ metric if "expressionType" in metric else metric["label"] # type: ignore for metric in metrics ] self.row_limit = row_limit self.filter = filters or [] self.timeseries_limit = timeseries_limit self.timeseries_limit_metric = timeseries_limit_metric self.order_desc = order_desc self.extras = extras if app.config[ "SIP_15_ENABLED"] and "time_range_endpoints" not in self.extras: self.extras["time_range_endpoints"] = get_time_range_endpoints( form_data={}) self.columns = columns or [] if is_sip_38 and groupby: self.columns += groupby logger.warning( f"The field `groupby` is deprecated. Viz plugins should " f"pass all selectables via the `columns` field") self.orderby = orderby or [] # rename deprecated fields for field in DEPRECATED_FIELDS: if field.old_name in kwargs: logger.warning( f"The field `{field.old_name}` is deprecated, please use " f"`{field.new_name}` instead.") value = kwargs[field.old_name] if value: if hasattr(self, field.new_name): logger.warning( f"The field `{field.new_name}` is already populated, " f"replacing value with contents from `{field.old_name}`." ) setattr(self, field.new_name, value) # move deprecated extras fields to extras for field in DEPRECATED_EXTRAS_FIELDS: if field.old_name in kwargs: logger.warning( f"The field `{field.old_name}` is deprecated and should be " f"passed to `extras` via the `{field.new_name}` property.") value = kwargs[field.old_name] if value: if hasattr(self.extras, field.new_name): logger.warning( f"The field `{field.new_name}` is already populated in " f"`extras`, replacing value with contents " f"from `{field.old_name}`.") self.extras[field.new_name] = value
def __init__( self, granularity: str, metrics: List[Union[Dict, str]], groupby: Optional[List[str]] = None, filters: Optional[List[str]] = None, time_range: Optional[str] = None, time_shift: Optional[str] = None, is_timeseries: bool = False, timeseries_limit: int = 0, row_limit: int = app.config["ROW_LIMIT"], timeseries_limit_metric: Optional[Dict] = None, order_desc: bool = True, extras: Optional[Dict] = None, columns: Optional[List[str]] = None, orderby: Optional[List[List]] = None, post_processing: Optional[List[Dict[str, Any]]] = None, relative_start: str = app.config["DEFAULT_RELATIVE_START_TIME"], relative_end: str = app.config["DEFAULT_RELATIVE_END_TIME"], ): is_sip_38 = is_feature_enabled("SIP_38_VIZ_REARCHITECTURE") self.granularity = granularity self.from_dttm, self.to_dttm = utils.get_since_until( relative_start=relative_start, relative_end=relative_end, time_range=time_range, time_shift=time_shift, ) self.is_timeseries = is_timeseries self.time_range = time_range self.time_shift = utils.parse_human_timedelta(time_shift) self.post_processing = post_processing or [] if not is_sip_38: self.groupby = groupby or [] # Temporary solution for backward compatibility issue due the new format of # non-ad-hoc metric which needs to adhere to superset-ui per # https://git.io/Jvm7P. self.metrics = [ metric if "expressionType" in metric else metric["label"] # type: ignore for metric in metrics ] self.row_limit = row_limit self.filter = filters or [] self.timeseries_limit = timeseries_limit self.timeseries_limit_metric = timeseries_limit_metric self.order_desc = order_desc self.extras = extras or {} if app.config[ "SIP_15_ENABLED"] and "time_range_endpoints" not in self.extras: self.extras["time_range_endpoints"] = get_time_range_endpoints( form_data={}) self.columns = columns or [] if is_sip_38 and groupby: self.columns += groupby logger.warning( f"The field groupby is deprecated. Viz plugins should " f"pass all selectables via the columns field") self.orderby = orderby or []