<commit_before>import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormGroup, FormControl, FormBuilder } from '@angular/forms';
import { TasksService } from '../tasks.service';
import { validateBulkTaskDefinitions } from './task-bulk-define-validators';

@Component({
  selector: 'app-task-bulk-define',
  templateUrl: './task-bulk-define.component.html',
})
export class TaskBulkDefineComponent implements OnInit {

  form: FormGroup;
  definitions = new FormControl('', validateBulkTaskDefinitions);

  constructor(
    private tasksService: TasksService,
    private router: Router,
    private fb: FormBuilder
  ) {
    this.form = fb.group({
      'definitions': this.definitions
    });
  }

  ngOnInit() {
  }

  cancelBulkDefineTasks() {
    this.router.navigate(['tasks/definitions']);
  }

  bulkDefineTasks() {
    console.log('bulkDefineTasks');
    if (this.definitions.value) {
      for (const def of this.definitions.value.split('\n')) {
        if (def.length > 2) {
          const keyValue = def.split('=');
          if (keyValue.length === 2) {
            this.tasksService.createDefinition(keyValue[1], keyValue[0]).subscribe();
          }
        }
      }
    }
    this.router.navigate(['tasks/definitions']);<commit_msg>gh-325 Fix: Validation not working for bulk task definitions<commit_after>import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormGroup, FormControl, FormBuilder } from '@angular/forms';

import { Observable } from 'rxjs/Observable';

import { TasksService } from '../tasks.service';
import { ToastyService } from 'ng2-toasty';

import { validateBulkTaskDefinitions } from './task-bulk-define-validators';

@Component({
  selector: 'app-task-bulk-define',
  templateUrl: './task-bulk-define.component.html',
})
export class TaskBulkDefineComponent implements OnInit {

  form: FormGroup;
  definitions = new FormControl('', validateBulkTaskDefinitions);

  constructor(
    private tasksService: TasksService,
    private toastyService: ToastyService,
    private router: Router,
    private fb: FormBuilder
  ) {
    this.form = fb.group({
      'definitions': this.definitions
    });
  }

  ngOnInit() {
  }

  cancelBulkDefineTasks() {
    this.router.navigate(['tasks/definitions']);
  }

  bulkDefineTasks() {
    const definitions = this.definitions.value.split('\n');

    const observables: Observable<Response>[] = [];

    console.log(`Bulk-Define ${definitions.length} Task(s)`);
    if (this.definitions.value) {
      for (const def of definitions) {
        if (def.length > 2) {
          const keyValue = def.split('=');
          if (keyValue.length === 2) {
            const observable = this.tasksService.createDefinition(keyValue[1], keyValue[0])
            .catch(error => {
              return Observable.of({
                isError: true,
                errorMessage: error
              });
            });
            observables.push(observable);
          }
        }
      }
    }
    Observable.forkJoin(observables).subscribe(
      data => {
        let completed = 0;
        let failed = 0;
        for (const dataItem of data as any) {
          if (dataItem.isError) {
            this.toastyService.error(dataItem.errorMessage);
            failed++;
          } else {
            completed++;
          }
        }
        if (failed === 0) {
          this.toastyService.success(`${data.length} task(s) defined.`);
          this.router.navigate(['tasks/definitions']);
        } else if (failed > 0 && completed > 0) {
          this.toastyService.error(`${failed} of ${data.length} task(s) could not be defined.`);
        } else {
          this.toastyService.error(`All ${data.length} task(s) could not be defined.`);
        }
      }
    );
{'fraction_non_alphanumeric': 0.13805678562125553, 'fraction_numerical': 0.004428236519927064, 'mean_word_length': 2.681687440076702, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 10, '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': '531821', 'n_tokens_mistral': 1146, 'n_tokens_neox': 1109, 'n_words': 239}