<commit_before>import { HttpService } from './../shared/http-service';
import { Injectable, Component, Inject } from '@angular/core';
import { Headers } from '@angular/http';

import 'rxjs/add/operator/toPromise';
import { Subscription } from 'rxjs/Subscription';
import { Observable } from 'rxjs/Observable';

import { cloneDeep } from 'lodash';
import { DropdownOption } from 'ngx-widgets';
import { Broadcaster, Logger } from 'ngx-base';
import {
  AuthenticationService,
  User,
  UserService
} from 'ngx-login-client';
import { Space, Spaces } from 'ngx-fabric8-wit';
import { WIT_API_URL } from 'ngx-fabric8-wit';

import {
  Comment,
  Comments,
  CommentPost
} from '../models/comment';

import { AreaModel } from '../models/area.model';
import { AreaService } from '../area/area.service';
import { IterationModel } from '../models/iteration.model';
import { IterationService } from '../iteration/iteration.service';
import { LinkType } from '../models/link-type';
import { Link } from '../models/link';
import {
  LinkDict,
  WorkItem
} from '../models/work-item';
import { WorkItemType } from './../models/work-item-type';

@Injectable()
export class WorkItemService {

  private workItemUrl: string = null;
  private workItemTypeUrl: string = null;
  private linkTypesUrl: string = null;
  private linksUrl: string = null;
  private reorderUrl: string = null;
  private baseSearchUrl: string = null;
  private renderUrl: string = null;

  private headers = new Headers({'Content-Type': 'application/json'});
  private availableStates: DropdownOption[] = [];
  public workItemTypes: WorkItemType[] = [];

  // FIXME: this is the live list of work items, held in this instance of
  // workItemService. This prevents us from displaying two different lists of WIs.
  // This might have to change in the future.
  private workItems: WorkItem[] = [];
  private nextLink: string = null;
  private initialWorkItemFetchDone = false;
  private userIdMap = {};
  private workItemIdIndexMap = {};
  private prevFilters: any = [];
  private iterations: IterationModel[] = [];
  public _currentSpace;

  constructor(private http: HttpService,
    private broadcaster: Broadcaster,
    private logger: Logger,
    private areaService: AreaService,
    private auth: AuthenticationService,
    private iterationService: IterationService,
    private userService: UserService,
    private spaces: Spaces,
    @Inject(WIT_API_URL) private baseApiUrl: string)
  {
    this.spaces.current.subscribe(val => this._currentSpace = val);
    if (this.auth.getToken() != null) {
      this.headers.set('Authorization', 'Bearer ' + this.auth.getToken());
    }
  }

  // switchSpace(space: Space) {
  //   this.logger.log('[WorkItemService] New Space selected: ' + space.name);
  //   this.workItemUrl = space.spaceBaseUrl + 'workitems';
  //   this.workItemTypeUrl = space.spaceBaseUrl + 'workitemtypes';
  //   this.linkTypesUrl = space.spaceBaseUrl + 'workitemlinktypes';
  //   this.linksUrl = space.spaceBaseUrl + 'workitemlinks';
  //   this.reorderUrl = space.spaceBaseUrl + 'workitems/reorder';
  //   this.baseSearchUrl = space.spaceBaseUrl + 'search?q=';
  //   this.renderUrl = space.spaceBaseUrl + 'render';
  //   this.logger.log('WorkItemService using url ' + this.workItemUrl);
  // }

  getChildren(parent: WorkItem): Observable<WorkItem[]> {
    console.log('############# GET CHILDREN');
    if (parent.relationships.children) {
      this.logger.log('Requesting children for work item ' + parent.id);
      let url = parent.relationships.children.links.related;
      return this.http
        .get(url, { headers: this.headers })
        .map(response => {
          let wItems: WorkItem[];
          wItems = response.json().data as WorkItem[];
          wItems.forEach((item) => {
            // Resolve the assignee and creator
            this.resolveUsersForWorkItem(item);
            this.resolveIterationForWorkItem(item);
            this.resolveType(item);
            this.resolveAreaForWorkItem(item);
          });
          console.log('############# RETURN CHILDREN');
          console.log(wItems);
          return wItems;
        });
    } else {
      this.logger.log('Work item does not have child related link, skipping: ' + parent.id);
      return Observable.of([]);
    }
  }

