Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ profile_default/
ipython_config.py

# pyenv
.python-version
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
Expand Down
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.10.14
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
include:
- stage: 'Tests'
language: python
python: 3.8
python: 3.10
before_install:
- sudo docker network create elastic8
- >
Expand Down
15 changes: 14 additions & 1 deletion pybana/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,20 @@ def config(self, using=None):
"""
Return the config associated to the current version of elastic
"""
return self._get(Config, self.config_id(using), using=using)
config_id = self.config_id(using)
try:
return self._get(Config, config_id, using=using)
except NotFoundError:
# Kibana config ids are versioned (e.g. config:8.18.4). In tests the
# seeded config may exist for another patch version, so fallback to
# any config document for the same major version.
major_version = config_id.split(":")[-1].split(".")[0]
hits = self.objects("config", using=using).extra(size=50).execute()
for hit in hits:
hit_id = hit.meta.id
if hit_id.startswith(f"config:{major_version}."):
return self._get(Config, hit_id, using=using)
raise

def is_v8(self, using=None):
elastic = self.get_es(using)
Expand Down
8 changes: 5 additions & 3 deletions pybana/elastic/fixes_for_v8.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,11 @@ def fix_histogram(self, params: Union[List, Dict]):
# cf https://www.elastic.co/docs/reference/aggregations/search-aggregations-bucket-datehistogram-aggregation
interval = v["interval"]
v[
"calendar_interval"
if _is_calendar_interval(interval)
else "fixed_interval"
(
"calendar_interval"
if _is_calendar_interval(interval)
else "fixed_interval"
)
] = v["interval"]
del v["interval"]
elif isinstance(v, dict) or isinstance(v, list):
Expand Down
16 changes: 8 additions & 8 deletions pybana/helpers/datasweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ def ds_if(cond, yes, no):
out.append(
ds_if(
cond_item,
yes
if not isinstance(yes, list)
else yes[i]
if len(yes) > i
else None,
(
yes
if not isinstance(yes, list)
else yes[i] if len(yes) > i else None
),
no if not isinstance(no, list) else no[i] if len(no) > i else None,
)
)
Expand Down Expand Up @@ -149,9 +149,9 @@ def datasweet_eval(expr, bucket):
val = (
value["std_deviation"]
if "std_deviation" in value
else value["values"]["50.0"]
if "values" in value
else value["value"]
else (
value["values"]["50.0"] if "values" in value else value["value"]
)
)
scope[f"agg{key}"] = float("nan") if val is None else val
try:
Expand Down
2 changes: 1 addition & 1 deletion pybana/helpers/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def get_scaled_date_format(config, interval):
scaled_date_formats = json.loads(config.get("dateFormat:scaled", "[]"))
scaled_date_formats.reverse()
default_date_format = config.get("dateFormat")
for (duration, date_format) in scaled_date_formats:
for duration, date_format in scaled_date_formats:
if not duration or interval >= pendulum.parse(duration):
return date_format
return default_date_format
16 changes: 10 additions & 6 deletions pybana/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class DataView(BaseDocument):
class Search(KibanaSavedObjectReferencesMixin, BaseDocument):
_type = "search"

def index(self, using):
def index(self, using, raise_error=True):
"""
Returns the index-pattern associated to the visualization. Go through the
search if needed.
Expand All @@ -139,9 +139,11 @@ def index(self, using):
refs = getattr(self, "_kibana_references", [])
doc_id = resolve_index_pattern_document_id(search_source, refs)
if not doc_id:
raise ValueError(
"Could not resolve data source from searchSourceJSON (missing index / references)"
)
if raise_error:
raise ValueError(
"Could not resolve data source from searchSourceJSON (missing index / references)"
)
return None
return get_index_pattern_or_data_view(doc_id, self.meta.index, using=using)


Expand All @@ -167,7 +169,9 @@ def index(self, using, raise_error=True):
search if needed.
"""
if hasattr(self.visualization, "savedSearchId"):
return self.related_search(using=using).index(using=using)
return self.related_search(using=using).index(
using=using, raise_error=raise_error
)
search_source = self.visualization.kibanaSavedObjectMeta.searchSourceJSON
refs = getattr(self, "_kibana_references", [])
doc_id = resolve_index_pattern_document_id(search_source, refs)
Expand All @@ -185,7 +189,7 @@ def index(self, using, raise_error=True):
"Could not resolve data source from searchSourceJSON, references, "
"or input control visState (missing index / indexPattern)"
)
return
return None

