Skip to content

Commit

Permalink
Update export and report queries
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaHegde committed Jul 15, 2024
1 parent ac01e76 commit ad9db04
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
});
$: scheduledReportsQueryArgs = getDimensionTableExportArgs(ctx);
$: console.log($scheduledReportsQueryArgs);
</script>

<WithTogglableFloatingElement
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { getComparisonRequestMeasures } from "@rilldata/web-common/features/dashboards/dashboard-utils";
import { getDimensionFilterWithSearch } from "@rilldata/web-common/features/dashboards/dimension-table/dimension-table-utils";
import {
ComparisonDeltaAbsoluteSuffix,
ComparisonDeltaRelativeSuffix,
} from "@rilldata/web-common/features/dashboards/filters/measure-filters/measure-filter-entry";
import { mergeMeasureFilters } from "@rilldata/web-common/features/dashboards/filters/measure-filters/measure-filter-utils";
import { SortDirection } from "@rilldata/web-common/features/dashboards/proto-state/derived-types";
import { useMetricsView } from "@rilldata/web-common/features/dashboards/selectors/index";
import type { StateManagers } from "@rilldata/web-common/features/dashboards/state-managers/state-managers";
import { sanitiseExpression } from "@rilldata/web-common/features/dashboards/stores/filter-utils";
import { MetricsExplorerEntity } from "@rilldata/web-common/features/dashboards/stores/metrics-explorer-entity";
import { useTimeControlStore } from "@rilldata/web-common/features/dashboards/time-controls/time-control-store";
import {
mapComparisonTimeRange,
Expand All @@ -16,6 +19,7 @@ import { DashboardState_LeaderboardSortType } from "@rilldata/web-common/proto/g
import type {
V1MetricsViewAggregationMeasure,
V1MetricsViewAggregationRequest,
V1TimeRange,
} from "@rilldata/web-common/runtime-client";
import { runtime } from "@rilldata/web-common/runtime-client/runtime-store";
import { derived, get, Readable } from "svelte/store";
Expand All @@ -42,55 +46,76 @@ export function getDimensionTableExportArgs(
timeRange,
);

// api now expects measure names for which comparison are calculated
let comparisonMeasures: string[] = [];
if (comparisonTimeRange) {
comparisonMeasures = [dashboardState.leaderboardMeasureName];
}
return getDimensionTableAggregationRequestForTime(
metricViewName,
dashboardState,
timeRange,
comparisonTimeRange,
);
},
);
}

const measures: V1MetricsViewAggregationMeasure[] = [
...dashboardState.visibleMeasureKeys,
].map((name) => ({
name: name,
}));
export function getDimensionTableAggregationRequestForTime(
metricsView: string,
dashboardState: MetricsExplorerEntity,
timeRange: V1TimeRange,
comparisonTimeRange: V1TimeRange | undefined,
): V1MetricsViewAggregationRequest {
const measures: V1MetricsViewAggregationMeasure[] = [
...dashboardState.visibleMeasureKeys,
].map((name) => ({
name: name,
}));

let apiSortName = dashboardState.leaderboardMeasureName;
if (comparisonTimeRange && timeControlState.selectedComparisonTimeRange) {
// insert beside the correct measure
measures.splice(
measures.findIndex((m) => m.name === apiSortName),
0,
...getComparisonRequestMeasures(apiSortName),
);
switch (dashboardState.dashboardSortType) {
case DashboardState_LeaderboardSortType.DELTA_ABSOLUTE:
apiSortName += ComparisonDeltaAbsoluteSuffix;
break;
case DashboardState_LeaderboardSortType.DELTA_PERCENT:
apiSortName += ComparisonDeltaRelativeSuffix;
break;
}
}
let apiSortName = dashboardState.leaderboardMeasureName;
if (comparisonTimeRange) {
// insert beside the correct measure
measures.splice(
measures.findIndex((m) => m.name === apiSortName),
0,
...getComparisonRequestMeasures(apiSortName),
);
switch (dashboardState.dashboardSortType) {
case DashboardState_LeaderboardSortType.DELTA_ABSOLUTE:
apiSortName += ComparisonDeltaAbsoluteSuffix;
break;
case DashboardState_LeaderboardSortType.DELTA_PERCENT:
apiSortName += ComparisonDeltaRelativeSuffix;
break;
}
}

return {
instanceId: get(runtime).instanceId,
metricsView: metricViewName,
dimension: {
name: dashboardState.selectedDimensionName,
},
measures,
comparisonMeasures: comparisonMeasures,
timeRange,
...(comparisonTimeRange ? { comparisonTimeRange } : {}),
sort: [
{
name: apiSortName,
desc: dashboardState.sortDirection === SortDirection.DESCENDING,
},
],
where: sanitiseExpression(dashboardState.whereFilter, undefined),
offset: "0",
};
},
const where = sanitiseExpression(
mergeMeasureFilters(
dashboardState,
getDimensionFilterWithSearch(
dashboardState?.whereFilter,
dashboardState?.dimensionSearchText ?? "",
dashboardState.selectedDimensionName!,
),
),
undefined,
);

