<commit_before>import {Http, Headers} from '@angular/http';
import {Injectable} from '@angular/core';
import {PortalService} from './portal.service';
import {UserService} from './user.service';
import {FunctionsService} from '../services/functions.service';
import {GlobalStateService} from '../services/global-state.service';
import {FunctionInvocations, FunctionInvocationDetails, FunctionAggregates} from '../models/function-monitor'
import {Observable} from 'rxjs/Rx';

@Injectable()
export class FunctionMonitorService {
    private token: string;

    constructor(
        private _userService: UserService,
        private _http: Http,
        private _functionsService: FunctionsService,
        private _globalStateService: GlobalStateService
    ) {
        if (!this._globalStateService.showTryView) {
            this._userService.getToken().subscribe(t => this.token = t);
        }
    }

    private getHeadersForScmSite(scmCreds?: string): Headers {
        var contentType = 'application/json';
        var headers = new Headers();
        headers.append('Content-Type', contentType);

        if (scmCreds) {
            headers.append('Authorization', `Basic ${scmCreds}`);
        } else if (this.token) {
            headers.append('Authorization', `Bearer ${this.token}`);
        }

        return headers;
    }

    getInvocationsDataForSelctedFunction(selectedFunctionName: string) {
        let funcName = selectedFunctionName.toLocaleLowerCase(); // workaround API bug the functionName needs to be lowercase
        var url = this._functionsService.getScmUrl() + "/azurejobs/api/functions/definitions/" + funcName + "/invocations?limit=20";
        return this._http.get(url, {
            headers: this.getHeadersForScmSite(this._globalStateService.ScmCreds)
        })
            .map<FunctionInvocations[]>(r => r.json().entries)
            .catch(e => {
                if (e.status === 404) {
                    return Observable.of([]);
                } else {
                    throw e;
                }
            });
    }

    getInvocationDetailsForSelectedInvocation(invocationId: string) {
        var url = this._functionsService.getScmUrl() + "/azurejobs/api/functions/invocations/" + invocationId;
        return this._http.get(url, {
            headers: this.getHeadersForScmSite(this._globalStateService.ScmCreds)
        }).map<FunctionInvocationDetails[]>(r => r.json().parameters)
            .catch(e => {
                if (e.status === 404) {
                    return Observable.of([]);
                } else {
                    throw e;
                }
            });
    }

    getOutputDetailsForSelectedInvocation(invocationId: string) {
        var url = this._functionsService.getScmUrl() + "/azurejobs/api/log/output/" + invocationId;
        return this._http.get(url, {
            headers: this.getHeadersForScmSite(this._globalStateService.ScmCreds)
        }).map<string>(r => r.text())
            .catch(e => {
                if (e.status === 404) {
                    return Observable.of("");
                } else {
                    throw e;
                }
            });
    }

    getAggregateErrorsAndInvocationsForSelectedFunction(selectedFunctionName: string) {
        let funcName = selectedFunctionName.toLocaleLowerCase();  // workaround API bug the functionName needs to be lowercase
        var url = this._functionsService.getScmUrl() + "/azurejobs/api/functions/definitions?limit=11";
        return this._http.get(url, {
            headers: this.getHeadersForScmSite(this._globalStateService.ScmCreds)
        })
            .map<FunctionAggregates>(r => r.json().entries.find(x => x.functionName === funcName));
    }
}<commit_msg>FW: making code foolproof to API changes<commit_after>import {Http, Headers} from '@angular/http';
import {Injectable} from '@angular/core';
import {PortalService} from './portal.service';
import {UserService} from './user.service';
import {FunctionsService} from '../services/functions.service';
import {GlobalStateService} from '../services/global-state.service';
import {FunctionInvocations, FunctionInvocationDetails, FunctionAggregates} from '../models/function-monitor'
import {Observable} from 'rxjs/Rx';

@Injectable()
export class FunctionMonitorService {
    private token: string;

    constructor(
        private _userService: UserService,
        private _http: Http,
        private _functionsService: FunctionsService,
        private _globalStateService: GlobalStateService
    ) {
        if (!this._globalStateService.showTryView) {
            this._userService.getToken().subscribe(t => this.token = t);
        }
    }

    private getHeadersForScmSite(scmCreds?: string): Headers {
        var contentType = 'application/json';
        var headers = new Headers();
        headers.append('Content-Type', contentType);

        if (scmCreds) {
            headers.append('Authorization', `Basic ${scmCreds}`);
        } else if (this.token) {
            headers.append('Authorization', `Bearer ${this.token}`);
        }

        return headers;
    }

    getInvocationsDataForSelctedFunction(selectedFunctionName: string) {
        let funcName = selectedFunctionName.toLocaleLowerCase();
        var url = this._functionsService.getScmUrl() + "/azurejobs/api/functions/definitions/" + funcName + "/invocations?limit=20";
        return this._http.get(url, {
            headers: this.getHeadersForScmSite(this._globalStateService.ScmCreds)
        })
            .map<FunctionInvocations[]>(r => r.json().entries)
            .catch(e => {
                if (e.status === 404) {
                    return Observable.of([]);
                } else {
                    throw e;
                }
            });
    }

    getInvocationDetailsForSelectedInvocation(invocationId: string) {
        var url = this._functionsService.getScmUrl() + "/azurejobs/api/functions/invocations/" + invocationId;
        return this._http.get(url, {
            headers: this.getHeadersForScmSite(this._globalStateService.ScmCreds)
        }).map<FunctionInvocationDetails[]>(r => r.json().parameters)
            .catch(e => {
                if (e.status === 404) {
                    return Observable.of([]);
                } else {
                    throw e;
                }
            });
    }

    getOutputDetailsForSelectedInvocation(invocationId: string) {
        var url = this._functionsService.getScmUrl() + "/azurejobs/api/log/output/" + invocationId;
        return this._http.get(url, {
            headers: this.getHeadersForScmSite(this._globalStateService.ScmCreds)
        }).map<string>(r => r.text())
            .catch(e => {
                if (e.status === 404) {
                    return Observable.of("");
                } else {
                    throw e;
                }
            });
    }

    getAggregateErrorsAndInvocationsForSelectedFunction(selectedFunctionName: string) {
        let funcName = selectedFunctionName.toLocaleLowerCase();
        var url = this._functionsService.getScmUrl() + "/azurejobs/api/functions/definitions?limit=11";
        return this._http.get(url, {
            headers: this.getHeadersForScmSite(this._globalStateService.ScmCreds)
        })
            .map<FunctionAggregates>(r => r.json().entries.find(x => x.functionName.toLowerCase() === funcName));
    }
}
{'fraction_non_alphanumeric': 0.12206319200648123, 'fraction_numerical': 0.0035106670267350797, 'mean_word_length': 2.371415566681839, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 29, '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': '136790', 'n_tokens_mistral': 2049, 'n_tokens_neox': 1975, 'n_words': 370}