<commit_before>import { Component, OnInit, OnDestroy } from '@angular/core';
import { Store } from '@ngrx/store';
import { Subscription } from 'rxjs/Subscription';
import { FilterModel, INTERVAL_15MIN, INTERVAL_HOURLY, INTERVAL_DAILY } from '../../ngrx/model';
import { CastleFilterAction } from '../../ngrx/actions';
import { isMoreThanXDays, beginningOfTodayUTC, endOfTodayUTC } from '../util/date.util';
import * as moment from 'moment';

export const TODAY = 'Today';
export const THIS_WEEK = 'This week';
export const TWO_WEEKS = '2 weeks';
export const THIS_MONTH = 'This month';
export const THREE_MONTHS = '3 months';
export const THIS_YEAR = 'This year';
export const YESTERDAY = 'Yesterday';
export const LAST_WEEK = 'Last week';
export const PRIOR_TWO_WEEKS = 'Prior 2 weeks';
export const LAST_MONTH = 'Last month';
export const PRIOR_THREE_MONTHS = 'Prior 3 months';
export const LAST_YEAR = 'Last year';

@Component({
  selector: 'metrics-canned-range',
  template: `
    <button class="btn-link" disabled="{{prevDisabled}}" (click)="prev()">&lt;&lt;&nbsp;PREV</button>
    <div>
      <span>When:</span>
      <span>
        <prx-select single="true" [options]="whenOptions" [selected]="selection" (onSelect)="onWhenChange($event)"></prx-select>
      </span>
    </div>
    <button class="btn-link" disabled="{{nextDisabled}}" (click)="next()">NEXT&nbsp;&gt;&gt;</button>
  `,
  styleUrls: ['./canned-range.component.css']
})
export class CannedRangeComponent implements OnInit, OnDestroy {
  filterStoreSub: Subscription;
  filter: FilterModel;
  whenOptions: any[][];
  selected: any[];
  lastChosenRange: any[];

  constructor(public store: Store<any>) {}

  ngOnInit() {
    this.filterStoreSub = this.store.select('filter').subscribe(state => {
      this.filter = state;
      this.genWhenDates();
    });
  }

  ngOnDestroy() {
    if (this.filterStoreSub) { this.filterStoreSub.unsubscribe(); }
  }

