Skip to content

Commit

Permalink
Update jaacard algorithm to suggest User
Browse files Browse the repository at this point in the history
  • Loading branch information
QuangChanVi committed Nov 28, 2023
1 parent 52338db commit c1d3762
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DATABASE_URL=
IS_LOGGING=1

# Link Public = https://roxqm2ljb8.execute-api.ap-southeast-1.amazonaws.com/
# For tool generatePosts
AWS_ACCESS_KEY=
AWS_SECRET_ACCESS=
Expand Down
19 changes: 11 additions & 8 deletions src/resolvers/Common/suggestUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,15 @@ export class UserSuggestion {
userList.forEach((user) => {
// Getting Similar for skill
const userSkillIds = user.endorsements.map((each) => each.skillId);
const skillSimilar = this.getJaacardIndex(currUserSkillIds, userSkillIds);
const skillSimilar = this.#getJaacardIndex(
currUserSkillIds,
userSkillIds,
);
similarMap.set(user.id, skillSimilar * this._configs.following);

// Getting Similar for interesst
const userInterestIds = user.interestCategories.map((each) => each.id);
const interestSimilar = this.getJaacardIndex(
const interestSimilar = this.#getJaacardIndex(
currUserInterestIds,
userInterestIds,
);
Expand All @@ -89,7 +92,7 @@ export class UserSuggestion {
const userFollowingIds = user.followings.userFollowing.map(
(each) => each.id,
);
const followingSimilar = this.getJaacardIndex(
const followingSimilar = this.#getJaacardIndex(
currUserFollowingIds,
userFollowingIds,
);
Expand Down Expand Up @@ -121,12 +124,12 @@ export class UserSuggestion {
* @param {Ids} dataset1
* @param {Ids} dataset2
*/
getJaacardIndex = (dataset1, dataset2) => {
#getJaacardIndex = (dataset1, dataset2) => {
if (dataset1.length === 0 || dataset2.length === 0) {
return 0;
}
const intersection = this.getIntersection(dataset1, dataset2);
const union = this.getUnion(dataset1, dataset2);
const intersection = this.#getIntersection(dataset1, dataset2);
const union = this.#getUnion(dataset1, dataset2);

return intersection.length / union.length;
};
Expand All @@ -135,15 +138,15 @@ export class UserSuggestion {
* @param {Ids} dataset1
* @param {Ids} dataset2
*/
getIntersection = (dataset1, dataset2) => {
#getIntersection = (dataset1, dataset2) => {
return dataset1.filter((value) => dataset2.includes(value));
};

/**
* @param {Ids} dataset1
* @param {Ids} dataset2
*/
getUnion = (dataset1, dataset2) => {
#getUnion = (dataset1, dataset2) => {
return [...new Set([...dataset1, ...dataset2])];
};
}
70 changes: 35 additions & 35 deletions src/resolvers/Query/USER.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _ from 'lodash';
import _, { indexOf } from 'lodash';
import { prisma } from '../../prisma/database.js';
import { DEFAULT_LIMIT } from '../../constants.js';
import { UserSuggestion } from '../Common/suggestUser.js';
Expand Down Expand Up @@ -43,31 +43,14 @@ const userQuery = {
where: {
id: userId,
},
// include: {
// endorsements: true,
// followings: true,
// interestCategories: true,
// },
include: {
endorsements: true,
followings: true,
interestCategories: true,
},
});

// const testAll = await prisma.user.findMany({
// where: {
// id: { not: userId },
// isAdmin: 0,
// NOT: {
// id: { in: currentUser.userFollowing },
// },
// },
// include: {
// endorsements: true,
// followings: true,
// interestCategories: true,
// },
// });
// const test = new UserSuggestion();
// const testSuggest = test.getUserSuggestion(currentUser, testAll);

const [result, count] = await Promise.all([
const [allUsers, count] = await Promise.all([
prisma.user.findMany({
where: {
id: { not: userId },
Expand All @@ -76,15 +59,11 @@ const userQuery = {
id: { in: currentUser.userFollowing },
},
},
take: limit || DEFAULT_LIMIT,
...(after && {
skip: 1,
}),
...(after && {
cursor: {
id: after,
},
}),
include: {
endorsements: true,
followings: true,
interestCategories: true,
},
}),
prisma.user.count({
where: {
Expand All @@ -97,8 +76,13 @@ const userQuery = {
}),
]);

// console.log('Result', result);
// console.log('count', count);
const userSuggestion = new UserSuggestion();
const suggestedUsers = userSuggestion.getUserSuggestion(
currentUser,
allUsers,
);

const result = manualPagination(limit, after, suggestedUsers);

const hasNextPage =
result.length !== 0 && result.length < count && result.length === limit;
Expand Down Expand Up @@ -150,4 +134,20 @@ const userQuery = {
},
};

/**
* @param {number} limit
* @param {string} index
* @param {import('../Common/suggestUser.js').UserInfo[]} list
*/
const manualPagination = (limit = DEFAULT_LIMIT, after, list) => {
if (!after) {
const result = list.slice(0, limit);
return result;
}

const breakPoint = list.findIndex((each) => each.id === after);
const result = list.slice(breakPoint, limit + breakPoint);
return result;
};

export default userQuery;

0 comments on commit c1d3762

Please sign in to comment.