Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix : Filters not taking affect on shared public dashboard links (v3) #411 #421

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions frontend/src2/charts/SharedChart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import ChartRenderer from './components/ChartRenderer.vue'
import { getFormattedRows } from '../query/helpers'
import { showErrorToast } from '../helpers'

const props = defineProps<{ chart_name: string }>()
const props = defineProps<{ chart_name: string; query?: Record<string, string[]>}>()

const chart = reactive({
doc: {} as WorkbookChart,
result: {} as QueryResult,
})

const fetchingData = ref(true)
call('insights.api.workbooks.fetch_shared_chart_data', { chart_name: props.chart_name })
call('insights.api.workbooks.fetch_shared_chart_data', { chart_name: props.chart_name, filters: props.query })
.then((res: any) => {
fetchingData.value = false
chart.doc = res.chart
Expand Down
18 changes: 17 additions & 1 deletion frontend/src2/dashboard/SharedDashboard.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
<script setup lang="ts">
import { call } from 'frappe-ui'
import { ref } from 'vue'
import { useRoute } from 'vue-router'
import SharedChart from '../charts/SharedChart.vue'
import { WorkbookDashboard } from '../types/workbook.types'
import VueGridLayout from './VueGridLayout.vue'

const props = defineProps<{ dashboard_name: string }>()

const dashboard = ref<WorkbookDashboard>()
const route = useRoute();
const queryString = new URLSearchParams(route.query);
const keyValuePairs: Record<string, string[]> = {};

Object.entries(route.query).forEach(([key, value]) => {
if (Array.isArray(value)) {
keyValuePairs[key] = value
} else {
keyValuePairs[key] = [value]
}
})



dashboard.value = await call('insights.api.dashboards.fetch_workbook_dashboard', {
dashboard_name: props.dashboard_name,
Expand All @@ -26,7 +40,9 @@ dashboard.value = await call('insights.api.dashboards.fetch_workbook_dashboard',
>
<template #item="{ index }">
<div class="relative h-full w-full rounded p-2">
<SharedChart :chart_name="dashboard.items[index].chart" />
<SharedChart :chart_name="dashboard.items[index].chart"
:query="keyValuePairs"
/>
</div>
</template>
</VueGridLayout>
Expand Down
5 changes: 3 additions & 2 deletions insights/api/workbooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ def update_share_permissions(


@frappe.whitelist(allow_guest=True)
def fetch_shared_chart_data(chart_name: str):
def fetch_shared_chart_data(chart_name: str, filters: dict = None):
filters = frappe.parse_json(filters) if filters else {}
workbooks = frappe.get_all(
"Insights Workbook",
filters={"charts": ["like", f"%{chart_name}%"]},
Expand All @@ -241,4 +242,4 @@ def fetch_shared_chart_data(chart_name: str):
frappe.throw("Chart not found")

workbook = frappe.get_doc("Insights Workbook", workbooks[0])
return workbook.get_shared_chart_data(chart_name)
return workbook.get_shared_chart_data(chart_name, filters)
23 changes: 19 additions & 4 deletions insights/insights/doctype/insights_workbook/insights_workbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def track_view(self):
def query_map(self):
return {q["name"]: q for q in frappe.parse_json(self.queries)}

def get_shared_chart_data(self, chart_name):
def get_shared_chart_data(self, chart_name, filters: dict= None):
chart = next(
(c for c in frappe.parse_json(self.charts) if c["name"] == chart_name), None
)
Expand All @@ -81,9 +81,23 @@ def get_shared_chart_data(self, chart_name):
"results": [],
}

if filters:
for key, value in filters.items():
for operation in operations:
if "table" in operation and "operations" in operation["table"]:
for table_op in operation["table"]["operations"]:
if table_op.get("type") == "filter_group":
for filter in table_op["filters"]:
if filter['column']['column_name'] == key:
filter['value'] = []
for data in value:
filter['value'].append(data)

chart_query = self.query_map.get(chart["query"])

use_live_connection = chart_query.get("use_live_connection", True)
operations = self.resolve_query_tables(operations)

operations = self.resolve_query_tables(operations, shared = True)

frappe.flags.ignore_insights_permissions = True
results = fetch_query_results(
Expand Down Expand Up @@ -122,7 +136,7 @@ def can_access_shared_chart(self, chart):

return False

def resolve_query_tables(self, operations):
def resolve_query_tables(self, operations, shared):
if not operations:
return operations

Expand All @@ -140,7 +154,8 @@ def resolve_query_tables(self, operations):
query_table = self.query_map.get(query_name)
if not query_table:
frappe.throw(f"Query {query_name} not found")

if shared:
continue
op["table"]["operations"] = self.resolve_query_tables(
query_table["operations"]
)
Expand Down