<commit_before>/*
 * reviews_generator.js
 * Copyright(c) 2015 Bitergia
 * Author: <NAME> <<EMAIL>>,
 * <NAME> <<EMAIL>>
 * MIT Licensed

  Generates random reviews for restaurants in orion

  First it gets all restaurant information
  Then a random automatic review is generated
  Then the review is added to Orion CB

  TODO:
  - Create more real reviews using templates for comments and random ratings
*/

// jshint node: true

'use strict';

var utils = require('../utils');
var async = require('async');
var shortid = require('shortid'); // unique ids generator
var authRequest = require('../authrequest');
var apiRestSimtasks = 2; // number of simultaneous calls to API REST
var reviewsAdded = 0;
var restaurantsData; // All data for the restaurants to be reviewed

var feedOrionReviews = function() {
  var returnPost = function(data) {
    reviewsAdded++;
    console.log(reviewsAdded + '/' + restaurantsData.length);
  };

  // restaurantsData = restaurantsData.slice(0,5); // debug with few items

  console.log('Feeding reviews info in orion.');
  console.log('Total tried: ' + restaurantsData.length);

  // Limit the number of calls to be done in parallel to orion
  var q = async.queue(function(task, callback) {
    var attributes = task.attributes;

    authRequest('/v2/entities', 'POST', attributes)
    .then(callback)
    .catch(function(err) {
      console.log(err);
    });
  }, apiRestSimtasks);

  q.drain = function() {
    console.log('Total reviews added: ' + reviewsAdded);
  };

  Object.keys(restaurantsData).forEach(function(element, pos) {
    // Call orion to append the entity
    var rname = restaurantsData[pos].id;
    rname += '-' + shortid.generate();
    // Time to add first attribute to orion as first approach

    var attr = {
      'type': 'Review',
      'id': rname,
      'itemReviewed': {},
      'reviewRating': {},
      'name': 'Rating description',
      'author': {},
      'reviewBody': 'Body review',
      'publisher': {}
    };

    attr.itemReviewed.type = 'Restaurant';
    attr.itemReviewed.name = restaurantsData[pos].id;

    attr.reviewRating.type = 'Rating';
    attr.reviewRating.ratingValue = utils.randomIntInc(1, 5);

    attr.author.type = 'Person';
    attr.author.name = 'user' + utils.randomIntInc(1, 10);

    attr.publisher.type = 'Organization';
    attr.publisher.name = 'Bitergia';

    q.push({
      'attributes': attr
    }, returnPost);
  });
};

// Load restaurant data from Orion
var loadRestaurantData = function() {

  var processRestaurants = function(data) {
    restaurantsData = JSON.parse(JSON.stringify(data.body));
    feedOrionReviews();
  };

  authRequest(
    '/v2/entities',
    'GET',
    {'type': 'Restaurant', 'limit': '1000'})
  .then(processRestaurants)
  .catch(function(err) {
    console.log(err);
  });
};

console.log('Generating random reviews for restaurants ...');

loadRestaurantData();<commit_msg>Added logic in reviews generator to PATCH the score in the restaurant<commit_after>/*
 * reviews_generator.js
 * Copyright(c) 2015 Bitergia
 * Author: <NAME> <<EMAIL>>,
 * <NAME> <<EMAIL>>
 * MIT Licensed

  Generates random reviews for restaurants in orion

  First it gets all restaurant information
  Then a random automatic review is generated
  Then the review is added to Orion CB

  TODO:
  - Create more real reviews using templates for comments and random ratings
*/

// jshint node: true

'use strict';

var utils = require('../utils');
var async = require('async');
var shortid = require('shortid'); // unique ids generator
var authRequest = require('../authrequest');
var apiRestSimtasks = 1; // number of simultaneous calls to API REST
var reviewsAdded = 0;
var restaurantsData; // All data for the restaurants to be reviewed
var delay = 200; //time in ms
var restaurantsModified = 0;

var feedOrionReviews = function() {
  var returnPost = function(data) {
    reviewsAdded++;
    console.log(reviewsAdded + '/' + restaurantsData.length);
  };

  // restaurantsData = restaurantsData.slice(0,5); // debug with few items

  console.log('Feeding reviews info in orion.');
  console.log('Total tried: ' + restaurantsData.length);

  // Limit the number of calls to be done in parallel to orion
  var q = async.queue(function(task, callback) {
    var attributes = task.attributes;

    authRequest('/v2/entities', 'POST', attributes)
    .then(callback)
    .catch(function(err) {
      console.log(err);
    });
  }, apiRestSimtasks);

  q.drain = function() {
    console.log('Total reviews added: ' + reviewsAdded);
    triggerRestaurantsRatings();
  };

  Object.keys(restaurantsData).forEach(function(element, pos) {
    // Call orion to append the entity
    var rname = restaurantsData[pos].id;
    rname += '-' + shortid.generate();
    // Time to add first attribute to orion as first approach

    var attr = {
      'type': 'Review',
      'id': rname,
      'itemReviewed': {},
      'reviewRating': {},
      'name': 'Rating description',
      'author': {},
      'reviewBody': 'Body review',
      'publisher': {}
    };

    attr.itemReviewed.type = 'Restaurant';
    attr.itemReviewed.name = restaurantsData[pos].id;

    attr.reviewRating.type = 'Rating';
    attr.reviewRating.ratingValue = utils.randomIntInc(1, 5);

    attr.author.type = 'Person';
    attr.author.name = 'user' + utils.randomIntInc(1, 10);

    attr.publisher.type = 'Organization';
    attr.publisher.name = 'Bitergia';

    q.push({
      'attributes': attr
    }, returnPost);
  });
};

var triggerRestaurantsRatings = function() {

  var returnPost = function(data) {
    restaurantsModified++;
    console.log(restaurantsModified + '/' + restaurantsData.length);
  };

  var reviews;
  var restaurantReviews;

  var q = async.queue(function(task, callback) {

    setTimeout(function() {
      utils.sendRequest('PATCH', task.aggregateRatings, task.restaurantName)
      .then(callback)
      .catch(function(err) {
        console.log(err.error);
      });
    }, delay);

  }, apiRestSimtasks);

  q.drain = function() {
    console.log('Total restaurants modified: ' + restaurantsModified);
  };

  utils.getListByType('Review')
  .then(function(data) {
    reviews = data.body;
    Object.keys(restaurantsData).forEach(function(element, pos) {
      var restaurantName = restaurantsData[pos].id;
      restaurantReviews = utils.getRestaurantReviews(
        restaurantName,
        reviews);
      var ratings = utils.getAggregateRating(
        restaurantReviews);
      q.push({'aggregateRatings': ratings,
        'restaurantName': restaurantName}, returnPost);
    });
  })
  .catch(function(err) {
    console.log(err);
  });
};

// Load restaurant data from Orion
var loadRestaurantData = function() {

  var processRestaurants = function(data) {
    restaurantsData = JSON.parse(JSON.stringify(data.body));
    feedOrionReviews();
  };

  authRequest(
    '/v2/entities',
    'GET',
    {'type': 'Restaurant', 'limit': '1000'})
  .then(processRestaurants)
  .catch(function(err) {
    console.log(err);
  });
};

console.log('Generating random reviews for restaurants ...');

loadRestaurantData();
{'fraction_non_alphanumeric': 0.1372824256035935, 'fraction_numerical': 0.005895564289724873, 'mean_word_length': 3.395434916718075, 'pattern_counts': {'":': 0, '<': 15, '<?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': '2472289', 'n_tokens_mistral': 2211, 'n_tokens_neox': 2130, 'n_words': 560}