Skip to content

Commit

Permalink
limit chart data by schema
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisclark committed Aug 2, 2024
1 parent c2ac5c6 commit 5bb7c16
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
13 changes: 7 additions & 6 deletions explorer/charts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
BAR_WIDTH = 0.2


def get_chart(result: QueryResult, chart_type: str) -> Optional[str]:
def get_chart(result: QueryResult, chart_type: str, num_rows: int) -> Optional[str]:
import matplotlib.pyplot as plt
"""
Return a line or bar chart in SVG format if the result table adheres to the expected format.
Expand All @@ -21,26 +21,27 @@ def get_chart(result: QueryResult, chart_type: str) -> Optional[str]:
return
if len(result.data) < 1:
return None
data = result.data[:num_rows]
numeric_columns = [
c for c in range(1, len(result.data[0]))
if all([isinstance(col[c], (int, float)) or col[c] is None for col in result.data])
c for c in range(1, len(data[0]))
if all([isinstance(col[c], (int, float)) or col[c] is None for col in data])
]
# Don't create charts for > 10 series. This is a lightweight visualization.
if len(numeric_columns) < 1 or len(numeric_columns) > 10:
return None
labels = [row[0] for row in result.data]
labels = [row[0] for row in data]
fig, ax = plt.subplots(figsize=(10, 3.8))
bars = []
bar_positions = []
for idx, col_num in enumerate(numeric_columns):
if chart_type == "bar":
values = [row[col_num] for row in result.data]
values = [row[col_num] for row in data]
bar_container = ax.bar([x + idx * BAR_WIDTH
for x in range(len(labels))], values, BAR_WIDTH, label=result.headers[col_num])
bars.append(bar_container)
bar_positions.append([(rect.get_x(), rect.get_height()) for rect in bar_container])
if chart_type == "line":
ax.plot(labels, [row[col_num] for row in result.data], label=result.headers[col_num])
ax.plot(labels, [row[col_num] for row in data], label=result.headers[col_num])

ax.set_xlabel(result.headers[0])

Expand Down
2 changes: 0 additions & 2 deletions explorer/templates/explorer/schema.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<div class="schema-wrapper">
<h4>{% translate "Schema" %}</h4>
<div id="schema-contents">
{% if m|length > 1 %}
<p><input class="search form-control" placeholder="{% translate "Search Tables" %}" /></p>
<div class="row">
<div class="col">
Expand All @@ -20,7 +19,6 @@ <h4>{% translate "Schema" %}</h4>
</a>
</div>
</div>
{% endif %}
<div class="mt-3">
<ul class="list">
{% for m in schema %}
Expand Down
4 changes: 2 additions & 2 deletions explorer/views/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def query_viewmodel(request, query, title=None, form=None, message=None,

try:
if app_settings.EXPLORER_CHARTS_ENABLED and has_valid_results:
charts["line_chart_svg"] = get_chart(res,"line")
charts["bar_chart_svg"] = get_chart(res,"bar")
charts["line_chart_svg"] = get_chart(res,"line", rows)
charts["bar_chart_svg"] = get_chart(res,"bar", rows)
except TypeError as e:
if ql is not None:
msg = f"Error generating charts for querylog {ql.id}: {e}"
Expand Down

0 comments on commit 5bb7c16

Please sign in to comment.