  getWorkItems(pageSize: number = 20, filters: any[] = []): Observable<{workItems: WorkItem[], nextLink: string | null}> {
    if (this._currentSpace) {
      this.workItemUrl = this._currentSpace.links.self + '/workitems';
      let url = this.workItemUrl + '?page[limit]=' + pageSize;
      filters.forEach((item) => {<commit_msg>fix(listview): fixed children display.<commit_after>import { HttpService } from './../shared/http-service';
import { Injectable, Component, Inject } from '@angular/core';
import { Headers } from '@angular/http';

import 'rxjs/add/operator/toPromise';
import { Subscription } from 'rxjs/Subscription';
import { Observable } from 'rxjs/Observable';

import { cloneDeep } from 'lodash';
import { DropdownOption } from 'ngx-widgets';
import { Broadcaster, Logger } from 'ngx-base';
import {
  AuthenticationService,
  User,
  UserService
} from 'ngx-login-client';
import { Space, Spaces } from 'ngx-fabric8-wit';
import { WIT_API_URL } from 'ngx-fabric8-wit';

import {
  Comment,
  Comments,
  CommentPost
} from '../models/comment';

import { AreaModel } from '../models/area.model';
import { AreaService } from '../area/area.service';
import { IterationModel } from '../models/iteration.model';
import { IterationService } from '../iteration/iteration.service';
import { LinkType } from '../models/link-type';
import { Link } from '../models/link';
import {
  LinkDict,
  WorkItem
} from '../models/work-item';
import { WorkItemType } from './../models/work-item-type';

@Injectable()
export class WorkItemService {

  private workItemUrl: string = null;
  private workItemTypeUrl: string = null;
  private linkTypesUrl: string = null;
  private linksUrl: string = null;
  private reorderUrl: string = null;
  private baseSearchUrl: string = null;
  private renderUrl: string = null;

  private headers = new Headers({'Content-Type': 'application/json'});
  private availableStates: DropdownOption[] = [];
  public workItemTypes: WorkItemType[] = [];

  // FIXME: this is the live list of work items, held in this instance of
  // workItemService. This prevents us from displaying two different lists of WIs.
  // This might have to change in the future.
  private workItems: WorkItem[] = [];
  private nextLink: string = null;
  private initialWorkItemFetchDone = false;
  private userIdMap = {};
  private workItemIdIndexMap = {};
  private prevFilters: any = [];
  private iterations: IterationModel[] = [];
  public _currentSpace;

  constructor(private http: HttpService,
    private broadcaster: Broadcaster,
    private logger: Logger,
    private areaService: AreaService,
    private auth: AuthenticationService,
    private iterationService: IterationService,
    private userService: UserService,
    private spaces: Spaces,
    @Inject(WIT_API_URL) private baseApiUrl: string)
  {
    this.spaces.current.subscribe(val => this._currentSpace = val);
    if (this.auth.getToken() != null) {
      this.headers.set('Authorization', 'Bearer ' + this.auth.getToken());
    }
  }

  // switchSpace(space: Space) {
  //   this.logger.log('[WorkItemService] New Space selected: ' + space.name);
  //   this.workItemUrl = space.spaceBaseUrl + 'workitems';
  //   this.workItemTypeUrl = space.spaceBaseUrl + 'workitemtypes';
  //   this.linkTypesUrl = space.spaceBaseUrl + 'workitemlinktypes';
  //   this.linksUrl = space.spaceBaseUrl + 'workitemlinks';
  //   this.reorderUrl = space.spaceBaseUrl + 'workitems/reorder';
  //   this.baseSearchUrl = space.spaceBaseUrl + 'search?q=';
  //   this.renderUrl = space.spaceBaseUrl + 'render';
  //   this.logger.log('WorkItemService using url ' + this.workItemUrl);
  // }

  getChildren(parent: WorkItem): Promise<WorkItem[]> {
    if (parent.relationships.children) {
      this.logger.log('Requesting children for work item ' + parent.id);
      let url = parent.relationships.children.links.related;
      return this.http
        .get(url, { headers: this.headers })
        .map(response => {
          let wItems: WorkItem[];
          wItems = response.json().data as WorkItem[];
          wItems.forEach((item) => {
            // Resolve the assignee and creator
            this.resolveUsersForWorkItem(item);
            this.resolveIterationForWorkItem(item);
            this.resolveType(item);
            this.resolveAreaForWorkItem(item);
          });
          return wItems;
        }).toPromise();
    } else {
      this.logger.log('Work item does not have child related link, skipping: ' + parent.id);
      return Observable.of([]).toPromise();
    }
  }

  getWorkItems(pageSize: number = 20, filters: any[] = []): Observable<{workItems: WorkItem[], nextLink: string | null}> {
    if (this._currentSpace) {
      this.workItemUrl = this._currentSpace.links.self + '/workitems';
      let url = this.workItemUrl + '?page[limit]=' + pageSize;
      filters.forEach((item) => {
{'fraction_non_alphanumeric': 0.1369773519163763, 'fraction_numerical': 0.0008710801393728223, 'mean_word_length': 3.46524064171123, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '2350866', 'n_tokens_mistral': 2732, 'n_tokens_neox': 2701, 'n_words': 673}