<commit_before>import Ember from 'ember';
import fetch from 'fetch';
import moment from 'moment';
import RSVP from 'rsvp';
import _ from 'lodash';
import { checkStatus } from 'thirdeye-frontend/helpers/utils';

/**
 * Parses stringified object from payload
 * @param {String} filters
 * @returns {Object}
 */
const parseProps = (filters) => {
  filters = filters || '';

  return filters.split(';')
    .filter(prop => prop)
    .map(prop => prop.split('='))
    .reduce(function (aggr, prop) {
      const [ propName, value ] = prop;
      aggr[propName] = value;

      return aggr;
    }, {});
};

export default Ember.Route.extend({
  model(params) {
    const { alertId: id } = params;
    if (!id) { return; }
    const alertUrl = `/onboard/function/${id}`;

    return RSVP.hash({
      function: fetch(alertUrl).then(checkStatus)
    });
  },

  afterModel(model) {
    const {
      metric: metricName,
      collection: dataset,
      filters,
      bucketUnit: granularity,
      id
     } = model.function;

    let metricId = '';
    let allGroupNames = [];
    let allGroups = [];

    return fetch(`/data/autocomplete/metric?name=${dataset}::${metricName}`).then(checkStatus)
      .then((metricsByName) => {
        const metric = metricsByName.pop();
        metricId = metric.id;
        return fetch(`/data/maxDataTime/metricId/${metricId}`).then(checkStatus);
      })
      .then((maxTime) => {
        const currentEnd = moment(maxTime).isValid()
          ? moment(maxTime).valueOf()
          : moment().subtract(1, 'day').endOf('day').valueOf();
        const formattedFilters = JSON.stringify(parseProps(filters));
        const dimension = 'All';
        const currentStart = moment(currentEnd).subtract(1, 'months').valueOf();
        const baselineStart = moment(currentStart).subtract(1, 'week').valueOf();
        const baselineEnd = moment(currentEnd).subtract(1, 'week');
        const metricDataUrl =  `/timeseries/compare/${metricId}/${currentStart}/${currentEnd}/` +
          `${baselineStart}/${baselineEnd}?dimension=${dimension}&granularity=${granularity}` +
          `&filters=${encodeURIComponent(formattedFilters)}`;
        return fetch(metricDataUrl).then(checkStatus);
      })
      .then((metricData) => {
        Object.assign(metricData, { color: 'blue' })
        Object.assign(model, { metricData });
        return fetch(`/thirdeye/entity/ALERT_CONFIG`).then(checkStatus);
      })
      .then((allConfigGroups) => {
        // TODO: confirm dedupe
        const uniqueGroups = _.uniq(allConfigGroups, name);
        Object.assign(model, { allConfigGroups: uniqueGroups });
        return fetch(`/thirdeye/email/function/${id}`).then(checkStatus);
      })
      .then((groupByAlertId) => {
        const originalConfigGroup = groupByAlertId ? groupByAlertId.pop() : null;
        const selectedAppName = originalConfigGroup ? originalConfigGroup.application : null;
        Object.assign(model, { originalConfigGroup, selectedAppName });
        return fetch('/thirdeye/entity/APPLICATION').then(checkStatus);
      })
      .then((allApps) => {
        Object.assign(model, { allApps });
      })
      .catch((errors) => {
        Object.assign(model, { loadError: true, loadErrorMsg: errors });
      });
  },
<commit_msg>TE Self-Serve Edit Alert: bug fix THIRDEYE-1703 (#1944)<commit_after>import Ember from 'ember';
import fetch from 'fetch';
import moment from 'moment';
import RSVP from 'rsvp';
import _ from 'lodash';
import { checkStatus } from 'thirdeye-frontend/helpers/utils';

/**
 * Parses stringified object from payload
 * @param {String} filters
 * @returns {Object}
 */
const parseProps = (filters) => {
  filters = filters || '';

  return filters.split(';')
    .filter(prop => prop)
    .map(prop => prop.split('='))
    .reduce(function (aggr, prop) {
      const [ propName, value ] = prop;
      aggr[propName] = value;

      return aggr;
    }, {});
};

export default Ember.Route.extend({
  model(params) {
    const { alertId: id } = params;
    if (!id) { return; }
    const alertUrl = `/onboard/function/${id}`;

    return RSVP.hash({
      function: fetch(alertUrl).then(checkStatus)
    });
  },

  afterModel(model) {
    const {
      metric: metricName,
      collection: dataset,
      filters,
      bucketSize,
      bucketUnit,
      id
     } = model.function;

    let metricId = '';
    let allGroupNames = [];
    let allGroups = [];

    return fetch(`/data/autocomplete/metric?name=${dataset}::${metricName}`).then(checkStatus)
      .then((metricsByName) => {
        const metric = metricsByName.pop();
        metricId = metric.id;
        return fetch(`/data/maxDataTime/metricId/${metricId}`).then(checkStatus);
      })
      .then((maxTime) => {
        const currentEnd = moment(maxTime).isValid()
          ? moment(maxTime).valueOf()
          : moment().subtract(1, 'day').endOf('day').valueOf();
        const formattedFilters = JSON.stringify(parseProps(filters));
        const dimension = 'All';
        const currentStart = moment(currentEnd).subtract(1, 'months').valueOf();
        const baselineStart = moment(currentStart).subtract(1, 'week').valueOf();
        const baselineEnd = moment(currentEnd).subtract(1, 'week');
        const metricDataUrl =  `/timeseries/compare/${metricId}/${currentStart}/${currentEnd}/` +
          `${baselineStart}/${baselineEnd}?dimension=${dimension}&granularity=` +
          `${bucketSize + '_' + bucketUnit}&filters=${encodeURIComponent(formattedFilters)}`;
        return fetch(metricDataUrl).then(checkStatus);
      })
      .then((metricData) => {
        Object.assign(metricData, { color: 'blue' })
        Object.assign(model, { metricData });
        return fetch(`/thirdeye/entity/ALERT_CONFIG`).then(checkStatus);
      })
      .then((allConfigGroups) => {
        // TODO: confirm dedupe
        const uniqueGroups = _.uniq(allConfigGroups, name);
        Object.assign(model, { allConfigGroups: uniqueGroups });
        return fetch(`/thirdeye/email/function/${id}`).then(checkStatus);
      })
      .then((groupByAlertId) => {
        const originalConfigGroup = groupByAlertId ? groupByAlertId.pop() : null;
        const selectedAppName = originalConfigGroup ? originalConfigGroup.application : null;
        Object.assign(model, { originalConfigGroup, selectedAppName });
        return fetch('/thirdeye/entity/APPLICATION').then(checkStatus);
      })
      .then((allApps) => {
        Object.assign(model, { allApps });
      })
      .catch((errors) => {
        Object.assign(model, { loadError: true, loadErrorMsg: errors });
      });
  },

{'fraction_non_alphanumeric': 0.1569811320754717, 'fraction_numerical': 0.0024150943396226416, 'mean_word_length': 3.035322777101096, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '4440846', 'n_tokens_mistral': 1975, 'n_tokens_neox': 1958, 'n_words': 364}