All files / src/dialogs/retention-dialog retention-dialog.tsx

57.5% Statements 46/80
35.71% Branches 10/28
33.33% Functions 7/21
57.58% Lines 38/66

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                    8x 8x 8x 8x   8x 8x 8x 8x   8x   8x 5x   5x   5x   5x                                 8x             1x   1x         1x         1x 1x             8x 1x 1x     1x             1x                         1x                   8x                     1x 2x 2x   2x                         1x               1x                           8x 2x 2x   2x                                                                         8x  
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import { Button, FormGroup } from '@blueprintjs/core';
import { IconNames } from '@blueprintjs/icons';
import axios from 'axios';
import React from 'react';
 
import { ExternalLink, RuleEditor } from '../../components';
import { QueryManager } from '../../utils';
import { DRUID_DOCS_VERSION } from '../../variables';
import { SnitchDialog } from '../snitch-dialog/snitch-dialog';
 
import './retention-dialog.scss';
 
export function reorderArray<T>(items: T[], oldIndex: number, newIndex: number): T[] {
  const newItems = items.concat();
 
  if (newIndex > oldIndex) newIndex--;
 
  newItems.splice(newIndex, 0, newItems.splice(oldIndex, 1)[0]);
 
  return newItems;
}
 
export interface RetentionDialogProps {
  datasource: string;
  rules: any[];
  tiers: string[];
  onEditDefaults: () => void;
  onCancel: () => void;
  onSave: (datasource: string, newRules: any[], comment: string) => void;
}
 
export interface RetentionDialogState {
  currentRules: any[];
  historyRecords: any[];
}
 
export class RetentionDialog extends React.PureComponent<
  RetentionDialogProps,
  RetentionDialogState
> {
  private historyQueryManager: QueryManager<string, any>;
 
  constructor(props: RetentionDialogProps) {
    super(props);
 
    this.state = {
      currentRules: props.rules,
      historyRecords: [],
    };
 
    this.historyQueryManager = new QueryManager({
      processQuery: async datasource => {
        const historyResp = await axios(`/druid/coordinator/v1/rules/${datasource}/history`);
        return historyResp.data;
      },
      onStateChange: ({ result }) => {
        this.setState({
          historyRecords: result,
        });
      },
    });
  }
 
  componentDidMount() {
    const { datasource } = this.props;
    this.historyQueryManager.runQuery(datasource);
  }
 
  private save = (comment: string) => {
    const { datasource, onSave } = this.props;
    const { currentRules } = this.state;
 
    onSave(datasource, currentRules, comment);
  };
 
  private changeRule = (newRule: any, index: number) => {
    const { currentRules } = this.state;
 
    const newRules = (currentRules || []).map((r, i) => {
      if (i === index) return newRule;
      return r;
    });
 
    this.setState({
      currentRules: newRules,
    });
  };
 
  onDeleteRule = (index: number) => {
    const { currentRules } = this.state;
 
    const newRules = (currentRules || []).filter((_r, i) => i !== index);
 
    this.setState({
      currentRules: newRules,
    });
  };
 
  moveRule(index: number, offset: number) {
    const { currentRules } = this.state;
    if (!currentRules) return;
 
    const newIndex = index + offset;
 
    this.setState({
      currentRules: reorderArray(currentRules, index, newIndex),
    });
  }
 
  renderRule = (rule: any, index: number) => {
    const { tiers } = this.props;
    const { currentRules } = this.state;
 
    return (
      <RuleEditor
        rule={rule}
        tiers={tiers}
        key={index}
        onChange={r => this.changeRule(r, index)}
        onDelete={() => this.onDeleteRule(index)}
        moveUp={index > 0 ? () => this.moveRule(index, -1) : null}
        moveDown={index < (currentRules || []).length - 1 ? () => this.moveRule(index, 2) : null}
      />
    );
  };
 
  reset = () => {
    const { rules } = this.props;
 
    this.setState({
      currentRules: rules.concat(),
    });
  };
 
  addRule = () => {
    const { tiers } = this.props;
    const { currentRules } = this.state;
 
    const newRules = (currentRules || []).concat({
      type: 'loadForever',
      tieredReplicants: { [tiers[0]]: 2 },
    });
 
    this.setState({
      currentRules: newRules,
    });
  };
 
  render(): JSX.Element {
    const { datasource, onCancel, onEditDefaults } = this.props;
    const { currentRules, historyRecords } = this.state;
 
    return (
      <SnitchDialog
        className="retention-dialog"
        saveDisabled={false}
        onClose={onCancel}
        title={`Edit retention rules: ${datasource}${
          datasource === '_default' ? ' (cluster defaults)' : ''
        }`}
        onReset={this.reset}
        onSave={this.save}
        historyRecords={historyRecords}
      >
        <p>
          Druid uses rules to determine what data should be retained in the cluster. The rules are
          evaluated in order from top to bottom. For more information please refer to the{' '}
          <ExternalLink
            href={`https://druid.apache.org/docs/${DRUID_DOCS_VERSION}/operations/rule-configuration.html`}
          >
            documentation
          </ExternalLink>
          .
        </p>
        <FormGroup>{(currentRules || []).map(this.renderRule)}</FormGroup>
        <FormGroup className="right">
          <Button icon={IconNames.PLUS} onClick={this.addRule}>
            New rule
          </Button>
        </FormGroup>
        {!currentRules.length && datasource !== '_default' && (
          <p>
            This datasource currently has no rules, it will use the cluster defaults (
            <a onClick={onEditDefaults}>edit cluster defaults</a>)
          </p>
        )}
      </SnitchDialog>
    );
  }
}