  genWhenDates() {
    this.selected = null;

    let utcEndDate = endOfTodayUTC();
    let utcBeginDate = beginningOfTodayUTC();
    const daysIntoWeek = utcEndDate.day();

    this.whenOptions = [[TODAY, {beginDate: utcBeginDate.toDate(), endDate: utcEndDate.toDate()}]];

    utcBeginDate.subtract(daysIntoWeek, 'days');
    this.whenOptions.push([THIS_WEEK, {beginDate: utcBeginDate.toDate(), endDate: utcEndDate.toDate()}]);

    utcBeginDate.subtract(7, 'days');
    if (this.filter.interval !== INTERVAL_15MIN ||
      (this.filter.interval === INTERVAL_15MIN && !isMoreThanXDays(10, utcBeginDate, utcEndDate))) {
      this.whenOptions.push([TWO_WEEKS, {beginDate: utcBeginDate.toDate(), endDate: utcEndDate.toDate()}]);
    }

    utcBeginDate = beginningOfTodayUTC().date(1);
    if (this.filter.interval !== INTERVAL_15MIN ||
      (this.filter.interval === INTERVAL_15MIN && !isMoreThanXDays(10, utcBeginDate, utcEndDate))) {
      this.whenOptions.push([THIS_MONTH, {beginDate: utcBeginDate.toDate(), endDate: utcEndDate.toDate()}]);
    }

    utcBeginDate.subtract(2, 'months');
    if (this.filter.interval === INTERVAL_DAILY ||
      (this.filter.interval === INTERVAL_15MIN && !isMoreThanXDays(10, utcBeginDate, utcEndDate)) ||
      (this.filter.interval === INTERVAL_HOURLY && !isMoreThanXDays(40, utcBeginDate, utcEndDate))) {
      this.whenOptions.push([THREE_MONTHS, {beginDate: utcBeginDate.toDate(), endDate: utcEndDate.toDate()}]);
    }

    utcBeginDate = beginningOfTodayUTC().month(0).date(1);
    if (this.filter.interval === INTERVAL_DAILY ||
      (this.filter.interval === INTERVAL_15MIN && !isMoreThanXDays(10, utcBeginDate, utcEndDate)) ||
      (this.filter.interval === INTERVAL_HOURLY && !isMoreThanXDays(40, utcBeginDate, utcEndDate))) {
      this.whenOptions.push([THIS_YEAR, {beginDate: utcBeginDate.toDate(), endDate: utcEndDate.toDate()}]);
    }

    utcEndDate.subtract(1, 'days');
    utcBeginDate = beginningOfTodayUTC().subtract(1, 'days');
    this.whenOptions.push([YESTERDAY, {beginDate: utcBeginDate.toDate(), endDate: utcEndDate.toDate()}]);

    utcEndDate = endOfTodayUTC().subtract(daysIntoWeek + 1, 'days');
    utcBeginDate = beginningOfTodayUTC().subtract(daysIntoWeek + 7, 'days');
    this.whenOptions.push([LAST_WEEK, {beginDate: utcBeginDate.toDate(), endDate: utcEndDate.toDate()}]);

    if (this.filter.interval !== INTERVAL_15MIN) {
      utcEndDate = endOfTodayUTC().subtract(daysIntoWeek + 8, 'days');
      utcBeginDate = beginningOfTodayUTC().subtract(daysIntoWeek + 21, 'days');
      this.whenOptions.push([PRIOR_TWO_WEEKS, {beginDate: utcBeginDate.toDate(), endDate: utcEndDate.toDate()}]);

      utcEndDate = endOfTodayUTC().date(1).subtract(1, 'days'); // 1st of month - 1 day
      utcBeginDate = beginningOfTodayUTC().date(1).subtract(1, 'months'); // 1st of month - 1 month
      this.whenOptions.push([LAST_MONTH, {beginDate: utcBeginDate.toDate(), endDate: utcEndDate.toDate()}]);
    }

    if (this.filter.interval !== INTERVAL_15MIN && this.filter.interval !== INTERVAL_HOURLY) {
      // first of this month - 1 day and 2 months
      utcEndDate = endOfTodayUTC().date(1).subtract(2, 'months').subtract(1, 'days');
      // first of this month - 5 months
      utcBeginDate = beginningOfTodayUTC().date(1).subtract(5, 'months');
      this.whenOptions.push([PRIOR_THREE_MONTHS, {beginDate: utcBeginDate.toDate(), endDate: utcEndDate.toDate()}]);

      // last day of year minus 1 year
      utcEndDate = endOfTodayUTC().month(11).date(31).subtract(1, 'years');
      // first day of year minus 1 year
      utcBeginDate = beginningOfTodayUTC().month(0).date(1).subtract(1, 'years');
      this.whenOptions.push([LAST_YEAR, {beginDate: utcBeginDate.toDate(), endDate: utcEndDate.toDate()}]);
    }

    // We don't have back data yet, but users want an All time option,
    //  suppose that would just use the pub date of the very first episode as the begin date
    // this.whenOptions.push(['All time', {beginDate: utcBeginDate.toDate(), endDate: utcEndDate.toDate()}]);

    this.whenOptions.forEach(this.setSelectedIfFilterIsRange.bind(this));
  }

  get selection(): any {
    return this.selected && this.selected.length ? this.selected[1] : null;
  }

  setSelectedIfFilterIsRange(range) {
    const { beginDate, endDate } = range[1];
    if (this.filter.beginDate && this.filter.endDate &&
      this.filter.beginDate.valueOf() === beginDate.valueOf() &&
      this.filter.endDate.valueOf() === endDate.valueOf()) {
      this.selected = range;
      this.setLastChosenRange();
    }
  }

  setLastChosenRange() {
    switch (this.selected[0]) {
      case TODAY:
      case YESTERDAY:
        this.lastChosenRange = [1, 'days'];
        break;
      case THIS_WEEK:
      case LAST_WEEK:
        this.lastChosenRange =  [1, 'weeks'];
        break;
      case TWO_WEEKS:
      case PRIOR_TWO_WEEKS:
        this.lastChosenRange = [2, 'weeks'];
        break;
      case THIS_MONTH:
      case LAST_MONTH:
        this.lastChosenRange = [1, 'months'];
        break;
      case THREE_MONTHS:
      case PRIOR_THREE_MONTHS:
        this.lastChosenRange = [3, 'months'];
        break;
      case THIS_YEAR:
      case LAST_YEAR:
        this.lastChosenRange = [1, 'year'];
        break;
      default:
        break;
    }
  }

