From 738ea6ee65b159d229205c59dbe99308c2e148da Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andreas=20Sch=C3=A4rtl?= <andreas@schaertl.me>
Date: Thu, 21 May 2020 15:11:27 +0200
Subject: [PATCH] uloapi: fetch number of triplets and return in /statistics

---
 .../info/mathhub/uloapi/query/GraphDB.java    |  2 +-
 .../java/info/mathhub/uloapi/query/Query.java | 27 ++++++++++++++++++-
 .../info/mathhub/uloapi/query/Statistics.java |  2 +-
 .../java/info/mathhub/uloapi/rest/Routes.java |  2 +-
 .../src/main/resources/uloapi.properties      |  4 +--
 5 files changed, 31 insertions(+), 6 deletions(-)

diff --git a/experimental/uloapi/src/main/java/info/mathhub/uloapi/query/GraphDB.java b/experimental/uloapi/src/main/java/info/mathhub/uloapi/query/GraphDB.java
index 055c86d..7d3983d 100644
--- a/experimental/uloapi/src/main/java/info/mathhub/uloapi/query/GraphDB.java
+++ b/experimental/uloapi/src/main/java/info/mathhub/uloapi/query/GraphDB.java
@@ -23,7 +23,7 @@ public class GraphDB {
      * Functional interface that defines a function for running operations on
      * a GraphDB database.
      */
-    private interface Operation<T> {
+    public interface Operation<T> {
         /**
          * Run some operation connected to a database.
          *
diff --git a/experimental/uloapi/src/main/java/info/mathhub/uloapi/query/Query.java b/experimental/uloapi/src/main/java/info/mathhub/uloapi/query/Query.java
index c3d8612..70175d7 100644
--- a/experimental/uloapi/src/main/java/info/mathhub/uloapi/query/Query.java
+++ b/experimental/uloapi/src/main/java/info/mathhub/uloapi/query/Query.java
@@ -1,7 +1,32 @@
 package info.mathhub.uloapi.query;
 
+import info.mathhub.uloapi.config.Config;
+
 public class Query {
+    private final String serverUrl;
+    private final String repository;
+
+    private static Query singleton;
+
+    public Query(String serverUrl, String repository) {
+        this.serverUrl = serverUrl;
+        this.repository = repository;
+    }
+
+    public static synchronized Query getSingleton() {
+        if (Query.singleton == null) {
+            Query.singleton = new Query(Config.serverUrl(), Config.repository());
+        }
+
+        return Query.singleton;
+    }
+
     public Statistics getStatistics() {
-        return new Statistics(10);
+        final GraphDB.Operation<Long> operation = (manager, repository, connection) -> {
+            return connection.size();
+        };
+
+        final long numTriplets = GraphDB.execute(this.serverUrl, this.repository, operation);
+        return new Statistics(numTriplets);
     }
 }
diff --git a/experimental/uloapi/src/main/java/info/mathhub/uloapi/query/Statistics.java b/experimental/uloapi/src/main/java/info/mathhub/uloapi/query/Statistics.java
index f32b9db..380e6ca 100644
--- a/experimental/uloapi/src/main/java/info/mathhub/uloapi/query/Statistics.java
+++ b/experimental/uloapi/src/main/java/info/mathhub/uloapi/query/Statistics.java
@@ -9,7 +9,7 @@ public class Statistics {
      */
     public long numTriplets;
 
-    Statistics(int numTriplets) {
+    Statistics(long numTriplets) {
         this.numTriplets = numTriplets;
     }
 }
diff --git a/experimental/uloapi/src/main/java/info/mathhub/uloapi/rest/Routes.java b/experimental/uloapi/src/main/java/info/mathhub/uloapi/rest/Routes.java
index 9202dfb..d563690 100644
--- a/experimental/uloapi/src/main/java/info/mathhub/uloapi/rest/Routes.java
+++ b/experimental/uloapi/src/main/java/info/mathhub/uloapi/rest/Routes.java
@@ -18,7 +18,7 @@ public class Routes {
     };
 
     public static final Route statistics = (Request request, Response response) -> {
-        return new Query().getStatistics();
+        return Query.getSingleton().getStatistics();
     };
 
     public static final Route name = (Request request, Response response) -> {
diff --git a/experimental/uloapi/src/main/resources/uloapi.properties b/experimental/uloapi/src/main/resources/uloapi.properties
index b67e220..ee82139 100644
--- a/experimental/uloapi/src/main/resources/uloapi.properties
+++ b/experimental/uloapi/src/main/resources/uloapi.properties
@@ -1,2 +1,2 @@
-info.mathhub.uloapi.config.serverurl="http://graphdb:7200"
-info.mathhub.uloapi.config.repository="myulo"
\ No newline at end of file
+info.mathhub.uloapi.config.serverurl=http://rdf:7200
+info.mathhub.uloapi.config.repository=myulo
\ No newline at end of file
-- 
GitLab