Skip to content

Commit

Permalink
add ratio metric frontend support
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyl committed Sep 3, 2023
1 parent 8476dd2 commit 772eb0a
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 79 deletions.
90 changes: 28 additions & 62 deletions backend/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,14 @@ def dashboard():
return app.send_static_file('index.html')


@app.route('/api/bqinsight', methods=['POST'])
def getBqInsight():
data = request.get_json()

table_name = data['tableName']
def parse_data(data):
baseDateRange = data['baseDateRange']
comparisonDateRange = data['comparisonDateRange']
selectedColumns = data['selectedColumns']
date_column = data['dateColumn']
# TODO(liuyl): Fix this, right now did not pass this value to backend
date_column_type = data['dateColumnType']
date_column_type = data['dateColumnType'] if 'dateColumnType' in data else 'date'
group_by_columns = data['groupByColumns']
metric_column = data['metricColumn']

baselineStart = datetime.strptime(
baseDateRange['from'], '%Y-%m-%dT%H:%M:%S.%fZ').date()
Expand All @@ -75,15 +71,26 @@ def getBqInsight():
comparisonEnd = datetime.strptime(
comparisonDateRange['to'], '%Y-%m-%dT%H:%M:%S.%fZ').date()

agg_method = list(filter(lambda x: x[1]['type'] == 'metric' or x[1]
['type'] == 'supporting_metric', selectedColumns.items()))
expected_value = list(filter(lambda x: x[1]['type'] == 'metric', selectedColumns.items()))[
0][1]['expectedValue']

metrics_name = {k: k for k, v in agg_method}
metrics_name = {metric_column['columnNames']
[0]: metric_column['columnNames'][0]}
agg_method = {metric_column['columnNames']
[0]: metric_column['aggregationOption']}
metrics_name.update({date_column: 'count'})
agg_method = {k: agg_method_map[v['aggregationOption']]
for k, v in agg_method}
agg_method.update({date_column: 'count'})

# TODO: Fix this, right now did not pass this value to backend
expected_value = 0

return (baselineStart, baselineEnd, comparisonStart, comparisonEnd, date_column, date_column_type, agg_method, metrics_name, group_by_columns, expected_value)


@app.route('/api/bqinsight', methods=['POST'])
def getBqInsight():
data = request.get_json()
table_name = data['tableName']

(baselineStart, baselineEnd, comparisonStart, comparisonEnd, date_column, date_column_type,
agg_method, metrics_name, group_by_columns, expected_value) = parse_data(data)

