示例#1
0
    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 compute_time_compare(granularity, periods):
    if not granularity:
        return None
    # convert old db_engine_spec granularity to ISO duration
    if granularity in db_engine_specs_map:
        granularity = db_engine_specs_map[granularity]

    try:
        obj = isodate.parse_duration(granularity) * periods
    except isodate.isoerror.ISO8601Error:
        # if parse_human_timedelta can parse it, return it directly
        delta = '{0} {1}{2}'.format(periods, granularity, 's' if periods > 1 else '')
        obj = parse_human_timedelta(delta)
        if obj:
            return delta
        raise Exception('Unable to parse: {0}'.format(granularity))

    if isinstance(obj, isodate.duration.Duration):
        return isodate_duration_to_string(obj)
    elif isinstance(obj, datetime.timedelta):
        return timedelta_to_string(obj)
示例#3
0
 def test_parse_human_timedelta(self, mock_now):
     mock_now.return_value = datetime(2016, 12, 1)
     self.assertEquals(parse_human_timedelta('now'), timedelta(0))
示例#4
0
    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[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,
        **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
示例#5
0
 def test_parse_human_timedelta(self, mock_datetime):
     mock_datetime.now.return_value = datetime(2019, 4, 1)
     mock_datetime.side_effect = lambda *args, **kw: datetime(*args, **kw)
     self.assertEquals(parse_human_timedelta("now"), timedelta(0))
     self.assertEquals(parse_human_timedelta("1 year"), timedelta(366))
     self.assertEquals(parse_human_timedelta("-1 year"), timedelta(-365))
示例#6
0
    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 []
示例#7
0
 def test_parse_human_timedelta(self, mock_now):
     mock_now.return_value = datetime(2016, 12, 1)
     self.assertEquals(parse_human_timedelta('now'), timedelta(0))