all files / src/explore/ AdhocFilter.js

91.67% Statements 44/48
77.27% Branches 34/44
100% Functions 7/7
91.3% Lines 42/46
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110                                                     53× 53× 36× 36× 36× 36× 36× 17× 17×     17× 17× 17× 17×   53×   53×       18×                         29×             16× 11×                                  
import { MULTI_OPERATORS } from './constants';
 
export const EXPRESSION_TYPES = {
  SIMPLE: 'SIMPLE',
  SQL: 'SQL',
};
 
export const CLAUSES = {
  HAVING: 'HAVING',
  WHERE: 'WHERE',
};
 
const OPERATORS_TO_SQL = {
  '==': '=',
  '!=': '<>',
  '>': '>',
  '<': '<',
  '>=': '>=',
  '<=': '<=',
  in: 'in',
  'not in': 'not in',
  LIKE: 'like',
};
 
function translateToSql(adhocMetric, { useSimple } = {}) {
  if (EadhocMetric.expressionType === EXPRESSION_TYPES.SIMPLE || useSimple) {
    const isMulti = MULTI_OPERATORS.indexOf(adhocMetric.operator) >= 0;
    const subject = adhocMetric.subject;
    const operator = OPERATORS_TO_SQL[adhocMetric.operator];
    const comparator = isMulti ? adhocMetric.comparator.join("','") : adhocMetric.comparator;
    return `${subject} ${operator} ${isMulti ? '(\'' : ''}${comparator}${isMulti ? '\')' : ''}`;
  } else if (adhocMetric.expressionType === EXPRESSION_TYPES.SQL) {
    return adhocMetric.sqlExpression;
  }
  return '';
}
 
export default class AdhocFilter {
  constructor(adhocFilter) {
    this.expressionType = adhocFilter.expressionType || EXPRESSION_TYPES.SIMPLE;
    if (this.expressionType === EXPRESSION_TYPES.SIMPLE) {
      this.subject = adhocFilter.subject;
      this.operator = adhocFilter.operator;
      this.comparator = adhocFilter.comparator;
      this.clause = adhocFilter.clause;
      this.sqlExpression = null;
    } else Eif (this.expressionType === EXPRESSION_TYPES.SQL) {
      this.sqlExpression = typeof adhocFilter.sqlExpression === 'string' ?
        adhocFilter.sqlExpression :
        translateToSql(adhocFilter, { useSimple: true });
      this.clause = adhocFilter.clause;
      this.subject = null;
      this.operator = null;
      this.comparator = null;
    }
    this.fromFormData = !!adhocFilter.filterOptionName;
 
    this.filterOptionName = adhocFilter.filterOptionName ||
      `filter_${Math.random().toString(36).substring(2, 15)}_${Math.random().toString(36).substring(2, 15)}`;
  }
 
  duplicateWith(nextFields) {
    return new AdhocFilter({
      ...this,
      expressionType: this.expressionType,
      subject: this.subject,
      operator: this.operator,
      clause: this.clause,
      sqlExpression: this.sqlExpression,
      fromFormData: this.fromFormData,
      filterOptionName: this.filterOptionName,
      ...nextFields,
    });
  }
 
  equals(adhocFilter) {
    return adhocFilter.expressionType === this.expressionType &&
      adhocFilter.sqlExpression === this.sqlExpression &&
      adhocFilter.operator === this.operator &&
      adhocFilter.comparator === this.comparator &&
      adhocFilter.subject === this.subject;
  }
 
  isValid() {
    if (this.expressionType === EXPRESSION_TYPES.SIMPLE) {
      return !!(
        this.operator &&
        this.subject &&
        this.comparator &&
        this.comparator.length > 0 &&
        this.clause
      );
    } else Eif (this.expressionType === EXPRESSION_TYPES.SQL) {
      return !!(this.sqlExpression && this.clause);
    }
    return false;
  }
 
  getDefaultLabel() {
    const label = this.translateToSql();
    return label.length < 43 ?
      label :
      label.substring(0, 40) + '...';
  }
 
  translateToSql() {
    return translateToSql(this);
  }
}