You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by vo...@apache.org on 2024/05/08 21:15:53 UTC

(druid) 03/04: add abs

This is an automated email from the ASF dual-hosted git repository.

vogievetsky pushed a commit to branch sod
in repository https://gitbox.apache.org/repos/asf/druid.git

commit 5fe3eb9cf25c9fc6bad55165711dc5c85495b1e9
Author: Vadim Ogievetsky <va...@ogievetsky.com>
AuthorDate: Tue May 7 15:13:25 2024 -0700

    add abs
---
 .../explore-view/modules/table-react-module.tsx    | 86 +++++++++++++++-------
 1 file changed, 60 insertions(+), 26 deletions(-)

diff --git a/web-console/src/views/explore-view/modules/table-react-module.tsx b/web-console/src/views/explore-view/modules/table-react-module.tsx
index efdc922217f..e03d3e90851 100644
--- a/web-console/src/views/explore-view/modules/table-react-module.tsx
+++ b/web-console/src/views/explore-view/modules/table-react-module.tsx
@@ -232,18 +232,23 @@ export default typedVisualModule({
       },
     },
 
-    showDelta: {
-      type: 'boolean',
-      control: {
-        visible: ({ params }) => Boolean((params.compares || []).length),
-      },
-    },
-    showPercent: {
-      type: 'boolean',
+    compareTypes: {
+      type: 'options',
+      options: ['value', 'delta', 'absDelta', 'percent', 'absPercent'],
+      default: ['value', 'delta'],
       control: {
-        visible: ({ params }) => Boolean((params.compares || []).length),
+        label: 'Compare types',
+        visible: ({ params }) => Boolean((params.compares || []).length) && !params.pivotColumn,
+        optionLabels: {
+          value: 'Value',
+          delta: 'Delta',
+          absDelta: 'Abs. delta',
+          percent: 'Percent',
+          absPercent: 'Abs. percent',
+        },
       },
     },
+
     maxRows: {
       type: 'number',
       default: 200,
@@ -319,8 +324,7 @@ function TableModule(props: TableModuleProps) {
     const pivotColumn: ExpressionMeta = parameterValues.pivotColumn;
     const metrics: ExpressionMeta[] = parameterValues.metrics;
     const compares: string[] = parameterValues.compares || [];
-    const showDelta: boolean = parameterValues.showDelta;
-    const showPercent: boolean = parameterValues.showPercent;
+    const compareTypes: CompareType[] = parameterValues.compareTypes;
     const maxRows: number = parameterValues.maxRows;
 
     const pivotValues = pivotColumn ? pivotValueState.data : undefined;
@@ -329,14 +333,16 @@ function TableModule(props: TableModuleProps) {
     const effectiveOrderBy =
       orderBy || C(metrics[0]?.name || splitColumns[0]?.name).toOrderByExpression('DESC');
 
-    const hasCompare = Boolean(compares.length);
+    const hasCompare = !pivotColumn && Boolean(compares.length) && Boolean(compareTypes.length);
 
     const orderByColumnName = (effectiveOrderBy.expression as SqlColumn).getName();
     let orderByCompareMeasure: string | undefined;
     let orderByCompareDuration: string | undefined;
     let orderByCompareType: CompareType | undefined;
     if (hasCompare) {
-      const m = orderByColumnName.match(/^(.+):cmp:([^:]+):(value|delta|percent)$/);
+      const m = orderByColumnName.match(
+        /^(.+):cmp:([^:]+):(value|delta|absDelta|percent|absPercent)$/,
+      );
       if (m) {
         orderByCompareMeasure = m[1];
         orderByCompareDuration = m[2];
@@ -356,9 +362,9 @@ function TableModule(props: TableModuleProps) {
           q.addSelect(toShowColumnExpression(showColumn, multipleValueMode)),
         ),
       )
-      .applyForEach(pivotValues || [''], (q, pivotValue) =>
+      .applyForEach(pivotValues || [''], (q, pivotValue, i) =>
         q.applyForEach(metrics, (q, metric) => {
-          const alias = `${metric.name}${pivotColumn ? `:${pivotValue}` : ''}`;
+          const alias = `${metric.name}${pivotColumn && i > 0 ? `:${pivotValue}` : ''}`;
           if (pivotColumn) {
             columnHints.set(alias, { displayName: metric.name, group: pivotValue });
           }
@@ -449,27 +455,43 @@ function TableModule(props: TableModuleProps) {
                   .applyIf(NEEDS_GROUPING_TO_ORDER, anyValue)
                   .applyIf(compare !== orderByCompareDuration, coalesce0);
 
-                const valueName = `${metric.name}:cmp:${compare}:value`;
-                columnHints.set(valueName, {
-                  group: `Comparison to ${compare}`,
-                  displayName: `${metric.name} (value)`,
-                });
-                const ret = [c.as(valueName)];
                 const mainMetric = main
                   .column(metric.name)
                   .applyIf(NEEDS_GROUPING_TO_ORDER, anyValue)
-                  .applyIf(orderByCompareDuration, ex => F('COALESCE', ex, 0));
+                  .applyIf(orderByCompareDuration, coalesce0);
+
+                const diff = SqlExpression.parse(`${mainMetric} - ${c}`);
+
+                const ret: SqlExpression[] = [];
+
+                if (compareTypes.includes('value')) {
+                  const valueName = `${metric.name}:cmp:${compare}:value`;
+                  columnHints.set(valueName, {
+                    group: `Comparison to ${compare}`,
+                    displayName: `${metric.name} (value)`,
+                  });
+                  ret.push(c.as(valueName));
+                }
 
-                if (showDelta) {
+                if (compareTypes.includes('delta')) {
                   const deltaName = `${metric.name}:cmp:${compare}:delta`;
                   columnHints.set(deltaName, {
                     group: `Comparison to ${compare}`,
                     displayName: `${metric.name} (delta)`,
                   });
-                  ret.push(SqlExpression.parse(`${mainMetric} - ${c}`).as(deltaName));
+                  ret.push(diff.as(deltaName));
                 }
 
-                if (showPercent) {
+                if (compareTypes.includes('absDelta')) {
+                  const deltaName = `${metric.name}:cmp:${compare}:absDelta`;
+                  columnHints.set(deltaName, {
+                    group: `Comparison to ${compare}`,
+                    displayName: `${metric.name} (Abs. delta)`,
+                  });
+                  ret.push(F('ABS', diff).as(deltaName));
+                }
+
+                if (compareTypes.includes('percent')) {
                   const percentName = `${metric.name}:cmp:${compare}:percent`;
                   columnHints.set(percentName, {
                     group: `Comparison to ${compare}`,
@@ -477,7 +499,19 @@ function TableModule(props: TableModuleProps) {
                     formatter: formatPercent,
                   });
                   ret.push(
-                    F('SAFE_DIVIDE', SqlExpression.parse(`(${mainMetric} - ${c}) * 1.0`), c).as(
+                    F('SAFE_DIVIDE', SqlExpression.parse(`(${diff}) * 1.0`), c).as(percentName),
+                  );
+                }
+
+                if (compareTypes.includes('absPercent')) {
+                  const percentName = `${metric.name}:cmp:${compare}:absPercent`;
+                  columnHints.set(percentName, {
+                    group: `Comparison to ${compare}`,
+                    displayName: `${metric.name} (abs. %)`,
+                    formatter: formatPercent,
+                  });
+                  ret.push(
+                    F('ABS', F('SAFE_DIVIDE', SqlExpression.parse(`(${diff}) * 1.0`), c)).as(
                       percentName,
                     ),
                   );


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org