all files / src/explore/components/controls/ AnnotationLayer.jsx

26.7% Statements 47/176
0% Branches 0/92
0% Functions 0/42
26.74% Lines 46/172
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
import React from 'react';
import PropTypes from 'prop-types';
import { CompactPicker } from 'react-color';
import { Button } from 'react-bootstrap';
 
import $ from 'jquery';
import mathjs from 'mathjs';
 
import SelectControl from './SelectControl';
import TextControl from './TextControl';
import CheckboxControl from './CheckboxControl';
 
import AnnotationTypes, {
  DEFAULT_ANNOTATION_TYPE,
  ANNOTATION_SOURCE_TYPES,
  getAnnotationSourceTypeLabels,
  getAnnotationTypeLabel,
  getSupportedSourceTypes,
  getSupportedAnnotationTypes,
  requiresQuery,
} from '../../../modules/AnnotationTypes';
 
import { ALL_COLOR_SCHEMES } from '../../../modules/colors';
import PopoverSection from '../../../components/PopoverSection';
import ControlHeader from '../ControlHeader';
import { nonEmpty } from '../../validators';
import vizTypes from '../../visTypes';
 
const AUTOMATIC_COLOR = '';
 
const propTypes = {
  name: PropTypes.string,
  annotationType: PropTypes.string,
  sourceType: PropTypes.string,
  color: PropTypes.string,
  opacity: PropTypes.string,
  style: PropTypes.string,
  width: PropTypes.number,
  value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
  overrides: PropTypes.object,
  show: PropTypes.bool,
  titleColumn: PropTypes.string,
  descriptionColumns: PropTypes.arrayOf(PropTypes.string),
  timeColumn: PropTypes.string,
  intervalEndColumn: PropTypes.string,
  vizType: PropTypes.string,
 
  error: PropTypes.string,
  colorScheme: PropTypes.string,
 
  addAnnotationLayer: PropTypes.func,
  removeAnnotationLayer: PropTypes.func,
  close: PropTypes.func,
};
 
const defaultProps = {
  name: '',
  annotationType: DEFAULT_ANNOTATION_TYPE,
  sourceType: '',
  color: AUTOMATIC_COLOR,
  opacity: '',
  style: 'solid',
  width: 1,
  overrides: {},
  colorScheme: 'd3Category10',
  show: true,
  titleColumn: '',
  descriptionColumns: [],
  timeColumn: '',
  intervalEndColumn: '',
 
  addAnnotationLayer: () => {},
  removeAnnotationLayer: () => {},
  close: () => {},
};
 
export default class AnnotationLayer extends React.PureComponent {
  constructor(props) {
    super(props);
    const { name, annotationType, sourceType,
      color, opacity, style, width, value,
      overrides, show, titleColumn, descriptionColumns,
      timeColumn, intervalEndColumn } = props;
    this.state = {
      // base
      name,
      oldName: !this.props.name ? null : name,
      annotationType,
      sourceType,
      value,
      overrides,
      show,
      // slice
      titleColumn,
      descriptionColumns,
      timeColumn,
      intervalEndColumn,
      // display
      color: color || AUTOMATIC_COLOR,
      opacity,
      style,
      width,
      // refData
      isNew: !this.props.name,
      isLoadingOptions: true,
      valueOptions: [],
      validationErrors: {},
    };
    this.submitAnnotation = this.submitAnnotation.bind(this);
    this.deleteAnnotation = this.deleteAnnotation.bind(this);
    this.applyAnnotation = this.applyAnnotation.bind(this);
    this.fetchOptions = this.fetchOptions.bind(this);
    this.handleAnnotationType = this.handleAnnotationType.bind(this);
    this.handleAnnotationSourceType =
        this.handleAnnotationSourceType.bind(this);
    this.handleValue = this.handleValue.bind(this);
    this.isValidForm = this.isValidForm.bind(this);
  }
 
  componentDidMount() {
    const { annotationType, sourceType, isLoadingOptions } = this.state;
    this.fetchOptions(annotationType, sourceType, isLoadingOptions);
  }
 
  componentDidUpdate(prevProps, prevState) {
    if (prevState.sourceType !== this.state.sourceType) {
      this.fetchOptions(this.state.annotationType, this.state.sourceType, true);
    }
  }
 
  isValidFormula(value, annotationType) {
    if (annotationType === AnnotationTypes.FORMULA) {
      try {
        mathjs.parse(value).compile().eval({ x: 0 });
      } catch (err) {
        return true;
      }
    }
    return false;
  }
 
