Search code examples
typescriptgremlinamazon-neptune

Query statistics on number of nodes created or edges added


OpenCypher provides statistics on number of nodes created or number of edges updated as a result of query execution :

queryResult.summary?.updateStatistics?.updates()?.nodesCreated

I'm trying to find if there's a similar mechanism for Gremlin; explain/profile API return predicates but not the information on number of edges/nodes affected.

I'd appreciate any insights.

Edit: For example, the query below:

const traversal = this.__.addV(label) .property(GREMLIN_ID, id) .property("created_timestamp", new Date().getTime()); this.logger.info("Add vertex if not exist "); 
const v = await this.g .V(id) .fold() .coalesce(this.__.unfold(), traversal) .elementMap() .next(); 
return v.value;

This would add a new vertex only if it doesn't already exist; when it does get executed though, would like to find if there's a way to see the number of "new" nodes added as a result of the query execution.

Update: With Kevin's solution, was able to retrieve edge counts as well:

g.inject(0).store('edgeCount').coalesce(
          V('0000x', 'facade00-0000-4000-a000-000000000000')
          .hasLabel("FINGERPRINT")
          .outE()
          .where(V().with('0000x', 'facade00-0000-4000-a000-000000000000')),
        V('0000x', 'facade00-0000-4000-a000-000000000000')
        .hasLabel("FINGERPRINT")
        .as("from")
        .V('0000x', 'facade00-0000-4000-a000-000000000000')
        .hasLabel("RDC_VISITOR_ID")
        .as("to")
        .addE("ANONYMOUS")
        .from("from")
        .to("to")
        .sideEffect(constant(1).store('edgeCount'))
      ).project('edge','count').by().by(cap('edgeCount').unfold().sum())

This gives:

{'edge': e[191][0000x-ANONYMOUS->facade00-0000-4000-a000-000000000000], 'count': 1}

And count becomes 0 with further executions. Thanks!


Solution

  • Here is a way to keep track of work done by the query using store. I was not able to get it to work the way I wanted using sack. If I figure that out, I will update the answer.

    The count should be one as we are creating a new vertex.

    gremlin> g.inject(0).store('x').
    ......1>   V('A1').
    ......2>   fold().
    ......3>   coalesce(
    ......4>     unfold(),
    ......5>     addV('test').property(id,'A1').sideEffect(constant(1).store('x'))).
    ......6>   project('vertex','count').
    ......7>     by().
    ......8>     by(cap('x').unfold().sum()) 
    ==>[vertex:v[A1],count:1]
    

    It's created now, so the count should be 0.

    gremlin> g.inject(0).store('x').
    ......1>   V('A1').
    ......2>   fold().
    ......3>   coalesce(
    ......4>     unfold(),
    ......5>     addV('test').property(id,'A1').sideEffect(constant(1).store('x'))).
    ......6>   project('vertex','count').
    ......7>     by().
    ......8>     by(cap('x').unfold().sum()) 
    
    ==>[vertex:v[A1],count:0]