<commit_before>import * as React from 'react';
import { get } from 'lodash';
import { Formik, Form, FormikProps, FormikErrors, FormikActions, FormikTouched, Field } from 'formik';
import { Modal } from 'react-bootstrap';

import {
  noop,
  IModalComponentProps,
  ReactModal,
  Application,
  ModalClose,
  SubmitButton,
  TetheredSelect,
  HelpField,
  MapEditor,
  HoverablePopover,
} from '@spinnaker/core';

import { ICanaryConfigSummary, IKayentaAccount, KayentaAccountType, ICanaryExecutionRequest } from '../domain';
import { CanaryScores } from '../components/canaryScores';
import { startCanaryRun } from '../service/canaryRun.service';

const RESOURCE_TYPES = {
  prometheus: [
    { label: '', value: '' },
    { label: 'gce_instance', value: 'gce_instance' },
    { label: 'aws_ec2_instance', value: 'aws_ec2_instance' },
  ],
  stackdriver: [
    { label: 'gce_instance', value: 'gce_instance' },
    { label: 'aws_ec2_instance', value: 'aws_ec2_instance' },
    { label: 'gae_app', value: 'gae_app' },
    { label: 'k8s_container', value: 'k8s_container' },
    { label: 'k8s_pod', value: 'k8s_pod' },
    { label: 'k8s_node', value: 'k8s_node' },
    { label: 'gke_container', value: 'gke_container' },
    { label: 'https_lb_rule', value: 'https_lb_rule' },
    { label: 'global', value: 'global' },
  ],
};

export interface IManualAnalysisModalProps extends IModalComponentProps {
  title: string;
  application: Application;
  accounts: IKayentaAccount[];
  initialValues?: IManualAnalysisModalFormProps;
}

export interface IManualAnalysisModalState {
  showAllControlLocations: boolean;
  showAllExperimentLocations: boolean;
}

export interface IManualAnalysisModalFormProps {
  configId: string;
  startTime: string;
  endTime: string;
  step: string;
  baselineScope: string;
  canaryScope: string;
  baselineLocation: string;
  canaryLocation: string;
  extendedScopeParams: { [key: string]: string };
  resourceType: string;
  marginalThreshold: string;
  passThreshold: string;
  metricsAccountName: string;
  storageAccountName: string;
}

const transformFormPropsToExecutionRequest = (
  values: IManualAnalysisModalFormProps,
  accounts: IKayentaAccount[],
): ICanaryExecutionRequest => {
  const step = parseInt(values.step, 10);
  const pass = parseInt(values.passThreshold, 10);
  const marginal = parseInt(values.marginalThreshold, 10);

  const metricsAccount = accounts.find(({ name }) => values.metricsAccountName === name);

  const accountSpecificParams: { [param: string]: string } = {};

  if (values.resourceType) {
    accountSpecificParams.resourceType = values.resourceType;
  }

  if (metricsAccount.type === 'atlas') {
    accountSpecificParams.type = 'cluster';
  }

  const extendedScopeParams = { ...values.extendedScopeParams, ...accountSpecificParams };

  return {
    scopes: {
      default: {
        controlScope: {
          scope: values.baselineScope,
          start: values.startTime,
          end: values.endTime,
          location: values.baselineLocation,
          step,
          extendedScopeParams,
        },
        experimentScope: {<commit_msg>fix(rerun): do not override scope type when re-running analysis (#543)<commit_after>import * as React from 'react';
import { get } from 'lodash';
import { Formik, Form, FormikProps, FormikErrors, FormikActions, FormikTouched, Field } from 'formik';
import { Modal } from 'react-bootstrap';

import {
  noop,
  IModalComponentProps,
  ReactModal,
  Application,
  ModalClose,
  SubmitButton,
  TetheredSelect,
  HelpField,
  MapEditor,
  HoverablePopover,
} from '@spinnaker/core';

import { ICanaryConfigSummary, IKayentaAccount, KayentaAccountType, ICanaryExecutionRequest } from '../domain';
import { CanaryScores } from '../components/canaryScores';
import { startCanaryRun } from '../service/canaryRun.service';

const RESOURCE_TYPES = {
  prometheus: [
    { label: '', value: '' },
    { label: 'gce_instance', value: 'gce_instance' },
    { label: 'aws_ec2_instance', value: 'aws_ec2_instance' },
  ],
  stackdriver: [
    { label: 'gce_instance', value: 'gce_instance' },
    { label: 'aws_ec2_instance', value: 'aws_ec2_instance' },
    { label: 'gae_app', value: 'gae_app' },
    { label: 'k8s_container', value: 'k8s_container' },
    { label: 'k8s_pod', value: 'k8s_pod' },
    { label: 'k8s_node', value: 'k8s_node' },
    { label: 'gke_container', value: 'gke_container' },
    { label: 'https_lb_rule', value: 'https_lb_rule' },
    { label: 'global', value: 'global' },
  ],
};

export interface IManualAnalysisModalProps extends IModalComponentProps {
  title: string;
  application: Application;
  accounts: IKayentaAccount[];
  initialValues?: IManualAnalysisModalFormProps;
}

export interface IManualAnalysisModalState {
  showAllControlLocations: boolean;
  showAllExperimentLocations: boolean;
}

export interface IManualAnalysisModalFormProps {
  configId: string;
  startTime: string;
  endTime: string;
  step: string;
  baselineScope: string;
  canaryScope: string;
  baselineLocation: string;
  canaryLocation: string;
  extendedScopeParams: { [key: string]: string };
  resourceType: string;
  marginalThreshold: string;
  passThreshold: string;
  metricsAccountName: string;
  storageAccountName: string;
}

const transformFormPropsToExecutionRequest = (
  values: IManualAnalysisModalFormProps,
  accounts: IKayentaAccount[],
): ICanaryExecutionRequest => {
  const step = parseInt(values.step, 10);
  const pass = parseInt(values.passThreshold, 10);
  const marginal = parseInt(values.marginalThreshold, 10);

  const metricsAccount = accounts.find(({ name }) => values.metricsAccountName === name);

  const accountSpecificParams: { [param: string]: string } = {};

  if (values.resourceType) {
    accountSpecificParams.resourceType = values.resourceType;
  }

  if (metricsAccount.type === 'atlas' && !values.extendedScopeParams?.type) {
    accountSpecificParams.type = 'cluster';
  }

  const extendedScopeParams = { ...values.extendedScopeParams, ...accountSpecificParams };

  return {
    scopes: {
      default: {
        controlScope: {
          scope: values.baselineScope,
          start: values.startTime,
          end: values.endTime,
          location: values.baselineLocation,
          step,
          extendedScopeParams,
        },
        experimentScope: {
{'fraction_non_alphanumeric': 0.12235554152194505, 'fraction_numerical': 0.005525734133249131, 'mean_word_length': 4.292397660818714, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3743346', 'n_tokens_mistral': 2006, 'n_tokens_neox': 1958, 'n_words': 434}