  isValidForm() {
    const {
      name, annotationType, sourceType,
      value, timeColumn, intervalEndColumn,
    } = this.state;
    const errors = [nonEmpty(name), nonEmpty(annotationType), nonEmpty(value)];
    if (sourceType !== ANNOTATION_SOURCE_TYPES.NATIVE) {
      if (annotationType === AnnotationTypes.EVENT) {
        errors.push(nonEmpty(timeColumn));
      }
      if (annotationType === AnnotationTypes.INTERVAL) {
        errors.push(nonEmpty(timeColumn));
        errors.push(nonEmpty(intervalEndColumn));
      }
    }
    errors.push(this.isValidFormula(value, annotationType));
    return !errors.filter(x => x).length;
  }
 
 
  handleAnnotationType(annotationType) {
    this.setState({
      annotationType,
      sourceType: null,
      validationErrors: {},
      value: null,
    });
  }
 
  handleAnnotationSourceType(sourceType) {
    this.setState({
      sourceType,
      isLoadingOptions: true,
      validationErrors: {},
      value: null,
    });
  }
 
  handleValue(value) {
    this.setState({
      value,
      descriptionColumns: null,
      intervalEndColumn: null,
      timeColumn: null,
      titleColumn: null,
      overrides: { since: null, until: null },
    });
  }
 
  fetchOptions(annotationType, sourceType, isLoadingOptions) {
    if (isLoadingOptions === true) {
      if (sourceType === ANNOTATION_SOURCE_TYPES.NATIVE) {
        $.ajax({
          type: 'GET',
          url: '/annotationlayermodelview/api/read?',
        }).then((data) => {
          const layers = data ? data.result.map(layer => ({
            value: layer.id,
            label: layer.name,
          })) : [];
          this.setState({
            isLoadingOptions: false,
            valueOptions: layers,
          });
        });
      } else if (requiresQuery(sourceType)) {
        $.ajax({
          type: 'GET',
          url: '/superset/user_slices',
        }).then(data =>
          this.setState({
            isLoadingOptions: false,
            valueOptions: data.filter(
                x => getSupportedSourceTypes(annotationType)
                .find(v => v === x.viz_type))
                .map(x => ({ value: x.id, label: x.title, slice: x }),
              ),
          }),
        );
      } else {
        this.setState({
          isLoadingOptions: false,
          valueOptions: [],
        });
      }
    }
  }
 
  deleteAnnotation() {
    this.props.close();
    if (!this.state.isNew) {
      this.props.removeAnnotationLayer(this.state);
    }
  }
 
  applyAnnotation() {
    if (this.state.name.length) {
      const annotation = {};
      Object.keys(this.state).forEach((k) => {
        if (this.state[k] !== null) {
          annotation[k] = this.state[k];
        }
      });
      delete annotation.isNew;
      delete annotation.valueOptions;
      delete annotation.isLoadingOptions;
      delete annotation.validationErrors;
      annotation.color = annotation.color === AUTOMATIC_COLOR ? null : annotation.color;
      this.props.addAnnotationLayer(annotation);
      this.setState({ isNew: false, oldName: this.state.name });
    }
  }
 
  submitAnnotation() {
    this.applyAnnotation();
    this.props.close();
  }
 
  renderValueConfiguration() {
    const { annotationType, sourceType, value,
      valueOptions, isLoadingOptions } = this.state;
    let label = '';
    let description = '';
    if (requiresQuery(sourceType)) {
      if (sourceType === ANNOTATION_SOURCE_TYPES.NATIVE) {
        label = 'Annotation Layer';
        description = 'Select the Annotation Layer you would like to use.';
      } else {
        label = 'Slice';
        description = `Use a pre defined Superset Slice as a source for annotations and overlays. 
        'your chart must be one of these visualization types:
        '[${getSupportedSourceTypes(annotationType)
            .map(x => vizTypes[x].label).join(', ')}]'`;
      }
    } else if (annotationType === AnnotationTypes.FORMULA) {
      label = 'Formula';
      description = `Expects a formula with depending time parameter 'x'
        in milliseconds since epoch. mathjs is used to evaluate the formulas.
        Example: '2x+5'`;
    }
    if (requiresQuery(sourceType)) {
      return (
        <SelectControl
          name="annotation-layer-value"
          showHeader
          hovered
          description={description}
          label={label}
          placeholder=""
          options={valueOptions}
          isLoading={isLoadingOptions}
          value={value}
          onChange={this.handleValue}
          validationErrors={!value ? ['Mandatory'] : []}
        />
      );
    } if (annotationType === AnnotationTypes.FORMULA) {
      return (
        <TextControl
          name="annotation-layer-value"
          hovered
          showHeader
          description={description}
          label={label}
          placeholder=""
          value={value}
          onChange={this.handleValue}
          validationErrors={this.isValidFormula(value, annotationType) ? ['Bad formula.'] : []}
        />
      );
    }
    return '';
  }
 
