<commit_before>'use strict';

(function () {

  function StreamingResultService(appConfig, $q, OptionService) {
    function mapToVotes(votesResultSet, options) {

      var optionsMap = {};
      options.forEach(function (option) {
        optionsMap[option.objectId] = {label: option.Title, value: 0};
      });

      votesResultSet.results.forEach(function (vote) {
        optionsMap[vote.option_id].value++;
      });

      var votes = [];
      for (var option in optionsMap) {
        votes.push(optionsMap[option]);
      }

      return votes;
    }

    return {
      getPoll: function (questionId, onDataCallback, onPatchCallback) {
        var deferred = $q.defer();
        var param = {question_id: questionId};
        var jsonParam = angular.toJson(param);

        OptionService.get({'where': jsonParam}).$promise.then(function (options) {
          var url = 'https://api.parse.com/1/classes/Vote?where=' + encodeURIComponent('{\"question_id\": \"' + questionId + '\"}');

          // you can store your key pair in a json file instead, more details in documentation
          streamdataio.Pk = appConfig.streamdataioAppToken;
          streamdataio.pk = appConfig.streamdataioPrivateKey;

          var headers = ['X-Parse-Application-ID: ' + appConfig.parseAppId, 'X-Parse-REST-API-Key:' + appConfig.parseRestApiId];
          // create the streamdata.io source
          var streamdata = streamdataio.createEventSource(url, headers);

          streamdata.onData(function (data) {
            streamdata.resultSet = data;
            var votes = mapToVotes(data, options);

            onDataCallback(votes);

          }).onPatch(function (patch) {
            jsonpatch.apply(streamdata.resultSet, patch);
            var votes = mapToVotes(streamdata.resultSet, options);

            onPatchCallback(votes);
          });

          deferred.resolve(streamdata);
<commit_msg>doc(streaming): added comments to the code<commit_after>'use strict';

(function () {

  function StreamingResultService(appConfig, $q, OptionService) {
    function mapToVotes(votesResultSet, options) {

      var optionsMap = {};
      options.forEach(function (option) {
        optionsMap[option.objectId] = {label: option.Title, value: 0};
      });

      votesResultSet.results.forEach(function (vote) {
        optionsMap[vote.option_id].value++;
      });

      var votes = [];
      for (var option in optionsMap) {
        votes.push(optionsMap[option]);
      }

      return votes;
    }

    return {
      getPoll: function (questionId, onDataCallback, onPatchCallback) {
        var deferred = $q.defer();
        var param = {question_id: questionId};
        var jsonParam = angular.toJson(param);

        OptionService.get({'where': jsonParam}).$promise.then(function (options) {
          var url = 'https://api.parse.com/1/classes/Vote?where=' + encodeURIComponent('{\"question_id\": \"' + questionId + '\"}');

          // you can store your key pair in a json file instead, more details in documentation
          streamdataio.Pk = appConfig.streamdataioAppToken;
          streamdataio.pk = appConfig.streamdataioPrivateKey;

          var headers = ['X-Parse-Application-ID: ' + appConfig.parseAppId, 'X-Parse-REST-API-Key:' + appConfig.parseRestApiId];
          // create the streamdata.io source
          var streamdata = streamdataio.createEventSource(url, headers);

          streamdata.onData(function (data) {
            // add a callback when data is sent by streamdata.io: it's the first set of data sent (snapshot)
            streamdata.resultSet = data;
            var votes = mapToVotes(data, options);

            onDataCallback(votes);

          }).onPatch(function (patch) {
            // a callback when a patch is sent by streamdata.io: whenever the snapshot has changed, streamdata.io sent the diff
            jsonpatch.apply(streamdata.resultSet, patch);
            var votes = mapToVotes(streamdata.resultSet, options);

            onPatchCallback(votes);

          }).onError(function(error) {
            console.error(error);

          });


          deferred.resolve(streamdata);

{'fraction_non_alphanumeric': 0.11927710843373494, 'fraction_numerical': 0.0009638554216867469, 'mean_word_length': 2.578448275862069, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '5922602', 'n_tokens_mistral': 1183, 'n_tokens_neox': 1151, 'n_words': 258}