bq_metric = BqMetrics(
table_name=table_name,
Expand All @@ -103,28 +110,9 @@ def get_time_series():
data = request.get_json()

fileId = data['fileId']
baseDateRange = data['baseDateRange']
comparisonDateRange = data['comparisonDateRange']
selectedColumns = data['selectedColumns']
date_column = data['dateColumn']

baselineStart = datetime.strptime(
baseDateRange['from'], '%Y-%m-%dT%H:%M:%S.%fZ').date()
baselineEnd = datetime.strptime(
baseDateRange['to'], '%Y-%m-%dT%H:%M:%S.%fZ').date()
comparisonStart = datetime.strptime(
comparisonDateRange['from'], '%Y-%m-%dT%H:%M:%S.%fZ').date()
comparisonEnd = datetime.strptime(
comparisonDateRange['to'], '%Y-%m-%dT%H:%M:%S.%fZ').date()

agg_method = list(filter(lambda x: x[1]['type'] == 'metric' or x[1]
['type'] == 'supporting_metric', selectedColumns.items()))

metrics_name = {k: k for k, v in agg_method}
metrics_name.update({date_column: 'count'})
agg_method = {k: agg_method_map[v['aggregationOption']]
for k, v in agg_method}
agg_method.update({date_column: 'count'})
(baselineStart, baselineEnd, comparisonStart, comparisonEnd, date_column, date_column_type,
agg_method, metrics_name, group_by_columns, expected_value) = parse_data(data)

segment_key = data['segmentKey']
filtering_clause = polars.lit(True)
Expand Down Expand Up @@ -152,31 +140,9 @@ def get_time_series():
def getInsight():
data = request.get_json()
file_id = data['fileId']
base_date_range = data['baseDateRange']
comparison_date_range = data['comparisonDateRange']
selected_columns = data['selectedColumns']
date_column = data['dateColumn']
group_by_columns = data['groupByColumns']

baseline_start = datetime.strptime(
base_date_range['from'], '%Y-%m-%dT%H:%M:%S.%fZ').date()
baseline_end = datetime.strptime(
base_date_range['to'], '%Y-%m-%dT%H:%M:%S.%fZ').date()
comparison_start = datetime.strptime(
comparison_date_range['from'], '%Y-%m-%dT%H:%M:%S.%fZ').date()
comparison_end = datetime.strptime(
comparison_date_range['to'], '%Y-%m-%dT%H:%M:%S.%fZ').date()

agg_method = list(filter(lambda x: x[1]['type'] == 'metric' or x[1]
['type'] == 'supporting_metric', selected_columns.items()))
expected_value = list(filter(lambda x: x[1]['type'] == 'metric', selected_columns.items()))[
0][1]['expectedValue']

metrics_name = {k: k for k, v in agg_method}
metrics_name.update({date_column: 'count'})
agg_method = {k: agg_method_map[v['aggregationOption']]
for k, v in agg_method}
agg_method.update({date_column: 'count'})
(baselineStart, baselineEnd, comparisonStart, comparisonEnd, date_column, date_column_type,
agg_method, metrics_name, group_by_columns, expected_value) = parse_data(data)

logger.info('Reading file')
df = polars.read_csv(f'/tmp/dsensei/{file_id}') \
Expand All @@ -185,8 +151,8 @@ def getInsight():

metrics = MetricsController(
df,
(baseline_start, baseline_end),
(comparison_start, comparison_end),
(baselineStart, baselineEnd),
(comparisonStart, comparisonEnd),
date_column,
group_by_columns,
agg_method,
Expand Down
80 changes: 64 additions & 16 deletions frontend/src/components/uploader/report-config/MetricConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ type Props = {


const MetricConfig = (props: Props) => {
const { getValidMetricColumns, selectedColumns, onSelectMetrics, onSelectMetricAggregationOption, targetDirection, setTargetDirection } = props;
const { getValidMetricColumns, onSelectMetricAggregationOption, targetDirection, setTargetDirection } = props;
const [metricType, setMetricType] = useState<AggregationType>("sum");
const [metricColumns, setMetricColumns] = useState<string[]>([]);
const [metricColumns, setMetricColumns] = useState<string[]>(["", ""]);
const [metricColumnsType, setMetricColumnsType] = useState<AggregationType[]>(["sum", "sum"]);

useEffect(() => {
if (metricColumns.length > 0) {
onSelectMetricAggregationOption({
columnNames: metricColumns,
aggregationOption: metricType,
columnAggregationTypes: metricType === "ratio" ? metricColumnsType : undefined,
} as MetricColumn, false);
}
}, [metricColumns, metricType, onSelectMetricAggregationOption]);
}, [metricColumns, metricType, metricColumnsType, onSelectMetricAggregationOption]);

const singularMetrics = () => {
return (
Expand All @@ -45,25 +47,71 @@ const MetricConfig = (props: Props) => {
}
labels={getValidMetricColumns()}
values={getValidMetricColumns()}
selectedValue={
Object.keys(selectedColumns).filter(
(c) => selectedColumns[c]["type"] === "metric"
).length > 0
? Object.keys(selectedColumns).filter(
(c) => selectedColumns[c]["type"] === "metric"
)[0]
: ""
}
selectedValue={metricColumns[0]}
onValueChange={(metric) => {
setMetricColumns([metric]);
onSelectMetrics([metric], "metric");
setMetricColumns([metric])
}}
/>);
};

const complexMetrics = () => {
return (
<>
{/* Numerator config */}
<SingleSelector
title={
<Text className="pr-4 text-black">
Select the numerator metric type
</Text>
}
labels={["Sum", "Count", "Distinct"]}
values={["sum", "count", "nunique"]}
selectedValue={metricColumnsType[0]}
onValueChange={(metric) => {
setMetricColumnsType([metric as AggregationType, metricColumnsType[1]]);
}}
/>
<SingleSelector
title={
<Text className="pr-4 text-black">
Select the numerator metric column
</Text>
}
labels={getValidMetricColumns()}
values={getValidMetricColumns()}
selectedValue={metricColumns[0]}
onValueChange={(metric) => {
setMetricColumns([metric, metricColumns[1]])
}}
/>

{/* Demoninator config */}
<SingleSelector
title={
<Text className="pr-4 text-black">
Select the denominator metric type
</Text>
}
labels={["Sum", "Count", "Distinct"]}
values={["sum", "count", "nunique"]}
selectedValue={metricColumnsType[1]}
onValueChange={(metric) => {
setMetricColumnsType([metricColumnsType[0], metric as AggregationType]);
}}
/>
<SingleSelector
title={
<Text className="pr-4 text-black">
Select the denominator metric column
</Text>
}
labels={getValidMetricColumns()}
values={getValidMetricColumns()}
selectedValue={metricColumns[1]}
onValueChange={(metric) => {
setMetricColumns([metricColumns[0], metric])
}}
/>

</>
);
Expand All @@ -77,8 +125,8 @@ const MetricConfig = (props: Props) => {
Select the metric type
</Text>
}
labels={["Sum", "Count", "Count Distinct", "Ratio"]}
values={["sum", "count", "count_distinct", "ratio"]}
labels={["Sum", "Count", "Distinct", "Ratio"]}
values={["sum", "count", "nunique", "ratio"]}
selectedValue={metricType}
onValueChange={(metric) => {
setMetricType(metric as AggregationType);
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/types/report-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ export type AggregationType = "sum" | "count" | "distinct" | "ratio";
export type TargetDirection = "increasing" | "decreasing";

export interface MetricColumn {
columnNames: string[];
aggregationOption: AggregationType;
columnNames: string[];
columnAggregationTypes?: AggregationType[];
expectedValue?: number;
}

Expand Down

0 comments on commit 772eb0a

Please sign in to comment.