LCOV - code coverage report
Current view: top level - Servidor/matches - UserMatcher.cpp (source / functions) Hit Total Coverage
Test: coverage_merged.info Lines: 71 80 88.8 %
Date: 2016-07-11 17:43:33 Functions: 14 15 93.3 %

          Line data    Source code
       1             : /*
       2             :  * UserMatcher.cpp
       3             :  *
       4             :  *  Created on: Jun 22, 2016
       5             :  *      Author: gaston
       6             :  */
       7             : 
       8             : #include "UserMatcher.h"
       9             : 
      10             : #include <algorithm>
      11             : #include <iterator>
      12             : 
      13             : 
      14          18 : UserMatcher::UserMatcher(){
      15             : 
      16          18 : }
      17             : 
      18             : /*
      19             :  * Deja en la lista los usuarios con las siguientes caracteristicas en relacion al usuario de la app:
      20             :  * 1) De sexo que se esta buscando.
      21             :  * 2) Con edad a no mas de AGE_RANGE anios de distancia.
      22             :  * 3) Localidad a menos de MAX_DISTANCE.
      23             :  * 4) Al menos un interes en comun.
      24             :  * 5) Que no se haya interactuado antes.
      25             :  */
      26          18 : list<User*> UserMatcher::filterPossibleMatches(User* appUser, list<User*>* users, string userMatches) {
      27          18 :         list<User*> filteredUsers;
      28          18 :         LOGG(DEBUG) << "Filtering users.";
      29          18 :         LOGG(DEBUG) << "Past interactions with: " << userMatches;
      30         308 :         for (User* user : *users) {
      31         290 :                 LOGG(DEBUG) << "Checking if user is a match candidate: " << user->getMail();
      32         290 :                 if(!sameUser(appUser, user)){
      33         275 :                         if(!userInMatches(user, userMatches)){
      34         577 :                                 if (isLookingForUserSex(appUser, user) && isInAgeRange(appUser, user) && isNearby(appUser, user)
      35         293 :                                                 && hasCommonInterest(appUser, user)) {
      36          12 :                                         LOGG(INFO) << "Found a match candidate: " << user->getMail();
      37          12 :                                         filteredUsers.push_back(user);
      38             :                                 }
      39             :                         }
      40             :                 }
      41             :         }
      42             : 
      43          18 :         return filteredUsers;
      44             : 
      45             : }
      46             : 
      47         290 : bool UserMatcher::sameUser(User* appUser, User* user){
      48         290 :         return (appUser->getID() == user->getID());
      49             : }
      50             : 
      51         275 : bool UserMatcher::userInMatches(User* user, string userMatches){
      52         275 :         string user_mail = user->getMail();
      53         550 :         CsvParser csv;
      54         550 :         std::istringstream f(userMatches);
      55         550 :         std::string line;
      56         550 :         while (std::getline(f, line)) {
      57           0 :                 vector<string> elems = csv.parseLine(&line);
      58           0 :                 if (elems[MAIL_FULL_IDX].compare(user_mail) == 0) {
      59           0 :                         LOGG(DEBUG)<< "Already interacted with " << user_mail;
      60           0 :                         return true;
      61             :                 }
      62           0 :         }
      63         550 :         return false;
      64             : }
      65             : 
      66          42 : bool UserMatcher::contains(vector<string> vect, const string& stringToFind) {
      67          42 :         return (find(vect.begin(), vect.end(), stringToFind) != vect.end());
      68             : }
      69             : 
      70         275 : bool UserMatcher::isLookingForUserSex(User* appUser, User* user) {
      71         275 :         string userSex = user->getSex();
      72         550 :         string category = SEX_CATEGORY;
      73             : 
      74         275 :         Interests* appUserInterests = appUser->getInterests();
      75             : 
      76         550 :         map<string,vector<string>> appUserInterestMap = appUserInterests->allInterests();
      77             : 
      78         275 :         if ( appUserInterestMap.find(category) != appUserInterestMap.end() ) {
      79          42 :                 vector<string> sexesLookingFor = appUserInterestMap.at(category);
      80          42 :                 if (contains(sexesLookingFor, userSex)) {
      81          27 :                         return true;
      82          15 :                 }
      83             :         }
      84         523 :         return false;
      85             : }
      86             : 
      87          27 : bool UserMatcher::isInAgeRange(User* appUser, User* user) {
      88          27 :         int diff = user->getAge() - appUser->getAge();
      89          27 :         return (abs(diff) <= AGE_RANGE);
      90             : }
      91             : 
      92          18 : bool UserMatcher::isNearby(User* appUser, User* user) {
      93          18 :         float userX = user->getX();
      94          18 :         float userY = user->getY();
      95          18 :         float appUserX = appUser->getX();
      96          18 :         float appUserY = appUser->getY();
      97             : 
      98          18 :         float distance = sqrt(pow(userX - appUserX, 2) + pow(userY - appUserY, 2));
      99             : 
     100          18 :         return (distance <= MAX_DISTANCE);
     101             : 
     102             : }
     103             : 
     104          12 : bool atLeastOneValueInCommon(vector<string> values1, vector<string> values2){
     105          12 :         for(string str1 : values1){
     106          18 :                 for (string str2 : values2){
     107          18 :                         if(str1 == str2){
     108          12 :                                 return true;
     109             :                         }
     110           6 :                 }
     111           0 :         }
     112           0 :         return false;
     113             : }
     114             : 
     115          12 : bool UserMatcher::containsKey(map<string, vector<string> >& map, const string& keyToFind) {
     116          12 :         return map.find(keyToFind) != map.end();
     117             : }
     118             : 
     119          18 : bool UserMatcher::hasCommonInterest(User* appUser, User* user) {
     120          18 :         Interests* userInterests = user->getInterests();
     121          18 :         Interests* appUserInterests = appUser->getInterests();
     122             : 
     123          18 :         map<string,vector<string>> userInterestMap = userInterests->allInterests();
     124          36 :         map<string,vector<string>> appUserInterestMap = appUserInterests->allInterests();
     125             : 
     126          18 :         for (map<string,vector<string>>::iterator it=userInterestMap.begin(); it!=userInterestMap.end(); ++it){
     127          12 :             string category = it->first;
     128          12 :             vector<string> values = it->second;
     129          12 :             if (category != SEX_CATEGORY){
     130          12 :                         if (containsKey(appUserInterestMap, category)) {
     131          12 :                                 vector<string> appUserValues = appUserInterestMap.at(category);
     132          12 :                                 if (atLeastOneValueInCommon(values, appUserValues)){
     133          12 :                                         return true;
     134           0 :                                 }
     135             :                         }
     136             :             }
     137           0 :         }
     138          24 :         return false;
     139             : }
     140             : 
     141          18 : UserMatcher::~UserMatcher() {
     142             :         // TODO Auto-generated destructor stub
     143          84 : }
     144             : 

Generated by: LCOV version 1.12-4-g04a3c0e