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

feat: unified actions selectors #2749

Merged
merged 24 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from 13 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
142 changes: 113 additions & 29 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion web-admin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"@playwright/test": "^1.25.0",
"@sveltejs/adapter-static": "^1.0.0",
"@sveltejs/kit": "^1.5.0",
"@tanstack/svelte-query": "^4.27.0",
"@skokenes/svelte-query": "^4.29.19-fork",
"@tanstack/svelte-query": "npm:@skokenes/[email protected]",
"@types/cookie": "^0.5.1",
"@typescript-eslint/eslint-plugin": "^5.27.0",
"@typescript-eslint/parser": "^5.27.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import ProjectBuilding from "../../../../components/projects/ProjectBuilding.svelte";
import ProjectErrored from "../../../../components/projects/ProjectErrored.svelte";
import DashboardStateProvider from "@rilldata/web-common/features/dashboards/proto-state/DashboardStateProvider.svelte";
import BusinessModelProvider from "@rilldata/web-common/features/dashboards/business-model/BusinessModelProvider.svelte";

const queryClient = useQueryClient();

Expand Down Expand Up @@ -105,11 +106,13 @@
{:else if currentDashboard && !currentDashboard.isValid}
<ProjectErrored organization={orgName} project={projectName} />
{:else}
<DashboardStateProvider metricViewName={dashboardName}>
<Dashboard
leftMargin={"48px"}
hasTitle={false}
metricViewName={dashboardName}
/>
</DashboardStateProvider>
<BusinessModelProvider metricsViewName={dashboardName}>
<DashboardStateProvider metricViewName={dashboardName}>
<Dashboard
leftMargin={"48px"}
hasTitle={false}
metricViewName={dashboardName}
/>
</DashboardStateProvider>
</BusinessModelProvider>
{/if}
3 changes: 2 additions & 1 deletion web-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"@storybook/svelte": "^7.0.18",
"@storybook/svelte-vite": "^7.0.18",
"@storybook/testing-library": "^0.0.14-next.2",
"@tanstack/svelte-query": "^4.27.0",
"@skokenes/svelte-query": "^4.29.19-fork",
"@tanstack/svelte-query": "npm:@skokenes/[email protected]",
"@testing-library/svelte": "^3.2.2",
"@typescript-eslint/eslint-plugin": "^5.27.0",
"@typescript-eslint/parser": "^5.27.0",
Expand Down
41 changes: 41 additions & 0 deletions web-common/src/features/dashboards/actions/index.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this!

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { get } from "svelte/store";
import type { BusinessModel } from "../business-model/business-model";
import { cancelDashboardQueries } from "../dashboard-queries";
import { metricsExplorerStore } from "../dashboard-stores";

export function clearFilterForDimension(
ctx: BusinessModel,
dimensionId,
include: boolean
) {
const metricViewName = get(ctx.metricsViewName);
cancelDashboardQueries(ctx.queryClient, metricViewName);
metricsExplorerStore.clearFilterForDimension(
skokenes marked this conversation as resolved.
Show resolved Hide resolved
metricViewName,
dimensionId,
include
);
}

export function clearAllFilters(ctx: BusinessModel) {
const filters = get(ctx.dashboardStore).filters;
const hasFilters =
(filters && filters.include.length > 0) || filters.exclude.length > 0;
const metricViewName = get(ctx.metricsViewName);
if (hasFilters) {
cancelDashboardQueries(ctx.queryClient, metricViewName);
metricsExplorerStore.clearFilters(metricViewName);
}
}

export function toggleDimensionValue(ctx: BusinessModel, event, item) {
const metricViewName = get(ctx.metricsViewName);
cancelDashboardQueries(ctx.queryClient, metricViewName);
metricsExplorerStore.toggleFilter(metricViewName, item.name, event.detail);
}

export function toggleFilterMode(ctx: BusinessModel, dimensionName) {
const metricViewName = get(ctx.metricsViewName);
cancelDashboardQueries(ctx.queryClient, metricViewName);
metricsExplorerStore.toggleFilterMode(metricViewName, dimensionName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script lang="ts">
import { setContext } from "svelte";
import { useQueryClient } from "@tanstack/svelte-query";
import { createBusinessModel, DEFAULT_STORE_KEY } from "./business-model";

export let metricsViewName: string;

const queryClient = useQueryClient();
const businessModel = createBusinessModel({ queryClient, metricsViewName });
setContext(DEFAULT_STORE_KEY, businessModel);

$: {
// update metrics view name
businessModel.setMetricsViewName(metricsViewName);
}
</script>

<slot />
17 changes: 17 additions & 0 deletions web-common/src/features/dashboards/business-model/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Using the Business Model Provider
The BusinessModelProvider provides an object with all dependent services for the Rill UI's business model logic.
Here is an example of using it:
```svelte
<script lang="ts">
import { getBusinessModel } from "./business-model";

const businessModel = getBusinessModel();
const { dashboardStore, metricsViewName } = businessModel;
</script>

<div>The dashboard is: {$metricsViewName}</div>
<div>
The filters are: <pre>{JSON.stringify($dashboardStore.filters, null, 2)}</pre>
</div>

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { writable, Writable, Readable, derived } from "svelte/store";
import { getContext } from "svelte";
import type { QueryClient } from "@tanstack/svelte-query";
import type { Runtime } from "@rilldata/web-common/runtime-client/runtime-store";
import {
MetricsExplorerEntity,
MetricsExplorerStoreType,
metricsExplorerStore,
useDashboardStore,
} from "../dashboard-stores";
import { runtime } from "@rilldata/web-common/runtime-client/runtime-store";

export type BusinessModel = {
runtime: Writable<Runtime>;
metricsViewName: Writable<string>;
metricsStore: Readable<MetricsExplorerStoreType>;
dashboardStore: Readable<MetricsExplorerEntity>;
queryClient: QueryClient;
setMetricsViewName: (s: string) => void;
};

export const DEFAULT_STORE_KEY = Symbol("business-model");

export function getBusinessModel(): BusinessModel {
return getContext(DEFAULT_STORE_KEY);
}

export function createBusinessModel({
queryClient,
metricsViewName,
}: {
queryClient: QueryClient;
metricsViewName: string;
}): BusinessModel {
const metricsViewNameStore = writable(metricsViewName);
const dashboardStore = derived([metricsViewNameStore], ([name], set) => {
const store = useDashboardStore(name);
return store.subscribe(set);
});
return {
runtime: runtime,
metricsViewName: metricsViewNameStore,
metricsStore: metricsExplorerStore,
queryClient,
dashboardStore,
setMetricsViewName: (name) => {
metricsViewNameStore.set(name);
},
} as BusinessModel;
}
Loading
Loading