return {
instanceId: get(runtime).instanceId,
metricsView,
dimensions: [
{
name: dashboardState.selectedDimensionName,
},
],
measures,
timeRange,
...(comparisonTimeRange ? { comparisonTimeRange } : {}),
sort: [
{
name: apiSortName,
desc: dashboardState.sortDirection === SortDirection.DESCENDING,
},
],
where,
offset: "0",
};
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import { getDimensionFilterWithSearch } from "@rilldata/web-common/features/dashboards/dimension-table/dimension-table-utils";
import { mergeMeasureFilters } from "@rilldata/web-common/features/dashboards/filters/measure-filters/measure-filter-utils";
import { getDimensionTableAggregationRequestForTime } from "@rilldata/web-common/features/dashboards/dimension-table/dimension-table-export-utils";
import type { StateManagers } from "@rilldata/web-common/features/dashboards/state-managers/state-managers";
import { sanitiseExpression } from "@rilldata/web-common/features/dashboards/stores/filter-utils";
import {
fetchResource,
ResourceKind,
} from "@rilldata/web-common/features/entity-management/resource-selectors";
import {
V1ExportFormat,
V1MetricsViewAggregationMeasure,
createQueryServiceExport,
} from "@rilldata/web-common/runtime-client";
import { get } from "svelte/store";
import { runtime } from "../../../runtime-client/runtime-store";
import { getQuerySortType } from "../leaderboard/leaderboard-utils";
import { SortDirection } from "../proto-state/derived-types";

export default async function exportToplist({
ctx,
Expand All @@ -30,83 +21,25 @@ export default async function exportToplist({
const timeControlState = get(
ctx.selectors.timeRangeSelectors.timeControlsState,
);
const instanceId = get(runtime).instanceId;
const metricsViewResource = await fetchResource(
ctx.queryClient,
instanceId,
metricsViewName,
ResourceKind.MetricsView,
);
const measuresSpec =
metricsViewResource?.metricsView?.state?.validSpec?.measures ?? [];
const measures = [...dashboard.visibleMeasureKeys]
.filter((vm) => {
const m = measuresSpec.find((m) => m.name === vm);
return !!m && !m.window && !m.requiredDimensions?.length;
})
.map(
(name) =>
<V1MetricsViewAggregationMeasure>{
name: name,
},
);

// CAST SAFETY: by definition, a dimension is selected when in the Dimension Table
const dimensionName = dashboard.selectedDimensionName as string;

// api now expects measure names for which comparison are calculated
let comparisonMeasures: string[] = [];
if (
timeControlState.comparisonTimeStart &&
timeControlState.comparisonTimeStart
) {
comparisonMeasures = [dashboard.leaderboardMeasureName];
}

const where = sanitiseExpression(
mergeMeasureFilters(
dashboard,
getDimensionFilterWithSearch(
dashboard?.whereFilter,
dashboard?.dimensionSearchText ?? "",
dimensionName,
),
),
undefined,
);

const result = await get(query).mutateAsync({
instanceId: get(runtime).instanceId,
data: {
format,
query: {
metricsViewComparisonRequest: {
instanceId,
metricsViewName,
dimension: {
name: dimensionName,
},
measures,
comparisonMeasures: comparisonMeasures,
timeRange: {
start: timeControlState.timeStart,
end: timeControlState.timeEnd,
},
comparisonTimeRange: {
start: timeControlState.comparisonTimeStart,
end: timeControlState.comparisonTimeEnd,
},
sort: [
metricsViewAggregationRequest:
getDimensionTableAggregationRequestForTime(
metricsViewName,
dashboard,
{
start: timeControlState.timeStart,
end: timeControlState.timeEnd,
},
{
name: dashboard.leaderboardMeasureName,
desc: dashboard.sortDirection === SortDirection.DESCENDING,
sortType: getQuerySortType(dashboard.dashboardSortType),
start: timeControlState.comparisonTimeStart,
end: timeControlState.comparisonTimeEnd,
},
],
where,
limit: undefined, // the backend handles export limits
offset: "0",
},
),
},
},
});
Expand Down

0 comments on commit ad9db04

Please sign in to comment.