package marcelocf.janusgraph;

import org.apache.tinkerpop.gremlin.process.traversal.P;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Vertex;

import java.util.Date;

/**
 * Created by marcelo on 17/06/21.
 */
public class HadoopQueryRunner extends QueryRunner {

  public HadoopQueryRunner(GraphTraversalSource traversalSource, String userName) throws Exception {
    super(traversalSource, userName);
  }

  public long countCommonFollowedUsers(String userB) {
    return countCommonFollowedUsers(getUser(userB).next());
  }

  public long countCommonFollowedUsers(Vertex otherUser) {
    return g.
        V(otherUser.id()).aggregate("otherUser"). // store otherUser in a side effect for later usage
        V(getUser().next().id()).                     // then starts traversing from userA
        out("follows").
        in("follows").
        where(P.within("otherUser")).
        count().
        next(); // we don't need to check for existence because this can be just null
  }



  public long countPostsPerDaySince(long since) {
    return countPostsPerDaySince(getUser().next(), since);
  }

  public long countPostsPerDaySince(String userName, long since) {
    return countPostsPerDaySince(getUser(userName).next(), since);
  }

  public long countPostsPerDaySince(Vertex user, long since) {
    long days = ( (new Date()).getTime() - since ) / (1000*60*60*24);
    if(days == 0) {
      days = 1;
    }
    return countPostsSince(user, since) / days;
  }

  public long countPostsSince(Vertex user, long since) {
    return g.
        V(user).
        outE(Schema.POSTS).
        has(Schema.CREATED_AT, P.gte(since)).
        count().
        next();
  }

}

@@ -163,92 +163,431 @@
 rsal
-Source;%0Aimport org.apache.tinkerpop.gremlin.structure.Vertex;%0A%0Aimport java.util.Date
+;%0Aimport org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;%0Aimport org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;%0Aimport org.apache.tinkerpop.gremlin.structure.Vertex;%0A%0Aimport java.util.Date;%0A%0Aimport static marcelocf.janusgraph.Schema.*;%0Aimport static org.apache.tinkerpop.gremlin.process.traversal.Order.decr;%0Aimport static org.apache.tinkerpop.gremlin.process.traversal.P.without
 ;%0A%0A/
@@ -1406,17 +1406,16 @@
 l%0A  %7D%0A%0A%0A
-%0A
   public
@@ -2087,10 +2087,904 @@
 );%0A  %7D%0A%0A
+  @Override%0A  public GraphTraversal%3CVertex, Vertex%3E getFollowRecommendation()%7B%0A    return getUser().%0A        aggregate(%22me%22).%0A        aggregate(%22ignore%22).%0A        outE(Schema.FOLLOWS).%0A        order().by(CreateWeightIndex.WEIGHT, decr).%0A        outV().%0A        aggregate(%22ignore%22).%0A        cap(%22me%22).%0A        unfold().%0A        inE(Schema.FOLLOWS).%0A        order().by(CreateWeightIndex.WEIGHT, decr).%0A        inV().%0A        where(without(%22ignore%22));%0A  %7D%0A%0A  @Override%0A  public GraphTraversal%3CVertex, Vertex%3E getTimeline(int limit)%7B%0A    return getUser().%0A        aggregate(%22users%22).%0A        local(%0A          __.outE(FOLLOWS).%0A          order().by(CreateWeightIndex.WEIGHT, decr).%0A          limit(50).%0A          outV()%0A        ).%0A        aggregate(%22users%22).%0A        cap(%22users%22).%0A        unfold().%0A        outE(POSTS).%0A        order().by(CREATED_AT, decr).%0A        limit(limit).%0A        inV();%0A  %7D%0A
 %7D%0A

implement newer queries using the weight