2019-01-15 18:53:27 -05:00
|
|
|
# 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.
|
2018-04-28 23:08:09 -04:00
|
|
|
# pylint: disable=C,R,W
|
2017-11-14 00:06:51 -05:00
|
|
|
"""This module contains the 'Viz' objects
|
2016-03-16 23:25:41 -04:00
|
|
|
|
|
|
|
These objects represent the backend of all the visualizations that
|
2016-11-10 02:08:22 -05:00
|
|
|
Superset can render.
|
2016-03-18 02:44:58 -04:00
|
|
|
"""
|
2016-05-10 12:39:33 -04:00
|
|
|
import copy
|
2016-04-08 02:01:40 -04:00
|
|
|
import hashlib
|
2017-10-04 13:17:33 -04:00
|
|
|
import inspect
|
2016-03-16 23:25:41 -04:00
|
|
|
import logging
|
2018-02-03 23:18:24 -05:00
|
|
|
import math
|
2018-10-13 12:38:46 -04:00
|
|
|
import pickle as pkl
|
2018-04-11 00:21:24 -04:00
|
|
|
import re
|
2016-03-18 02:44:58 -04:00
|
|
|
import uuid
|
2019-10-18 17:44:27 -04:00
|
|
|
from collections import defaultdict, OrderedDict
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from functools import reduce
|
|
|
|
from itertools import product
|
2019-12-31 02:26:23 -05:00
|
|
|
from typing import Any, Dict, List, Optional, Tuple, TYPE_CHECKING
|
2016-06-14 00:59:03 -04:00
|
|
|
|
2019-10-18 17:44:27 -04:00
|
|
|
import geohash
|
|
|
|
import numpy as np
|
|
|
|
import pandas as pd
|
|
|
|
import polyline
|
|
|
|
import simplejson as json
|
2017-11-07 23:23:40 -05:00
|
|
|
from dateutil import relativedelta as rdelta
|
2018-05-02 18:24:44 -04:00
|
|
|
from flask import request
|
2016-06-27 23:10:40 -04:00
|
|
|
from flask_babel import lazy_gettext as _
|
2018-02-20 17:41:35 -05:00
|
|
|
from geopy.point import Point
|
2016-03-18 02:44:58 -04:00
|
|
|
from markdown import markdown
|
2017-12-07 00:50:33 -05:00
|
|
|
from pandas.tseries.frequencies import to_offset
|
2016-03-18 02:44:58 -04:00
|
|
|
|
2018-10-16 20:59:34 -04:00
|
|
|
from superset import app, cache, get_css_manifest_files
|
2019-12-03 19:11:15 -05:00
|
|
|
from superset.constants import NULL_STRING
|
2018-07-16 16:42:07 -04:00
|
|
|
from superset.exceptions import NullValueException, SpatialException
|
2019-12-31 02:26:23 -05:00
|
|
|
from superset.models.helpers import QueryResult
|
2018-10-16 20:59:34 -04:00
|
|
|
from superset.utils import core as utils
|
|
|
|
from superset.utils.core import (
|
2018-08-19 19:04:01 -04:00
|
|
|
DTTM_ALIAS,
|
|
|
|
JS_MAX_INTEGER,
|
|
|
|
merge_extra_filters,
|
|
|
|
to_adhoc,
|
|
|
|
)
|
2016-03-18 02:44:58 -04:00
|
|
|
|
2019-12-31 02:26:23 -05:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from superset.connectors.base.models import BaseDatasource
|
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
config = app.config
|
2019-10-30 19:19:16 -04:00
|
|
|
stats_logger = config["STATS_LOGGER"]
|
|
|
|
relative_start = config["DEFAULT_RELATIVE_START_TIME"]
|
|
|
|
relative_end = config["DEFAULT_RELATIVE_END_TIME"]
|
2016-03-18 02:44:58 -04:00
|
|
|
|
2018-05-15 13:37:34 -04:00
|
|
|
METRIC_KEYS = [
|
2019-06-25 16:34:48 -04:00
|
|
|
"metric",
|
|
|
|
"metrics",
|
|
|
|
"percent_metrics",
|
|
|
|
"metric_2",
|
|
|
|
"secondary_metric",
|
|
|
|
"x",
|
|
|
|
"y",
|
|
|
|
"size",
|
2018-05-15 13:37:34 -04:00
|
|
|
]
|
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
|
2019-12-31 02:26:23 -05:00
|
|
|
class BaseViz:
|
2016-03-18 02:44:58 -04:00
|
|
|
|
|
|
|
"""All visualizations derive this base class"""
|
|
|
|
|
2019-09-10 12:55:17 -04:00
|
|
|
viz_type: Optional[str] = None
|
2019-06-25 16:34:48 -04:00
|
|
|
verbose_name = "Base Viz"
|
|
|
|
credits = ""
|
2016-03-18 02:44:58 -04:00
|
|
|
is_timeseries = False
|
2019-06-25 16:34:48 -04:00
|
|
|
cache_type = "df"
|
2018-06-14 01:45:03 -04:00
|
|
|
enforce_numerical_metrics = True
|
2016-03-18 02:44:58 -04:00
|
|
|
|
2019-12-31 02:26:23 -05:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
datasource: "BaseDatasource",
|
|
|
|
form_data: Dict[str, Any],
|
|
|
|
force: bool = False,
|
|
|
|
):
|
2016-04-03 10:37:18 -04:00
|
|
|
if not datasource:
|
2019-06-25 16:34:48 -04:00
|
|
|
raise Exception(_("Viz is missing a datasource"))
|
2018-07-16 17:27:40 -04:00
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
self.datasource = datasource
|
|
|
|
self.request = request
|
2019-06-25 16:34:48 -04:00
|
|
|
self.viz_type = form_data.get("viz_type")
|
2017-02-16 20:28:35 -05:00
|
|
|
self.form_data = form_data
|
2016-03-18 02:44:58 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
self.query = ""
|
|
|
|
self.token = self.form_data.get("token", "token_" + uuid.uuid4().hex[:8])
|
2018-03-28 20:41:29 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
self.groupby = self.form_data.get("groupby") or []
|
2017-12-16 19:10:45 -05:00
|
|
|
self.time_shift = timedelta()
|
2016-03-18 02:44:58 -04:00
|
|
|
|
2017-01-05 13:00:39 -05:00
|
|
|
self.status = None
|
2019-06-25 16:34:48 -04:00
|
|
|
self.error_msg = ""
|
2019-12-31 02:26:23 -05:00
|
|
|
self.results: Optional[QueryResult] = None
|
2017-01-05 13:00:39 -05:00
|
|
|
self.error_message = None
|
2018-02-07 17:49:19 -05:00
|
|
|
self.force = force
|
|
|
|
|
|
|
|
# Keeping track of whether some data came from cache
|
2018-07-19 19:45:56 -04:00
|
|
|
# this is useful to trigger the <CachedLabel /> when
|
2018-02-07 17:49:19 -05:00
|
|
|
# in the cases where visualization have many queries
|
|
|
|
# (FilterBox for instance)
|
2019-12-31 02:26:23 -05:00
|
|
|
self._any_cache_key: Optional[str] = None
|
|
|
|
self._any_cached_dttm: Optional[str] = None
|
|
|
|
self._extra_chart_data: List[Tuple[str, pd.DataFrame]] = []
|
2018-02-07 17:49:19 -05:00
|
|
|
|
2018-05-15 13:37:34 -04:00
|
|
|
self.process_metrics()
|
|
|
|
|
|
|
|
def process_metrics(self):
|
2018-10-04 11:53:26 -04:00
|
|
|
# metrics in TableViz is order sensitive, so metric_dict should be
|
|
|
|
# OrderedDict
|
2018-08-28 14:09:10 -04:00
|
|
|
self.metric_dict = OrderedDict()
|
2018-05-15 13:37:34 -04:00
|
|
|
fd = self.form_data
|
|
|
|
for mkey in METRIC_KEYS:
|
|
|
|
val = fd.get(mkey)
|
|
|
|
if val:
|
|
|
|
if not isinstance(val, list):
|
|
|
|
val = [val]
|
|
|
|
for o in val:
|
2019-01-18 11:24:11 -05:00
|
|
|
label = utils.get_metric_name(o)
|
2018-08-21 16:45:42 -04:00
|
|
|
self.metric_dict[label] = o
|
2018-05-15 13:37:34 -04:00
|
|
|
|
|
|
|
# Cast to list needed to return serializable object in py3
|
|
|
|
self.all_metrics = list(self.metric_dict.values())
|
|
|
|
self.metric_labels = list(self.metric_dict.keys())
|
|
|
|
|
2018-04-04 03:36:23 -04:00
|
|
|
@staticmethod
|
|
|
|
def handle_js_int_overflow(data):
|
2019-06-25 16:34:48 -04:00
|
|
|
for d in data.get("records", dict()):
|
2018-04-04 03:36:23 -04:00
|
|
|
for k, v in list(d.items()):
|
|
|
|
if isinstance(v, int):
|
|
|
|
# if an int is too big for Java Script to handle
|
|
|
|
# convert it to a string
|
|
|
|
if abs(v) > JS_MAX_INTEGER:
|
2018-10-13 12:38:46 -04:00
|
|
|
d[k] = str(v)
|
2018-04-04 03:36:23 -04:00
|
|
|
return data
|
|
|
|
|
2018-02-07 17:49:19 -05:00
|
|
|
def run_extra_queries(self):
|
2018-07-19 19:45:56 -04:00
|
|
|
"""Lifecycle method to use when more than one query is needed
|
2018-02-07 17:49:19 -05:00
|
|
|
|
|
|
|
In rare-ish cases, a visualization may need to execute multiple
|
|
|
|
queries. That is the case for FilterBox or for time comparison
|
|
|
|
in Line chart for instance.
|
|
|
|
|
|
|
|
In those cases, we need to make sure these queries run before the
|
|
|
|
main `get_payload` method gets called, so that the overall caching
|
|
|
|
metadata can be right. The way it works here is that if any of
|
|
|
|
the previous `get_df_payload` calls hit the cache, the main
|
|
|
|
payload's metadata will reflect that.
|
|
|
|
|
|
|
|
The multi-query support may need more work to become a first class
|
|
|
|
use case in the framework, and for the UI to reflect the subtleties
|
|
|
|
(show that only some of the queries were served from cache for
|
|
|
|
instance). In the meantime, since multi-query is rare, we treat
|
|
|
|
it with a bit of a hack. Note that the hack became necessary
|
|
|
|
when moving from caching the visualization's data itself, to caching
|
|
|
|
the underlying query(ies).
|
|
|
|
"""
|
|
|
|
pass
|
2017-01-05 13:00:39 -05:00
|
|
|
|
2018-09-20 16:51:39 -04:00
|
|
|
def get_samples(self):
|
|
|
|
query_obj = self.query_obj()
|
2019-06-25 16:34:48 -04:00
|
|
|
query_obj.update(
|
|
|
|
{
|
|
|
|
"groupby": [],
|
|
|
|
"metrics": [],
|
|
|
|
"row_limit": 1000,
|
|
|
|
"columns": [o.column_name for o in self.datasource.columns],
|
|
|
|
}
|
|
|
|
)
|
2018-09-20 16:51:39 -04:00
|
|
|
df = self.get_df(query_obj)
|
2019-06-25 16:34:48 -04:00
|
|
|
return df.to_dict(orient="records")
|
2018-09-20 16:51:39 -04:00
|
|
|
|
2019-09-10 12:55:17 -04:00
|
|
|
def get_df(
|
|
|
|
self, query_obj: Optional[Dict[str, Any]] = None
|
|
|
|
) -> Optional[pd.DataFrame]:
|
2016-03-18 02:44:58 -04:00
|
|
|
"""Returns a pandas dataframe based on the query object"""
|
|
|
|
if not query_obj:
|
|
|
|
query_obj = self.query_obj()
|
2017-12-26 13:47:29 -05:00
|
|
|
if not query_obj:
|
|
|
|
return None
|
2016-03-18 02:44:58 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
self.error_msg = ""
|
2016-03-18 02:44:58 -04:00
|
|
|
|
2016-06-28 00:33:44 -04:00
|
|
|
timestamp_format = None
|
2019-06-25 16:34:48 -04:00
|
|
|
if self.datasource.type == "table":
|
2019-12-31 02:26:23 -05:00
|
|
|
granularity_col = self.datasource.get_column(query_obj["granularity"])
|
|
|
|
if granularity_col:
|
|
|
|
timestamp_format = granularity_col.python_date_format
|
2016-06-28 00:33:44 -04:00
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
# The datasource here can be different backend but the interface is common
|
2017-02-16 20:28:35 -05:00
|
|
|
self.results = self.datasource.query(query_obj)
|
|
|
|
self.query = self.results.query
|
2017-01-05 13:00:39 -05:00
|
|
|
self.status = self.results.status
|
|
|
|
self.error_message = self.results.error_message
|
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
df = self.results.df
|
2016-06-28 00:33:44 -04:00
|
|
|
# Transform the timestamp we received from database to pandas supported
|
|
|
|
# datetime format. If no python_date_format is specified, the pattern will
|
|
|
|
# be considered as the default ISO date format
|
|
|
|
# If the datetime format is unix, the parse will use the corresponding
|
|
|
|
# parsing logic.
|
2018-08-20 20:01:03 -04:00
|
|
|
if df is not None and not df.empty:
|
2016-11-15 00:35:10 -05:00
|
|
|
if DTTM_ALIAS in df.columns:
|
2019-06-25 16:34:48 -04:00
|
|
|
if timestamp_format in ("epoch_s", "epoch_ms"):
|
2018-06-27 12:09:42 -04:00
|
|
|
# Column has already been formatted as a timestamp.
|
2019-02-05 20:04:19 -05:00
|
|
|
dttm_col = df[DTTM_ALIAS]
|
|
|
|
one_ts_val = dttm_col[0]
|
|
|
|
|
|
|
|
# convert time column to pandas Timestamp, but different
|
|
|
|
# ways to convert depending on string or int types
|
|
|
|
try:
|
|
|
|
int(one_ts_val)
|
|
|
|
is_integral = True
|
2019-05-08 01:35:14 -04:00
|
|
|
except (ValueError, TypeError):
|
2019-02-05 20:04:19 -05:00
|
|
|
is_integral = False
|
|
|
|
if is_integral:
|
2019-06-25 16:34:48 -04:00
|
|
|
unit = "s" if timestamp_format == "epoch_s" else "ms"
|
|
|
|
df[DTTM_ALIAS] = pd.to_datetime(
|
|
|
|
dttm_col, utc=False, unit=unit, origin="unix"
|
|
|
|
)
|
2019-02-05 20:04:19 -05:00
|
|
|
else:
|
|
|
|
df[DTTM_ALIAS] = dttm_col.apply(pd.Timestamp)
|
2016-06-28 00:33:44 -04:00
|
|
|
else:
|
2016-11-15 00:35:10 -05:00
|
|
|
df[DTTM_ALIAS] = pd.to_datetime(
|
2019-06-25 16:34:48 -04:00
|
|
|
df[DTTM_ALIAS], utc=False, format=timestamp_format
|
|
|
|
)
|
2016-03-18 02:44:58 -04:00
|
|
|
if self.datasource.offset:
|
2016-11-15 00:35:10 -05:00
|
|
|
df[DTTM_ALIAS] += timedelta(hours=self.datasource.offset)
|
2017-12-16 19:10:45 -05:00
|
|
|
df[DTTM_ALIAS] += self.time_shift
|
2018-04-03 00:48:14 -04:00
|
|
|
|
2018-06-14 01:45:03 -04:00
|
|
|
if self.enforce_numerical_metrics:
|
2018-09-11 02:01:34 -04:00
|
|
|
self.df_metrics_to_num(df)
|
2018-04-03 00:48:14 -04:00
|
|
|
|
2019-03-20 20:14:15 -04:00
|
|
|
df.replace([np.inf, -np.inf], np.nan, inplace=True)
|
2016-03-18 02:44:58 -04:00
|
|
|
return df
|
|
|
|
|
2018-09-11 02:01:34 -04:00
|
|
|
def df_metrics_to_num(self, df):
|
2018-04-03 00:48:14 -04:00
|
|
|
"""Converting metrics to numeric when pandas.read_sql cannot"""
|
2018-09-11 02:01:34 -04:00
|
|
|
metrics = self.metric_labels
|
2018-04-03 00:48:14 -04:00
|
|
|
for col, dtype in df.dtypes.items():
|
|
|
|
if dtype.type == np.object_ and col in metrics:
|
2019-06-25 16:34:48 -04:00
|
|
|
df[col] = pd.to_numeric(df[col], errors="coerce")
|
2018-04-03 00:48:14 -04:00
|
|
|
|
2018-09-20 16:51:39 -04:00
|
|
|
def process_query_filters(self):
|
|
|
|
utils.convert_legacy_filters_into_adhoc(self.form_data)
|
|
|
|
merge_extra_filters(self.form_data)
|
|
|
|
utils.split_adhoc_filters_into_base_filters(self.form_data)
|
|
|
|
|
2019-12-31 02:26:23 -05:00
|
|
|
def query_obj(self) -> Dict[str, Any]:
|
2016-03-18 02:44:58 -04:00
|
|
|
"""Building a query object"""
|
|
|
|
form_data = self.form_data
|
2018-09-20 16:51:39 -04:00
|
|
|
self.process_query_filters()
|
2019-06-25 16:34:48 -04:00
|
|
|
gb = form_data.get("groupby") or []
|
2018-05-15 13:37:34 -04:00
|
|
|
metrics = self.all_metrics or []
|
2019-06-25 16:34:48 -04:00
|
|
|
columns = form_data.get("columns") or []
|
2019-12-31 02:26:23 -05:00
|
|
|
groupby = list(set(gb + columns))
|
2017-08-03 18:42:26 -04:00
|
|
|
|
|
|
|
is_timeseries = self.is_timeseries
|
|
|
|
if DTTM_ALIAS in groupby:
|
|
|
|
groupby.remove(DTTM_ALIAS)
|
|
|
|
is_timeseries = True
|
2017-03-14 15:18:33 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
granularity = form_data.get("granularity") or form_data.get("granularity_sqla")
|
|
|
|
limit = int(form_data.get("limit") or 0)
|
|
|
|
timeseries_limit_metric = form_data.get("timeseries_limit_metric")
|
2019-10-30 19:19:16 -04:00
|
|
|
row_limit = int(form_data.get("row_limit") or config["ROW_LIMIT"])
|
2017-03-14 15:18:33 -04:00
|
|
|
|
2017-09-12 12:48:43 -04:00
|
|
|
# default order direction
|
2019-06-25 16:34:48 -04:00
|
|
|
order_desc = form_data.get("order_desc", True)
|
|
|
|
|
|
|
|
since, until = utils.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"),
|
|
|
|
)
|
|
|
|
time_shift = form_data.get("time_shift", "")
|
2019-06-19 13:10:18 -04:00
|
|
|
self.time_shift = utils.parse_past_timedelta(time_shift)
|
2018-01-09 19:54:18 -05:00
|
|
|
from_dttm = None if since is None else (since - self.time_shift)
|
|
|
|
to_dttm = None if until is None else (until - self.time_shift)
|
2017-08-28 12:16:23 -04:00
|
|
|
if from_dttm and to_dttm and from_dttm > to_dttm:
|
2019-06-25 16:34:48 -04:00
|
|
|
raise Exception(_("From date cannot be larger than to date"))
|
2016-03-18 02:44:58 -04:00
|
|
|
|
2017-09-27 23:40:07 -04:00
|
|
|
self.from_dttm = from_dttm
|
|
|
|
self.to_dttm = to_dttm
|
|
|
|
|
2018-09-20 16:51:39 -04:00
|
|
|
# extras are used to query elements specific to a datasource type
|
|
|
|
# for instance the extra where clause that applies only to Tables
|
2018-05-23 14:46:00 -04:00
|
|
|
extras = {
|
2019-10-28 17:23:12 -04:00
|
|
|
"druid_time_origin": form_data.get("druid_time_origin", ""),
|
2019-06-25 16:34:48 -04:00
|
|
|
"having": form_data.get("having", ""),
|
|
|
|
"having_druid": form_data.get("having_filters", []),
|
|
|
|
"time_grain_sqla": form_data.get("time_grain_sqla", ""),
|
2019-10-28 17:23:12 -04:00
|
|
|
"time_range_endpoints": form_data.get("time_range_endpoints"),
|
|
|
|
"where": form_data.get("where", ""),
|
2018-05-23 14:46:00 -04:00
|
|
|
}
|
2018-05-10 13:41:10 -04:00
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
d = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"granularity": granularity,
|
|
|
|
"from_dttm": from_dttm,
|
|
|
|
"to_dttm": to_dttm,
|
|
|
|
"is_timeseries": is_timeseries,
|
|
|
|
"groupby": groupby,
|
|
|
|
"metrics": metrics,
|
|
|
|
"row_limit": row_limit,
|
|
|
|
"filter": self.form_data.get("filters", []),
|
|
|
|
"timeseries_limit": limit,
|
|
|
|
"extras": extras,
|
|
|
|
"timeseries_limit_metric": timeseries_limit_metric,
|
|
|
|
"order_desc": order_desc,
|
2016-03-18 02:44:58 -04:00
|
|
|
}
|
|
|
|
return d
|
|
|
|
|
2016-03-16 23:25:41 -04:00
|
|
|
@property
|
|
|
|
def cache_timeout(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
if self.form_data.get("cache_timeout") is not None:
|
|
|
|
return int(self.form_data.get("cache_timeout"))
|
2018-07-02 18:32:21 -04:00
|
|
|
if self.datasource.cache_timeout is not None:
|
2016-03-30 19:35:37 -04:00
|
|
|
return self.datasource.cache_timeout
|
2016-04-25 15:41:30 -04:00
|
|
|
if (
|
2019-06-25 16:34:48 -04:00
|
|
|
hasattr(self.datasource, "database")
|
|
|
|
and self.datasource.database.cache_timeout
|
|
|
|
) is not None:
|
2016-03-30 19:35:37 -04:00
|
|
|
return self.datasource.database.cache_timeout
|
2019-10-30 19:19:16 -04:00
|
|
|
return config["CACHE_DEFAULT_TIMEOUT"]
|
2016-03-16 23:25:41 -04:00
|
|
|
|
2018-02-07 17:49:19 -05:00
|
|
|
def get_json(self):
|
2017-01-05 13:00:39 -05:00
|
|
|
return json.dumps(
|
2019-06-25 16:34:48 -04:00
|
|
|
self.get_payload(), default=utils.json_int_dttm_ser, ignore_nan=True
|
|
|
|
)
|
2017-01-05 13:00:39 -05:00
|
|
|
|
2018-09-07 18:45:28 -04:00
|
|
|
def cache_key(self, query_obj, **extra):
|
2018-01-12 15:05:12 -05:00
|
|
|
"""
|
2018-09-07 18:45:28 -04:00
|
|
|
The cache key is made out of the key/values in `query_obj`, plus any
|
|
|
|
other key/values in `extra`.
|
2018-01-28 12:46:13 -05:00
|
|
|
|
2018-09-07 18:45:28 -04:00
|
|
|
We remove datetime bounds that are hard values, and replace them with
|
|
|
|
the use-provided inputs to bounds, which may be time-relative (as in
|
|
|
|
"5 days ago" or "now").
|
|
|
|
|
|
|
|
The `extra` arguments are currently used by time shift queries, since
|
|
|
|
different time shifts wil differ only in the `from_dttm` and `to_dttm`
|
|
|
|
values which are stripped.
|
2018-01-12 15:05:12 -05:00
|
|
|
"""
|
2018-05-15 13:37:34 -04:00
|
|
|
cache_dict = copy.copy(query_obj)
|
2018-09-07 18:45:28 -04:00
|
|
|
cache_dict.update(extra)
|
2018-01-28 12:46:13 -05:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
for k in ["from_dttm", "to_dttm"]:
|
2018-01-28 12:46:13 -05:00
|
|
|
del cache_dict[k]
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
cache_dict["time_range"] = self.form_data.get("time_range")
|
|
|
|
cache_dict["datasource"] = self.datasource.uid
|
2019-07-20 12:12:35 -04:00
|
|
|
cache_dict["extra_cache_keys"] = self.datasource.get_extra_cache_keys(query_obj)
|
2018-01-28 12:46:13 -05:00
|
|
|
json_data = self.json_dumps(cache_dict, sort_keys=True)
|
2019-06-25 16:34:48 -04:00
|
|
|
return hashlib.md5(json_data.encode("utf-8")).hexdigest()
|
2017-02-16 20:28:35 -05:00
|
|
|
|
2018-02-07 17:49:19 -05:00
|
|
|
def get_payload(self, query_obj=None):
|
|
|
|
"""Returns a payload of metadata and data"""
|
2018-03-16 12:09:00 -04:00
|
|
|
self.run_extra_queries()
|
2018-02-07 17:49:19 -05:00
|
|
|
payload = self.get_df_payload(query_obj)
|
2018-02-09 18:55:45 -05:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
df = payload.get("df")
|
2018-02-20 17:41:35 -05:00
|
|
|
if self.status != utils.QueryStatus.FAILED:
|
2018-03-07 00:19:13 -05:00
|
|
|
if df is not None and df.empty:
|
2019-06-25 16:34:48 -04:00
|
|
|
payload["error"] = "No data"
|
2018-02-27 18:11:01 -05:00
|
|
|
else:
|
2019-06-25 16:34:48 -04:00
|
|
|
payload["data"] = self.get_data(df)
|
|
|
|
if "df" in payload:
|
|
|
|
del payload["df"]
|
2018-02-07 17:49:19 -05:00
|
|
|
return payload
|
|
|
|
|
2018-09-07 18:45:28 -04:00
|
|
|
def get_df_payload(self, query_obj=None, **kwargs):
|
2018-02-07 17:49:19 -05:00
|
|
|
"""Handles caching around the df payload retrieval"""
|
|
|
|
if not query_obj:
|
|
|
|
query_obj = self.query_obj()
|
2018-09-07 18:45:28 -04:00
|
|
|
cache_key = self.cache_key(query_obj, **kwargs) if query_obj else None
|
2019-06-25 16:34:48 -04:00
|
|
|
logging.info("Cache key: {}".format(cache_key))
|
2018-02-07 17:49:19 -05:00
|
|
|
is_loaded = False
|
2018-01-12 15:05:12 -05:00
|
|
|
stacktrace = None
|
2018-02-07 17:49:19 -05:00
|
|
|
df = None
|
2019-06-25 16:34:48 -04:00
|
|
|
cached_dttm = datetime.utcnow().isoformat().split(".")[0]
|
2018-02-07 17:49:19 -05:00
|
|
|
if cache_key and cache and not self.force:
|
2018-01-12 15:05:12 -05:00
|
|
|
cache_value = cache.get(cache_key)
|
|
|
|
if cache_value:
|
2019-06-25 16:34:48 -04:00
|
|
|
stats_logger.incr("loaded_from_cache")
|
2018-01-12 15:05:12 -05:00
|
|
|
try:
|
2018-02-07 17:49:19 -05:00
|
|
|
cache_value = pkl.loads(cache_value)
|
2019-06-25 16:34:48 -04:00
|
|
|
df = cache_value["df"]
|
|
|
|
self.query = cache_value["query"]
|
|
|
|
self._any_cached_dttm = cache_value["dttm"]
|
2018-03-21 16:13:36 -04:00
|
|
|
self._any_cache_key = cache_key
|
|
|
|
self.status = utils.QueryStatus.SUCCESS
|
|
|
|
is_loaded = True
|
2018-01-12 15:05:12 -05:00
|
|
|
except Exception as e:
|
2018-02-07 17:49:19 -05:00
|
|
|
logging.exception(e)
|
2019-06-25 16:34:48 -04:00
|
|
|
logging.error(
|
|
|
|
"Error reading cache: " + utils.error_msg_from_exception(e)
|
|
|
|
)
|
|
|
|
logging.info("Serving from cache")
|
2016-06-14 00:59:03 -04:00
|
|
|
|
2018-02-07 17:49:19 -05:00
|
|
|
if query_obj and not is_loaded:
|
2017-01-24 17:03:17 -05:00
|
|
|
try:
|
2018-01-12 15:05:12 -05:00
|
|
|
df = self.get_df(query_obj)
|
2018-02-27 18:11:01 -05:00
|
|
|
if self.status != utils.QueryStatus.FAILED:
|
2019-06-25 16:34:48 -04:00
|
|
|
stats_logger.incr("loaded_from_source")
|
2018-02-27 18:11:01 -05:00
|
|
|
is_loaded = True
|
2017-01-24 17:03:17 -05:00
|
|
|
except Exception as e:
|
|
|
|
logging.exception(e)
|
|
|
|
if not self.error_message:
|
2019-06-25 16:34:48 -04:00
|
|
|
self.error_message = "{}".format(e)
|
2017-01-24 17:03:17 -05:00
|
|
|
self.status = utils.QueryStatus.FAILED
|
2019-07-12 11:58:44 -04:00
|
|
|
stacktrace = utils.get_stacktrace()
|
2018-01-12 15:05:12 -05:00
|
|
|
|
2018-01-18 11:28:26 -05:00
|
|
|
if (
|
2019-06-25 16:34:48 -04:00
|
|
|
is_loaded
|
|
|
|
and cache_key
|
|
|
|
and cache
|
|
|
|
and self.status != utils.QueryStatus.FAILED
|
|
|
|
):
|
2017-02-17 12:50:39 -05:00
|
|
|
try:
|
2018-02-07 17:49:19 -05:00
|
|
|
cache_value = dict(
|
|
|
|
dttm=cached_dttm,
|
|
|
|
df=df if df is not None else None,
|
2018-03-21 16:13:36 -04:00
|
|
|
query=self.query,
|
2018-02-07 17:49:19 -05:00
|
|
|
)
|
2019-06-25 16:34:48 -04:00
|
|
|
cache_value = pkl.dumps(cache_value, protocol=pkl.HIGHEST_PROTOCOL)
|
2018-02-07 17:49:19 -05:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
logging.info(
|
|
|
|
"Caching {} chars at key {}".format(len(cache_value), cache_key)
|
|
|
|
)
|
2018-02-07 17:49:19 -05:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
stats_logger.incr("set_cache_key")
|
|
|
|
cache.set(cache_key, cache_value, timeout=self.cache_timeout)
|
2017-02-17 12:50:39 -05:00
|
|
|
except Exception as e:
|
|
|
|
# cache.set call can fail if the backend is down or if
|
|
|
|
# the key is too large or whatever other reasons
|
2019-06-25 16:34:48 -04:00
|
|
|
logging.warning("Could not cache key {}".format(cache_key))
|
2017-02-17 12:50:39 -05:00
|
|
|
logging.exception(e)
|
|
|
|
cache.delete(cache_key)
|
2018-01-12 15:05:12 -05:00
|
|
|
return {
|
2019-06-25 16:34:48 -04:00
|
|
|
"cache_key": self._any_cache_key,
|
|
|
|
"cached_dttm": self._any_cached_dttm,
|
|
|
|
"cache_timeout": self.cache_timeout,
|
|
|
|
"df": df,
|
|
|
|
"error": self.error_message,
|
|
|
|
"form_data": self.form_data,
|
|
|
|
"is_cached": self._any_cache_key is not None,
|
|
|
|
"query": self.query,
|
|
|
|
"status": self.status,
|
|
|
|
"stacktrace": stacktrace,
|
|
|
|
"rowcount": len(df.index) if df is not None else 0,
|
2018-01-12 15:05:12 -05:00
|
|
|
}
|
2016-04-12 00:22:54 -04:00
|
|
|
|
2018-01-28 12:46:13 -05:00
|
|
|
def json_dumps(self, obj, sort_keys=False):
|
|
|
|
return json.dumps(
|
2019-06-25 16:34:48 -04:00
|
|
|
obj, default=utils.json_int_dttm_ser, ignore_nan=True, sort_keys=sort_keys
|
2018-01-28 12:46:13 -05:00
|
|
|
)
|
2017-01-12 20:05:34 -05:00
|
|
|
|
2018-11-07 20:23:52 -05:00
|
|
|
def payload_json_and_has_error(self, payload):
|
2019-06-25 16:34:48 -04:00
|
|
|
has_error = (
|
|
|
|
payload.get("status") == utils.QueryStatus.FAILED
|
|
|
|
or payload.get("error") is not None
|
|
|
|
)
|
2018-11-07 20:23:52 -05:00
|
|
|
return self.json_dumps(payload), has_error
|
|
|
|
|
2016-04-12 00:22:54 -04:00
|
|
|
@property
|
|
|
|
def data(self):
|
2016-07-13 23:45:05 -04:00
|
|
|
"""This is the data object serialized to the js layer"""
|
2016-04-12 00:22:54 -04:00
|
|
|
content = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"form_data": self.form_data,
|
|
|
|
"token": self.token,
|
|
|
|
"viz_name": self.viz_type,
|
|
|
|
"filter_select_enabled": self.datasource.filter_select_enabled,
|
2016-04-12 00:22:54 -04:00
|
|
|
}
|
|
|
|
return content
|
2016-03-18 02:44:58 -04:00
|
|
|
|
|
|
|
def get_csv(self):
|
|
|
|
df = self.get_df()
|
2016-04-06 11:22:49 -04:00
|
|
|
include_index = not isinstance(df.index, pd.RangeIndex)
|
2019-10-30 19:19:16 -04:00
|
|
|
return df.to_csv(index=include_index, **config["CSV_EXPORT"])
|
2016-03-18 02:44:58 -04:00
|
|
|
|
2017-02-16 20:28:35 -05:00
|
|
|
def get_data(self, df):
|
2019-06-25 16:34:48 -04:00
|
|
|
return df.to_dict(orient="records")
|
2016-03-18 02:44:58 -04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def json_data(self):
|
2016-06-09 21:05:58 -04:00
|
|
|
return json.dumps(self.data)
|
2016-03-18 02:44:58 -04:00
|
|
|
|
2016-03-16 23:25:41 -04:00
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
class TableViz(BaseViz):
|
2016-03-16 23:25:41 -04:00
|
|
|
|
|
|
|
"""A basic html table that is sortable and searchable"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "table"
|
|
|
|
verbose_name = _("Table View")
|
2016-11-10 02:08:22 -05:00
|
|
|
credits = 'a <a href="https://github.com/airbnb/superset">Superset</a> original'
|
2016-03-18 02:44:58 -04:00
|
|
|
is_timeseries = False
|
2018-06-14 01:45:03 -04:00
|
|
|
enforce_numerical_metrics = False
|
2016-03-18 02:44:58 -04:00
|
|
|
|
2017-02-27 16:58:05 -05:00
|
|
|
def should_be_timeseries(self):
|
|
|
|
fd = self.form_data
|
|
|
|
# TODO handle datasource-type-specific code in datasource
|
2019-06-25 16:34:48 -04:00
|
|
|
conditions_met = (fd.get("granularity") and fd.get("granularity") != "all") or (
|
|
|
|
fd.get("granularity_sqla") and fd.get("time_grain_sqla")
|
2017-02-27 16:58:05 -05:00
|
|
|
)
|
2019-06-25 16:34:48 -04:00
|
|
|
if fd.get("include_time") and not conditions_met:
|
|
|
|
raise Exception(
|
|
|
|
_("Pick a granularity in the Time section or " "uncheck 'Include Time'")
|
|
|
|
)
|
|
|
|
return fd.get("include_time")
|
2017-02-27 16:58:05 -05:00
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
def query_obj(self):
|
2019-04-12 00:48:35 -04:00
|
|
|
d = super().query_obj()
|
2016-03-18 02:44:58 -04:00
|
|
|
fd = self.form_data
|
2017-02-27 16:58:05 -05:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
if fd.get("all_columns") and (fd.get("groupby") or fd.get("metrics")):
|
|
|
|
raise Exception(
|
|
|
|
_(
|
|
|
|
"Choose either fields to [Group By] and [Metrics] or "
|
|
|
|
"[Columns], not both"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
sort_by = fd.get("timeseries_limit_metric")
|
|
|
|
if fd.get("all_columns"):
|
|
|
|
d["columns"] = fd.get("all_columns")
|
|
|
|
d["groupby"] = []
|
|
|
|
order_by_cols = fd.get("order_by_cols") or []
|
|
|
|
d["orderby"] = [json.loads(t) for t in order_by_cols]
|
2017-09-13 23:18:47 -04:00
|
|
|
elif sort_by:
|
2018-06-05 14:14:36 -04:00
|
|
|
sort_by_label = utils.get_metric_name(sort_by)
|
2019-06-25 16:34:48 -04:00
|
|
|
if sort_by_label not in utils.get_metric_names(d["metrics"]):
|
|
|
|
d["metrics"] += [sort_by]
|
|
|
|
d["orderby"] = [(sort_by, not fd.get("order_desc", True))]
|
2017-02-27 16:58:05 -05:00
|
|
|
|
2017-10-16 23:16:20 -04:00
|
|
|
# Add all percent metrics that are not already in the list
|
2019-06-25 16:34:48 -04:00
|
|
|
if "percent_metrics" in fd:
|
|
|
|
d["metrics"] = d["metrics"] + list(
|
|
|
|
filter(lambda m: m not in d["metrics"], fd["percent_metrics"] or [])
|
|
|
|
)
|
2017-10-16 23:16:20 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
d["is_timeseries"] = self.should_be_timeseries()
|
2016-03-18 02:44:58 -04:00
|
|
|
return d
|
|
|
|
|
2017-02-16 20:28:35 -05:00
|
|
|
def get_data(self, df):
|
2017-10-16 23:16:20 -04:00
|
|
|
fd = self.form_data
|
2019-06-25 16:34:48 -04:00
|
|
|
if not self.should_be_timeseries() and df is not None and DTTM_ALIAS in df:
|
2016-11-15 00:35:10 -05:00
|
|
|
del df[DTTM_ALIAS]
|
2016-03-18 02:44:58 -04:00
|
|
|
|
2017-10-16 23:16:20 -04:00
|
|
|
# Sum up and compute percentages for all percent metrics
|
2019-06-25 16:34:48 -04:00
|
|
|
percent_metrics = fd.get("percent_metrics") or []
|
2019-01-18 11:24:11 -05:00
|
|
|
percent_metrics = [utils.get_metric_name(m) for m in percent_metrics]
|
2018-05-15 13:37:34 -04:00
|
|
|
|
2017-10-16 23:16:20 -04:00
|
|
|
if len(percent_metrics):
|
|
|
|
percent_metrics = list(filter(lambda m: m in df, percent_metrics))
|
|
|
|
metric_sums = {
|
2019-06-25 16:34:48 -04:00
|
|
|
m: reduce(lambda a, b: a + b, df[m]) for m in percent_metrics
|
2017-10-16 23:16:20 -04:00
|
|
|
}
|
|
|
|
metric_percents = {
|
2019-06-25 16:34:48 -04:00
|
|
|
m: list(
|
|
|
|
map(
|
|
|
|
lambda a: None if metric_sums[m] == 0 else a / metric_sums[m],
|
|
|
|
df[m],
|
|
|
|
)
|
|
|
|
)
|
2017-10-16 23:16:20 -04:00
|
|
|
for m in percent_metrics
|
|
|
|
}
|
|
|
|
for m in percent_metrics:
|
2019-06-25 16:34:48 -04:00
|
|
|
m_name = "%" + m
|
2017-10-16 23:16:20 -04:00
|
|
|
df[m_name] = pd.Series(metric_percents[m], name=m_name)
|
|
|
|
# Remove metrics that are not in the main metrics list
|
2019-06-25 16:34:48 -04:00
|
|
|
metrics = fd.get("metrics") or []
|
2019-01-18 11:24:11 -05:00
|
|
|
metrics = [utils.get_metric_name(m) for m in metrics]
|
2017-10-16 23:16:20 -04:00
|
|
|
for m in filter(
|
2019-06-25 16:34:48 -04:00
|
|
|
lambda m: m not in metrics and m in df.columns, percent_metrics
|
2017-10-16 23:16:20 -04:00
|
|
|
):
|
|
|
|
del df[m]
|
|
|
|
|
2018-04-04 03:36:23 -04:00
|
|
|
data = self.handle_js_int_overflow(
|
2019-06-25 16:34:48 -04:00
|
|
|
dict(records=df.to_dict(orient="records"), columns=list(df.columns))
|
|
|
|
)
|
2018-04-04 03:36:23 -04:00
|
|
|
|
|
|
|
return data
|
2016-03-18 02:44:58 -04:00
|
|
|
|
2018-01-28 12:46:13 -05:00
|
|
|
def json_dumps(self, obj, sort_keys=False):
|
2018-12-27 14:38:05 -05:00
|
|
|
return json.dumps(
|
2019-06-25 16:34:48 -04:00
|
|
|
obj, default=utils.json_iso_dttm_ser, sort_keys=sort_keys, ignore_nan=True
|
|
|
|
)
|
2017-07-26 00:30:40 -04:00
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
|
2017-10-04 13:17:33 -04:00
|
|
|
class TimeTableViz(BaseViz):
|
|
|
|
|
|
|
|
"""A data table with rich time-series related columns"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "time_table"
|
|
|
|
verbose_name = _("Time Table View")
|
2017-10-04 13:17:33 -04:00
|
|
|
credits = 'a <a href="https://github.com/airbnb/superset">Superset</a> original'
|
|
|
|
is_timeseries = True
|
|
|
|
|
|
|
|
def query_obj(self):
|
2019-04-12 00:48:35 -04:00
|
|
|
d = super().query_obj()
|
2017-10-04 13:17:33 -04:00
|
|
|
fd = self.form_data
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
if not fd.get("metrics"):
|
|
|
|
raise Exception(_("Pick at least one metric"))
|
2017-10-04 13:17:33 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
if fd.get("groupby") and len(fd.get("metrics")) > 1:
|
|
|
|
raise Exception(
|
|
|
|
_("When using 'Group By' you are limited to use a single metric")
|
|
|
|
)
|
2017-10-04 13:17:33 -04:00
|
|
|
return d
|
|
|
|
|
|
|
|
def get_data(self, df):
|
|
|
|
fd = self.form_data
|
|
|
|
columns = None
|
2018-05-15 13:37:34 -04:00
|
|
|
values = self.metric_labels
|
2019-06-25 16:34:48 -04:00
|
|
|
if fd.get("groupby"):
|
2018-05-15 13:37:34 -04:00
|
|
|
values = self.metric_labels[0]
|
2019-06-25 16:34:48 -04:00
|
|
|
columns = fd.get("groupby")
|
2019-08-16 14:25:57 -04:00
|
|
|
pt = df.pivot_table(index=DTTM_ALIAS, columns=columns, values=values)
|
2017-10-04 13:17:33 -04:00
|
|
|
pt.index = pt.index.map(str)
|
|
|
|
pt = pt.sort_index()
|
|
|
|
return dict(
|
2019-06-25 16:34:48 -04:00
|
|
|
records=pt.to_dict(orient="index"),
|
2017-10-04 13:17:33 -04:00
|
|
|
columns=list(pt.columns),
|
2019-06-25 16:34:48 -04:00
|
|
|
is_group_by=len(fd.get("groupby")) > 0,
|
2017-10-04 13:17:33 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
class PivotTableViz(BaseViz):
|
2016-03-16 23:25:41 -04:00
|
|
|
|
|
|
|
"""A pivot table view, define your rows, columns and metrics"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "pivot_table"
|
|
|
|
verbose_name = _("Pivot Table")
|
2016-11-10 02:08:22 -05:00
|
|
|
credits = 'a <a href="https://github.com/airbnb/superset">Superset</a> original'
|
2016-03-18 02:44:58 -04:00
|
|
|
is_timeseries = False
|
|
|
|
|
|
|
|
def query_obj(self):
|
2019-04-12 00:48:35 -04:00
|
|
|
d = super().query_obj()
|
2019-07-01 21:05:15 -04:00
|
|
|
groupby = self.form_data.get("groupby")
|
|
|
|
columns = self.form_data.get("columns")
|
|
|
|
metrics = self.form_data.get("metrics")
|
|
|
|
transpose = self.form_data.get("transpose_pivot")
|
2016-03-18 02:44:58 -04:00
|
|
|
if not columns:
|
|
|
|
columns = []
|
|
|
|
if not groupby:
|
|
|
|
groupby = []
|
|
|
|
if not groupby:
|
2017-11-14 00:06:51 -05:00
|
|
|
raise Exception(_("Please choose at least one 'Group by' field "))
|
2019-07-01 18:44:01 -04:00
|
|
|
if transpose and not columns:
|
2019-07-01 21:05:15 -04:00
|
|
|
raise Exception(
|
|
|
|
_(
|
|
|
|
(
|
|
|
|
"Please choose at least one 'Columns' field when "
|
|
|
|
"select 'Transpose Pivot' option"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2016-03-18 02:44:58 -04:00
|
|
|
if not metrics:
|
2019-06-25 16:34:48 -04:00
|
|
|
raise Exception(_("Please choose at least one metric"))
|
|
|
|
if any(v in groupby for v in columns) or any(v in columns for v in groupby):
|
2017-11-14 00:06:51 -05:00
|
|
|
raise Exception(_("Group By' and 'Columns' can't overlap"))
|
2016-03-18 02:44:58 -04:00
|
|
|
return d
|
|
|
|
|
2017-02-16 20:28:35 -05:00
|
|
|
def get_data(self, df):
|
2019-06-25 16:34:48 -04:00
|
|
|
if self.form_data.get("granularity") == "all" and DTTM_ALIAS in df:
|
2016-11-15 00:35:10 -05:00
|
|
|
del df[DTTM_ALIAS]
|
2019-03-20 20:14:15 -04:00
|
|
|
|
2019-07-04 16:23:02 -04:00
|
|
|
aggfunc = self.form_data.get("pandas_aggfunc") or "sum"
|
2019-03-20 20:14:15 -04:00
|
|
|
|
|
|
|
# Ensure that Pandas's sum function mimics that of SQL.
|
2019-06-25 16:34:48 -04:00
|
|
|
if aggfunc == "sum":
|
2019-10-18 17:44:27 -04:00
|
|
|
aggfunc = lambda x: x.sum(min_count=1)
|
2019-03-20 20:14:15 -04:00
|
|
|
|
2019-07-01 21:05:15 -04:00
|
|
|
groupby = self.form_data.get("groupby")
|
|
|
|
columns = self.form_data.get("columns")
|
|
|
|
if self.form_data.get("transpose_pivot"):
|
2019-07-01 18:44:01 -04:00
|
|
|
groupby, columns = columns, groupby
|
2016-03-18 02:44:58 -04:00
|
|
|
df = df.pivot_table(
|
2019-07-01 18:44:01 -04:00
|
|
|
index=groupby,
|
|
|
|
columns=columns,
|
2019-07-01 21:05:15 -04:00
|
|
|
values=[utils.get_metric_name(m) for m in self.form_data.get("metrics")],
|
2019-03-20 20:14:15 -04:00
|
|
|
aggfunc=aggfunc,
|
2019-06-25 16:34:48 -04:00
|
|
|
margins=self.form_data.get("pivot_margins"),
|
2016-03-18 02:44:58 -04:00
|
|
|
)
|
2017-07-28 17:16:38 -04:00
|
|
|
# Display metrics side by side with each column
|
2019-06-25 16:34:48 -04:00
|
|
|
if self.form_data.get("combine_metric"):
|
2017-07-28 17:16:38 -04:00
|
|
|
df = df.stack(0).unstack()
|
2017-06-20 01:10:54 -04:00
|
|
|
return dict(
|
|
|
|
columns=list(df.columns),
|
|
|
|
html=df.to_html(
|
2019-06-25 16:34:48 -04:00
|
|
|
na_rep="null",
|
2017-06-20 01:10:54 -04:00
|
|
|
classes=(
|
2019-06-25 16:34:48 -04:00
|
|
|
"dataframe table table-striped table-bordered "
|
|
|
|
"table-condensed table-hover"
|
|
|
|
).split(" "),
|
|
|
|
),
|
2017-06-20 01:10:54 -04:00
|
|
|
)
|
2016-03-18 02:44:58 -04:00
|
|
|
|
|
|
|
|
|
|
|
class MarkupViz(BaseViz):
|
2016-03-16 23:25:41 -04:00
|
|
|
|
|
|
|
"""Use html or markdown to create a free form widget"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "markup"
|
|
|
|
verbose_name = _("Markup")
|
2016-03-18 02:44:58 -04:00
|
|
|
is_timeseries = False
|
|
|
|
|
2018-01-17 16:54:10 -05:00
|
|
|
def query_obj(self):
|
|
|
|
return None
|
|
|
|
|
2019-09-10 12:55:17 -04:00
|
|
|
def get_df(
|
|
|
|
self, query_obj: Optional[Dict[str, Any]] = None
|
|
|
|
) -> Optional[pd.DataFrame]:
|
2018-03-07 00:19:13 -05:00
|
|
|
return None
|
2017-02-16 20:28:35 -05:00
|
|
|
|
|
|
|
def get_data(self, df):
|
2019-06-25 16:34:48 -04:00
|
|
|
markup_type = self.form_data.get("markup_type")
|
|
|
|
code = self.form_data.get("code", "")
|
|
|
|
if markup_type == "markdown":
|
2017-01-05 13:00:39 -05:00
|
|
|
code = markdown(code)
|
2019-06-25 16:34:48 -04:00
|
|
|
return dict(html=code, theme_css=get_css_manifest_files("theme"))
|
2016-03-18 02:44:58 -04:00
|
|
|
|
|
|
|
|
2016-07-13 23:40:52 -04:00
|
|
|
class SeparatorViz(MarkupViz):
|
|
|
|
|
|
|
|
"""Use to create section headers in a dashboard, similar to `Markup`"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "separator"
|
|
|
|
verbose_name = _("Separator")
|
2016-07-13 23:40:52 -04:00
|
|
|
|
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
class WordCloudViz(BaseViz):
|
|
|
|
|
2016-03-16 23:25:41 -04:00
|
|
|
"""Build a colorful word cloud
|
2016-03-18 02:44:58 -04:00
|
|
|
|
2016-03-16 23:25:41 -04:00
|
|
|
Uses the nice library at:
|
2016-03-18 02:44:58 -04:00
|
|
|
https://github.com/jasondavies/d3-cloud
|
|
|
|
"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "word_cloud"
|
|
|
|
verbose_name = _("Word Cloud")
|
2016-03-18 02:44:58 -04:00
|
|
|
is_timeseries = False
|
|
|
|
|
|
|
|
def query_obj(self):
|
2019-04-12 00:48:35 -04:00
|
|
|
d = super().query_obj()
|
2019-06-25 16:34:48 -04:00
|
|
|
d["groupby"] = [self.form_data.get("series")]
|
2016-03-18 02:44:58 -04:00
|
|
|
return d
|
|
|
|
|
|
|
|
|
2016-04-13 23:27:00 -04:00
|
|
|
class TreemapViz(BaseViz):
|
|
|
|
|
|
|
|
"""Tree map visualisation for hierarchical data."""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "treemap"
|
|
|
|
verbose_name = _("Treemap")
|
2016-04-13 23:27:00 -04:00
|
|
|
credits = '<a href="https://d3js.org">d3.js</a>'
|
|
|
|
is_timeseries = False
|
|
|
|
|
|
|
|
def _nest(self, metric, df):
|
|
|
|
nlevels = df.index.nlevels
|
|
|
|
if nlevels == 1:
|
2019-06-25 16:34:48 -04:00
|
|
|
result = [{"name": n, "value": v} for n, v in zip(df.index, df[metric])]
|
2016-04-13 23:27:00 -04:00
|
|
|
else:
|
2019-06-25 16:34:48 -04:00
|
|
|
result = [
|
|
|
|
{"name": l, "children": self._nest(metric, df.loc[l])}
|
|
|
|
for l in df.index.levels[0]
|
|
|
|
]
|
2016-04-13 23:27:00 -04:00
|
|
|
return result
|
|
|
|
|
2017-02-16 20:28:35 -05:00
|
|
|
def get_data(self, df):
|
2019-06-25 16:34:48 -04:00
|
|
|
df = df.set_index(self.form_data.get("groupby"))
|
|
|
|
chart_data = [
|
|
|
|
{"name": metric, "children": self._nest(metric, df)}
|
|
|
|
for metric in df.columns
|
|
|
|
]
|
2016-04-13 23:27:00 -04:00
|
|
|
return chart_data
|
|
|
|
|
|
|
|
|
2016-05-16 20:59:38 -04:00
|
|
|
class CalHeatmapViz(BaseViz):
|
|
|
|
|
|
|
|
"""Calendar heatmap."""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "cal_heatmap"
|
|
|
|
verbose_name = _("Calendar Heatmap")
|
|
|
|
credits = "<a href=https://github.com/wa0x6e/cal-heatmap>cal-heatmap</a>"
|
2016-05-16 20:59:38 -04:00
|
|
|
is_timeseries = True
|
2017-02-16 20:28:35 -05:00
|
|
|
|
|
|
|
def get_data(self, df):
|
2016-05-16 20:59:38 -04:00
|
|
|
form_data = self.form_data
|
|
|
|
|
2018-04-12 20:16:45 -04:00
|
|
|
data = {}
|
2019-06-25 16:34:48 -04:00
|
|
|
records = df.to_dict("records")
|
2018-05-15 13:37:34 -04:00
|
|
|
for metric in self.metric_labels:
|
2018-11-27 23:54:25 -05:00
|
|
|
values = {}
|
|
|
|
for obj in records:
|
|
|
|
v = obj[DTTM_ALIAS]
|
2019-06-25 16:34:48 -04:00
|
|
|
if hasattr(v, "value"):
|
2018-11-27 23:54:25 -05:00
|
|
|
v = v.value
|
2019-06-25 16:34:48 -04:00
|
|
|
values[str(v / 10 ** 9)] = obj.get(metric)
|
2018-11-27 23:54:25 -05:00
|
|
|
data[metric] = values
|
2016-05-16 20:59:38 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
start, end = utils.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"),
|
|
|
|
)
|
2018-04-12 20:16:45 -04:00
|
|
|
if not start or not end:
|
2019-06-25 16:34:48 -04:00
|
|
|
raise Exception("Please provide both time bounds (Since and Until)")
|
|
|
|
domain = form_data.get("domain_granularity")
|
2016-05-16 20:59:38 -04:00
|
|
|
diff_delta = rdelta.relativedelta(end, start)
|
|
|
|
diff_secs = (end - start).total_seconds()
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
if domain == "year":
|
2016-05-16 20:59:38 -04:00
|
|
|
range_ = diff_delta.years + 1
|
2019-06-25 16:34:48 -04:00
|
|
|
elif domain == "month":
|
2016-05-16 20:59:38 -04:00
|
|
|
range_ = diff_delta.years * 12 + diff_delta.months + 1
|
2019-06-25 16:34:48 -04:00
|
|
|
elif domain == "week":
|
2016-05-16 20:59:38 -04:00
|
|
|
range_ = diff_delta.years * 53 + diff_delta.weeks + 1
|
2019-06-25 16:34:48 -04:00
|
|
|
elif domain == "day":
|
2017-11-08 23:34:23 -05:00
|
|
|
range_ = diff_secs // (24 * 60 * 60) + 1
|
2016-05-16 20:59:38 -04:00
|
|
|
else:
|
2017-11-08 23:34:23 -05:00
|
|
|
range_ = diff_secs // (60 * 60) + 1
|
2016-05-16 20:59:38 -04:00
|
|
|
|
|
|
|
return {
|
2019-06-25 16:34:48 -04:00
|
|
|
"data": data,
|
|
|
|
"start": start,
|
|
|
|
"domain": domain,
|
|
|
|
"subdomain": form_data.get("subdomain_granularity"),
|
|
|
|
"range": range_,
|
2016-05-16 20:59:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
def query_obj(self):
|
2019-04-12 00:48:35 -04:00
|
|
|
d = super().query_obj()
|
2018-04-12 20:16:45 -04:00
|
|
|
fd = self.form_data
|
2019-06-25 16:34:48 -04:00
|
|
|
d["metrics"] = fd.get("metrics")
|
2018-04-12 20:16:45 -04:00
|
|
|
return d
|
2016-05-16 20:59:38 -04:00
|
|
|
|
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
class NVD3Viz(BaseViz):
|
|
|
|
|
|
|
|
"""Base class for all nvd3 vizs"""
|
|
|
|
|
2016-04-09 16:17:20 -04:00
|
|
|
credits = '<a href="http://nvd3.org/">NVD3.org</a>'
|
2019-09-10 12:55:17 -04:00
|
|
|
viz_type: Optional[str] = None
|
2019-06-25 16:34:48 -04:00
|
|
|
verbose_name = "Base NVD3 Viz"
|
2016-03-18 02:44:58 -04:00
|
|
|
is_timeseries = False
|
|
|
|
|
|
|
|
|
2016-04-10 19:15:25 -04:00
|
|
|
class BoxPlotViz(NVD3Viz):
|
|
|
|
|
|
|
|
"""Box plot viz from ND3"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "box_plot"
|
|
|
|
verbose_name = _("Box Plot")
|
2016-04-10 19:15:25 -04:00
|
|
|
sort_series = False
|
2019-06-05 18:05:26 -04:00
|
|
|
is_timeseries = True
|
2016-04-10 19:15:25 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
def to_series(self, df, classed="", title_suffix=""):
|
|
|
|
label_sep = " - "
|
2017-02-16 20:28:35 -05:00
|
|
|
chart_data = []
|
2019-06-25 16:34:48 -04:00
|
|
|
for index_value, row in zip(df.index, df.to_dict(orient="records")):
|
2017-02-16 20:28:35 -05:00
|
|
|
if isinstance(index_value, tuple):
|
|
|
|
index_value = label_sep.join(index_value)
|
|
|
|
boxes = defaultdict(dict)
|
|
|
|
for (label, key), value in row.items():
|
2019-06-25 16:34:48 -04:00
|
|
|
if key == "nanmedian":
|
|
|
|
key = "Q2"
|
2017-02-16 20:28:35 -05:00
|
|
|
boxes[label][key] = value
|
|
|
|
for label, box in boxes.items():
|
2019-06-25 16:34:48 -04:00
|
|
|
if len(self.form_data.get("metrics")) > 1:
|
2017-02-16 20:28:35 -05:00
|
|
|
# need to render data labels with metrics
|
|
|
|
chart_label = label_sep.join([index_value, label])
|
|
|
|
else:
|
|
|
|
chart_label = index_value
|
2019-06-25 16:34:48 -04:00
|
|
|
chart_data.append({"label": chart_label, "values": box})
|
2017-02-16 20:28:35 -05:00
|
|
|
return chart_data
|
2016-04-10 19:15:25 -04:00
|
|
|
|
2017-02-16 20:28:35 -05:00
|
|
|
def get_data(self, df):
|
|
|
|
form_data = self.form_data
|
2016-04-10 19:15:25 -04:00
|
|
|
|
|
|
|
# conform to NVD3 names
|
|
|
|
def Q1(series): # need to be named functions - can't use lambdas
|
2019-03-20 20:14:15 -04:00
|
|
|
return np.nanpercentile(series, 25)
|
2016-04-10 19:15:25 -04:00
|
|
|
|
|
|
|
def Q3(series):
|
2019-03-20 20:14:15 -04:00
|
|
|
return np.nanpercentile(series, 75)
|
2016-04-10 19:15:25 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
whisker_type = form_data.get("whisker_options")
|
|
|
|
if whisker_type == "Tukey":
|
2016-04-10 19:15:25 -04:00
|
|
|
|
|
|
|
def whisker_high(series):
|
|
|
|
upper_outer_lim = Q3(series) + 1.5 * (Q3(series) - Q1(series))
|
2019-03-20 20:14:15 -04:00
|
|
|
return series[series <= upper_outer_lim].max()
|
2016-04-10 19:15:25 -04:00
|
|
|
|
|
|
|
def whisker_low(series):
|
|
|
|
lower_outer_lim = Q1(series) - 1.5 * (Q3(series) - Q1(series))
|
2019-03-20 20:14:15 -04:00
|
|
|
return series[series >= lower_outer_lim].min()
|
2016-04-10 19:15:25 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
elif whisker_type == "Min/max (no outliers)":
|
2016-04-10 19:15:25 -04:00
|
|
|
|
|
|
|
def whisker_high(series):
|
|
|
|
return series.max()
|
|
|
|
|
|
|
|
def whisker_low(series):
|
|
|
|
return series.min()
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
elif " percentiles" in whisker_type:
|
|
|
|
low, high = whisker_type.replace(" percentiles", "").split("/")
|
2016-04-10 19:15:25 -04:00
|
|
|
|
|
|
|
def whisker_high(series):
|
2019-03-20 20:14:15 -04:00
|
|
|
return np.nanpercentile(series, int(high))
|
2016-04-10 19:15:25 -04:00
|
|
|
|
|
|
|
def whisker_low(series):
|
2019-03-20 20:14:15 -04:00
|
|
|
return np.nanpercentile(series, int(low))
|
2016-04-10 19:15:25 -04:00
|
|
|
|
|
|
|
else:
|
2019-06-25 16:34:48 -04:00
|
|
|
raise ValueError("Unknown whisker type: {}".format(whisker_type))
|
2016-04-10 19:15:25 -04:00
|
|
|
|
|
|
|
def outliers(series):
|
|
|
|
above = series[series > whisker_high(series)]
|
|
|
|
below = series[series < whisker_low(series)]
|
|
|
|
# pandas sometimes doesn't like getting lists back here
|
|
|
|
return set(above.tolist() + below.tolist())
|
|
|
|
|
2019-03-20 20:14:15 -04:00
|
|
|
aggregate = [Q1, np.nanmedian, Q3, whisker_high, whisker_low, outliers]
|
2019-06-25 16:34:48 -04:00
|
|
|
df = df.groupby(form_data.get("groupby")).agg(aggregate)
|
2016-04-10 19:15:25 -04:00
|
|
|
chart_data = self.to_series(df)
|
|
|
|
return chart_data
|
|
|
|
|
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
class BubbleViz(NVD3Viz):
|
|
|
|
|
|
|
|
"""Based on the NVD3 bubble chart"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "bubble"
|
|
|
|
verbose_name = _("Bubble Chart")
|
2016-03-18 02:44:58 -04:00
|
|
|
is_timeseries = False
|
|
|
|
|
|
|
|
def query_obj(self):
|
|
|
|
form_data = self.form_data
|
2019-04-12 00:48:35 -04:00
|
|
|
d = super().query_obj()
|
2019-06-25 16:34:48 -04:00
|
|
|
d["groupby"] = [form_data.get("entity")]
|
|
|
|
if form_data.get("series"):
|
|
|
|
d["groupby"].append(form_data.get("series"))
|
|
|
|
self.x_metric = form_data.get("x")
|
|
|
|
self.y_metric = form_data.get("y")
|
|
|
|
self.z_metric = form_data.get("size")
|
|
|
|
self.entity = form_data.get("entity")
|
|
|
|
self.series = form_data.get("series") or self.entity
|
|
|
|
d["row_limit"] = form_data.get("limit")
|
|
|
|
|
2019-08-14 12:27:18 -04:00
|
|
|
d["metrics"] = [self.z_metric, self.x_metric, self.y_metric]
|
|
|
|
if len(set(self.metric_labels)) < 3:
|
|
|
|
raise Exception(_("Please use 3 different metric labels"))
|
2019-06-25 16:34:48 -04:00
|
|
|
if not all(d["metrics"] + [self.entity]):
|
|
|
|
raise Exception(_("Pick a metric for x, y and size"))
|
2016-03-18 02:44:58 -04:00
|
|
|
return d
|
|
|
|
|
2017-02-16 20:28:35 -05:00
|
|
|
def get_data(self, df):
|
2019-06-25 16:34:48 -04:00
|
|
|
df["x"] = df[[utils.get_metric_name(self.x_metric)]]
|
|
|
|
df["y"] = df[[utils.get_metric_name(self.y_metric)]]
|
|
|
|
df["size"] = df[[utils.get_metric_name(self.z_metric)]]
|
|
|
|
df["shape"] = "circle"
|
|
|
|
df["group"] = df[[self.series]]
|
2016-03-18 02:44:58 -04:00
|
|
|
|
|
|
|
series = defaultdict(list)
|
2019-06-25 16:34:48 -04:00
|
|
|
for row in df.to_dict(orient="records"):
|
|
|
|
series[row["group"]].append(row)
|
2016-03-18 02:44:58 -04:00
|
|
|
chart_data = []
|
|
|
|
for k, v in series.items():
|
2019-06-25 16:34:48 -04:00
|
|
|
chart_data.append({"key": k, "values": v})
|
2016-03-16 23:25:41 -04:00
|
|
|
return chart_data
|
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
|
2016-12-12 11:58:13 -05:00
|
|
|
class BulletViz(NVD3Viz):
|
|
|
|
|
|
|
|
"""Based on the NVD3 bullet chart"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "bullet"
|
|
|
|
verbose_name = _("Bullet Chart")
|
2016-12-12 11:58:13 -05:00
|
|
|
is_timeseries = False
|
|
|
|
|
|
|
|
def query_obj(self):
|
|
|
|
form_data = self.form_data
|
2019-04-12 00:48:35 -04:00
|
|
|
d = super().query_obj()
|
2019-06-25 16:34:48 -04:00
|
|
|
self.metric = form_data.get("metric")
|
2016-12-12 11:58:13 -05:00
|
|
|
|
|
|
|
def as_strings(field):
|
|
|
|
value = form_data.get(field)
|
2019-06-25 16:34:48 -04:00
|
|
|
return value.split(",") if value else []
|
2016-12-12 11:58:13 -05:00
|
|
|
|
|
|
|
def as_floats(field):
|
|
|
|
return [float(x) for x in as_strings(field)]
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
self.ranges = as_floats("ranges")
|
|
|
|
self.range_labels = as_strings("range_labels")
|
|
|
|
self.markers = as_floats("markers")
|
|
|
|
self.marker_labels = as_strings("marker_labels")
|
|
|
|
self.marker_lines = as_floats("marker_lines")
|
|
|
|
self.marker_line_labels = as_strings("marker_line_labels")
|
2016-12-12 11:58:13 -05:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
d["metrics"] = [self.metric]
|
2016-12-12 11:58:13 -05:00
|
|
|
if not self.metric:
|
2019-06-25 16:34:48 -04:00
|
|
|
raise Exception(_("Pick a metric to display"))
|
2016-12-12 11:58:13 -05:00
|
|
|
return d
|
|
|
|
|
2017-02-16 20:28:35 -05:00
|
|
|
def get_data(self, df):
|
2019-06-25 16:34:48 -04:00
|
|
|
df["metric"] = df[[utils.get_metric_name(self.metric)]]
|
|
|
|
values = df["metric"].values
|
2016-12-12 11:58:13 -05:00
|
|
|
return {
|
2019-06-25 16:34:48 -04:00
|
|
|
"measures": values.tolist(),
|
|
|
|
"ranges": self.ranges or [0, values.max() * 1.1],
|
|
|
|
"rangeLabels": self.range_labels or None,
|
|
|
|
"markers": self.markers or None,
|
|
|
|
"markerLabels": self.marker_labels or None,
|
|
|
|
"markerLines": self.marker_lines or None,
|
|
|
|
"markerLineLabels": self.marker_line_labels or None,
|
2016-12-12 11:58:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
class BigNumberViz(BaseViz):
|
2016-03-16 23:25:41 -04:00
|
|
|
|
|
|
|
"""Put emphasis on a single metric with this big number viz"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "big_number"
|
|
|
|
verbose_name = _("Big Number with Trendline")
|
2016-11-10 02:08:22 -05:00
|
|
|
credits = 'a <a href="https://github.com/airbnb/superset">Superset</a> original'
|
2016-03-18 02:44:58 -04:00
|
|
|
is_timeseries = True
|
|
|
|
|
|
|
|
def query_obj(self):
|
2019-04-12 00:48:35 -04:00
|
|
|
d = super().query_obj()
|
2019-06-25 16:34:48 -04:00
|
|
|
metric = self.form_data.get("metric")
|
2016-03-18 02:44:58 -04:00
|
|
|
if not metric:
|
2019-06-25 16:34:48 -04:00
|
|
|
raise Exception(_("Pick a metric!"))
|
|
|
|
d["metrics"] = [self.form_data.get("metric")]
|
|
|
|
self.form_data["metric"] = metric
|
2016-03-18 02:44:58 -04:00
|
|
|
return d
|
|
|
|
|
|
|
|
|
2016-04-07 17:22:12 -04:00
|
|
|
class BigNumberTotalViz(BaseViz):
|
|
|
|
|
|
|
|
"""Put emphasis on a single metric with this big number viz"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "big_number_total"
|
|
|
|
verbose_name = _("Big Number")
|
2016-11-10 02:08:22 -05:00
|
|
|
credits = 'a <a href="https://github.com/airbnb/superset">Superset</a> original'
|
2016-04-07 17:22:12 -04:00
|
|
|
is_timeseries = False
|
|
|
|
|
|
|
|
def query_obj(self):
|
2019-04-12 00:48:35 -04:00
|
|
|
d = super().query_obj()
|
2019-06-25 16:34:48 -04:00
|
|
|
metric = self.form_data.get("metric")
|
2016-04-07 17:22:12 -04:00
|
|
|
if not metric:
|
2019-06-25 16:34:48 -04:00
|
|
|
raise Exception(_("Pick a metric!"))
|
|
|
|
d["metrics"] = [self.form_data.get("metric")]
|
|
|
|
self.form_data["metric"] = metric
|
2019-08-07 00:15:47 -04:00
|
|
|
|
|
|
|
# Limiting rows is not required as only one cell is returned
|
|
|
|
d["row_limit"] = None
|
2016-04-07 17:22:12 -04:00
|
|
|
return d
|
|
|
|
|
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
class NVD3TimeSeriesViz(NVD3Viz):
|
2016-03-16 23:25:41 -04:00
|
|
|
|
|
|
|
"""A rich line chart component with tons of options"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "line"
|
|
|
|
verbose_name = _("Time Series - Line Chart")
|
2016-03-18 02:44:58 -04:00
|
|
|
sort_series = False
|
|
|
|
is_timeseries = True
|
2019-12-02 11:53:21 -05:00
|
|
|
pivot_fill_value: Optional[int] = None
|
2016-03-18 02:44:58 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
def to_series(self, df, classed="", title_suffix=""):
|
2017-02-16 20:28:35 -05:00
|
|
|
cols = []
|
|
|
|
for col in df.columns:
|
2019-06-25 16:34:48 -04:00
|
|
|
if col == "":
|
|
|
|
cols.append("N/A")
|
2017-02-16 20:28:35 -05:00
|
|
|
elif col is None:
|
2019-06-25 16:34:48 -04:00
|
|
|
cols.append("NULL")
|
2017-02-16 20:28:35 -05:00
|
|
|
else:
|
|
|
|
cols.append(col)
|
|
|
|
df.columns = cols
|
2019-06-25 16:34:48 -04:00
|
|
|
series = df.to_dict("series")
|
2017-02-16 20:28:35 -05:00
|
|
|
|
|
|
|
chart_data = []
|
|
|
|
for name in df.T.index.tolist():
|
|
|
|
ys = series[name]
|
2019-06-25 16:34:48 -04:00
|
|
|
if df[name].dtype.kind not in "biufc":
|
2017-02-16 20:28:35 -05:00
|
|
|
continue
|
2018-04-04 03:36:23 -04:00
|
|
|
if isinstance(name, list):
|
2018-10-13 12:38:46 -04:00
|
|
|
series_title = [str(title) for title in name]
|
2018-04-04 03:36:23 -04:00
|
|
|
elif isinstance(name, tuple):
|
2018-10-13 12:38:46 -04:00
|
|
|
series_title = tuple(str(title) for title in name)
|
2018-04-04 03:36:23 -04:00
|
|
|
else:
|
2018-10-13 12:38:46 -04:00
|
|
|
series_title = str(name)
|
2017-10-02 13:42:16 -04:00
|
|
|
if (
|
2019-06-25 16:34:48 -04:00
|
|
|
isinstance(series_title, (list, tuple))
|
|
|
|
and len(series_title) > 1
|
|
|
|
and len(self.metric_labels) == 1
|
|
|
|
):
|
2017-10-02 13:42:16 -04:00
|
|
|
# Removing metric from series name if only one metric
|
|
|
|
series_title = series_title[1:]
|
2018-04-07 18:19:58 -04:00
|
|
|
if title_suffix:
|
2018-10-13 12:38:46 -04:00
|
|
|
if isinstance(series_title, str):
|
2018-04-07 18:19:58 -04:00
|
|
|
series_title = (series_title, title_suffix)
|
|
|
|
elif isinstance(series_title, (list, tuple)):
|
|
|
|
series_title = series_title + (title_suffix,)
|
2017-02-16 20:28:35 -05:00
|
|
|
|
2017-12-07 00:50:33 -05:00
|
|
|
values = []
|
2019-04-19 11:59:19 -04:00
|
|
|
non_nan_cnt = 0
|
2017-12-07 00:50:33 -05:00
|
|
|
for ds in df.index:
|
|
|
|
if ds in ys:
|
2019-06-25 16:34:48 -04:00
|
|
|
d = {"x": ds, "y": ys[ds]}
|
2019-04-19 11:59:19 -04:00
|
|
|
if not np.isnan(ys[ds]):
|
|
|
|
non_nan_cnt += 1
|
2017-12-07 00:50:33 -05:00
|
|
|
else:
|
|
|
|
d = {}
|
|
|
|
values.append(d)
|
|
|
|
|
2019-04-19 11:59:19 -04:00
|
|
|
if non_nan_cnt == 0:
|
|
|
|
continue
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
d = {"key": series_title, "values": values}
|
2017-12-07 00:50:33 -05:00
|
|
|
if classed:
|
2019-06-25 16:34:48 -04:00
|
|
|
d["classed"] = classed
|
2017-02-16 20:28:35 -05:00
|
|
|
chart_data.append(d)
|
|
|
|
return chart_data
|
|
|
|
|
2017-10-13 00:54:59 -04:00
|
|
|
def process_data(self, df, aggregate=False):
|
2017-02-16 20:28:35 -05:00
|
|
|
fd = self.form_data
|
2019-06-25 16:34:48 -04:00
|
|
|
if fd.get("granularity") == "all":
|
|
|
|
raise Exception(_("Pick a time granularity for your time series"))
|
2019-01-18 11:24:11 -05:00
|
|
|
|
2019-07-02 14:10:50 -04:00
|
|
|
if aggregate:
|
2017-10-13 00:54:59 -04:00
|
|
|
df = df.pivot_table(
|
|
|
|
index=DTTM_ALIAS,
|
2019-06-25 16:34:48 -04:00
|
|
|
columns=fd.get("groupby"),
|
2019-03-20 20:14:15 -04:00
|
|
|
values=self.metric_labels,
|
2019-07-02 14:10:50 -04:00
|
|
|
fill_value=0,
|
|
|
|
aggfunc=sum,
|
2019-03-20 20:14:15 -04:00
|
|
|
)
|
2017-10-13 00:54:59 -04:00
|
|
|
else:
|
|
|
|
df = df.pivot_table(
|
2019-12-02 11:53:21 -05:00
|
|
|
index=DTTM_ALIAS,
|
|
|
|
columns=fd.get("groupby"),
|
|
|
|
values=self.metric_labels,
|
|
|
|
fill_value=self.pivot_fill_value,
|
2019-03-20 20:14:15 -04:00
|
|
|
)
|
2016-03-18 02:44:58 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
rule = fd.get("resample_rule")
|
2019-07-02 14:10:50 -04:00
|
|
|
method = fd.get("resample_method")
|
|
|
|
|
|
|
|
if rule and method:
|
|
|
|
df = getattr(df.resample(rule), method)()
|
2016-03-18 02:44:58 -04:00
|
|
|
|
|
|
|
if self.sort_series:
|
|
|
|
dfs = df.sum()
|
2016-08-02 02:02:16 -04:00
|
|
|
dfs.sort_values(ascending=False, inplace=True)
|
2016-03-18 02:44:58 -04:00
|
|
|
df = df[dfs.index]
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
rolling_type = fd.get("rolling_type")
|
|
|
|
rolling_periods = int(fd.get("rolling_periods") or 0)
|
|
|
|
min_periods = int(fd.get("min_periods") or 0)
|
2016-03-18 02:44:58 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
if rolling_type in ("mean", "std", "sum") and rolling_periods:
|
|
|
|
kwargs = dict(window=rolling_periods, min_periods=min_periods)
|
|
|
|
if rolling_type == "mean":
|
2018-07-04 18:04:57 -04:00
|
|
|
df = df.rolling(**kwargs).mean()
|
2019-06-25 16:34:48 -04:00
|
|
|
elif rolling_type == "std":
|
2018-07-04 18:04:57 -04:00
|
|
|
df = df.rolling(**kwargs).std()
|
2019-06-25 16:34:48 -04:00
|
|
|
elif rolling_type == "sum":
|
2018-07-04 18:04:57 -04:00
|
|
|
df = df.rolling(**kwargs).sum()
|
2019-06-25 16:34:48 -04:00
|
|
|
elif rolling_type == "cumsum":
|
2016-03-18 02:44:58 -04:00
|
|
|
df = df.cumsum()
|
2017-08-30 16:32:07 -04:00
|
|
|
if min_periods:
|
|
|
|
df = df[min_periods:]
|
2017-01-11 00:09:02 -05:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
if fd.get("contribution"):
|
2019-03-14 17:05:06 -04:00
|
|
|
dft = df.T
|
|
|
|
df = (dft / dft.sum()).T
|
|
|
|
|
2017-08-25 14:08:15 -04:00
|
|
|
return df
|
|
|
|
|
2018-02-07 17:49:19 -05:00
|
|
|
def run_extra_queries(self):
|
2017-08-25 14:08:15 -04:00
|
|
|
fd = self.form_data
|
2018-06-01 00:18:36 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
time_compare = fd.get("time_compare") or []
|
2018-06-01 00:18:36 -04:00
|
|
|
# backwards compatibility
|
|
|
|
if not isinstance(time_compare, list):
|
|
|
|
time_compare = [time_compare]
|
|
|
|
|
|
|
|
for option in time_compare:
|
2016-03-18 02:44:58 -04:00
|
|
|
query_object = self.query_obj()
|
2019-06-19 13:10:18 -04:00
|
|
|
delta = utils.parse_past_timedelta(option)
|
2019-06-25 16:34:48 -04:00
|
|
|
query_object["inner_from_dttm"] = query_object["from_dttm"]
|
|
|
|
query_object["inner_to_dttm"] = query_object["to_dttm"]
|
|
|
|
|
|
|
|
if not query_object["from_dttm"] or not query_object["to_dttm"]:
|
|
|
|
raise Exception(
|
|
|
|
_(
|
|
|
|
"`Since` and `Until` time bounds should be specified "
|
|
|
|
"when using the `Time Shift` feature."
|
|
|
|
)
|
|
|
|
)
|
|
|
|
query_object["from_dttm"] -= delta
|
|
|
|
query_object["to_dttm"] -= delta
|
2016-03-18 02:44:58 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
df2 = self.get_df_payload(query_object, time_compare=option).get("df")
|
2018-07-16 19:30:11 -04:00
|
|
|
if df2 is not None and DTTM_ALIAS in df2:
|
2019-06-25 16:34:48 -04:00
|
|
|
label = "{} offset".format(option)
|
2018-02-12 14:48:14 -05:00
|
|
|
df2[DTTM_ALIAS] += delta
|
|
|
|
df2 = self.process_data(df2)
|
2018-07-16 19:30:11 -04:00
|
|
|
self._extra_chart_data.append((label, df2))
|
2018-02-07 17:49:19 -05:00
|
|
|
|
|
|
|
def get_data(self, df):
|
2018-07-16 19:30:11 -04:00
|
|
|
fd = self.form_data
|
2019-06-25 16:34:48 -04:00
|
|
|
comparison_type = fd.get("comparison_type") or "values"
|
2018-02-07 17:49:19 -05:00
|
|
|
df = self.process_data(df)
|
2019-06-25 16:34:48 -04:00
|
|
|
if comparison_type == "values":
|
2019-04-22 14:53:08 -04:00
|
|
|
# Filter out series with all NaN
|
2019-06-25 16:34:48 -04:00
|
|
|
chart_data = self.to_series(df.dropna(axis=1, how="all"))
|
2019-04-22 14:53:08 -04:00
|
|
|
|
2018-07-16 19:30:11 -04:00
|
|
|
for i, (label, df2) in enumerate(self._extra_chart_data):
|
|
|
|
chart_data.extend(
|
|
|
|
self.to_series(
|
2019-06-25 16:34:48 -04:00
|
|
|
df2, classed="time-shift-{}".format(i), title_suffix=label
|
|
|
|
)
|
|
|
|
)
|
2018-07-16 19:30:11 -04:00
|
|
|
else:
|
|
|
|
chart_data = []
|
|
|
|
for i, (label, df2) in enumerate(self._extra_chart_data):
|
|
|
|
# reindex df2 into the df2 index
|
|
|
|
combined_index = df.index.union(df2.index)
|
2019-06-25 16:34:48 -04:00
|
|
|
df2 = (
|
|
|
|
df2.reindex(combined_index)
|
|
|
|
.interpolate(method="time")
|
2018-07-16 19:30:11 -04:00
|
|
|
.reindex(df.index)
|
2019-06-25 16:34:48 -04:00
|
|
|
)
|
2018-07-16 19:30:11 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
if comparison_type == "absolute":
|
2018-07-16 19:30:11 -04:00
|
|
|
diff = df - df2
|
2019-06-25 16:34:48 -04:00
|
|
|
elif comparison_type == "percentage":
|
2018-07-16 19:30:11 -04:00
|
|
|
diff = (df - df2) / df2
|
2019-06-25 16:34:48 -04:00
|
|
|
elif comparison_type == "ratio":
|
2018-07-16 19:30:11 -04:00
|
|
|
diff = df / df2
|
|
|
|
else:
|
|
|
|
raise Exception(
|
2019-06-25 16:34:48 -04:00
|
|
|
"Invalid `comparison_type`: {0}".format(comparison_type)
|
|
|
|
)
|
2018-02-07 17:49:19 -05:00
|
|
|
|
2018-07-16 19:30:11 -04:00
|
|
|
# remove leading/trailing NaNs from the time shift difference
|
2019-06-25 16:34:48 -04:00
|
|
|
diff = diff[diff.first_valid_index() : diff.last_valid_index()]
|
2018-07-16 19:30:11 -04:00
|
|
|
|
|
|
|
chart_data.extend(
|
|
|
|
self.to_series(
|
2019-06-25 16:34:48 -04:00
|
|
|
diff, classed="time-shift-{}".format(i), title_suffix=label
|
|
|
|
)
|
|
|
|
)
|
2018-07-16 19:30:11 -04:00
|
|
|
|
2019-01-25 13:30:31 -05:00
|
|
|
if not self.sort_series:
|
2019-06-25 16:34:48 -04:00
|
|
|
chart_data = sorted(chart_data, key=lambda x: tuple(x["key"]))
|
2019-01-25 13:30:31 -05:00
|
|
|
return chart_data
|
2016-03-18 02:44:58 -04:00
|
|
|
|
|
|
|
|
2018-05-21 20:47:21 -04:00
|
|
|
class MultiLineViz(NVD3Viz):
|
|
|
|
|
|
|
|
"""Pile on multiple line charts"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "line_multi"
|
|
|
|
verbose_name = _("Time Series - Multiple Line Charts")
|
2018-05-21 20:47:21 -04:00
|
|
|
|
|
|
|
is_timeseries = True
|
|
|
|
|
|
|
|
def query_obj(self):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_data(self, df):
|
|
|
|
fd = self.form_data
|
|
|
|
# Late imports to avoid circular import issues
|
2019-12-18 14:40:45 -05:00
|
|
|
from superset.models.slice import Slice
|
2018-05-21 20:47:21 -04:00
|
|
|
from superset import db
|
2019-06-25 16:34:48 -04:00
|
|
|
|
|
|
|
slice_ids1 = fd.get("line_charts")
|
2018-05-21 20:47:21 -04:00
|
|
|
slices1 = db.session.query(Slice).filter(Slice.id.in_(slice_ids1)).all()
|
2019-06-25 16:34:48 -04:00
|
|
|
slice_ids2 = fd.get("line_charts_2")
|
2018-05-21 20:47:21 -04:00
|
|
|
slices2 = db.session.query(Slice).filter(Slice.id.in_(slice_ids2)).all()
|
|
|
|
return {
|
2019-06-25 16:34:48 -04:00
|
|
|
"slices": {
|
|
|
|
"axis1": [slc.data for slc in slices1],
|
|
|
|
"axis2": [slc.data for slc in slices2],
|
|
|
|
}
|
2018-05-21 20:47:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-06 13:30:27 -05:00
|
|
|
class NVD3DualLineViz(NVD3Viz):
|
|
|
|
|
|
|
|
"""A rich line chart with dual axis"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "dual_line"
|
|
|
|
verbose_name = _("Time Series - Dual Axis Line Chart")
|
2017-01-06 13:30:27 -05:00
|
|
|
sort_series = False
|
|
|
|
is_timeseries = True
|
|
|
|
|
2017-01-11 13:07:30 -05:00
|
|
|
def query_obj(self):
|
2019-04-12 00:48:35 -04:00
|
|
|
d = super().query_obj()
|
2019-06-25 16:34:48 -04:00
|
|
|
m1 = self.form_data.get("metric")
|
|
|
|
m2 = self.form_data.get("metric_2")
|
|
|
|
d["metrics"] = [m1, m2]
|
2017-02-16 20:28:35 -05:00
|
|
|
if not m1:
|
2019-06-25 16:34:48 -04:00
|
|
|
raise Exception(_("Pick a metric for left axis!"))
|
2017-02-16 20:28:35 -05:00
|
|
|
if not m2:
|
2019-06-25 16:34:48 -04:00
|
|
|
raise Exception(_("Pick a metric for right axis!"))
|
2017-02-16 20:28:35 -05:00
|
|
|
if m1 == m2:
|
2019-06-25 16:34:48 -04:00
|
|
|
raise Exception(
|
|
|
|
_("Please choose different metrics" " on left and right axis")
|
|
|
|
)
|
2017-01-11 13:07:30 -05:00
|
|
|
return d
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
def to_series(self, df, classed=""):
|
2017-01-06 13:30:27 -05:00
|
|
|
cols = []
|
|
|
|
for col in df.columns:
|
2019-06-25 16:34:48 -04:00
|
|
|
if col == "":
|
|
|
|
cols.append("N/A")
|
2017-01-06 13:30:27 -05:00
|
|
|
elif col is None:
|
2019-06-25 16:34:48 -04:00
|
|
|
cols.append("NULL")
|
2017-01-06 13:30:27 -05:00
|
|
|
else:
|
|
|
|
cols.append(col)
|
|
|
|
df.columns = cols
|
2019-06-25 16:34:48 -04:00
|
|
|
series = df.to_dict("series")
|
2017-01-06 13:30:27 -05:00
|
|
|
chart_data = []
|
2019-06-25 16:34:48 -04:00
|
|
|
metrics = [self.form_data.get("metric"), self.form_data.get("metric_2")]
|
2017-02-10 15:54:03 -05:00
|
|
|
for i, m in enumerate(metrics):
|
2018-06-22 17:31:43 -04:00
|
|
|
m = utils.get_metric_name(m)
|
2017-02-10 15:54:03 -05:00
|
|
|
ys = series[m]
|
2019-06-25 16:34:48 -04:00
|
|
|
if df[m].dtype.kind not in "biufc":
|
2017-01-06 13:30:27 -05:00
|
|
|
continue
|
2017-02-10 15:54:03 -05:00
|
|
|
series_title = m
|
2017-01-06 13:30:27 -05:00
|
|
|
d = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"key": series_title,
|
|
|
|
"classed": classed,
|
|
|
|
"values": [
|
|
|
|
{"x": ds, "y": ys[ds] if ds in ys else None} for ds in df.index
|
2017-01-06 13:30:27 -05:00
|
|
|
],
|
2019-06-25 16:34:48 -04:00
|
|
|
"yAxis": i + 1,
|
|
|
|
"type": "line",
|
2017-01-06 13:30:27 -05:00
|
|
|
}
|
|
|
|
chart_data.append(d)
|
|
|
|
return chart_data
|
|
|
|
|
2017-02-16 20:28:35 -05:00
|
|
|
def get_data(self, df):
|
|
|
|
fd = self.form_data
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
if self.form_data.get("granularity") == "all":
|
|
|
|
raise Exception(_("Pick a time granularity for your time series"))
|
2017-02-16 20:28:35 -05:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
metric = utils.get_metric_name(fd.get("metric"))
|
|
|
|
metric_2 = utils.get_metric_name(fd.get("metric_2"))
|
2019-08-16 14:25:57 -04:00
|
|
|
df = df.pivot_table(index=DTTM_ALIAS, values=[metric, metric_2])
|
2017-01-06 13:30:27 -05:00
|
|
|
|
|
|
|
chart_data = self.to_series(df)
|
|
|
|
return chart_data
|
|
|
|
|
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
class NVD3TimeSeriesBarViz(NVD3TimeSeriesViz):
|
2016-03-16 23:25:41 -04:00
|
|
|
|
|
|
|
"""A bar chart where the x axis is time"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "bar"
|
2016-03-18 02:44:58 -04:00
|
|
|
sort_series = True
|
2019-06-25 16:34:48 -04:00
|
|
|
verbose_name = _("Time Series - Bar Chart")
|
2016-03-18 02:44:58 -04:00
|
|
|
|
|
|
|
|
2017-12-07 00:50:33 -05:00
|
|
|
class NVD3TimePivotViz(NVD3TimeSeriesViz):
|
|
|
|
|
|
|
|
"""Time Series - Periodicity Pivot"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "time_pivot"
|
2017-12-07 00:50:33 -05:00
|
|
|
sort_series = True
|
2019-06-25 16:34:48 -04:00
|
|
|
verbose_name = _("Time Series - Period Pivot")
|
2017-12-07 00:50:33 -05:00
|
|
|
|
|
|
|
def query_obj(self):
|
2019-04-12 00:48:35 -04:00
|
|
|
d = super().query_obj()
|
2019-06-25 16:34:48 -04:00
|
|
|
d["metrics"] = [self.form_data.get("metric")]
|
2017-12-07 00:50:33 -05:00
|
|
|
return d
|
|
|
|
|
|
|
|
def get_data(self, df):
|
|
|
|
fd = self.form_data
|
|
|
|
df = self.process_data(df)
|
2019-06-25 16:34:48 -04:00
|
|
|
freq = to_offset(fd.get("freq"))
|
2019-08-07 17:17:23 -04:00
|
|
|
try:
|
|
|
|
freq = type(freq)(freq.n, normalize=True, **freq.kwds)
|
|
|
|
except ValueError:
|
|
|
|
freq = type(freq)(freq.n, **freq.kwds)
|
2019-08-02 18:28:39 -04:00
|
|
|
df.index.name = None
|
2017-12-07 00:50:33 -05:00
|
|
|
df[DTTM_ALIAS] = df.index.map(freq.rollback)
|
2019-06-25 16:34:48 -04:00
|
|
|
df["ranked"] = df[DTTM_ALIAS].rank(method="dense", ascending=False) - 1
|
2017-12-07 00:50:33 -05:00
|
|
|
df.ranked = df.ranked.map(int)
|
2019-06-25 16:34:48 -04:00
|
|
|
df["series"] = "-" + df.ranked.map(str)
|
|
|
|
df["series"] = df["series"].str.replace("-0", "current")
|
2017-12-07 00:50:33 -05:00
|
|
|
rank_lookup = {
|
2019-06-25 16:34:48 -04:00
|
|
|
row["series"]: row["ranked"] for row in df.to_dict(orient="records")
|
2017-12-07 00:50:33 -05:00
|
|
|
}
|
|
|
|
max_ts = df[DTTM_ALIAS].max()
|
2019-06-25 16:34:48 -04:00
|
|
|
max_rank = df["ranked"].max()
|
2017-12-07 00:50:33 -05:00
|
|
|
df[DTTM_ALIAS] = df.index + (max_ts - df[DTTM_ALIAS])
|
|
|
|
df = df.pivot_table(
|
|
|
|
index=DTTM_ALIAS,
|
2019-06-25 16:34:48 -04:00
|
|
|
columns="series",
|
|
|
|
values=utils.get_metric_name(fd.get("metric")),
|
2019-03-20 20:14:15 -04:00
|
|
|
)
|
2017-12-07 00:50:33 -05:00
|
|
|
chart_data = self.to_series(df)
|
|
|
|
for serie in chart_data:
|
2019-06-25 16:34:48 -04:00
|
|
|
serie["rank"] = rank_lookup[serie["key"]]
|
|
|
|
serie["perc"] = 1 - (serie["rank"] / (max_rank + 1))
|
2017-12-07 00:50:33 -05:00
|
|
|
return chart_data
|
|
|
|
|
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
class NVD3CompareTimeSeriesViz(NVD3TimeSeriesViz):
|
2016-03-16 23:25:41 -04:00
|
|
|
|
|
|
|
"""A line chart component where you can compare the % change over time"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "compare"
|
|
|
|
verbose_name = _("Time Series - Percent Change")
|
2016-03-18 02:44:58 -04:00
|
|
|
|
|
|
|
|
|
|
|
class NVD3TimeSeriesStackedViz(NVD3TimeSeriesViz):
|
2016-03-16 23:25:41 -04:00
|
|
|
|
|
|
|
"""A rich stack area chart"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "area"
|
|
|
|
verbose_name = _("Time Series - Stacked")
|
2016-03-18 02:44:58 -04:00
|
|
|
sort_series = True
|
2019-12-02 11:53:21 -05:00
|
|
|
pivot_fill_value = 0
|
2016-03-18 02:44:58 -04:00
|
|
|
|
|
|
|
|
|
|
|
class DistributionPieViz(NVD3Viz):
|
2016-03-16 23:25:41 -04:00
|
|
|
|
|
|
|
"""Annoy visualization snobs with this controversial pie chart"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "pie"
|
|
|
|
verbose_name = _("Distribution - NVD3 - Pie Chart")
|
2016-03-18 02:44:58 -04:00
|
|
|
is_timeseries = False
|
|
|
|
|
2017-02-16 20:28:35 -05:00
|
|
|
def get_data(self, df):
|
2018-05-15 13:37:34 -04:00
|
|
|
metric = self.metric_labels[0]
|
2019-08-16 14:25:57 -04:00
|
|
|
df = df.pivot_table(index=self.groupby, values=[metric])
|
2018-05-15 13:37:34 -04:00
|
|
|
df.sort_values(by=metric, ascending=False, inplace=True)
|
2017-03-21 00:10:06 -04:00
|
|
|
df = df.reset_index()
|
2019-06-25 16:34:48 -04:00
|
|
|
df.columns = ["x", "y"]
|
|
|
|
return df.to_dict(orient="records")
|
2016-03-18 02:44:58 -04:00
|
|
|
|
|
|
|
|
2016-08-10 18:04:19 -04:00
|
|
|
class HistogramViz(BaseViz):
|
|
|
|
|
|
|
|
"""Histogram"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "histogram"
|
|
|
|
verbose_name = _("Histogram")
|
2016-08-10 18:04:19 -04:00
|
|
|
is_timeseries = False
|
|
|
|
|
|
|
|
def query_obj(self):
|
|
|
|
"""Returns the query object for this visualization"""
|
2019-04-12 00:48:35 -04:00
|
|
|
d = super().query_obj()
|
2019-10-30 19:19:16 -04:00
|
|
|
d["row_limit"] = self.form_data.get("row_limit", int(config["VIZ_ROW_LIMIT"]))
|
2019-06-25 16:34:48 -04:00
|
|
|
numeric_columns = self.form_data.get("all_columns_x")
|
2018-04-11 00:21:24 -04:00
|
|
|
if numeric_columns is None:
|
2019-06-25 16:34:48 -04:00
|
|
|
raise Exception(_("Must have at least one numeric column specified"))
|
2019-08-30 17:11:46 -04:00
|
|
|
self.columns = numeric_columns
|
|
|
|
d["columns"] = numeric_columns + self.groupby
|
2018-04-11 00:21:24 -04:00
|
|
|
# override groupby entry to avoid aggregation
|
2019-06-25 16:34:48 -04:00
|
|
|
d["groupby"] = []
|
2016-08-10 18:04:19 -04:00
|
|
|
return d
|
|
|
|
|
2018-11-19 17:01:20 -05:00
|
|
|
def labelify(self, keys, column):
|
2019-08-30 17:11:46 -04:00
|
|
|
if isinstance(keys, str):
|
2018-11-19 17:01:20 -05:00
|
|
|
keys = (keys,)
|
|
|
|
# removing undesirable characters
|
2019-08-30 17:11:46 -04:00
|
|
|
labels = [re.sub(r"\W+", r"_", k) for k in keys]
|
|
|
|
if len(self.columns) > 1 or not self.groupby:
|
2018-11-19 17:01:20 -05:00
|
|
|
# Only show numeric column in label if there are many
|
|
|
|
labels = [column] + labels
|
2019-06-25 16:34:48 -04:00
|
|
|
return "__".join(labels)
|
2018-11-19 17:01:20 -05:00
|
|
|
|
2017-02-16 20:28:35 -05:00
|
|
|
def get_data(self, df):
|
2016-08-10 18:04:19 -04:00
|
|
|
"""Returns the chart data"""
|
2018-04-11 00:21:24 -04:00
|
|
|
chart_data = []
|
|
|
|
if len(self.groupby) > 0:
|
|
|
|
groups = df.groupby(self.groupby)
|
|
|
|
else:
|
|
|
|
groups = [((), df)]
|
|
|
|
for keys, data in groups:
|
2019-06-25 16:34:48 -04:00
|
|
|
chart_data.extend(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"key": self.labelify(keys, column),
|
|
|
|
"values": data[column].tolist(),
|
|
|
|
}
|
|
|
|
for column in self.columns
|
|
|
|
]
|
|
|
|
)
|
2016-08-10 18:04:19 -04:00
|
|
|
return chart_data
|
|
|
|
|
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
class DistributionBarViz(DistributionPieViz):
|
2016-03-16 23:25:41 -04:00
|
|
|
|
|
|
|
"""A good old bar chart"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "dist_bar"
|
|
|
|
verbose_name = _("Distribution - Bar Chart")
|
2016-03-18 02:44:58 -04:00
|
|
|
is_timeseries = False
|
|
|
|
|
|
|
|
def query_obj(self):
|
2019-10-18 17:44:27 -04:00
|
|
|
d = super().query_obj()
|
2016-03-18 02:44:58 -04:00
|
|
|
fd = self.form_data
|
2019-06-25 16:34:48 -04:00
|
|
|
if len(d["groupby"]) < len(fd.get("groupby") or []) + len(
|
|
|
|
fd.get("columns") or []
|
2017-11-10 15:06:22 -05:00
|
|
|
):
|
2019-06-25 16:34:48 -04:00
|
|
|
raise Exception(_("Can't have overlap between Series and Breakdowns"))
|
|
|
|
if not fd.get("metrics"):
|
|
|
|
raise Exception(_("Pick at least one metric"))
|
|
|
|
if not fd.get("groupby"):
|
|
|
|
raise Exception(_("Pick at least one field for [Series]"))
|
2016-03-18 02:44:58 -04:00
|
|
|
return d
|
|
|
|
|
2017-02-16 20:28:35 -05:00
|
|
|
def get_data(self, df):
|
2016-03-18 02:44:58 -04:00
|
|
|
fd = self.form_data
|
2018-05-15 13:37:34 -04:00
|
|
|
metrics = self.metric_labels
|
2019-12-03 19:11:15 -05:00
|
|
|
columns = fd.get("columns") or []
|
|
|
|
|
|
|
|
# pandas will throw away nulls when grouping/pivoting,
|
|
|
|
# so we substitute NULL_STRING for any nulls in the necessary columns
|
|
|
|
filled_cols = self.groupby + columns
|
|
|
|
df[filled_cols] = df[filled_cols].fillna(value=NULL_STRING)
|
2016-03-18 02:44:58 -04:00
|
|
|
|
2018-05-15 13:37:34 -04:00
|
|
|
row = df.groupby(self.groupby).sum()[metrics[0]].copy()
|
2016-08-02 02:02:16 -04:00
|
|
|
row.sort_values(ascending=False, inplace=True)
|
2019-08-16 14:25:57 -04:00
|
|
|
pt = df.pivot_table(index=self.groupby, columns=columns, values=metrics)
|
2019-06-25 16:34:48 -04:00
|
|
|
if fd.get("contribution"):
|
2016-06-20 12:16:51 -04:00
|
|
|
pt = pt.T
|
|
|
|
pt = (pt / pt.sum()).T
|
2016-03-18 02:44:58 -04:00
|
|
|
pt = pt.reindex(row.index)
|
|
|
|
chart_data = []
|
2018-04-03 00:48:14 -04:00
|
|
|
for name, ys in pt.items():
|
2019-06-25 16:34:48 -04:00
|
|
|
if pt[name].dtype.kind not in "biufc" or name in self.groupby:
|
2016-03-18 02:44:58 -04:00
|
|
|
continue
|
2018-10-13 12:38:46 -04:00
|
|
|
if isinstance(name, str):
|
2016-03-18 02:44:58 -04:00
|
|
|
series_title = name
|
|
|
|
else:
|
2018-08-15 12:35:44 -04:00
|
|
|
offset = 0 if len(metrics) > 1 else 1
|
2019-06-25 16:34:48 -04:00
|
|
|
series_title = ", ".join([str(s) for s in name[offset:]])
|
2017-02-27 02:44:08 -05:00
|
|
|
values = []
|
2018-04-03 00:48:14 -04:00
|
|
|
for i, v in ys.items():
|
2017-03-16 02:50:12 -04:00
|
|
|
x = i
|
|
|
|
if isinstance(x, (tuple, list)):
|
2019-06-25 16:34:48 -04:00
|
|
|
x = ", ".join([str(s) for s in x])
|
2017-03-16 02:50:12 -04:00
|
|
|
else:
|
2018-10-13 12:38:46 -04:00
|
|
|
x = str(x)
|
2019-06-25 16:34:48 -04:00
|
|
|
values.append({"x": x, "y": v})
|
|
|
|
d = {"key": series_title, "values": values}
|
2016-03-18 02:44:58 -04:00
|
|
|
chart_data.append(d)
|
2016-03-16 23:25:41 -04:00
|
|
|
return chart_data
|
2016-03-18 02:44:58 -04:00
|
|
|
|
|
|
|
|
|
|
|
class SunburstViz(BaseViz):
|
2016-03-16 23:25:41 -04:00
|
|
|
|
|
|
|
"""A multi level sunburst chart"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "sunburst"
|
|
|
|
verbose_name = _("Sunburst")
|
2016-03-18 02:44:58 -04:00
|
|
|
is_timeseries = False
|
2016-04-09 16:17:20 -04:00
|
|
|
credits = (
|
2019-06-25 16:34:48 -04:00
|
|
|
"Kerry Rodden "
|
|
|
|
'@<a href="https://bl.ocks.org/kerryrodden/7090426">bl.ocks.org</a>'
|
|
|
|
)
|
2017-02-16 20:28:35 -05:00
|
|
|
|
|
|
|
def get_data(self, df):
|
2018-02-09 17:27:22 -05:00
|
|
|
fd = self.form_data
|
2019-06-25 16:34:48 -04:00
|
|
|
cols = fd.get("groupby")
|
|
|
|
metric = utils.get_metric_name(fd.get("metric"))
|
|
|
|
secondary_metric = utils.get_metric_name(fd.get("secondary_metric"))
|
2018-02-09 17:27:22 -05:00
|
|
|
if metric == secondary_metric or secondary_metric is None:
|
2019-06-25 16:34:48 -04:00
|
|
|
df.columns = cols + ["m1"]
|
|
|
|
df["m2"] = df["m1"]
|
|
|
|
return json.loads(df.to_json(orient="values"))
|
2016-03-18 02:44:58 -04:00
|
|
|
|
|
|
|
def query_obj(self):
|
2019-04-12 00:48:35 -04:00
|
|
|
qry = super().query_obj()
|
2018-02-09 17:27:22 -05:00
|
|
|
fd = self.form_data
|
2019-06-25 16:34:48 -04:00
|
|
|
qry["metrics"] = [fd["metric"]]
|
|
|
|
secondary_metric = fd.get("secondary_metric")
|
|
|
|
if secondary_metric and secondary_metric != fd["metric"]:
|
|
|
|
qry["metrics"].append(secondary_metric)
|
2016-03-18 02:44:58 -04:00
|
|
|
return qry
|
|
|
|
|
|
|
|
|
|
|
|
class SankeyViz(BaseViz):
|
2016-03-16 23:25:41 -04:00
|
|
|
|
|
|
|
"""A Sankey diagram that requires a parent-child dataset"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "sankey"
|
|
|
|
verbose_name = _("Sankey")
|
2016-03-18 02:44:58 -04:00
|
|
|
is_timeseries = False
|
2016-04-09 16:17:20 -04:00
|
|
|
credits = '<a href="https://www.npmjs.com/package/d3-sankey">d3-sankey on npm</a>'
|
2016-03-18 02:44:58 -04:00
|
|
|
|
|
|
|
def query_obj(self):
|
2019-04-12 00:48:35 -04:00
|
|
|
qry = super().query_obj()
|
2019-06-25 16:34:48 -04:00
|
|
|
if len(qry["groupby"]) != 2:
|
|
|
|
raise Exception(_("Pick exactly 2 columns as [Source / Target]"))
|
|
|
|
qry["metrics"] = [self.form_data["metric"]]
|
2016-03-18 02:44:58 -04:00
|
|
|
return qry
|
|
|
|
|
2017-02-16 20:28:35 -05:00
|
|
|
def get_data(self, df):
|
2019-06-25 16:34:48 -04:00
|
|
|
df.columns = ["source", "target", "value"]
|
|
|
|
df["source"] = df["source"].astype(str)
|
|
|
|
df["target"] = df["target"].astype(str)
|
|
|
|
recs = df.to_dict(orient="records")
|
2016-04-09 03:17:31 -04:00
|
|
|
|
|
|
|
hierarchy = defaultdict(set)
|
|
|
|
for row in recs:
|
2019-06-25 16:34:48 -04:00
|
|
|
hierarchy[row["source"]].add(row["target"])
|
2016-04-09 03:17:31 -04:00
|
|
|
|
|
|
|
def find_cycle(g):
|
|
|
|
"""Whether there's a cycle in a directed graph"""
|
|
|
|
path = set()
|
2016-04-11 01:49:08 -04:00
|
|
|
|
2016-04-09 03:17:31 -04:00
|
|
|
def visit(vertex):
|
|
|
|
path.add(vertex)
|
|
|
|
for neighbour in g.get(vertex, ()):
|
|
|
|
if neighbour in path or visit(neighbour):
|
|
|
|
return (vertex, neighbour)
|
|
|
|
path.remove(vertex)
|
2016-04-11 01:49:08 -04:00
|
|
|
|
2016-04-09 03:17:31 -04:00
|
|
|
for v in g:
|
|
|
|
cycle = visit(v)
|
|
|
|
if cycle:
|
|
|
|
return cycle
|
|
|
|
|
|
|
|
cycle = find_cycle(hierarchy)
|
|
|
|
if cycle:
|
2019-06-25 16:34:48 -04:00
|
|
|
raise Exception(
|
|
|
|
_(
|
|
|
|
"There's a loop in your Sankey, please provide a tree. "
|
|
|
|
"Here's a faulty link: {}"
|
|
|
|
).format(cycle)
|
|
|
|
)
|
2016-04-09 03:17:31 -04:00
|
|
|
return recs
|
2016-03-18 02:44:58 -04:00
|
|
|
|
|
|
|
|
|
|
|
class DirectedForceViz(BaseViz):
|
2016-03-16 23:25:41 -04:00
|
|
|
|
|
|
|
"""An animated directed force layout graph visualization"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "directed_force"
|
|
|
|
verbose_name = _("Directed Force Layout")
|
2016-04-09 16:17:20 -04:00
|
|
|
credits = 'd3noob @<a href="http://bl.ocks.org/d3noob/5141278">bl.ocks.org</a>'
|
2016-03-18 02:44:58 -04:00
|
|
|
is_timeseries = False
|
2016-03-16 23:25:41 -04:00
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
def query_obj(self):
|
2019-04-12 00:48:35 -04:00
|
|
|
qry = super().query_obj()
|
2019-06-25 16:34:48 -04:00
|
|
|
if len(self.form_data["groupby"]) != 2:
|
2017-08-10 23:57:05 -04:00
|
|
|
raise Exception(_("Pick exactly 2 columns to 'Group By'"))
|
2019-06-25 16:34:48 -04:00
|
|
|
qry["metrics"] = [self.form_data["metric"]]
|
2016-03-18 02:44:58 -04:00
|
|
|
return qry
|
|
|
|
|
2017-02-16 20:28:35 -05:00
|
|
|
def get_data(self, df):
|
2019-06-25 16:34:48 -04:00
|
|
|
df.columns = ["source", "target", "value"]
|
|
|
|
return df.to_dict(orient="records")
|
2016-03-18 02:44:58 -04:00
|
|
|
|
|
|
|
|
2017-06-26 19:44:47 -04:00
|
|
|
class ChordViz(BaseViz):
|
|
|
|
|
|
|
|
"""A Chord diagram"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "chord"
|
|
|
|
verbose_name = _("Directed Force Layout")
|
2017-06-26 19:44:47 -04:00
|
|
|
credits = '<a href="https://github.com/d3/d3-chord">Bostock</a>'
|
|
|
|
is_timeseries = False
|
|
|
|
|
|
|
|
def query_obj(self):
|
2019-04-12 00:48:35 -04:00
|
|
|
qry = super().query_obj()
|
2017-06-26 19:44:47 -04:00
|
|
|
fd = self.form_data
|
2019-06-25 16:34:48 -04:00
|
|
|
qry["groupby"] = [fd.get("groupby"), fd.get("columns")]
|
|
|
|
qry["metrics"] = [utils.get_metric_name(fd.get("metric"))]
|
2017-06-26 19:44:47 -04:00
|
|
|
return qry
|
|
|
|
|
|
|
|
def get_data(self, df):
|
2019-06-25 16:34:48 -04:00
|
|
|
df.columns = ["source", "target", "value"]
|
2017-06-26 19:44:47 -04:00
|
|
|
|
|
|
|
# Preparing a symetrical matrix like d3.chords calls for
|
2019-06-25 16:34:48 -04:00
|
|
|
nodes = list(set(df["source"]) | set(df["target"]))
|
2017-06-26 19:44:47 -04:00
|
|
|
matrix = {}
|
|
|
|
for source, target in product(nodes, nodes):
|
|
|
|
matrix[(source, target)] = 0
|
|
|
|
for source, target, value in df.to_records(index=False):
|
|
|
|
matrix[(source, target)] = value
|
|
|
|
m = [[matrix[(n1, n2)] for n1 in nodes] for n2 in nodes]
|
2019-06-25 16:34:48 -04:00
|
|
|
return {"nodes": list(nodes), "matrix": m}
|
2017-06-26 19:44:47 -04:00
|
|
|
|
|
|
|
|
2017-05-23 13:43:33 -04:00
|
|
|
class CountryMapViz(BaseViz):
|
|
|
|
|
|
|
|
"""A country centric"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "country_map"
|
|
|
|
verbose_name = _("Country Map")
|
2017-05-23 13:43:33 -04:00
|
|
|
is_timeseries = False
|
2019-06-25 16:34:48 -04:00
|
|
|
credits = "From bl.ocks.org By john-guerra"
|
2017-05-23 13:43:33 -04:00
|
|
|
|
|
|
|
def query_obj(self):
|
2019-04-12 00:48:35 -04:00
|
|
|
qry = super().query_obj()
|
2019-06-25 16:34:48 -04:00
|
|
|
qry["metrics"] = [self.form_data["metric"]]
|
|
|
|
qry["groupby"] = [self.form_data["entity"]]
|
2017-05-23 13:43:33 -04:00
|
|
|
return qry
|
|
|
|
|
|
|
|
def get_data(self, df):
|
|
|
|
fd = self.form_data
|
2019-06-25 16:34:48 -04:00
|
|
|
cols = [fd.get("entity")]
|
2018-05-15 13:37:34 -04:00
|
|
|
metric = self.metric_labels[0]
|
2017-05-23 13:43:33 -04:00
|
|
|
cols += [metric]
|
|
|
|
ndf = df[cols]
|
|
|
|
df = ndf
|
2019-06-25 16:34:48 -04:00
|
|
|
df.columns = ["country_id", "metric"]
|
|
|
|
d = df.to_dict(orient="records")
|
2017-05-23 13:43:33 -04:00
|
|
|
return d
|
|
|
|
|
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
class WorldMapViz(BaseViz):
|
2016-03-16 23:25:41 -04:00
|
|
|
|
|
|
|
"""A country centric world map"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "world_map"
|
|
|
|
verbose_name = _("World Map")
|
2016-03-18 02:44:58 -04:00
|
|
|
is_timeseries = False
|
2016-04-09 16:17:20 -04:00
|
|
|
credits = 'datamaps on <a href="https://www.npmjs.com/package/datamaps">npm</a>'
|
2016-03-16 23:25:41 -04:00
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
def query_obj(self):
|
2019-04-12 00:48:35 -04:00
|
|
|
qry = super().query_obj()
|
2019-06-25 16:34:48 -04:00
|
|
|
qry["groupby"] = [self.form_data["entity"]]
|
2016-03-18 02:44:58 -04:00
|
|
|
return qry
|
|
|
|
|
2017-02-16 20:28:35 -05:00
|
|
|
def get_data(self, df):
|
2019-07-17 00:36:56 -04:00
|
|
|
from superset.examples import countries
|
2019-06-25 16:34:48 -04:00
|
|
|
|
2017-03-04 00:20:25 -05:00
|
|
|
fd = self.form_data
|
2019-06-25 16:34:48 -04:00
|
|
|
cols = [fd.get("entity")]
|
|
|
|
metric = utils.get_metric_name(fd.get("metric"))
|
|
|
|
secondary_metric = utils.get_metric_name(fd.get("secondary_metric"))
|
|
|
|
columns = ["country", "m1", "m2"]
|
2016-03-18 02:44:58 -04:00
|
|
|
if metric == secondary_metric:
|
|
|
|
ndf = df[cols]
|
2019-06-25 16:34:48 -04:00
|
|
|
ndf["m1"] = df[metric]
|
|
|
|
ndf["m2"] = ndf["m1"]
|
2016-03-18 02:44:58 -04:00
|
|
|
else:
|
2018-08-21 14:31:33 -04:00
|
|
|
if secondary_metric:
|
|
|
|
cols += [metric, secondary_metric]
|
|
|
|
else:
|
|
|
|
cols += [metric]
|
2019-06-25 16:34:48 -04:00
|
|
|
columns = ["country", "m1"]
|
2016-03-18 02:44:58 -04:00
|
|
|
ndf = df[cols]
|
|
|
|
df = ndf
|
2018-08-21 14:31:33 -04:00
|
|
|
df.columns = columns
|
2019-06-25 16:34:48 -04:00
|
|
|
d = df.to_dict(orient="records")
|
2016-03-18 02:44:58 -04:00
|
|
|
for row in d:
|
2016-07-10 22:38:12 -04:00
|
|
|
country = None
|
2019-06-25 16:34:48 -04:00
|
|
|
if isinstance(row["country"], str):
|
|
|
|
country = countries.get(fd.get("country_fieldtype"), row["country"])
|
2016-07-10 22:38:12 -04:00
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
if country:
|
2019-06-25 16:34:48 -04:00
|
|
|
row["country"] = country["cca3"]
|
|
|
|
row["latitude"] = country["lat"]
|
|
|
|
row["longitude"] = country["lng"]
|
|
|
|
row["name"] = country["name"]
|
2016-03-18 02:44:58 -04:00
|
|
|
else:
|
2019-06-25 16:34:48 -04:00
|
|
|
row["country"] = "XXX"
|
2016-03-16 23:25:41 -04:00
|
|
|
return d
|
2016-03-18 02:44:58 -04:00
|
|
|
|
|
|
|
|
|
|
|
class FilterBoxViz(BaseViz):
|
2016-03-16 23:25:41 -04:00
|
|
|
|
|
|
|
"""A multi filter, multi-choice filter box to make dashboards interactive"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "filter_box"
|
|
|
|
verbose_name = _("Filters")
|
2016-03-18 02:44:58 -04:00
|
|
|
is_timeseries = False
|
2016-11-10 02:08:22 -05:00
|
|
|
credits = 'a <a href="https://github.com/airbnb/superset">Superset</a> original'
|
2019-06-25 16:34:48 -04:00
|
|
|
cache_type = "get_data"
|
2019-01-11 18:28:23 -05:00
|
|
|
filter_row_limit = 1000
|
2016-03-16 23:25:41 -04:00
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
def query_obj(self):
|
2018-01-24 19:09:03 -05:00
|
|
|
return None
|
|
|
|
|
2018-02-07 17:49:19 -05:00
|
|
|
def run_extra_queries(self):
|
2019-04-12 00:48:35 -04:00
|
|
|
qry = super().query_obj()
|
2019-06-25 16:34:48 -04:00
|
|
|
filters = self.form_data.get("filter_configs") or []
|
|
|
|
qry["row_limit"] = self.filter_row_limit
|
2018-02-07 17:49:19 -05:00
|
|
|
self.dataframes = {}
|
|
|
|
for flt in filters:
|
2019-06-25 16:34:48 -04:00
|
|
|
col = flt.get("column")
|
2019-01-11 18:28:23 -05:00
|
|
|
if not col:
|
2019-06-25 16:34:48 -04:00
|
|
|
raise Exception(
|
|
|
|
_("Invalid filter configuration, please select a column")
|
|
|
|
)
|
|
|
|
qry["groupby"] = [col]
|
|
|
|
metric = flt.get("metric")
|
|
|
|
qry["metrics"] = [metric] if metric else []
|
|
|
|
df = self.get_df_payload(query_obj=qry).get("df")
|
2019-01-11 18:28:23 -05:00
|
|
|
self.dataframes[col] = df
|
2016-03-18 02:44:58 -04:00
|
|
|
|
2017-02-16 20:28:35 -05:00
|
|
|
def get_data(self, df):
|
2019-06-25 16:34:48 -04:00
|
|
|
filters = self.form_data.get("filter_configs") or []
|
2016-03-18 02:44:58 -04:00
|
|
|
d = {}
|
|
|
|
for flt in filters:
|
2019-06-25 16:34:48 -04:00
|
|
|
col = flt.get("column")
|
|
|
|
metric = flt.get("metric")
|
2019-01-11 18:28:23 -05:00
|
|
|
df = self.dataframes.get(col)
|
2019-12-04 14:34:56 -05:00
|
|
|
if df is not None:
|
|
|
|
if metric:
|
|
|
|
df = df.sort_values(
|
|
|
|
utils.get_metric_name(metric), ascending=flt.get("asc")
|
|
|
|
)
|
|
|
|
d[col] = [
|
|
|
|
{"id": row[0], "text": row[0], "metric": row[1]}
|
|
|
|
for row in df.itertuples(index=False)
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
df = df.sort_values(col, ascending=flt.get("asc"))
|
|
|
|
d[col] = [
|
|
|
|
{"id": row[0], "text": row[0]}
|
|
|
|
for row in df.itertuples(index=False)
|
|
|
|
]
|
2016-03-18 02:44:58 -04:00
|
|
|
return d
|
|
|
|
|
|
|
|
|
|
|
|
class IFrameViz(BaseViz):
|
2016-03-16 23:25:41 -04:00
|
|
|
|
|
|
|
"""You can squeeze just about anything in this iFrame component"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "iframe"
|
|
|
|
verbose_name = _("iFrame")
|
2016-11-10 02:08:22 -05:00
|
|
|
credits = 'a <a href="https://github.com/airbnb/superset">Superset</a> original'
|
2016-03-18 02:44:58 -04:00
|
|
|
is_timeseries = False
|
|
|
|
|
2018-01-17 16:54:10 -05:00
|
|
|
def query_obj(self):
|
|
|
|
return None
|
|
|
|
|
2019-09-10 12:55:17 -04:00
|
|
|
def get_df(self, query_obj: Dict[str, Any] = None) -> Optional[pd.DataFrame]:
|
2017-11-10 15:06:22 -05:00
|
|
|
return None
|
2017-04-13 14:37:27 -04:00
|
|
|
|
2018-10-25 15:03:33 -04:00
|
|
|
def get_data(self, df):
|
|
|
|
return {}
|
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
|
|
|
|
class ParallelCoordinatesViz(BaseViz):
|
2016-03-16 23:25:41 -04:00
|
|
|
|
|
|
|
"""Interactive parallel coordinate implementation
|
|
|
|
|
|
|
|
Uses this amazing javascript library
|
|
|
|
https://github.com/syntagmatic/parallel-coordinates
|
|
|
|
"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "para"
|
|
|
|
verbose_name = _("Parallel Coordinates")
|
2016-04-09 16:17:20 -04:00
|
|
|
credits = (
|
|
|
|
'<a href="https://syntagmatic.github.io/parallel-coordinates/">'
|
2019-06-25 16:34:48 -04:00
|
|
|
"Syntagmatic's library</a>"
|
|
|
|
)
|
2016-03-18 02:44:58 -04:00
|
|
|
is_timeseries = False
|
2016-03-16 23:25:41 -04:00
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
def query_obj(self):
|
2019-04-12 00:48:35 -04:00
|
|
|
d = super().query_obj()
|
2016-03-18 02:44:58 -04:00
|
|
|
fd = self.form_data
|
2019-06-25 16:34:48 -04:00
|
|
|
d["groupby"] = [fd.get("series")]
|
2016-03-18 02:44:58 -04:00
|
|
|
return d
|
|
|
|
|
2017-02-16 20:28:35 -05:00
|
|
|
def get_data(self, df):
|
2019-06-25 16:34:48 -04:00
|
|
|
return df.to_dict(orient="records")
|
2016-03-16 23:25:41 -04:00
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
|
|
|
|
class HeatmapViz(BaseViz):
|
2016-03-16 23:25:41 -04:00
|
|
|
|
|
|
|
"""A nice heatmap visualization that support high density through canvas"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "heatmap"
|
|
|
|
verbose_name = _("Heatmap")
|
2016-03-18 02:44:58 -04:00
|
|
|
is_timeseries = False
|
2016-04-09 16:17:20 -04:00
|
|
|
credits = (
|
|
|
|
'inspired from mbostock @<a href="http://bl.ocks.org/mbostock/3074470">'
|
2019-06-25 16:34:48 -04:00
|
|
|
"bl.ocks.org</a>"
|
|
|
|
)
|
2016-03-16 23:25:41 -04:00
|
|
|
|
2016-03-18 02:44:58 -04:00
|
|
|
def query_obj(self):
|
2019-04-12 00:48:35 -04:00
|
|
|
d = super().query_obj()
|
2016-03-18 02:44:58 -04:00
|
|
|
fd = self.form_data
|
2019-06-25 16:34:48 -04:00
|
|
|
d["metrics"] = [fd.get("metric")]
|
|
|
|
d["groupby"] = [fd.get("all_columns_x"), fd.get("all_columns_y")]
|
2016-03-18 02:44:58 -04:00
|
|
|
return d
|
|
|
|
|
2017-02-16 20:28:35 -05:00
|
|
|
def get_data(self, df):
|
2016-03-18 02:44:58 -04:00
|
|
|
fd = self.form_data
|
2019-06-25 16:34:48 -04:00
|
|
|
x = fd.get("all_columns_x")
|
|
|
|
y = fd.get("all_columns_y")
|
2018-05-15 13:37:34 -04:00
|
|
|
v = self.metric_labels[0]
|
2016-03-18 02:44:58 -04:00
|
|
|
if x == y:
|
2019-06-25 16:34:48 -04:00
|
|
|
df.columns = ["x", "y", "v"]
|
2016-03-18 02:44:58 -04:00
|
|
|
else:
|
|
|
|
df = df[[x, y, v]]
|
2019-06-25 16:34:48 -04:00
|
|
|
df.columns = ["x", "y", "v"]
|
|
|
|
norm = fd.get("normalize_across")
|
2016-03-18 02:44:58 -04:00
|
|
|
overall = False
|
2017-09-13 19:13:45 -04:00
|
|
|
max_ = df.v.max()
|
|
|
|
min_ = df.v.min()
|
2019-06-25 16:34:48 -04:00
|
|
|
if norm == "heatmap":
|
2016-03-18 02:44:58 -04:00
|
|
|
overall = True
|
|
|
|
else:
|
|
|
|
gb = df.groupby(norm, group_keys=False)
|
|
|
|
if len(gb) <= 1:
|
|
|
|
overall = True
|
|
|
|
else:
|
2019-06-25 16:34:48 -04:00
|
|
|
df["perc"] = gb.apply(
|
|
|
|
lambda x: (x.v - x.v.min()) / (x.v.max() - x.v.min())
|
2016-03-18 02:44:58 -04:00
|
|
|
)
|
2019-06-25 16:34:48 -04:00
|
|
|
df["rank"] = gb.apply(lambda x: x.v.rank(pct=True))
|
2016-03-18 02:44:58 -04:00
|
|
|
if overall:
|
2019-06-25 16:34:48 -04:00
|
|
|
df["perc"] = (df.v - min_) / (max_ - min_)
|
|
|
|
df["rank"] = df.v.rank(pct=True)
|
|
|
|
return {"records": df.to_dict(orient="records"), "extents": [min_, max_]}
|
2016-03-18 02:44:58 -04:00
|
|
|
|
|
|
|
|
2016-05-17 01:49:12 -04:00
|
|
|
class HorizonViz(NVD3TimeSeriesViz):
|
|
|
|
|
|
|
|
"""Horizon chart
|
|
|
|
|
|
|
|
https://www.npmjs.com/package/d3-horizon-chart
|
|
|
|
"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "horizon"
|
|
|
|
verbose_name = _("Horizon Charts")
|
2016-05-17 01:49:12 -04:00
|
|
|
credits = (
|
|
|
|
'<a href="https://www.npmjs.com/package/d3-horizon-chart">'
|
2019-06-25 16:34:48 -04:00
|
|
|
"d3-horizon-chart</a>"
|
|
|
|
)
|
2016-05-17 01:49:12 -04:00
|
|
|
|
|
|
|
|
Map visualization (#650)
* simple mapbox viz
use react-map-gl
superclustering of long/lat points
Added hook for map style, huge performance boost from bounding box fix, added count text on clusters
variable gradient size based on metric count
Ability to aggregate over any point property
This needed a change in the supercluster npm module, a PR was placed here:
https://github.com/mapbox/supercluster/pull/12
Aggregator function option in explore, tweaked visual defaults
better radius size management
clustering radius, point metric/unit options
scale cluster labels that don't fit, non-numeric labels for points
Minor fixes, label field affects points, text changes
serve mapbox apikey for slice
global opacity, viewport saves (hacky), bug in point labels
fixing mapbox-gl dependency
mapbox_api_key in config
expose row_limit, fix minor bugs
Add renderWhileDragging flag, groupby. Only show numerical columns for point radius
Implicitly group by lng/lat columns and error when label doesn't match groupby
'Fix' radius in miles problem, still some jankiness
derived fields cannot be typed as of now -> reverting numerical number change
better grouping error checking, expose count(*) for labelling
Custom colour for clusters/points + smart text colouring
Fixed bad positioning and overflow in explore view + small bugs + added thumbnail
* landscaping & eslint & use izip
* landscapin'
* address js code review
2016-06-24 17:16:51 -04:00
|
|
|
class MapboxViz(BaseViz):
|
|
|
|
|
|
|
|
"""Rich maps made with Mapbox"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "mapbox"
|
|
|
|
verbose_name = _("Mapbox")
|
Map visualization (#650)
* simple mapbox viz
use react-map-gl
superclustering of long/lat points
Added hook for map style, huge performance boost from bounding box fix, added count text on clusters
variable gradient size based on metric count
Ability to aggregate over any point property
This needed a change in the supercluster npm module, a PR was placed here:
https://github.com/mapbox/supercluster/pull/12
Aggregator function option in explore, tweaked visual defaults
better radius size management
clustering radius, point metric/unit options
scale cluster labels that don't fit, non-numeric labels for points
Minor fixes, label field affects points, text changes
serve mapbox apikey for slice
global opacity, viewport saves (hacky), bug in point labels
fixing mapbox-gl dependency
mapbox_api_key in config
expose row_limit, fix minor bugs
Add renderWhileDragging flag, groupby. Only show numerical columns for point radius
Implicitly group by lng/lat columns and error when label doesn't match groupby
'Fix' radius in miles problem, still some jankiness
derived fields cannot be typed as of now -> reverting numerical number change
better grouping error checking, expose count(*) for labelling
Custom colour for clusters/points + smart text colouring
Fixed bad positioning and overflow in explore view + small bugs + added thumbnail
* landscaping & eslint & use izip
* landscapin'
* address js code review
2016-06-24 17:16:51 -04:00
|
|
|
is_timeseries = False
|
2019-06-25 16:34:48 -04:00
|
|
|
credits = "<a href=https://www.mapbox.com/mapbox-gl-js/api/>Mapbox GL JS</a>"
|
Map visualization (#650)
* simple mapbox viz
use react-map-gl
superclustering of long/lat points
Added hook for map style, huge performance boost from bounding box fix, added count text on clusters
variable gradient size based on metric count
Ability to aggregate over any point property
This needed a change in the supercluster npm module, a PR was placed here:
https://github.com/mapbox/supercluster/pull/12
Aggregator function option in explore, tweaked visual defaults
better radius size management
clustering radius, point metric/unit options
scale cluster labels that don't fit, non-numeric labels for points
Minor fixes, label field affects points, text changes
serve mapbox apikey for slice
global opacity, viewport saves (hacky), bug in point labels
fixing mapbox-gl dependency
mapbox_api_key in config
expose row_limit, fix minor bugs
Add renderWhileDragging flag, groupby. Only show numerical columns for point radius
Implicitly group by lng/lat columns and error when label doesn't match groupby
'Fix' radius in miles problem, still some jankiness
derived fields cannot be typed as of now -> reverting numerical number change
better grouping error checking, expose count(*) for labelling
Custom colour for clusters/points + smart text colouring
Fixed bad positioning and overflow in explore view + small bugs + added thumbnail
* landscaping & eslint & use izip
* landscapin'
* address js code review
2016-06-24 17:16:51 -04:00
|
|
|
|
|
|
|
def query_obj(self):
|
2019-04-12 00:48:35 -04:00
|
|
|
d = super().query_obj()
|
Map visualization (#650)
* simple mapbox viz
use react-map-gl
superclustering of long/lat points
Added hook for map style, huge performance boost from bounding box fix, added count text on clusters
variable gradient size based on metric count
Ability to aggregate over any point property
This needed a change in the supercluster npm module, a PR was placed here:
https://github.com/mapbox/supercluster/pull/12
Aggregator function option in explore, tweaked visual defaults
better radius size management
clustering radius, point metric/unit options
scale cluster labels that don't fit, non-numeric labels for points
Minor fixes, label field affects points, text changes
serve mapbox apikey for slice
global opacity, viewport saves (hacky), bug in point labels
fixing mapbox-gl dependency
mapbox_api_key in config
expose row_limit, fix minor bugs
Add renderWhileDragging flag, groupby. Only show numerical columns for point radius
Implicitly group by lng/lat columns and error when label doesn't match groupby
'Fix' radius in miles problem, still some jankiness
derived fields cannot be typed as of now -> reverting numerical number change
better grouping error checking, expose count(*) for labelling
Custom colour for clusters/points + smart text colouring
Fixed bad positioning and overflow in explore view + small bugs + added thumbnail
* landscaping & eslint & use izip
* landscapin'
* address js code review
2016-06-24 17:16:51 -04:00
|
|
|
fd = self.form_data
|
2019-06-25 16:34:48 -04:00
|
|
|
label_col = fd.get("mapbox_label")
|
Map visualization (#650)
* simple mapbox viz
use react-map-gl
superclustering of long/lat points
Added hook for map style, huge performance boost from bounding box fix, added count text on clusters
variable gradient size based on metric count
Ability to aggregate over any point property
This needed a change in the supercluster npm module, a PR was placed here:
https://github.com/mapbox/supercluster/pull/12
Aggregator function option in explore, tweaked visual defaults
better radius size management
clustering radius, point metric/unit options
scale cluster labels that don't fit, non-numeric labels for points
Minor fixes, label field affects points, text changes
serve mapbox apikey for slice
global opacity, viewport saves (hacky), bug in point labels
fixing mapbox-gl dependency
mapbox_api_key in config
expose row_limit, fix minor bugs
Add renderWhileDragging flag, groupby. Only show numerical columns for point radius
Implicitly group by lng/lat columns and error when label doesn't match groupby
'Fix' radius in miles problem, still some jankiness
derived fields cannot be typed as of now -> reverting numerical number change
better grouping error checking, expose count(*) for labelling
Custom colour for clusters/points + smart text colouring
Fixed bad positioning and overflow in explore view + small bugs + added thumbnail
* landscaping & eslint & use izip
* landscapin'
* address js code review
2016-06-24 17:16:51 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
if not fd.get("groupby"):
|
|
|
|
if fd.get("all_columns_x") is None or fd.get("all_columns_y") is None:
|
|
|
|
raise Exception(_("[Longitude] and [Latitude] must be set"))
|
|
|
|
d["columns"] = [fd.get("all_columns_x"), fd.get("all_columns_y")]
|
Map visualization (#650)
* simple mapbox viz
use react-map-gl
superclustering of long/lat points
Added hook for map style, huge performance boost from bounding box fix, added count text on clusters
variable gradient size based on metric count
Ability to aggregate over any point property
This needed a change in the supercluster npm module, a PR was placed here:
https://github.com/mapbox/supercluster/pull/12
Aggregator function option in explore, tweaked visual defaults
better radius size management
clustering radius, point metric/unit options
scale cluster labels that don't fit, non-numeric labels for points
Minor fixes, label field affects points, text changes
serve mapbox apikey for slice
global opacity, viewport saves (hacky), bug in point labels
fixing mapbox-gl dependency
mapbox_api_key in config
expose row_limit, fix minor bugs
Add renderWhileDragging flag, groupby. Only show numerical columns for point radius
Implicitly group by lng/lat columns and error when label doesn't match groupby
'Fix' radius in miles problem, still some jankiness
derived fields cannot be typed as of now -> reverting numerical number change
better grouping error checking, expose count(*) for labelling
Custom colour for clusters/points + smart text colouring
Fixed bad positioning and overflow in explore view + small bugs + added thumbnail
* landscaping & eslint & use izip
* landscapin'
* address js code review
2016-06-24 17:16:51 -04:00
|
|
|
|
|
|
|
if label_col and len(label_col) >= 1:
|
2019-06-25 16:34:48 -04:00
|
|
|
if label_col[0] == "count":
|
|
|
|
raise Exception(
|
|
|
|
_(
|
|
|
|
"Must have a [Group By] column to have 'count' as the "
|
|
|
|
+ "[Label]"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
d["columns"].append(label_col[0])
|
Map visualization (#650)
* simple mapbox viz
use react-map-gl
superclustering of long/lat points
Added hook for map style, huge performance boost from bounding box fix, added count text on clusters
variable gradient size based on metric count
Ability to aggregate over any point property
This needed a change in the supercluster npm module, a PR was placed here:
https://github.com/mapbox/supercluster/pull/12
Aggregator function option in explore, tweaked visual defaults
better radius size management
clustering radius, point metric/unit options
scale cluster labels that don't fit, non-numeric labels for points
Minor fixes, label field affects points, text changes
serve mapbox apikey for slice
global opacity, viewport saves (hacky), bug in point labels
fixing mapbox-gl dependency
mapbox_api_key in config
expose row_limit, fix minor bugs
Add renderWhileDragging flag, groupby. Only show numerical columns for point radius
Implicitly group by lng/lat columns and error when label doesn't match groupby
'Fix' radius in miles problem, still some jankiness
derived fields cannot be typed as of now -> reverting numerical number change
better grouping error checking, expose count(*) for labelling
Custom colour for clusters/points + smart text colouring
Fixed bad positioning and overflow in explore view + small bugs + added thumbnail
* landscaping & eslint & use izip
* landscapin'
* address js code review
2016-06-24 17:16:51 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
if fd.get("point_radius") != "Auto":
|
|
|
|
d["columns"].append(fd.get("point_radius"))
|
Map visualization (#650)
* simple mapbox viz
use react-map-gl
superclustering of long/lat points
Added hook for map style, huge performance boost from bounding box fix, added count text on clusters
variable gradient size based on metric count
Ability to aggregate over any point property
This needed a change in the supercluster npm module, a PR was placed here:
https://github.com/mapbox/supercluster/pull/12
Aggregator function option in explore, tweaked visual defaults
better radius size management
clustering radius, point metric/unit options
scale cluster labels that don't fit, non-numeric labels for points
Minor fixes, label field affects points, text changes
serve mapbox apikey for slice
global opacity, viewport saves (hacky), bug in point labels
fixing mapbox-gl dependency
mapbox_api_key in config
expose row_limit, fix minor bugs
Add renderWhileDragging flag, groupby. Only show numerical columns for point radius
Implicitly group by lng/lat columns and error when label doesn't match groupby
'Fix' radius in miles problem, still some jankiness
derived fields cannot be typed as of now -> reverting numerical number change
better grouping error checking, expose count(*) for labelling
Custom colour for clusters/points + smart text colouring
Fixed bad positioning and overflow in explore view + small bugs + added thumbnail
* landscaping & eslint & use izip
* landscapin'
* address js code review
2016-06-24 17:16:51 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
d["columns"] = list(set(d["columns"]))
|
Map visualization (#650)
* simple mapbox viz
use react-map-gl
superclustering of long/lat points
Added hook for map style, huge performance boost from bounding box fix, added count text on clusters
variable gradient size based on metric count
Ability to aggregate over any point property
This needed a change in the supercluster npm module, a PR was placed here:
https://github.com/mapbox/supercluster/pull/12
Aggregator function option in explore, tweaked visual defaults
better radius size management
clustering radius, point metric/unit options
scale cluster labels that don't fit, non-numeric labels for points
Minor fixes, label field affects points, text changes
serve mapbox apikey for slice
global opacity, viewport saves (hacky), bug in point labels
fixing mapbox-gl dependency
mapbox_api_key in config
expose row_limit, fix minor bugs
Add renderWhileDragging flag, groupby. Only show numerical columns for point radius
Implicitly group by lng/lat columns and error when label doesn't match groupby
'Fix' radius in miles problem, still some jankiness
derived fields cannot be typed as of now -> reverting numerical number change
better grouping error checking, expose count(*) for labelling
Custom colour for clusters/points + smart text colouring
Fixed bad positioning and overflow in explore view + small bugs + added thumbnail
* landscaping & eslint & use izip
* landscapin'
* address js code review
2016-06-24 17:16:51 -04:00
|
|
|
else:
|
|
|
|
# Ensuring columns chosen are all in group by
|
2019-06-25 16:34:48 -04:00
|
|
|
if (
|
|
|
|
label_col
|
|
|
|
and len(label_col) >= 1
|
|
|
|
and label_col[0] != "count"
|
|
|
|
and label_col[0] not in fd.get("groupby")
|
|
|
|
):
|
|
|
|
raise Exception(_("Choice of [Label] must be present in [Group By]"))
|
|
|
|
|
|
|
|
if fd.get("point_radius") != "Auto" and fd.get(
|
|
|
|
"point_radius"
|
|
|
|
) not in fd.get("groupby"):
|
|
|
|
raise Exception(
|
|
|
|
_("Choice of [Point Radius] must be present in [Group By]")
|
|
|
|
)
|
|
|
|
|
|
|
|
if fd.get("all_columns_x") not in fd.get("groupby") or fd.get(
|
|
|
|
"all_columns_y"
|
|
|
|
) not in fd.get("groupby"):
|
|
|
|
raise Exception(
|
|
|
|
_(
|
|
|
|
"[Longitude] and [Latitude] columns must be present in "
|
|
|
|
+ "[Group By]"
|
|
|
|
)
|
|
|
|
)
|
Map visualization (#650)
* simple mapbox viz
use react-map-gl
superclustering of long/lat points
Added hook for map style, huge performance boost from bounding box fix, added count text on clusters
variable gradient size based on metric count
Ability to aggregate over any point property
This needed a change in the supercluster npm module, a PR was placed here:
https://github.com/mapbox/supercluster/pull/12
Aggregator function option in explore, tweaked visual defaults
better radius size management
clustering radius, point metric/unit options
scale cluster labels that don't fit, non-numeric labels for points
Minor fixes, label field affects points, text changes
serve mapbox apikey for slice
global opacity, viewport saves (hacky), bug in point labels
fixing mapbox-gl dependency
mapbox_api_key in config
expose row_limit, fix minor bugs
Add renderWhileDragging flag, groupby. Only show numerical columns for point radius
Implicitly group by lng/lat columns and error when label doesn't match groupby
'Fix' radius in miles problem, still some jankiness
derived fields cannot be typed as of now -> reverting numerical number change
better grouping error checking, expose count(*) for labelling
Custom colour for clusters/points + smart text colouring
Fixed bad positioning and overflow in explore view + small bugs + added thumbnail
* landscaping & eslint & use izip
* landscapin'
* address js code review
2016-06-24 17:16:51 -04:00
|
|
|
return d
|
|
|
|
|
2017-02-16 20:28:35 -05:00
|
|
|
def get_data(self, df):
|
2018-02-20 17:41:35 -05:00
|
|
|
if df is None:
|
|
|
|
return None
|
Map visualization (#650)
* simple mapbox viz
use react-map-gl
superclustering of long/lat points
Added hook for map style, huge performance boost from bounding box fix, added count text on clusters
variable gradient size based on metric count
Ability to aggregate over any point property
This needed a change in the supercluster npm module, a PR was placed here:
https://github.com/mapbox/supercluster/pull/12
Aggregator function option in explore, tweaked visual defaults
better radius size management
clustering radius, point metric/unit options
scale cluster labels that don't fit, non-numeric labels for points
Minor fixes, label field affects points, text changes
serve mapbox apikey for slice
global opacity, viewport saves (hacky), bug in point labels
fixing mapbox-gl dependency
mapbox_api_key in config
expose row_limit, fix minor bugs
Add renderWhileDragging flag, groupby. Only show numerical columns for point radius
Implicitly group by lng/lat columns and error when label doesn't match groupby
'Fix' radius in miles problem, still some jankiness
derived fields cannot be typed as of now -> reverting numerical number change
better grouping error checking, expose count(*) for labelling
Custom colour for clusters/points + smart text colouring
Fixed bad positioning and overflow in explore view + small bugs + added thumbnail
* landscaping & eslint & use izip
* landscapin'
* address js code review
2016-06-24 17:16:51 -04:00
|
|
|
fd = self.form_data
|
2019-06-25 16:34:48 -04:00
|
|
|
label_col = fd.get("mapbox_label")
|
2018-09-18 15:38:17 -04:00
|
|
|
has_custom_metric = label_col is not None and len(label_col) > 0
|
Map visualization (#650)
* simple mapbox viz
use react-map-gl
superclustering of long/lat points
Added hook for map style, huge performance boost from bounding box fix, added count text on clusters
variable gradient size based on metric count
Ability to aggregate over any point property
This needed a change in the supercluster npm module, a PR was placed here:
https://github.com/mapbox/supercluster/pull/12
Aggregator function option in explore, tweaked visual defaults
better radius size management
clustering radius, point metric/unit options
scale cluster labels that don't fit, non-numeric labels for points
Minor fixes, label field affects points, text changes
serve mapbox apikey for slice
global opacity, viewport saves (hacky), bug in point labels
fixing mapbox-gl dependency
mapbox_api_key in config
expose row_limit, fix minor bugs
Add renderWhileDragging flag, groupby. Only show numerical columns for point radius
Implicitly group by lng/lat columns and error when label doesn't match groupby
'Fix' radius in miles problem, still some jankiness
derived fields cannot be typed as of now -> reverting numerical number change
better grouping error checking, expose count(*) for labelling
Custom colour for clusters/points + smart text colouring
Fixed bad positioning and overflow in explore view + small bugs + added thumbnail
* landscaping & eslint & use izip
* landscapin'
* address js code review
2016-06-24 17:16:51 -04:00
|
|
|
metric_col = [None] * len(df.index)
|
2018-09-18 15:38:17 -04:00
|
|
|
if has_custom_metric:
|
2019-06-25 16:34:48 -04:00
|
|
|
if label_col[0] == fd.get("all_columns_x"):
|
|
|
|
metric_col = df[fd.get("all_columns_x")]
|
|
|
|
elif label_col[0] == fd.get("all_columns_y"):
|
|
|
|
metric_col = df[fd.get("all_columns_y")]
|
Map visualization (#650)
* simple mapbox viz
use react-map-gl
superclustering of long/lat points
Added hook for map style, huge performance boost from bounding box fix, added count text on clusters
variable gradient size based on metric count
Ability to aggregate over any point property
This needed a change in the supercluster npm module, a PR was placed here:
https://github.com/mapbox/supercluster/pull/12
Aggregator function option in explore, tweaked visual defaults
better radius size management
clustering radius, point metric/unit options
scale cluster labels that don't fit, non-numeric labels for points
Minor fixes, label field affects points, text changes
serve mapbox apikey for slice
global opacity, viewport saves (hacky), bug in point labels
fixing mapbox-gl dependency
mapbox_api_key in config
expose row_limit, fix minor bugs
Add renderWhileDragging flag, groupby. Only show numerical columns for point radius
Implicitly group by lng/lat columns and error when label doesn't match groupby
'Fix' radius in miles problem, still some jankiness
derived fields cannot be typed as of now -> reverting numerical number change
better grouping error checking, expose count(*) for labelling
Custom colour for clusters/points + smart text colouring
Fixed bad positioning and overflow in explore view + small bugs + added thumbnail
* landscaping & eslint & use izip
* landscapin'
* address js code review
2016-06-24 17:16:51 -04:00
|
|
|
else:
|
|
|
|
metric_col = df[label_col[0]]
|
|
|
|
point_radius_col = (
|
|
|
|
[None] * len(df.index)
|
2019-06-25 16:34:48 -04:00
|
|
|
if fd.get("point_radius") == "Auto"
|
|
|
|
else df[fd.get("point_radius")]
|
|
|
|
)
|
Map visualization (#650)
* simple mapbox viz
use react-map-gl
superclustering of long/lat points
Added hook for map style, huge performance boost from bounding box fix, added count text on clusters
variable gradient size based on metric count
Ability to aggregate over any point property
This needed a change in the supercluster npm module, a PR was placed here:
https://github.com/mapbox/supercluster/pull/12
Aggregator function option in explore, tweaked visual defaults
better radius size management
clustering radius, point metric/unit options
scale cluster labels that don't fit, non-numeric labels for points
Minor fixes, label field affects points, text changes
serve mapbox apikey for slice
global opacity, viewport saves (hacky), bug in point labels
fixing mapbox-gl dependency
mapbox_api_key in config
expose row_limit, fix minor bugs
Add renderWhileDragging flag, groupby. Only show numerical columns for point radius
Implicitly group by lng/lat columns and error when label doesn't match groupby
'Fix' radius in miles problem, still some jankiness
derived fields cannot be typed as of now -> reverting numerical number change
better grouping error checking, expose count(*) for labelling
Custom colour for clusters/points + smart text colouring
Fixed bad positioning and overflow in explore view + small bugs + added thumbnail
* landscaping & eslint & use izip
* landscapin'
* address js code review
2016-06-24 17:16:51 -04:00
|
|
|
|
2019-02-26 18:16:13 -05:00
|
|
|
# limiting geo precision as long decimal values trigger issues
|
|
|
|
# around json-bignumber in Mapbox
|
|
|
|
GEO_PRECISION = 10
|
Map visualization (#650)
* simple mapbox viz
use react-map-gl
superclustering of long/lat points
Added hook for map style, huge performance boost from bounding box fix, added count text on clusters
variable gradient size based on metric count
Ability to aggregate over any point property
This needed a change in the supercluster npm module, a PR was placed here:
https://github.com/mapbox/supercluster/pull/12
Aggregator function option in explore, tweaked visual defaults
better radius size management
clustering radius, point metric/unit options
scale cluster labels that don't fit, non-numeric labels for points
Minor fixes, label field affects points, text changes
serve mapbox apikey for slice
global opacity, viewport saves (hacky), bug in point labels
fixing mapbox-gl dependency
mapbox_api_key in config
expose row_limit, fix minor bugs
Add renderWhileDragging flag, groupby. Only show numerical columns for point radius
Implicitly group by lng/lat columns and error when label doesn't match groupby
'Fix' radius in miles problem, still some jankiness
derived fields cannot be typed as of now -> reverting numerical number change
better grouping error checking, expose count(*) for labelling
Custom colour for clusters/points + smart text colouring
Fixed bad positioning and overflow in explore view + small bugs + added thumbnail
* landscaping & eslint & use izip
* landscapin'
* address js code review
2016-06-24 17:16:51 -04:00
|
|
|
# using geoJSON formatting
|
|
|
|
geo_json = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"type": "FeatureCollection",
|
|
|
|
"features": [
|
Map visualization (#650)
* simple mapbox viz
use react-map-gl
superclustering of long/lat points
Added hook for map style, huge performance boost from bounding box fix, added count text on clusters
variable gradient size based on metric count
Ability to aggregate over any point property
This needed a change in the supercluster npm module, a PR was placed here:
https://github.com/mapbox/supercluster/pull/12
Aggregator function option in explore, tweaked visual defaults
better radius size management
clustering radius, point metric/unit options
scale cluster labels that don't fit, non-numeric labels for points
Minor fixes, label field affects points, text changes
serve mapbox apikey for slice
global opacity, viewport saves (hacky), bug in point labels
fixing mapbox-gl dependency
mapbox_api_key in config
expose row_limit, fix minor bugs
Add renderWhileDragging flag, groupby. Only show numerical columns for point radius
Implicitly group by lng/lat columns and error when label doesn't match groupby
'Fix' radius in miles problem, still some jankiness
derived fields cannot be typed as of now -> reverting numerical number change
better grouping error checking, expose count(*) for labelling
Custom colour for clusters/points + smart text colouring
Fixed bad positioning and overflow in explore view + small bugs + added thumbnail
* landscaping & eslint & use izip
* landscapin'
* address js code review
2016-06-24 17:16:51 -04:00
|
|
|
{
|
2019-06-25 16:34:48 -04:00
|
|
|
"type": "Feature",
|
|
|
|
"properties": {"metric": metric, "radius": point_radius},
|
|
|
|
"geometry": {
|
|
|
|
"type": "Point",
|
|
|
|
"coordinates": [
|
2019-02-26 18:16:13 -05:00
|
|
|
round(lon, GEO_PRECISION),
|
|
|
|
round(lat, GEO_PRECISION),
|
|
|
|
],
|
2017-11-08 00:32:45 -05:00
|
|
|
},
|
Map visualization (#650)
* simple mapbox viz
use react-map-gl
superclustering of long/lat points
Added hook for map style, huge performance boost from bounding box fix, added count text on clusters
variable gradient size based on metric count
Ability to aggregate over any point property
This needed a change in the supercluster npm module, a PR was placed here:
https://github.com/mapbox/supercluster/pull/12
Aggregator function option in explore, tweaked visual defaults
better radius size management
clustering radius, point metric/unit options
scale cluster labels that don't fit, non-numeric labels for points
Minor fixes, label field affects points, text changes
serve mapbox apikey for slice
global opacity, viewport saves (hacky), bug in point labels
fixing mapbox-gl dependency
mapbox_api_key in config
expose row_limit, fix minor bugs
Add renderWhileDragging flag, groupby. Only show numerical columns for point radius
Implicitly group by lng/lat columns and error when label doesn't match groupby
'Fix' radius in miles problem, still some jankiness
derived fields cannot be typed as of now -> reverting numerical number change
better grouping error checking, expose count(*) for labelling
Custom colour for clusters/points + smart text colouring
Fixed bad positioning and overflow in explore view + small bugs + added thumbnail
* landscaping & eslint & use izip
* landscapin'
* address js code review
2016-06-24 17:16:51 -04:00
|
|
|
}
|
2019-06-25 16:34:48 -04:00
|
|
|
for lon, lat, metric, point_radius in zip(
|
|
|
|
df[fd.get("all_columns_x")],
|
|
|
|
df[fd.get("all_columns_y")],
|
|
|
|
metric_col,
|
|
|
|
point_radius_col,
|
|
|
|
)
|
2017-11-08 00:32:45 -05:00
|
|
|
],
|
Map visualization (#650)
* simple mapbox viz
use react-map-gl
superclustering of long/lat points
Added hook for map style, huge performance boost from bounding box fix, added count text on clusters
variable gradient size based on metric count
Ability to aggregate over any point property
This needed a change in the supercluster npm module, a PR was placed here:
https://github.com/mapbox/supercluster/pull/12
Aggregator function option in explore, tweaked visual defaults
better radius size management
clustering radius, point metric/unit options
scale cluster labels that don't fit, non-numeric labels for points
Minor fixes, label field affects points, text changes
serve mapbox apikey for slice
global opacity, viewport saves (hacky), bug in point labels
fixing mapbox-gl dependency
mapbox_api_key in config
expose row_limit, fix minor bugs
Add renderWhileDragging flag, groupby. Only show numerical columns for point radius
Implicitly group by lng/lat columns and error when label doesn't match groupby
'Fix' radius in miles problem, still some jankiness
derived fields cannot be typed as of now -> reverting numerical number change
better grouping error checking, expose count(*) for labelling
Custom colour for clusters/points + smart text colouring
Fixed bad positioning and overflow in explore view + small bugs + added thumbnail
* landscaping & eslint & use izip
* landscapin'
* address js code review
2016-06-24 17:16:51 -04:00
|
|
|
}
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
x_series, y_series = df[fd.get("all_columns_x")], df[fd.get("all_columns_y")]
|
2018-09-11 16:33:54 -04:00
|
|
|
south_west = [x_series.min(), y_series.min()]
|
|
|
|
north_east = [x_series.max(), y_series.max()]
|
|
|
|
|
Map visualization (#650)
* simple mapbox viz
use react-map-gl
superclustering of long/lat points
Added hook for map style, huge performance boost from bounding box fix, added count text on clusters
variable gradient size based on metric count
Ability to aggregate over any point property
This needed a change in the supercluster npm module, a PR was placed here:
https://github.com/mapbox/supercluster/pull/12
Aggregator function option in explore, tweaked visual defaults
better radius size management
clustering radius, point metric/unit options
scale cluster labels that don't fit, non-numeric labels for points
Minor fixes, label field affects points, text changes
serve mapbox apikey for slice
global opacity, viewport saves (hacky), bug in point labels
fixing mapbox-gl dependency
mapbox_api_key in config
expose row_limit, fix minor bugs
Add renderWhileDragging flag, groupby. Only show numerical columns for point radius
Implicitly group by lng/lat columns and error when label doesn't match groupby
'Fix' radius in miles problem, still some jankiness
derived fields cannot be typed as of now -> reverting numerical number change
better grouping error checking, expose count(*) for labelling
Custom colour for clusters/points + smart text colouring
Fixed bad positioning and overflow in explore view + small bugs + added thumbnail
* landscaping & eslint & use izip
* landscapin'
* address js code review
2016-06-24 17:16:51 -04:00
|
|
|
return {
|
2019-06-25 16:34:48 -04:00
|
|
|
"geoJSON": geo_json,
|
|
|
|
"hasCustomMetric": has_custom_metric,
|
2019-10-30 19:19:16 -04:00
|
|
|
"mapboxApiKey": config["MAPBOX_API_KEY"],
|
2019-06-25 16:34:48 -04:00
|
|
|
"mapStyle": fd.get("mapbox_style"),
|
|
|
|
"aggregatorName": fd.get("pandas_aggfunc"),
|
|
|
|
"clusteringRadius": fd.get("clustering_radius"),
|
|
|
|
"pointRadiusUnit": fd.get("point_radius_unit"),
|
|
|
|
"globalOpacity": fd.get("global_opacity"),
|
|
|
|
"bounds": [south_west, north_east],
|
|
|
|
"renderWhileDragging": fd.get("render_while_dragging"),
|
|
|
|
"tooltip": fd.get("rich_tooltip"),
|
|
|
|
"color": fd.get("mapbox_color"),
|
Map visualization (#650)
* simple mapbox viz
use react-map-gl
superclustering of long/lat points
Added hook for map style, huge performance boost from bounding box fix, added count text on clusters
variable gradient size based on metric count
Ability to aggregate over any point property
This needed a change in the supercluster npm module, a PR was placed here:
https://github.com/mapbox/supercluster/pull/12
Aggregator function option in explore, tweaked visual defaults
better radius size management
clustering radius, point metric/unit options
scale cluster labels that don't fit, non-numeric labels for points
Minor fixes, label field affects points, text changes
serve mapbox apikey for slice
global opacity, viewport saves (hacky), bug in point labels
fixing mapbox-gl dependency
mapbox_api_key in config
expose row_limit, fix minor bugs
Add renderWhileDragging flag, groupby. Only show numerical columns for point radius
Implicitly group by lng/lat columns and error when label doesn't match groupby
'Fix' radius in miles problem, still some jankiness
derived fields cannot be typed as of now -> reverting numerical number change
better grouping error checking, expose count(*) for labelling
Custom colour for clusters/points + smart text colouring
Fixed bad positioning and overflow in explore view + small bugs + added thumbnail
* landscaping & eslint & use izip
* landscapin'
* address js code review
2016-06-24 17:16:51 -04:00
|
|
|
}
|
|
|
|
|
2017-10-04 13:17:33 -04:00
|
|
|
|
2017-12-26 13:47:29 -05:00
|
|
|
class DeckGLMultiLayer(BaseViz):
|
|
|
|
|
|
|
|
"""Pile on multiple DeckGL layers"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "deck_multi"
|
|
|
|
verbose_name = _("Deck.gl - Multiple Layers")
|
2017-12-26 13:47:29 -05:00
|
|
|
|
|
|
|
is_timeseries = False
|
|
|
|
credits = '<a href="https://uber.github.io/deck.gl/">deck.gl</a>'
|
|
|
|
|
|
|
|
def query_obj(self):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_data(self, df):
|
|
|
|
fd = self.form_data
|
|
|
|
# Late imports to avoid circular import issues
|
2019-12-18 14:40:45 -05:00
|
|
|
from superset.models.slice import Slice
|
2017-12-26 13:47:29 -05:00
|
|
|
from superset import db
|
2019-06-25 16:34:48 -04:00
|
|
|
|
|
|
|
slice_ids = fd.get("deck_slices")
|
2017-12-26 13:47:29 -05:00
|
|
|
slices = db.session.query(Slice).filter(Slice.id.in_(slice_ids)).all()
|
|
|
|
return {
|
2019-10-30 19:19:16 -04:00
|
|
|
"mapboxApiKey": config["MAPBOX_API_KEY"],
|
2019-06-25 16:34:48 -04:00
|
|
|
"slices": [slc.data for slc in slices],
|
2017-12-26 13:47:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-16 03:30:02 -05:00
|
|
|
class BaseDeckGLViz(BaseViz):
|
|
|
|
|
|
|
|
"""Base class for deck.gl visualizations"""
|
|
|
|
|
|
|
|
is_timeseries = False
|
|
|
|
credits = '<a href="https://uber.github.io/deck.gl/">deck.gl</a>'
|
2019-09-10 12:55:17 -04:00
|
|
|
spatial_control_keys: List[str] = []
|
2017-11-16 03:30:02 -05:00
|
|
|
|
|
|
|
def get_metrics(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
self.metric = self.form_data.get("size")
|
2017-12-15 14:47:27 -05:00
|
|
|
return [self.metric] if self.metric else []
|
2017-11-16 03:30:02 -05:00
|
|
|
|
2018-01-12 14:06:11 -05:00
|
|
|
def process_spatial_query_obj(self, key, group_by):
|
2018-08-19 19:04:01 -04:00
|
|
|
group_by.extend(self.get_spatial_columns(key))
|
|
|
|
|
|
|
|
def get_spatial_columns(self, key):
|
2018-01-12 14:06:11 -05:00
|
|
|
spatial = self.form_data.get(key)
|
|
|
|
if spatial is None:
|
2019-06-25 16:34:48 -04:00
|
|
|
raise ValueError(_("Bad spatial key"))
|
2018-01-12 14:06:11 -05:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
if spatial.get("type") == "latlong":
|
|
|
|
return [spatial.get("lonCol"), spatial.get("latCol")]
|
|
|
|
elif spatial.get("type") == "delimited":
|
|
|
|
return [spatial.get("lonlatCol")]
|
|
|
|
elif spatial.get("type") == "geohash":
|
|
|
|
return [spatial.get("geohashCol")]
|
2018-01-12 14:06:11 -05:00
|
|
|
|
2018-07-16 16:42:07 -04:00
|
|
|
@staticmethod
|
|
|
|
def parse_coordinates(s):
|
|
|
|
if not s:
|
|
|
|
return None
|
|
|
|
try:
|
|
|
|
p = Point(s)
|
2019-01-16 23:40:16 -05:00
|
|
|
return (p.latitude, p.longitude) # pylint: disable=no-member
|
2018-07-16 16:42:07 -04:00
|
|
|
except Exception:
|
2019-06-25 16:34:48 -04:00
|
|
|
raise SpatialException(_("Invalid spatial point encountered: %s" % s))
|
2018-07-16 16:42:07 -04:00
|
|
|
|
2018-08-22 01:58:47 -04:00
|
|
|
@staticmethod
|
|
|
|
def reverse_geohash_decode(geohash_code):
|
|
|
|
lat, lng = geohash.decode(geohash_code)
|
|
|
|
return (lng, lat)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def reverse_latlong(df, key):
|
2019-06-25 16:34:48 -04:00
|
|
|
df[key] = [tuple(reversed(o)) for o in df[key] if isinstance(o, (list, tuple))]
|
2018-08-22 01:58:47 -04:00
|
|
|
|
2018-01-12 14:06:11 -05:00
|
|
|
def process_spatial_data_obj(self, key, df):
|
|
|
|
spatial = self.form_data.get(key)
|
|
|
|
if spatial is None:
|
2019-06-25 16:34:48 -04:00
|
|
|
raise ValueError(_("Bad spatial key"))
|
|
|
|
|
|
|
|
if spatial.get("type") == "latlong":
|
|
|
|
df[key] = list(
|
|
|
|
zip(
|
|
|
|
pd.to_numeric(df[spatial.get("lonCol")], errors="coerce"),
|
|
|
|
pd.to_numeric(df[spatial.get("latCol")], errors="coerce"),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
elif spatial.get("type") == "delimited":
|
|
|
|
lon_lat_col = spatial.get("lonlatCol")
|
2018-07-16 16:42:07 -04:00
|
|
|
df[key] = df[lon_lat_col].apply(self.parse_coordinates)
|
|
|
|
del df[lon_lat_col]
|
2019-06-25 16:34:48 -04:00
|
|
|
elif spatial.get("type") == "geohash":
|
|
|
|
df[key] = df[spatial.get("geohashCol")].map(self.reverse_geohash_decode)
|
|
|
|
del df[spatial.get("geohashCol")]
|
2018-06-30 19:07:23 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
if spatial.get("reverseCheckbox"):
|
2018-08-22 01:58:47 -04:00
|
|
|
self.reverse_latlong(df, key)
|
|
|
|
|
2018-06-30 19:07:23 -04:00
|
|
|
if df.get(key) is None:
|
2019-06-25 16:34:48 -04:00
|
|
|
raise NullValueException(
|
|
|
|
_(
|
|
|
|
"Encountered invalid NULL spatial entry, \
|
|
|
|
please consider filtering those out"
|
|
|
|
)
|
|
|
|
)
|
2018-01-12 14:06:11 -05:00
|
|
|
return df
|
2017-11-16 03:30:02 -05:00
|
|
|
|
2018-08-19 19:04:01 -04:00
|
|
|
def add_null_filters(self):
|
2018-09-21 01:02:28 -04:00
|
|
|
fd = self.form_data
|
2018-08-19 19:04:01 -04:00
|
|
|
spatial_columns = set()
|
|
|
|
for key in self.spatial_control_keys:
|
|
|
|
for column in self.get_spatial_columns(key):
|
|
|
|
spatial_columns.add(column)
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
if fd.get("adhoc_filters") is None:
|
|
|
|
fd["adhoc_filters"] = []
|
2018-09-21 01:02:28 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
line_column = fd.get("line_column")
|
2018-09-21 01:02:28 -04:00
|
|
|
if line_column:
|
|
|
|
spatial_columns.add(line_column)
|
2018-08-19 19:04:01 -04:00
|
|
|
|
|
|
|
for column in sorted(spatial_columns):
|
2019-06-25 16:34:48 -04:00
|
|
|
filter_ = to_adhoc({"col": column, "op": "IS NOT NULL", "val": ""})
|
|
|
|
fd["adhoc_filters"].append(filter_)
|
2018-08-19 19:04:01 -04:00
|
|
|
|
2017-11-16 03:30:02 -05:00
|
|
|
def query_obj(self):
|
|
|
|
fd = self.form_data
|
2018-08-19 19:04:01 -04:00
|
|
|
|
|
|
|
# add NULL filters
|
2019-06-25 16:34:48 -04:00
|
|
|
if fd.get("filter_nulls", True):
|
2018-08-19 19:04:01 -04:00
|
|
|
self.add_null_filters()
|
|
|
|
|
2019-04-12 00:48:35 -04:00
|
|
|
d = super().query_obj()
|
2017-12-15 14:47:27 -05:00
|
|
|
gb = []
|
|
|
|
|
2018-01-12 14:06:11 -05:00
|
|
|
for key in self.spatial_control_keys:
|
|
|
|
self.process_spatial_query_obj(key, gb)
|
2017-12-15 14:47:27 -05:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
if fd.get("dimension"):
|
|
|
|
gb += [fd.get("dimension")]
|
2017-11-16 03:30:02 -05:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
if fd.get("js_columns"):
|
|
|
|
gb += fd.get("js_columns")
|
2017-12-15 14:47:27 -05:00
|
|
|
metrics = self.get_metrics()
|
2018-04-11 18:15:36 -04:00
|
|
|
gb = list(set(gb))
|
2017-12-15 14:47:27 -05:00
|
|
|
if metrics:
|
2019-06-25 16:34:48 -04:00
|
|
|
d["groupby"] = gb
|
|
|
|
d["metrics"] = metrics
|
|
|
|
d["columns"] = []
|
2017-12-15 14:47:27 -05:00
|
|
|
else:
|
2019-06-25 16:34:48 -04:00
|
|
|
d["columns"] = gb
|
2017-11-16 03:30:02 -05:00
|
|
|
return d
|
|
|
|
|
2018-01-11 18:42:44 -05:00
|
|
|
def get_js_columns(self, d):
|
2019-06-25 16:34:48 -04:00
|
|
|
cols = self.form_data.get("js_columns") or []
|
2018-01-11 18:42:44 -05:00
|
|
|
return {col: d.get(col) for col in cols}
|
|
|
|
|
2017-11-16 03:30:02 -05:00
|
|
|
def get_data(self, df):
|
2018-02-20 17:41:35 -05:00
|
|
|
if df is None:
|
|
|
|
return None
|
2018-07-16 16:42:07 -04:00
|
|
|
|
|
|
|
# Processing spatial info
|
2018-01-12 14:06:11 -05:00
|
|
|
for key in self.spatial_control_keys:
|
|
|
|
df = self.process_spatial_data_obj(key, df)
|
2017-12-15 14:47:27 -05:00
|
|
|
|
2017-11-16 03:30:02 -05:00
|
|
|
features = []
|
2019-06-25 16:34:48 -04:00
|
|
|
for d in df.to_dict(orient="records"):
|
2018-01-17 17:01:32 -05:00
|
|
|
feature = self.get_properties(d)
|
|
|
|
extra_props = self.get_js_columns(d)
|
|
|
|
if extra_props:
|
2019-06-25 16:34:48 -04:00
|
|
|
feature["extraProps"] = extra_props
|
2018-01-11 18:42:44 -05:00
|
|
|
features.append(feature)
|
2018-01-18 16:28:46 -05:00
|
|
|
|
2017-11-16 03:30:02 -05:00
|
|
|
return {
|
2019-06-25 16:34:48 -04:00
|
|
|
"features": features,
|
2019-10-30 19:19:16 -04:00
|
|
|
"mapboxApiKey": config["MAPBOX_API_KEY"],
|
2019-06-25 16:34:48 -04:00
|
|
|
"metricLabels": self.metric_labels,
|
2017-11-16 03:30:02 -05:00
|
|
|
}
|
|
|
|
|
2018-01-17 17:01:32 -05:00
|
|
|
def get_properties(self, d):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2017-11-16 03:30:02 -05:00
|
|
|
|
|
|
|
class DeckScatterViz(BaseDeckGLViz):
|
|
|
|
|
|
|
|
"""deck.gl's ScatterLayer"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "deck_scatter"
|
|
|
|
verbose_name = _("Deck.gl - Scatter plot")
|
|
|
|
spatial_control_keys = ["spatial"]
|
2018-02-15 20:55:11 -05:00
|
|
|
is_timeseries = True
|
2017-11-16 03:30:02 -05:00
|
|
|
|
|
|
|
def query_obj(self):
|
2017-12-15 14:47:27 -05:00
|
|
|
fd = self.form_data
|
2019-06-25 16:34:48 -04:00
|
|
|
self.is_timeseries = bool(fd.get("time_grain_sqla") or fd.get("granularity"))
|
|
|
|
self.point_radius_fixed = fd.get("point_radius_fixed") or {
|
|
|
|
"type": "fix",
|
|
|
|
"value": 500,
|
|
|
|
}
|
2019-04-12 00:48:35 -04:00
|
|
|
return super().query_obj()
|
2017-11-16 03:30:02 -05:00
|
|
|
|
|
|
|
def get_metrics(self):
|
2017-12-19 15:38:03 -05:00
|
|
|
self.metric = None
|
2019-06-25 16:34:48 -04:00
|
|
|
if self.point_radius_fixed.get("type") == "metric":
|
|
|
|
self.metric = self.point_radius_fixed.get("value")
|
2017-12-19 15:38:03 -05:00
|
|
|
return [self.metric]
|
2017-12-15 14:47:27 -05:00
|
|
|
return None
|
2017-11-16 03:30:02 -05:00
|
|
|
|
|
|
|
def get_properties(self, d):
|
|
|
|
return {
|
2019-06-25 16:34:48 -04:00
|
|
|
"metric": d.get(self.metric_label),
|
|
|
|
"radius": self.fixed_value
|
|
|
|
if self.fixed_value
|
|
|
|
else d.get(self.metric_label),
|
|
|
|
"cat_color": d.get(self.dim) if self.dim else None,
|
|
|
|
"position": d.get("spatial"),
|
2018-05-31 14:57:53 -04:00
|
|
|
DTTM_ALIAS: d.get(DTTM_ALIAS),
|
2017-11-16 03:30:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
def get_data(self, df):
|
|
|
|
fd = self.form_data
|
2019-06-25 16:34:48 -04:00
|
|
|
self.metric_label = utils.get_metric_name(self.metric) if self.metric else None
|
|
|
|
self.point_radius_fixed = fd.get("point_radius_fixed")
|
2017-11-16 03:30:02 -05:00
|
|
|
self.fixed_value = None
|
2019-06-25 16:34:48 -04:00
|
|
|
self.dim = self.form_data.get("dimension")
|
|
|
|
if self.point_radius_fixed.get("type") != "metric":
|
|
|
|
self.fixed_value = self.point_radius_fixed.get("value")
|
2019-04-12 00:48:35 -04:00
|
|
|
return super().get_data(df)
|
2017-11-16 03:30:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
class DeckScreengrid(BaseDeckGLViz):
|
|
|
|
|
|
|
|
"""deck.gl's ScreenGridLayer"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "deck_screengrid"
|
|
|
|
verbose_name = _("Deck.gl - Screen Grid")
|
|
|
|
spatial_control_keys = ["spatial"]
|
2018-04-09 17:02:20 -04:00
|
|
|
is_timeseries = True
|
|
|
|
|
|
|
|
def query_obj(self):
|
|
|
|
fd = self.form_data
|
2019-06-25 16:34:48 -04:00
|
|
|
self.is_timeseries = fd.get("time_grain_sqla") or fd.get("granularity")
|
2019-04-12 00:48:35 -04:00
|
|
|
return super().query_obj()
|
2018-01-12 14:06:11 -05:00
|
|
|
|
2018-01-17 17:01:32 -05:00
|
|
|
def get_properties(self, d):
|
|
|
|
return {
|
2019-06-25 16:34:48 -04:00
|
|
|
"position": d.get("spatial"),
|
|
|
|
"weight": d.get(self.metric_label) or 1,
|
|
|
|
"__timestamp": d.get(DTTM_ALIAS) or d.get("__time"),
|
2018-01-17 17:01:32 -05:00
|
|
|
}
|
2017-11-16 03:30:02 -05:00
|
|
|
|
2018-07-16 16:42:07 -04:00
|
|
|
def get_data(self, df):
|
2019-01-18 11:24:11 -05:00
|
|
|
self.metric_label = utils.get_metric_name(self.metric)
|
2019-04-12 00:48:35 -04:00
|
|
|
return super().get_data(df)
|
2018-07-16 16:42:07 -04:00
|
|
|
|
2017-11-16 03:30:02 -05:00
|
|
|
|
|
|
|
class DeckGrid(BaseDeckGLViz):
|
|
|
|
|
|
|
|
"""deck.gl's DeckLayer"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "deck_grid"
|
|
|
|
verbose_name = _("Deck.gl - 3D Grid")
|
|
|
|
spatial_control_keys = ["spatial"]
|
2018-01-12 14:06:11 -05:00
|
|
|
|
2018-01-17 17:01:32 -05:00
|
|
|
def get_properties(self, d):
|
2019-06-25 16:34:48 -04:00
|
|
|
return {"position": d.get("spatial"), "weight": d.get(self.metric_label) or 1}
|
2017-11-16 03:30:02 -05:00
|
|
|
|
2018-07-16 16:42:07 -04:00
|
|
|
def get_data(self, df):
|
2019-01-18 11:24:11 -05:00
|
|
|
self.metric_label = utils.get_metric_name(self.metric)
|
2019-04-12 00:48:35 -04:00
|
|
|
return super().get_data(df)
|
2018-07-16 16:42:07 -04:00
|
|
|
|
2017-11-16 03:30:02 -05:00
|
|
|
|
2018-08-28 14:09:10 -04:00
|
|
|
def geohash_to_json(geohash_code):
|
|
|
|
p = geohash.bbox(geohash_code)
|
|
|
|
return [
|
2019-06-25 16:34:48 -04:00
|
|
|
[p.get("w"), p.get("n")],
|
|
|
|
[p.get("e"), p.get("n")],
|
|
|
|
[p.get("e"), p.get("s")],
|
|
|
|
[p.get("w"), p.get("s")],
|
|
|
|
[p.get("w"), p.get("n")],
|
2018-08-28 14:09:10 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2017-12-19 15:38:03 -05:00
|
|
|
class DeckPathViz(BaseDeckGLViz):
|
|
|
|
|
|
|
|
"""deck.gl's PathLayer"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "deck_path"
|
|
|
|
verbose_name = _("Deck.gl - Paths")
|
|
|
|
deck_viz_key = "path"
|
2018-10-24 21:40:57 -04:00
|
|
|
is_timeseries = True
|
2017-12-19 15:38:03 -05:00
|
|
|
deser_map = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"json": json.loads,
|
|
|
|
"polyline": polyline.decode,
|
|
|
|
"geohash": geohash_to_json,
|
2017-12-19 15:38:03 -05:00
|
|
|
}
|
2018-01-12 14:06:11 -05:00
|
|
|
|
2017-12-19 15:38:03 -05:00
|
|
|
def query_obj(self):
|
2018-10-24 21:40:57 -04:00
|
|
|
fd = self.form_data
|
2019-06-25 16:34:48 -04:00
|
|
|
self.is_timeseries = fd.get("time_grain_sqla") or fd.get("granularity")
|
2019-04-12 00:48:35 -04:00
|
|
|
d = super().query_obj()
|
2019-06-25 16:34:48 -04:00
|
|
|
self.metric = fd.get("metric")
|
|
|
|
line_col = fd.get("line_column")
|
|
|
|
if d["metrics"]:
|
2018-08-28 14:09:10 -04:00
|
|
|
self.has_metrics = True
|
2019-06-25 16:34:48 -04:00
|
|
|
d["groupby"].append(line_col)
|
2018-01-11 18:42:44 -05:00
|
|
|
else:
|
2018-08-28 14:09:10 -04:00
|
|
|
self.has_metrics = False
|
2019-06-25 16:34:48 -04:00
|
|
|
d["columns"].append(line_col)
|
2017-12-19 15:38:03 -05:00
|
|
|
return d
|
|
|
|
|
2018-01-11 18:42:44 -05:00
|
|
|
def get_properties(self, d):
|
2017-12-19 15:38:03 -05:00
|
|
|
fd = self.form_data
|
2019-06-25 16:34:48 -04:00
|
|
|
line_type = fd.get("line_type")
|
2018-08-28 14:09:10 -04:00
|
|
|
deser = self.deser_map[line_type]
|
2019-06-25 16:34:48 -04:00
|
|
|
line_column = fd.get("line_column")
|
2018-08-28 14:09:10 -04:00
|
|
|
path = deser(d[line_column])
|
2019-06-25 16:34:48 -04:00
|
|
|
if fd.get("reverse_long_lat"):
|
2018-08-28 14:09:10 -04:00
|
|
|
path = [(o[1], o[0]) for o in path]
|
|
|
|
d[self.deck_viz_key] = path
|
2019-06-25 16:34:48 -04:00
|
|
|
if line_type != "geohash":
|
2018-08-28 14:09:10 -04:00
|
|
|
del d[line_column]
|
2019-06-25 16:34:48 -04:00
|
|
|
d["__timestamp"] = d.get(DTTM_ALIAS) or d.get("__time")
|
2018-08-28 14:09:10 -04:00
|
|
|
return d
|
2017-12-19 15:38:03 -05:00
|
|
|
|
2018-10-24 21:40:57 -04:00
|
|
|
def get_data(self, df):
|
2019-01-18 11:24:11 -05:00
|
|
|
self.metric_label = utils.get_metric_name(self.metric)
|
2019-04-12 00:48:35 -04:00
|
|
|
return super().get_data(df)
|
2018-10-24 21:40:57 -04:00
|
|
|
|
2017-12-19 15:38:03 -05:00
|
|
|
|
2018-01-18 16:28:46 -05:00
|
|
|
class DeckPolygon(DeckPathViz):
|
|
|
|
|
|
|
|
"""deck.gl's Polygon Layer"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "deck_polygon"
|
|
|
|
deck_viz_key = "polygon"
|
|
|
|
verbose_name = _("Deck.gl - Polygon")
|
2018-01-18 16:28:46 -05:00
|
|
|
|
2018-10-24 21:40:57 -04:00
|
|
|
def query_obj(self):
|
|
|
|
fd = self.form_data
|
2019-06-25 16:34:48 -04:00
|
|
|
self.elevation = fd.get("point_radius_fixed") or {"type": "fix", "value": 500}
|
2019-04-12 00:48:35 -04:00
|
|
|
return super().query_obj()
|
2018-10-24 21:40:57 -04:00
|
|
|
|
|
|
|
def get_metrics(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
metrics = [self.form_data.get("metric")]
|
|
|
|
if self.elevation.get("type") == "metric":
|
|
|
|
metrics.append(self.elevation.get("value"))
|
2018-10-24 21:40:57 -04:00
|
|
|
return [metric for metric in metrics if metric]
|
|
|
|
|
|
|
|
def get_properties(self, d):
|
2019-04-12 00:48:35 -04:00
|
|
|
super().get_properties(d)
|
2018-10-24 21:40:57 -04:00
|
|
|
fd = self.form_data
|
2019-06-25 16:34:48 -04:00
|
|
|
elevation = fd["point_radius_fixed"]["value"]
|
|
|
|
type_ = fd["point_radius_fixed"]["type"]
|
2019-08-15 12:20:49 -04:00
|
|
|
d["elevation"] = (
|
|
|
|
d.get(utils.get_metric_name(elevation)) if type_ == "metric" else elevation
|
|
|
|
)
|
2018-10-24 21:40:57 -04:00
|
|
|
return d
|
|
|
|
|
2018-01-18 16:28:46 -05:00
|
|
|
|
2017-11-16 03:30:02 -05:00
|
|
|
class DeckHex(BaseDeckGLViz):
|
|
|
|
|
|
|
|
"""deck.gl's DeckLayer"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "deck_hex"
|
|
|
|
verbose_name = _("Deck.gl - 3D HEX")
|
|
|
|
spatial_control_keys = ["spatial"]
|
2018-01-12 14:06:11 -05:00
|
|
|
|
2018-01-17 17:01:32 -05:00
|
|
|
def get_properties(self, d):
|
2019-06-25 16:34:48 -04:00
|
|
|
return {"position": d.get("spatial"), "weight": d.get(self.metric_label) or 1}
|
2017-11-16 03:30:02 -05:00
|
|
|
|
2018-07-16 16:42:07 -04:00
|
|
|
def get_data(self, df):
|
2019-01-18 11:24:11 -05:00
|
|
|
self.metric_label = utils.get_metric_name(self.metric)
|
2018-07-16 16:42:07 -04:00
|
|
|
return super(DeckHex, self).get_data(df)
|
|
|
|
|
2017-11-16 03:30:02 -05:00
|
|
|
|
2017-12-22 17:40:08 -05:00
|
|
|
class DeckGeoJson(BaseDeckGLViz):
|
|
|
|
|
|
|
|
"""deck.gl's GeoJSONLayer"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "deck_geojson"
|
|
|
|
verbose_name = _("Deck.gl - GeoJSON")
|
2017-12-22 17:40:08 -05:00
|
|
|
|
|
|
|
def query_obj(self):
|
2019-04-12 00:48:35 -04:00
|
|
|
d = super().query_obj()
|
2019-06-25 16:34:48 -04:00
|
|
|
d["columns"] += [self.form_data.get("geojson")]
|
|
|
|
d["metrics"] = []
|
|
|
|
d["groupby"] = []
|
2017-12-22 17:40:08 -05:00
|
|
|
return d
|
|
|
|
|
2018-01-17 17:01:32 -05:00
|
|
|
def get_properties(self, d):
|
2019-06-25 16:34:48 -04:00
|
|
|
geojson = d.get(self.form_data.get("geojson"))
|
2018-01-17 17:01:32 -05:00
|
|
|
return json.loads(geojson)
|
2017-12-22 17:40:08 -05:00
|
|
|
|
|
|
|
|
2018-01-12 14:06:11 -05:00
|
|
|
class DeckArc(BaseDeckGLViz):
|
|
|
|
|
|
|
|
"""deck.gl's Arc Layer"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "deck_arc"
|
|
|
|
verbose_name = _("Deck.gl - Arc")
|
|
|
|
spatial_control_keys = ["start_spatial", "end_spatial"]
|
2018-08-22 00:31:03 -04:00
|
|
|
is_timeseries = True
|
|
|
|
|
|
|
|
def query_obj(self):
|
|
|
|
fd = self.form_data
|
2019-06-25 16:34:48 -04:00
|
|
|
self.is_timeseries = bool(fd.get("time_grain_sqla") or fd.get("granularity"))
|
2019-04-12 00:48:35 -04:00
|
|
|
return super().query_obj()
|
2018-01-12 14:06:11 -05:00
|
|
|
|
2018-01-17 17:01:32 -05:00
|
|
|
def get_properties(self, d):
|
2019-06-25 16:34:48 -04:00
|
|
|
dim = self.form_data.get("dimension")
|
2018-01-17 17:01:32 -05:00
|
|
|
return {
|
2019-06-25 16:34:48 -04:00
|
|
|
"sourcePosition": d.get("start_spatial"),
|
|
|
|
"targetPosition": d.get("end_spatial"),
|
|
|
|
"cat_color": d.get(dim) if dim else None,
|
2018-08-22 00:31:03 -04:00
|
|
|
DTTM_ALIAS: d.get(DTTM_ALIAS),
|
2018-01-12 14:06:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
def get_data(self, df):
|
2019-04-12 00:48:35 -04:00
|
|
|
d = super().get_data(df)
|
2018-01-12 14:06:11 -05:00
|
|
|
|
2019-10-30 19:19:16 -04:00
|
|
|
return {"features": d["features"], "mapboxApiKey": config["MAPBOX_API_KEY"]}
|
2018-01-12 14:06:11 -05:00
|
|
|
|
|
|
|
|
2017-07-21 19:29:25 -04:00
|
|
|
class EventFlowViz(BaseViz):
|
2017-11-16 03:30:02 -05:00
|
|
|
|
2017-07-21 19:29:25 -04:00
|
|
|
"""A visualization to explore patterns in event sequences"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "event_flow"
|
|
|
|
verbose_name = _("Event flow")
|
2017-07-21 19:29:25 -04:00
|
|
|
credits = 'from <a href="https://github.com/williaster/data-ui">@data-ui</a>'
|
|
|
|
is_timeseries = True
|
|
|
|
|
|
|
|
def query_obj(self):
|
2019-04-12 00:48:35 -04:00
|
|
|
query = super().query_obj()
|
2017-07-21 19:29:25 -04:00
|
|
|
form_data = self.form_data
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
event_key = form_data.get("all_columns_x")
|
|
|
|
entity_key = form_data.get("entity")
|
2017-07-21 19:29:25 -04:00
|
|
|
meta_keys = [
|
2019-06-25 16:34:48 -04:00
|
|
|
col
|
|
|
|
for col in form_data.get("all_columns")
|
2017-10-04 13:17:33 -04:00
|
|
|
if col != event_key and col != entity_key
|
2017-07-21 19:29:25 -04:00
|
|
|
]
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
query["columns"] = [event_key, entity_key] + meta_keys
|
2017-07-21 19:29:25 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
if form_data["order_by_entity"]:
|
|
|
|
query["orderby"] = [(entity_key, True)]
|
2017-07-21 19:29:25 -04:00
|
|
|
|
|
|
|
return query
|
|
|
|
|
|
|
|
def get_data(self, df):
|
2019-06-25 16:34:48 -04:00
|
|
|
return df.to_dict(orient="records")
|
2017-07-21 19:29:25 -04:00
|
|
|
|
|
|
|
|
2017-09-26 18:11:35 -04:00
|
|
|
class PairedTTestViz(BaseViz):
|
|
|
|
|
|
|
|
"""A table displaying paired t-test values"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "paired_ttest"
|
|
|
|
verbose_name = _("Time Series - Paired t-test")
|
2017-09-26 18:11:35 -04:00
|
|
|
sort_series = False
|
|
|
|
is_timeseries = True
|
|
|
|
|
|
|
|
def get_data(self, df):
|
|
|
|
"""
|
|
|
|
Transform received data frame into an object of the form:
|
|
|
|
{
|
2017-11-14 00:06:51 -05:00
|
|
|
'metric1': [
|
2017-09-26 18:11:35 -04:00
|
|
|
{
|
|
|
|
groups: ('groupA', ... ),
|
|
|
|
values: [ {x, y}, ... ],
|
|
|
|
}, ...
|
|
|
|
], ...
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
fd = self.form_data
|
2019-06-25 16:34:48 -04:00
|
|
|
groups = fd.get("groupby")
|
2019-04-20 13:26:37 -04:00
|
|
|
metrics = self.metric_labels
|
2019-06-25 16:34:48 -04:00
|
|
|
df = df.pivot_table(index=DTTM_ALIAS, columns=groups, values=metrics)
|
2017-09-26 18:11:35 -04:00
|
|
|
cols = []
|
|
|
|
# Be rid of falsey keys
|
|
|
|
for col in df.columns:
|
2019-06-25 16:34:48 -04:00
|
|
|
if col == "":
|
|
|
|
cols.append("N/A")
|
2017-09-26 18:11:35 -04:00
|
|
|
elif col is None:
|
2019-06-25 16:34:48 -04:00
|
|
|
cols.append("NULL")
|
2017-09-26 18:11:35 -04:00
|
|
|
else:
|
|
|
|
cols.append(col)
|
|
|
|
df.columns = cols
|
|
|
|
data = {}
|
2019-06-25 16:34:48 -04:00
|
|
|
series = df.to_dict("series")
|
2017-09-26 18:11:35 -04:00
|
|
|
for nameSet in df.columns:
|
|
|
|
# If no groups are defined, nameSet will be the metric name
|
2018-10-13 12:38:46 -04:00
|
|
|
hasGroup = not isinstance(nameSet, str)
|
2017-09-26 18:11:35 -04:00
|
|
|
Y = series[nameSet]
|
|
|
|
d = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"group": nameSet[1:] if hasGroup else "All",
|
|
|
|
"values": [{"x": t, "y": Y[t] if t in Y else None} for t in df.index],
|
2017-09-26 18:11:35 -04:00
|
|
|
}
|
|
|
|
key = nameSet[0] if hasGroup else nameSet
|
|
|
|
if key in data:
|
|
|
|
data[key].append(d)
|
|
|
|
else:
|
|
|
|
data[key] = [d]
|
|
|
|
return data
|
|
|
|
|
Map visualization (#650)
* simple mapbox viz
use react-map-gl
superclustering of long/lat points
Added hook for map style, huge performance boost from bounding box fix, added count text on clusters
variable gradient size based on metric count
Ability to aggregate over any point property
This needed a change in the supercluster npm module, a PR was placed here:
https://github.com/mapbox/supercluster/pull/12
Aggregator function option in explore, tweaked visual defaults
better radius size management
clustering radius, point metric/unit options
scale cluster labels that don't fit, non-numeric labels for points
Minor fixes, label field affects points, text changes
serve mapbox apikey for slice
global opacity, viewport saves (hacky), bug in point labels
fixing mapbox-gl dependency
mapbox_api_key in config
expose row_limit, fix minor bugs
Add renderWhileDragging flag, groupby. Only show numerical columns for point radius
Implicitly group by lng/lat columns and error when label doesn't match groupby
'Fix' radius in miles problem, still some jankiness
derived fields cannot be typed as of now -> reverting numerical number change
better grouping error checking, expose count(*) for labelling
Custom colour for clusters/points + smart text colouring
Fixed bad positioning and overflow in explore view + small bugs + added thumbnail
* landscaping & eslint & use izip
* landscapin'
* address js code review
2016-06-24 17:16:51 -04:00
|
|
|
|
2018-02-03 23:18:24 -05:00
|
|
|
class RoseViz(NVD3TimeSeriesViz):
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "rose"
|
|
|
|
verbose_name = _("Time Series - Nightingale Rose Chart")
|
2018-02-03 23:18:24 -05:00
|
|
|
sort_series = False
|
|
|
|
is_timeseries = True
|
|
|
|
|
|
|
|
def get_data(self, df):
|
2019-04-12 00:48:35 -04:00
|
|
|
data = super().get_data(df)
|
2018-02-03 23:18:24 -05:00
|
|
|
result = {}
|
|
|
|
for datum in data:
|
2019-06-25 16:34:48 -04:00
|
|
|
key = datum["key"]
|
|
|
|
for val in datum["values"]:
|
|
|
|
timestamp = val["x"].value
|
2018-02-03 23:18:24 -05:00
|
|
|
if not result.get(timestamp):
|
|
|
|
result[timestamp] = []
|
2019-06-25 16:34:48 -04:00
|
|
|
value = 0 if math.isnan(val["y"]) else val["y"]
|
|
|
|
result[timestamp].append(
|
|
|
|
{
|
|
|
|
"key": key,
|
|
|
|
"value": value,
|
|
|
|
"name": ", ".join(key) if isinstance(key, list) else key,
|
|
|
|
"time": val["x"],
|
|
|
|
}
|
|
|
|
)
|
2018-02-03 23:18:24 -05:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
2017-10-13 00:54:59 -04:00
|
|
|
class PartitionViz(NVD3TimeSeriesViz):
|
|
|
|
|
|
|
|
"""
|
|
|
|
A hierarchical data visualization with support for time series.
|
|
|
|
"""
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
viz_type = "partition"
|
|
|
|
verbose_name = _("Partition Diagram")
|
2017-10-13 00:54:59 -04:00
|
|
|
|
|
|
|
def query_obj(self):
|
2019-04-12 00:48:35 -04:00
|
|
|
query_obj = super().query_obj()
|
2019-06-25 16:34:48 -04:00
|
|
|
time_op = self.form_data.get("time_series_option", "not_time")
|
2017-10-13 00:54:59 -04:00
|
|
|
# Return time series data if the user specifies so
|
2019-06-25 16:34:48 -04:00
|
|
|
query_obj["is_timeseries"] = time_op != "not_time"
|
2017-10-13 00:54:59 -04:00
|
|
|
return query_obj
|
|
|
|
|
|
|
|
def levels_for(self, time_op, groups, df):
|
|
|
|
"""
|
|
|
|
Compute the partition at each `level` from the dataframe.
|
|
|
|
"""
|
|
|
|
levels = {}
|
|
|
|
for i in range(0, len(groups) + 1):
|
|
|
|
agg_df = df.groupby(groups[:i]) if i else df
|
|
|
|
levels[i] = (
|
2019-06-25 16:34:48 -04:00
|
|
|
agg_df.mean()
|
|
|
|
if time_op == "agg_mean"
|
|
|
|
else agg_df.sum(numeric_only=True)
|
|
|
|
)
|
2017-10-13 00:54:59 -04:00
|
|
|
return levels
|
|
|
|
|
|
|
|
def levels_for_diff(self, time_op, groups, df):
|
|
|
|
# Obtain a unique list of the time grains
|
|
|
|
times = list(set(df[DTTM_ALIAS]))
|
|
|
|
times.sort()
|
|
|
|
until = times[len(times) - 1]
|
|
|
|
since = times[0]
|
|
|
|
# Function describing how to calculate the difference
|
|
|
|
func = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"point_diff": [pd.Series.sub, lambda a, b, fill_value: a - b],
|
|
|
|
"point_factor": [pd.Series.div, lambda a, b, fill_value: a / float(b)],
|
|
|
|
"point_percent": [
|
2017-10-13 00:54:59 -04:00
|
|
|
lambda a, b, fill_value=0: a.div(b, fill_value=fill_value) - 1,
|
|
|
|
lambda a, b, fill_value: a / float(b) - 1,
|
|
|
|
],
|
|
|
|
}[time_op]
|
|
|
|
agg_df = df.groupby(DTTM_ALIAS).sum()
|
2019-06-25 16:34:48 -04:00
|
|
|
levels = {
|
|
|
|
0: pd.Series(
|
|
|
|
{
|
|
|
|
m: func[1](agg_df[m][until], agg_df[m][since], 0)
|
|
|
|
for m in agg_df.columns
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2017-10-13 00:54:59 -04:00
|
|
|
for i in range(1, len(groups) + 1):
|
|
|
|
agg_df = df.groupby([DTTM_ALIAS] + groups[:i]).sum()
|
2019-06-25 16:34:48 -04:00
|
|
|
levels[i] = pd.DataFrame(
|
|
|
|
{
|
|
|
|
m: func[0](agg_df[m][until], agg_df[m][since], fill_value=0)
|
|
|
|
for m in agg_df.columns
|
|
|
|
}
|
|
|
|
)
|
2017-10-13 00:54:59 -04:00
|
|
|
return levels
|
|
|
|
|
|
|
|
def levels_for_time(self, groups, df):
|
|
|
|
procs = {}
|
|
|
|
for i in range(0, len(groups) + 1):
|
2019-06-25 16:34:48 -04:00
|
|
|
self.form_data["groupby"] = groups[:i]
|
2017-10-13 00:54:59 -04:00
|
|
|
df_drop = df.drop(groups[i:], 1)
|
2019-03-20 20:14:15 -04:00
|
|
|
procs[i] = self.process_data(df_drop, aggregate=True)
|
2019-06-25 16:34:48 -04:00
|
|
|
self.form_data["groupby"] = groups
|
2017-10-13 00:54:59 -04:00
|
|
|
return procs
|
|
|
|
|
|
|
|
def nest_values(self, levels, level=0, metric=None, dims=()):
|
|
|
|
"""
|
|
|
|
Nest values at each level on the back-end with
|
|
|
|
access and setting, instead of summing from the bottom.
|
|
|
|
"""
|
|
|
|
if not level:
|
2019-06-25 16:34:48 -04:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
"name": m,
|
|
|
|
"val": levels[0][m],
|
|
|
|
"children": self.nest_values(levels, 1, m),
|
|
|
|
}
|
|
|
|
for m in levels[0].index
|
|
|
|
]
|
2017-10-13 00:54:59 -04:00
|
|
|
if level == 1:
|
2019-06-25 16:34:48 -04:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
"name": i,
|
|
|
|
"val": levels[1][metric][i],
|
|
|
|
"children": self.nest_values(levels, 2, metric, (i,)),
|
|
|
|
}
|
|
|
|
for i in levels[1][metric].index
|
|
|
|
]
|
2017-10-13 00:54:59 -04:00
|
|
|
if level >= len(levels):
|
|
|
|
return []
|
2019-06-25 16:34:48 -04:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
"name": i,
|
|
|
|
"val": levels[level][metric][dims][i],
|
|
|
|
"children": self.nest_values(levels, level + 1, metric, dims + (i,)),
|
|
|
|
}
|
|
|
|
for i in levels[level][metric][dims].index
|
|
|
|
]
|
2017-10-13 00:54:59 -04:00
|
|
|
|
|
|
|
def nest_procs(self, procs, level=-1, dims=(), time=None):
|
|
|
|
if level == -1:
|
2019-06-25 16:34:48 -04:00
|
|
|
return [
|
|
|
|
{"name": m, "children": self.nest_procs(procs, 0, (m,))}
|
|
|
|
for m in procs[0].columns
|
|
|
|
]
|
2017-10-13 00:54:59 -04:00
|
|
|
if not level:
|
2019-06-25 16:34:48 -04:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
"name": t,
|
|
|
|
"val": procs[0][dims[0]][t],
|
|
|
|
"children": self.nest_procs(procs, 1, dims, t),
|
|
|
|
}
|
|
|
|
for t in procs[0].index
|
|
|
|
]
|
2017-10-13 00:54:59 -04:00
|
|
|
if level >= len(procs):
|
|
|
|
return []
|
2019-06-25 16:34:48 -04:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
"name": i,
|
|
|
|
"val": procs[level][dims][i][time],
|
|
|
|
"children": self.nest_procs(procs, level + 1, dims + (i,), time),
|
|
|
|
}
|
|
|
|
for i in procs[level][dims].columns
|
|
|
|
]
|
2017-10-13 00:54:59 -04:00
|
|
|
|
|
|
|
def get_data(self, df):
|
|
|
|
fd = self.form_data
|
2019-06-25 16:34:48 -04:00
|
|
|
groups = fd.get("groupby", [])
|
|
|
|
time_op = fd.get("time_series_option", "not_time")
|
2017-10-13 00:54:59 -04:00
|
|
|
if not len(groups):
|
2019-06-25 16:34:48 -04:00
|
|
|
raise ValueError("Please choose at least one groupby")
|
|
|
|
if time_op == "not_time":
|
|
|
|
levels = self.levels_for("agg_sum", groups, df)
|
|
|
|
elif time_op in ["agg_sum", "agg_mean"]:
|
2017-10-13 00:54:59 -04:00
|
|
|
levels = self.levels_for(time_op, groups, df)
|
2019-06-25 16:34:48 -04:00
|
|
|
elif time_op in ["point_diff", "point_factor", "point_percent"]:
|
2017-10-13 00:54:59 -04:00
|
|
|
levels = self.levels_for_diff(time_op, groups, df)
|
2019-06-25 16:34:48 -04:00
|
|
|
elif time_op == "adv_anal":
|
2017-10-13 00:54:59 -04:00
|
|
|
procs = self.levels_for_time(groups, df)
|
|
|
|
return self.nest_procs(procs)
|
|
|
|
else:
|
2019-06-25 16:34:48 -04:00
|
|
|
levels = self.levels_for("agg_sum", [DTTM_ALIAS] + groups, df)
|
2017-10-13 00:54:59 -04:00
|
|
|
return self.nest_values(levels)
|
|
|
|
|
|
|
|
|
2017-10-04 13:17:33 -04:00
|
|
|
viz_types = {
|
2019-06-25 16:34:48 -04:00
|
|
|
o.viz_type: o
|
|
|
|
for o in globals().values()
|
2017-10-04 13:17:33 -04:00
|
|
|
if (
|
2019-06-25 16:34:48 -04:00
|
|
|
inspect.isclass(o)
|
|
|
|
and issubclass(o, BaseViz)
|
2019-10-18 17:44:27 -04:00
|
|
|
and o.viz_type not in config["VIZ_TYPE_BLACKLIST"]
|
2019-06-25 16:34:48 -04:00
|
|
|
)
|
|
|
|
}
|