  onWhenChange(val) {
    const { beginDate, endDate } = val;
    this.store.dispatch(new CastleFilterAction({filter: {beginDate, endDate}}));<commit_msg>fixes error when canned range option is unselected<commit_after>import { Component, OnInit, OnDestroy } from '@angular/core';
import { Store } from '@ngrx/store';
import { Subscription } from 'rxjs/Subscription';
import { FilterModel, INTERVAL_15MIN, INTERVAL_HOURLY, INTERVAL_DAILY } from '../../ngrx/model';
import { CastleFilterAction } from '../../ngrx/actions';
import { isMoreThanXDays, beginningOfTodayUTC, endOfTodayUTC } from '../util/date.util';
import * as moment from 'moment';

export const TODAY = 'Today';
export const THIS_WEEK = 'This week';
export const TWO_WEEKS = '2 weeks';
export const THIS_MONTH = 'This month';
export const THREE_MONTHS = '3 months';
export const THIS_YEAR = 'This year';
export const YESTERDAY = 'Yesterday';
export const LAST_WEEK = 'Last week';
export const PRIOR_TWO_WEEKS = 'Prior 2 weeks';
export const LAST_MONTH = 'Last month';
export const PRIOR_THREE_MONTHS = 'Prior 3 months';
export const LAST_YEAR = 'Last year';

@Component({
  selector: 'metrics-canned-range',
  template: `
    <button class="btn-link" disabled="{{prevDisabled}}" (click)="prev()">&lt;&lt;&nbsp;PREV</button>
    <div>
      <span>When:</span>
      <span>
        <prx-select single="true" [options]="whenOptions" [selected]="selection" (onSelect)="onWhenChange($event)"></prx-select>
      </span>
    </div>
    <button class="btn-link" disabled="{{nextDisabled}}" (click)="next()">NEXT&nbsp;&gt;&gt;</button>
  `,
  styleUrls: ['./canned-range.component.css']
})
export class CannedRangeComponent implements OnInit, OnDestroy {
  filterStoreSub: Subscription;
  filter: FilterModel;
  whenOptions: any[][];
  selected: any[];
  lastChosenRange: any[];

  constructor(public store: Store<any>) {}

  ngOnInit() {
    this.filterStoreSub = this.store.select('filter').subscribe(state => {
      this.filter = state;
      this.genWhenDates();
    });
  }

  ngOnDestroy() {
    if (this.filterStoreSub) { this.filterStoreSub.unsubscribe(); }
  }

  genWhenDates() {
    this.selected = null;

    let utcEndDate = endOfTodayUTC();
    let utcBeginDate = beginningOfTodayUTC();
    const daysIntoWeek = utcEndDate.day();

    this.whenOptions = [[TODAY, {beginDate: utcBeginDate.toDate(), endDate: utcEndDate.toDate()}]];

    utcBeginDate.subtract(daysIntoWeek, 'days');
    this.whenOptions.push([THIS_WEEK, {beginDate: utcBeginDate.toDate(), endDate: utcEndDate.toDate()}]);

    utcBeginDate.subtract(7, 'days');
    if (this.filter.interval !== INTERVAL_15MIN ||
      (this.filter.interval === INTERVAL_15MIN && !isMoreThanXDays(10, utcBeginDate, utcEndDate))) {
      this.whenOptions.push([TWO_WEEKS, {beginDate: utcBeginDate.toDate(), endDate: utcEndDate.toDate()}]);
    }

    utcBeginDate = beginningOfTodayUTC().date(1);
    if (this.filter.interval !== INTERVAL_15MIN ||
      (this.filter.interval === INTERVAL_15MIN && !isMoreThanXDays(10, utcBeginDate, utcEndDate))) {
      this.whenOptions.push([THIS_MONTH, {beginDate: utcBeginDate.toDate(), endDate: utcEndDate.toDate()}]);
    }

    utcBeginDate.subtract(2, 'months');
    if (this.filter.interval === INTERVAL_DAILY ||
      (this.filter.interval === INTERVAL_15MIN && !isMoreThanXDays(10, utcBeginDate, utcEndDate)) ||
      (this.filter.interval === INTERVAL_HOURLY && !isMoreThanXDays(40, utcBeginDate, utcEndDate))) {
      this.whenOptions.push([THREE_MONTHS, {beginDate: utcBeginDate.toDate(), endDate: utcEndDate.toDate()}]);
    }

    utcBeginDate = beginningOfTodayUTC().month(0).date(1);
    if (this.filter.interval === INTERVAL_DAILY ||
      (this.filter.interval === INTERVAL_15MIN && !isMoreThanXDays(10, utcBeginDate, utcEndDate)) ||
      (this.filter.interval === INTERVAL_HOURLY && !isMoreThanXDays(40, utcBeginDate, utcEndDate))) {
      this.whenOptions.push([THIS_YEAR, {beginDate: utcBeginDate.toDate(), endDate: utcEndDate.toDate()}]);
    }

    utcEndDate.subtract(1, 'days');
    utcBeginDate = beginningOfTodayUTC().subtract(1, 'days');
    this.whenOptions.push([YESTERDAY, {beginDate: utcBeginDate.toDate(), endDate: utcEndDate.toDate()}]);

    utcEndDate = endOfTodayUTC().subtract(daysIntoWeek + 1, 'days');
    utcBeginDate = beginningOfTodayUTC().subtract(daysIntoWeek + 7, 'days');
    this.whenOptions.push([LAST_WEEK, {beginDate: utcBeginDate.toDate(), endDate: utcEndDate.toDate()}]);

    if (this.filter.interval !== INTERVAL_15MIN) {
      utcEndDate = endOfTodayUTC().subtract(daysIntoWeek + 8, 'days');
      utcBeginDate = beginningOfTodayUTC().subtract(daysIntoWeek + 21, 'days');
      this.whenOptions.push([PRIOR_TWO_WEEKS, {beginDate: utcBeginDate.toDate(), endDate: utcEndDate.toDate()}]);

      utcEndDate = endOfTodayUTC().date(1).subtract(1, 'days'); // 1st of month - 1 day
      utcBeginDate = beginningOfTodayUTC().date(1).subtract(1, 'months'); // 1st of month - 1 month
      this.whenOptions.push([LAST_MONTH, {beginDate: utcBeginDate.toDate(), endDate: utcEndDate.toDate()}]);
    }

    if (this.filter.interval !== INTERVAL_15MIN && this.filter.interval !== INTERVAL_HOURLY) {
      // first of this month - 1 day and 2 months
      utcEndDate = endOfTodayUTC().date(1).subtract(2, 'months').subtract(1, 'days');
      // first of this month - 5 months
      utcBeginDate = beginningOfTodayUTC().date(1).subtract(5, 'months');
      this.whenOptions.push([PRIOR_THREE_MONTHS, {beginDate: utcBeginDate.toDate(), endDate: utcEndDate.toDate()}]);

      // last day of year minus 1 year
      utcEndDate = endOfTodayUTC().month(11).date(31).subtract(1, 'years');
      // first day of year minus 1 year
      utcBeginDate = beginningOfTodayUTC().month(0).date(1).subtract(1, 'years');
      this.whenOptions.push([LAST_YEAR, {beginDate: utcBeginDate.toDate(), endDate: utcEndDate.toDate()}]);
    }

    // We don't have back data yet, but users want an All time option,
    //  suppose that would just use the pub date of the very first episode as the begin date
    // this.whenOptions.push(['All time', {beginDate: utcBeginDate.toDate(), endDate: utcEndDate.toDate()}]);

    this.whenOptions.forEach(this.setSelectedIfFilterIsRange.bind(this));
  }

  get selection(): any {
    return this.selected && this.selected.length ? this.selected[1] : null;
  }

  setSelectedIfFilterIsRange(range) {
    const { beginDate, endDate } = range[1];
    if (this.filter.beginDate && this.filter.endDate &&
      this.filter.beginDate.valueOf() === beginDate.valueOf() &&
      this.filter.endDate.valueOf() === endDate.valueOf()) {
      this.selected = range;
      this.setLastChosenRange();
    }
  }

  setLastChosenRange() {
    switch (this.selected[0]) {
      case TODAY:
      case YESTERDAY:
        this.lastChosenRange = [1, 'days'];
        break;
      case THIS_WEEK:
      case LAST_WEEK:
        this.lastChosenRange =  [1, 'weeks'];
        break;
      case TWO_WEEKS:
      case PRIOR_TWO_WEEKS:
        this.lastChosenRange = [2, 'weeks'];
        break;
      case THIS_MONTH:
      case LAST_MONTH:
        this.lastChosenRange = [1, 'months'];
        break;
      case THREE_MONTHS:
      case PRIOR_THREE_MONTHS:
        this.lastChosenRange = [3, 'months'];
        break;
      case THIS_YEAR:
      case LAST_YEAR:
        this.lastChosenRange = [1, 'year'];
        break;
      default:
        break;
    }
  }

  onWhenChange(val) {
    if (val && val.beginDate && val.endDate) {
      const { beginDate, endDate } = val;
      this.store.dispatch(new CastleFilterAction({filter: {beginDate, endDate}}));
    }
{'fraction_non_alphanumeric': 0.1523489932885906, 'fraction_numerical': 0.01087248322147651, 'mean_word_length': 3.9113381674357286, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 31, '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': '2005005', 'n_tokens_mistral': 5219, 'n_tokens_neox': 4983, 'n_words': 976}