package neo4j.repositories;

import neo4j.models.log.LogCategory;
import neo4j.models.log.PerformanceLog;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.annotation.QueryResult;
import org.springframework.data.neo4j.annotation.ResultColumn;
import org.springframework.data.neo4j.repository.GraphRepository;

import java.util.List;

public interface PerformanceLogRepository extends GraphRepository<PerformanceLog> {

  List<PerformanceLog> findByLogCategory(LogCategory logCategory);

  @Query("START perf=node:__types__(className=\"neo4j.models.log.PerformanceLog\") RETURN min(perf.duration) as min, max(perf.duration) as max, avg(perf.duration) as avg, count(perf) as total")
  RequestResult findRequestResult();

  @Query("START perf=node:__types__(className=\"neo4j.models.log.PerformanceLog\") WHERE has(perf.httpUrl) WITH distinct perf.httpUrl as url, perf WHERE perf.httpUrl = url WITH url, perf as matches RETURN url, count(matches) as total, min(matches.duration) as min, max(matches.duration) as max, avg(matches.duration) as avg")
  List<UrlMap> findRequestMaps();

  @Query("START perf=node:__types__(className=\"neo4j.models.log.PerformanceLog\") WHERE perf.created >= \"\" + (timestamp()- {0}) return count(perf) as result")
  Long findTimeResult(long diff);

  @QueryResult
  public interface UrlMap {

    @ResultColumn("url")
    String getUrl();

    @ResultColumn("max")
    long getMax();

    @ResultColumn("min")
    long getMin();

    @ResultColumn("avg")
    long getAvg();

    @ResultColumn("total")
    long getTotal();
  }

  @QueryResult
  public interface RequestResult {

    @ResultColumn("min")
    long getMin();

    @ResultColumn("max")
    long getMax();

    @ResultColumn("avg")
    long getAvg();

    @ResultColumn("total")
    long getTotal();
  }
}

@@ -538,63 +538,20 @@
 ry(%22
-START perf=node:__types__(className=%5C%22neo4j.models.log.
+MATCH(perf: 
 Perf
@@ -560,18 +560,16 @@
 manceLog
-%5C%22
 ) RETURN
@@ -725,63 +725,20 @@
 ry(%22
-START perf=node:__types__(className=%5C%22neo4j.models.log.
+MATCH(perf: 
 Perf
@@ -739,34 +739,32 @@
 : PerformanceLog
-%5C%22
 ) WHERE has(perf
@@ -1041,63 +1041,20 @@
 ry(%22
-START perf=node:__types__(className=%5C%22neo4j.models.log.
+MATCH(perf: 
 Perf
@@ -1063,18 +1063,16 @@
 manceLog
-%5C%22
 ) WHERE 

use match statements