-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0cd4855
commit d77ce48
Showing
5 changed files
with
234 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/main/java/com/maxdemarzi/recommendations/Recommendations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package com.maxdemarzi.recommendations; | ||
|
||
import com.maxdemarzi.RelationshipTypes; | ||
import org.codehaus.jackson.map.ObjectMapper; | ||
import org.neo4j.graphdb.*; | ||
|
||
import javax.ws.rs.*; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.Response; | ||
import java.io.IOException; | ||
import java.util.*; | ||
import java.util.concurrent.atomic.LongAdder; | ||
|
||
import static com.maxdemarzi.Properties.EMAIL; | ||
import static com.maxdemarzi.Properties.PASSWORD; | ||
import static com.maxdemarzi.users.Users.findUser; | ||
import static java.util.Collections.reverseOrder; | ||
|
||
@Path("/users/{username}/recommendations") | ||
public class Recommendations { | ||
|
||
private static final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
@GET | ||
@Path("/friends") | ||
public Response recommendFriends(@PathParam("username") final String username, | ||
@QueryParam("limit") @DefaultValue("25") final Integer limit, | ||
@Context GraphDatabaseService db) throws IOException { | ||
ArrayList<Map<String, Object>> results = new ArrayList<>(); | ||
try (Transaction tx = db.beginTx()) { | ||
Node user = findUser(username, db); | ||
HashSet<Long> following = new HashSet<>(); | ||
for (Relationship r1: user.getRelationships(Direction.OUTGOING, RelationshipTypes.FOLLOWS)) { | ||
following.add(r1.getEndNode().getId()); | ||
} | ||
|
||
for (Relationship r1 : user.getRelationships(Direction.INCOMING, RelationshipTypes.FOLLOWS)) { | ||
Node follower = r1.getStartNode(); | ||
if (!following.contains(follower.getId())) { | ||
Map<String, Object> properties = r1.getStartNode().getAllProperties(); | ||
properties.remove(PASSWORD); | ||
properties.remove(EMAIL); | ||
results.add(properties); | ||
} | ||
} | ||
tx.success(); | ||
} | ||
return Response.ok().entity(objectMapper.writeValueAsString( | ||
results.subList(0, Math.min(results.size(), limit)))) | ||
.build(); | ||
} | ||
|
||
@GET | ||
@Path("/follows") | ||
public Response recommendFollows(@PathParam("username") final String username, | ||
@QueryParam("limit") @DefaultValue("25") final Integer limit, | ||
@Context GraphDatabaseService db) throws IOException { | ||
ArrayList<Map<String, Object>> results = new ArrayList<>(); | ||
try (Transaction tx = db.beginTx()) { | ||
Node user = findUser(username, db); | ||
HashSet<Node> following = new HashSet<>(); | ||
for (Relationship r1: user.getRelationships(Direction.OUTGOING, RelationshipTypes.FOLLOWS)) { | ||
following.add(r1.getEndNode()); | ||
} | ||
|
||
HashMap<Node, LongAdder> fofs = new HashMap<>(); | ||
|
||
for (Node user2 : following) { | ||
for (Relationship r1 : user2.getRelationships(Direction.OUTGOING, RelationshipTypes.FOLLOWS)) { | ||
Node fof = r1.getEndNode(); | ||
if(fofs.containsKey(fof)) { | ||
fofs.get(fof).increment(); | ||
} else { | ||
LongAdder counter = new LongAdder(); | ||
counter.increment(); | ||
fofs.put(fof, counter); | ||
} | ||
} | ||
} | ||
fofs.remove(user); | ||
ArrayList<Map.Entry<Node, LongAdder>> fofList = new ArrayList<>(fofs.entrySet()); | ||
fofList.sort(Comparator.comparing(m -> (Long) m.getValue().longValue(), reverseOrder())); | ||
for (Map.Entry<Node, LongAdder> entry : fofList.subList(0, Math.min(fofList.size(), limit))) { | ||
Map<String, Object> properties = entry.getKey().getAllProperties(); | ||
properties.remove(PASSWORD); | ||
properties.remove(EMAIL); | ||
results.add(properties); | ||
} | ||
|
||
tx.success(); | ||
} | ||
return Response.ok().entity(objectMapper.writeValueAsString(results)).build(); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/test/java/com/maxdemarzi/recommendations/RecommendFollowsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.maxdemarzi.recommendations; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.neo4j.harness.junit.Neo4jRule; | ||
import org.neo4j.test.server.HTTP; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
|
||
public class RecommendFollowsTest { | ||
@Rule | ||
public Neo4jRule neo4j = new Neo4jRule() | ||
.withFixture(FIXTURE) | ||
.withExtension("/v1", Recommendations.class); | ||
|
||
@Test | ||
public void shouldRecommendFollows() { | ||
HTTP.POST(neo4j.httpURI().resolve("/v1/schema/create").toString()); | ||
|
||
HTTP.Response response = HTTP.GET(neo4j.httpURI().resolve("/v1/users/maxdemarzi/recommendations/follows").toString()); | ||
ArrayList<HashMap> actual = response.content(); | ||
Assert.assertEquals(expected, actual); | ||
} | ||
private static final String FIXTURE = | ||
"CREATE (max:User {username:'maxdemarzi', " + | ||
"email: 'max@neo4j.com', " + | ||
"name: 'Max De Marzi'," + | ||
"password: 'swordfish'})" + | ||
"CREATE (jexp:User {username:'jexp', " + | ||
"email: 'michael@neo4j.com', " + | ||
"name: 'Michael Hunger'," + | ||
"password: 'tunafish'})" + | ||
"CREATE (laeg:User {username:'laexample', " + | ||
"email: 'luke@neo4j.com', " + | ||
"name: 'Luke Gannon'," + | ||
"password: 'cuddlefish'})" + | ||
"CREATE (stefan:User {username:'darthvader42', " + | ||
"email: 'stefan@neo4j.com', " + | ||
"name: 'Stefan Armbruster'," + | ||
"password: 'catfish'})" + | ||
"CREATE (mark:User {username:'markhneedham', " + | ||
"email: 'mark@neo4j.com', " + | ||
"name: 'Mark Needham'," + | ||
"password: 'jellyfish'})" + | ||
|
||
"CREATE (max)-[:FOLLOWS]->(laeg)" + | ||
"CREATE (max)-[:FOLLOWS]->(jexp)" + | ||
"CREATE (laeg)-[:FOLLOWS]->(stefan)" + | ||
"CREATE (laeg)-[:FOLLOWS]->(mark)" + | ||
"CREATE (jexp)-[:FOLLOWS]->(mark)"; | ||
|
||
private static final ArrayList<HashMap<String, Object>> expected = new ArrayList<HashMap<String, Object>>() {{ | ||
add(new HashMap<String, Object>() {{ | ||
put("username", "markhneedham"); | ||
put("name", "Mark Needham"); | ||
}}); | ||
add(new HashMap<String, Object>() {{ | ||
put("username", "darthvader42"); | ||
put("name", "Stefan Armbruster"); | ||
}}); | ||
|
||
}}; | ||
} |
48 changes: 48 additions & 0 deletions
48
src/test/java/com/maxdemarzi/recommendations/RecommendFriendsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.maxdemarzi.recommendations; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.neo4j.harness.junit.Neo4jRule; | ||
import org.neo4j.test.server.HTTP; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
|
||
public class RecommendFriendsTest { | ||
@Rule | ||
public Neo4jRule neo4j = new Neo4jRule() | ||
.withFixture(FIXTURE) | ||
.withExtension("/v1", Recommendations.class); | ||
|
||
@Test | ||
public void shouldRecommendFriends() { | ||
HTTP.POST(neo4j.httpURI().resolve("/v1/schema/create").toString()); | ||
|
||
HTTP.Response response = HTTP.GET(neo4j.httpURI().resolve("/v1/users/maxdemarzi/recommendations/friends").toString()); | ||
ArrayList<HashMap> actual = response.content(); | ||
Assert.assertEquals(expected, actual); | ||
} | ||
private static final String FIXTURE = | ||
"CREATE (max:User {username:'maxdemarzi', " + | ||
"email: 'max@neo4j.com', " + | ||
"name: 'Max De Marzi'," + | ||
"password: 'swordfish'})" + | ||
"CREATE (jexp:User {username:'jexp', " + | ||
"email: 'michael@neo4j.com', " + | ||
"name: 'Michael Hunger'," + | ||
"password: 'tunafish'})" + | ||
"CREATE (laeg:User {username:'laexample', " + | ||
"email: 'luke@neo4j.com', " + | ||
"name: 'Luke Gannon'," + | ||
"password: 'cuddlefish'})" + | ||
"CREATE (max)-[:FOLLOWS]->(laeg)" + | ||
"CREATE (max)<-[:FOLLOWS]-(jexp)"; | ||
|
||
private static final ArrayList<HashMap<String, Object>> expected = new ArrayList<HashMap<String, Object>>() {{ | ||
add(new HashMap<String, Object>() {{ | ||
put("username", "jexp"); | ||
put("name", "Michael Hunger"); | ||
}}); | ||
}}; | ||
} |