<commit_before>import { Injectable } from '@angular/core';
import { Http, Response, RequestOptionsArgs, Headers } from '@angular/http';
import { Subject } from 'rxjs/Subject';
import 'rxjs/add/operator/concatMap';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/throttleTime';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/empty';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/map';
import { Subscriber } from 'rxjs/Subscriber';
import * as Models from '../models/Models';
import { EventSourceUtil } from "../util/EventSourceUtil";
import { TweetUtil } from "../util/TweetUtil";

@Injectable()
export class TweetEvents {

    actions = {
        clickPicture: new Subject<void>()
    }

    requests = {
        getSentiment: new Subject<string>()
    }

    responses = {
        getSentimentSuccess: new Subject<Models.Sentiment>(),
        getSentimentError: new Subject<Models.Error>(),
        getTweetStreamSuccess: new Subject<Models.Tweet>()
    }

    constructor(
        private http: Http,
        private eventSourceUtil: EventSourceUtil,
        private tweetUtil: TweetUtil
    ) { 
        this.initGetSentiment();
        this.initGetTweetStream();
    }

    private initGetSentiment(): void {
        this.requests.getSentiment
        .concatMap((search: string) => {
            let headers = new Headers();
            headers.append("Content-Type", "application/x-www-form-urlencoded");
            headers.append("Accept", "application/json");
            return this.http.post(
                '/sentiment',
                `txt=${search}`,
                { headers: headers }
            ).catch((response: Response) => {
                this.responses.getSentimentError.next(response.json());
                return Observable.empty();
            })
        })
        .map((response: Response): Models.Sentiment => {
            return response.json().result;
        })
        .subscribe((result) => {
            this.responses.getSentimentSuccess.next(result);
        });
    }

    private initGetTweetStream(): void {
        this.eventSourceUtil.fromEventSource("/tweets")
        .filter(this.tweetUtil.filterUnwantedTweets)
        .throttleTime(10000)
        // TODO: tweet mapper
        //.map(this.tweetMapper.MessageEvent_Tweet)
        .subscribe(tweet => {
            this.responses.getTweetStreamSuccess.next(tweet);<commit_msg>Using Observable.ajax<commit_after>import { Injectable } from '@angular/core';
import { AjaxResponse, AjaxError } from 'rxjs/observable/dom/AjaxObservable';
import { Subject } from 'rxjs/Subject';
import 'rxjs/add/operator/concatMap';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/throttleTime';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/empty';
import 'rxjs/add/observable/dom/ajax';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/map';
import { Subscriber } from 'rxjs/Subscriber';
import * as Models from '../models/Models';
import { EventSourceUtil } from '../util/EventSourceUtil';
import { TweetUtil } from '../util/TweetUtil';

@Injectable()
export class TweetEvents {

    actions = {
        clickPicture: new Subject<void>()
    }

    requests = {
        getSentiment: new Subject<string>()
    }

    responses = {
        getSentimentSuccess: new Subject<Models.Sentiment>(),
        getSentimentError: new Subject<Models.Error>(),
        getTweetStreamSuccess: new Subject<Models.Tweet>()
    }

    constructor(
        private eventSourceUtil: EventSourceUtil,
        private tweetUtil: TweetUtil
    ) { 
        this.initGetSentiment();
        this.initGetTweetStream();
    }

    private initGetSentiment(): void {
        this.requests.getSentiment
        .concatMap((search: string) => {
            return Observable.ajax({
                url: '/sentiment',
                body: `txt=${search}`,
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'Accept': 'application/json'
                }
            })
            .catch((ajax: AjaxError) => {
                this.responses.getSentimentError.next(ajax);
                return Observable.empty();
            })
        })
        .map((ajax: AjaxResponse): Models.Sentiment => {
            return ajax.response.result;
        })
        .subscribe((sentiment: Models.Sentiment) => {
            this.responses.getSentimentSuccess.next(sentiment);
        });
    }

    private initGetTweetStream(): void {
        this.eventSourceUtil.fromEventSource('/tweets')
        .filter(this.tweetUtil.filterUnwantedTweets)
        .throttleTime(10000)
        // TODO: tweet mapper
        //.map(this.tweetMapper.MessageEvent_Tweet)
        .subscribe(tweet => {
            this.responses.getTweetStreamSuccess.next(tweet);
{'fraction_non_alphanumeric': 0.13046104288302798, 'fraction_numerical': 0.0020132876988121604, 'mean_word_length': 2.476557032890133, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '816191', 'n_tokens_mistral': 1443, 'n_tokens_neox': 1392, 'n_words': 253}