  renderSliceConfiguration() {
    const { annotationType, sourceType, value, valueOptions, overrides, titleColumn,
      timeColumn, intervalEndColumn, descriptionColumns } = this.state;
    const slice = (valueOptions.find(x => x.value === value) || {}).slice;
    if (sourceType !== ANNOTATION_SOURCE_TYPES.NATIVE && slice) {
      const columns = (slice.data.groupby || []).concat(
        (slice.data.all_columns || [])).map(x => ({ value: x, label: x }));
      const timeColumnOptions = slice.data.include_time ?
        [{ value: '__timestamp', label: '__timestamp' }].concat(columns) : columns;
      return (
        <div style={{ marginRight: '2rem' }}>
          <PopoverSection
            isSelected
            onSelect={() => {
            }}
            title="Annotation Slice Configuration"
            info={
              `This section allows you to configure how to use the slice
               to generate annotations.`
            }
          >
            {
              (
                annotationType === AnnotationTypes.EVENT ||
                annotationType === AnnotationTypes.INTERVAL
              ) &&
              <SelectControl
                hovered
                name="annotation-layer-time-column"
                label={
                  annotationType === AnnotationTypes.INTERVAL ?
                    'Interval Start column' : 'Event Time Column'
                }
                description={'This column must contain date/time information.'}
                validationErrors={!timeColumn ? ['Mandatory'] : []}
                clearable={false}
                options={timeColumnOptions}
                value={timeColumn}
                onChange={v => this.setState({ timeColumn: v })}
              />
            }
            {
              annotationType === AnnotationTypes.INTERVAL &&
              <SelectControl
                hovered
                name="annotation-layer-intervalEnd"
                label="Interval End column"
                description={'This column must contain date/time information.'}
                validationErrors={!intervalEndColumn ? ['Mandatory'] : []}
                options={columns}
                value={intervalEndColumn}
                onChange={v => this.setState({ intervalEndColumn: v })}
              />
            }
            <SelectControl
              hovered
              name="annotation-layer-title"
              label="Title Column"
              description={'Pick a title for you annotation.'}
              options={
                [{ value: '', label: 'None' }].concat(columns)
              }
              value={titleColumn}
              onChange={v => this.setState({ titleColumn: v })}
            />
            {
              annotationType !== AnnotationTypes.TIME_SERIES &&
              <SelectControl
                hovered
                name="annotation-layer-title"
                label="Description Columns"
                description={`Pick one or more columns that should be shown in the
                  annotation. If you don't select a column all of them will be shown.`}
                multi
                options={
                  columns
                }
                value={descriptionColumns}
                onChange={v => this.setState({ descriptionColumns: v })}
              />
            }
            <div style={{ marginTop: '1rem' }}>
              <CheckboxControl
                hovered
                name="annotation-override-since"
                label="Override 'Since'"
                description={`This controls whether the "Since" field from the current
                  view should be passed down to the chart containing the annotation data.`}
                value={!!Object.keys(overrides).find(x => x === 'since')}
                onChange={(v) => {
                  delete overrides.since;
                  if (v) {
                    this.setState({ overrides: { ...overrides, since: null } });
                  } else {
                    this.setState({ overrides: { ...overrides } });
                  }
                }}
              />
              <CheckboxControl
                hovered
                name="annotation-override-until"
                label="Override 'Until'"
                description={`This controls whether the "Until" field from the current
                  view should be passed down to the chart containing the annotation data.`}
                value={!!Object.keys(overrides).find(x => x === 'until')}
                onChange={(v) => {
                  delete overrides.until;
                  if (v) {
                    this.setState({ overrides: { ...overrides, until: null } });
                  } else {
                    this.setState({ overrides: { ...overrides } });
                  }
                }}
              />
              <CheckboxControl
                hovered
                name="annotation-override-timegrain"
                label="Override time grain"
                description={`This controls whether the time grain field from the current
                  view should be passed down to the chart containing the annotation data.`}
                value={!!Object.keys(overrides).find(x => x === 'time_grain_sqla')}
                onChange={(v) => {
                  delete overrides.time_grain_sqla;
                  delete overrides.granularity;
                  if (v) {
                    this.setState({
                      overrides: {
                        ...overrides,
                        time_grain_sqla: null,
                        granularity: null,
                      },
                    });
                  } else {
                    this.setState({ overrides: { ...overrides } });
                  }
                }}
              />
              <TextControl
                hovered
                name="annotation-layer-timeshift"
                label="Time Shift"
                description={`Time delta in natural language
                  (example:  24 hours, 7 days, 56 weeks, 365 days)`}
                placeholder=""
                value={overrides.time_shift}
                onChange={v => this.setState({ overrides: { ...overrides, time_shift: v } })}
              />
            </div>
          </PopoverSection>
        </div>
      );
    }
    return ('');
  }
 