def filters(self):
"""
Expand Down
100 changes: 60 additions & 40 deletions pybana/translators/vega/vega.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ def __init__(self, using):
def conf(self, state):
return {
"$schema": "https://vega.github.io/schema/vega/v5.json",
"width": DEFAULT_PIE_WIDTH
if state.type() == "pie"
else DEFAULT_GAUGE_WIDTH
if state.type() in ["gauge", "goal"]
else DEFAULT_WIDTH,
"width": (
DEFAULT_PIE_WIDTH
if state.type() == "pie"
else (
DEFAULT_GAUGE_WIDTH
if state.type() in ["gauge", "goal"]
else DEFAULT_WIDTH
)
),
"height": DEFAULT_HEIGHT,
"padding": DEFAULT_PADDING,
}
Expand Down Expand Up @@ -187,9 +191,11 @@ def _iter_response(
)
tooltip = {
childpoint.get("x_label", "x"): childpoint["x"],
childpoint["metric"]: self._format_duration(y)
if self._is_duration_bucket(state, metric_agg, metric)
else y,
childpoint["metric"]: (
self._format_duration(y)
if self._is_duration_bucket(state, metric_agg, metric)
else y
),
}
if childpoint["group"]:
tooltip["group"] = childpoint["group"]
Expand Down Expand Up @@ -248,9 +254,11 @@ def data_line_bar(self, conf, state, response, scope):
"groupby": ["x"],
"field": state.y(ax),
"as": [state.y(ax) + "|0", state.y(ax) + "|1"],
"offset": "normalize"
if ax["scale"]["mode"] == "percentage"
else "zero",
"offset": (
"normalize"
if ax["scale"]["mode"] == "percentage"
else "zero"
),
}
]
elif state.metrics_stacked(ax):
Expand All @@ -261,9 +269,11 @@ def data_line_bar(self, conf, state, response, scope):
"field": state.y(ax),
"as": [state.y(ax) + "|0", state.y(ax) + "|1"],
"sort": {"field": "metric"},
"offset": "normalize"
if ax["scale"]["mode"] == "percentage"
else "zero",
"offset": (
"normalize"
if ax["scale"]["mode"] == "percentage"
else "zero"
),
}
]

Expand Down Expand Up @@ -376,9 +386,9 @@ def _scales_y(self, state):
else:
domain = {
"data": "table",
"field": state.y(ax) + "|1"
if state.stacked_applied(ax)
else state.y(ax),
"field": (
state.y(ax) + "|1" if state.stacked_applied(ax) else state.y(ax)
),
}
nice = True
zero = True
Expand Down Expand Up @@ -480,9 +490,11 @@ def legends_line_bar(self, conf, state):
{
"fill": "groupcolor",
"title": "",
"columns": 1
if state._state["params"]["legendPosition"] in ("left", "right")
else 10,
"columns": (
1
if state._state["params"]["legendPosition"] in ("left", "right")
else 10
),
"orient": state._state["params"]["legendPosition"],
}
]
Expand All @@ -491,9 +503,11 @@ def legends_line_bar(self, conf, state):
{
"fill": "metriccolor",
"title": "",
"columns": 1
if state._state["params"]["legendPosition"] in ("left", "right")
else 10,
"columns": (
1
if state._state["params"]["legendPosition"] in ("left", "right")
else 10
),
"orient": state._state["params"]["legendPosition"],
}
]
Expand Down Expand Up @@ -521,9 +535,9 @@ def marks_pie(self, conf, state, response):
"y": {"signal": "height / 2"},
"startAngle": {"field": "startAngle"},
"endAngle": {"field": "endAngle"},
"innerRadius": {"signal": "width * .35"}
if donut
else {"value": 0},
"innerRadius": (
{"signal": "width * .35"} if donut else {"value": 0}
),
"outerRadius": {"signal": "width / 2"},
**(
{"tooltip": {"field": "tooltip"}}
Expand Down Expand Up @@ -729,9 +743,11 @@ def get_color(value):
},
"update": {
"text": {
"signal": "format(mainValue*100/(maxValue - minValue), '.0f') + '%'"
if percentage_mode
else f"'{formatted_value}'"
"signal": (
"format(mainValue*100/(maxValue - minValue), '.0f') + '%'"
if percentage_mode
else f"'{formatted_value}'"
)
},
"y": {"signal": "centerY - 14*fontFactor"},
"fontSize": {"signal": "fontFactor*18"},
Expand Down Expand Up @@ -832,11 +848,11 @@ def _marks_histogram(self, state, ax):
x2 = (
"axis"
if state.multi_axis()
else "group"
if state.groups_side_by_side(ax)
else "metric"
if state.metrics_side_by_side(ax)
else "x"
else (
"group"
if state.groups_side_by_side(ax)
else "metric" if state.metrics_side_by_side(ax) else "x"
)
)
ret = [
{
Expand Down Expand Up @@ -933,9 +949,11 @@ def _marks_line(self, state, ax):
"x": {"scale": "xscale", "field": "x"},
"y": {
"scale": ax["id"],
"field": state.y(ax) + "|1"
if state.stacked_applied(ax)
else state.y(ax),
"field": (
state.y(ax) + "|1"
if state.stacked_applied(ax)
else state.y(ax)
),
},
"stroke": {"scale": stylescale, "field": stackgroupfield},
"strokeWidth": strokesizes,
Expand All @@ -952,9 +970,11 @@ def _marks_line(self, state, ax):
"x": {"scale": "xscale", "field": "x"},
"y": {
"scale": ax["id"],
"field": state.y(ax) + "|1"
if state.stacked_applied(ax)
else state.y(ax),
"field": (
state.y(ax) + "|1"
if state.stacked_applied(ax)
else state.y(ax)
),
},
"fill": {"scale": stylescale, "field": stackgroupfield},
"size": circlesizes,
Expand Down
9 changes: 5 additions & 4 deletions pybana/translators/vega/visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import json


__all__ = ("ContextVisualization",)


Expand Down Expand Up @@ -105,9 +104,11 @@ def metric_label(self, agg):
return "Count"
return "%s - %s" % (
agg["type"],
agg["params"]["field"]
if "field" in agg["params"]
else "Formula %(id)s" % agg,
(
agg["params"]["field"]
if "field" in agg["params"]
else "Formula %(id)s" % agg
),
)
return self.series_params(agg)["data"]["label"]

Expand Down
8 changes: 4 additions & 4 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
-r requirements.txt
black==18.6b4
black==26.1.0
coverage==4.5.3
flake8==3.7.7
flake8==7.3.0
pytest-cov==2.10.1
pytest==8.3.5
pytest==9.0.2
recommonmark==0.6.0
sphinx==1.8.5
sphinx==5.2.3
twine==3.0.0
wheel>=0.31.0
#pluggy==1.1.0
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ elasticsearch==6.4.0
elasticsearch-dsl==6.4.0
hjson==3.0.1
pendulum==2.1.2
pytz>=2019.1
pytz==2024.1
pynumeral==0.1.2
sentry-sdk==0.14.3
vl-convert-python==1.9.0.post1
Loading