  renderDisplayConfiguration() {
    const { color, opacity, style, width } = this.state;
    const colorScheme = [...ALL_COLOR_SCHEMES[this.props.colorScheme]];
    if (color && color !== AUTOMATIC_COLOR &&
      !colorScheme.find(x => x.toLowerCase() === color.toLowerCase())) {
      colorScheme.push(color);
    }
    return (
      <PopoverSection
        isSelected
        onSelect={() => {}}
        title="Display configuration"
        info="Configure your how you overlay is displayed here."
      >
        <SelectControl
          name="annotation-layer-stroke"
          label="Style"
            // see '../../../visualizations/nvd3_vis.css'
          options={[
              { value: 'solid', label: 'Solid' },
              { value: 'dashed', label: 'Dashed' },
              { value: 'longDashed', label: 'Long Dashed' },
              { value: 'dotted', label: 'Dotted' },
          ]}
          value={style}
          onChange={v => this.setState({ style: v })}
        />
        <SelectControl
          name="annotation-layer-opacity"
          label="Opacity"
            // see '../../../visualizations/nvd3_vis.css'
          options={[
              { value: '', label: 'Solid' },
              { value: 'opacityLow', label: '0.2' },
              { value: 'opacityMedium', label: '0.5' },
              { value: 'opacityHigh', label: '0.8' },
          ]}
          value={opacity}
          onChange={v => this.setState({ opacity: v })}
        />
        <div>
          <ControlHeader label="Color" />
          <div style={{ display: 'flex', flexDirection: 'column' }}>
            <CompactPicker
              color={color}
              colors={colorScheme}
              onChangeComplete={v => this.setState({ color: v.hex })}
            />
            <Button
              style={{ marginTop: '0.5rem', marginBottom: '0.5rem' }}
              bsStyle={color === AUTOMATIC_COLOR ? 'success' : 'default'}
              bsSize="xsmall"
              onClick={() => this.setState({ color: AUTOMATIC_COLOR })}
            >
              Automatic Color
            </Button>
          </div>
        </div>
        <TextControl
          name="annotation-layer-stroke-width"
          label="Line Width"
          isInt
          value={width}
          onChange={v => this.setState({ width: v })}
        />
      </PopoverSection>
    );
  }
 
  render() {
    const { isNew, name, annotationType,
      sourceType, show } = this.state;
    const isValid = this.isValidForm();
    return (
      <div>
        {
          this.props.error &&
          <span style={{ color: 'red' }}>
            ERROR: {this.props.error}
          </span>
        }
        <div style={{ display: 'flex', flexDirection: 'row' }}>
          <div style={{ marginRight: '2rem' }}>
            <PopoverSection
              isSelected
              onSelect={() => {}}
              title="Layer Configuration"
              info="Configure the basics of your Annotation Layer."
            >
              <TextControl
                name="annotation-layer-name"
                label="Name"
                placeholder=""
                value={name}
                onChange={v => this.setState({ name: v })}
                validationErrors={!name ? ['Mandatory'] : []}
              />
              <CheckboxControl
                name="annotation-layer-hide"
                label="Hide Layer"
                value={!show}
                onChange={v => this.setState({ show: !v })}
              />
              <SelectControl
                hovered
                description="Choose the Annotation Layer Type"
                label="Annotation Layer Type"
                name="annotation-layer-type"
                options={getSupportedAnnotationTypes(this.props.vizType).map(
                    x => ({ value: x, label: getAnnotationTypeLabel(x) }))}
                value={annotationType}
                onChange={this.handleAnnotationType}
              />
              {!!getSupportedSourceTypes(annotationType).length &&
                <SelectControl
                  hovered
                  description="Choose the source of your annotations"
                  label="Annotation Source"
                  name="annotation-source-type"
                  options={getSupportedSourceTypes(annotationType).map(
                        x => ({ value: x, label: getAnnotationSourceTypeLabels(x) }))}
                  value={sourceType}
                  onChange={this.handleAnnotationSourceType}
                />
              }
              { this.renderValueConfiguration() }
            </PopoverSection>
          </div>
          { this.renderSliceConfiguration() }
          { this.renderDisplayConfiguration() }
        </div>
        <div style={{ display: 'flex', justifyContent: 'space-between' }}>
          <Button
            bsSize="sm"
            onClick={this.deleteAnnotation}
          >
            { !isNew ? 'Remove' : 'Cancel' }
          </Button>
          <div>
            <Button
              bsSize="sm"
              disabled={!isValid}
              onClick={this.applyAnnotation}
            >
              Apply
            </Button>
 
            <Button
              bsSize="sm"
              disabled={!isValid}
              onClick={this.submitAnnotation}
            >
              OK
            </Button>
          </div>
        </div>
      </div>
    );
  }
}
AnnotationLayer.propTypes = propTypes;
AnnotationLayer.defaultProps = defaultProps;