changeset 238:e4148b0ab1d0

statistics URI
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 30 Nov 2012 16:09:24 +0100
parents 16a4ac70bd8a
children 30887f3593dd
files NEWS OrthancServer/DatabaseWrapper.cpp OrthancServer/DatabaseWrapper.h OrthancServer/OrthancRestApi.cpp OrthancServer/ServerIndex.cpp OrthancServer/ServerIndex.h
diffstat 6 files changed, 47 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Fri Nov 30 15:45:15 2012 +0100
+++ b/NEWS	Fri Nov 30 16:09:24 2012 +0100
@@ -15,6 +15,7 @@
 Minor changes
 -------------
 
+* "/statistics" URI
 * "last" flag to retrieve the last change from the "/changes" URI
 * Generate a sample configuration file from command line
 * "CompletedSeries" event in the changes API
--- a/OrthancServer/DatabaseWrapper.cpp	Fri Nov 30 15:45:15 2012 +0100
+++ b/OrthancServer/DatabaseWrapper.cpp	Fri Nov 30 16:09:24 2012 +0100
@@ -739,4 +739,21 @@
     db_.Register(signalRemainingAncestor_);
     db_.Register(new Internals::SignalFileDeleted(listener_));
   }
+
+  uint64_t DatabaseWrapper::GetResourceCount(ResourceType resourceType)
+  {
+    SQLite::Statement s(db_, SQLITE_FROM_HERE, 
+                        "SELECT COUNT(*) FROM Resources WHERE resourceType=?");
+    s.BindInt(0, resourceType);
+    
+    if (!s.Step())
+    {
+      throw OrthancException(ErrorCode_InternalError);
+    }
+
+    int64_t c = s.ColumnInt(0);
+    assert(!s.Step());
+
+    return c;
+  }
 }
--- a/OrthancServer/DatabaseWrapper.h	Fri Nov 30 15:45:15 2012 +0100
+++ b/OrthancServer/DatabaseWrapper.h	Fri Nov 30 16:09:24 2012 +0100
@@ -167,12 +167,15 @@
 
     void GetLastExportedResource(Json::Value& target);
 
+    // For unit testing only!
     int64_t GetTableRecordCount(const std::string& table);
     
     uint64_t GetTotalCompressedSize();
     
     uint64_t GetTotalUncompressedSize();
 
+    uint64_t GetResourceCount(ResourceType resourceType);
+
     void GetAllPublicIds(Json::Value& target,
                          ResourceType resourceType);
 
--- a/OrthancServer/OrthancRestApi.cpp	Fri Nov 30 15:45:15 2012 +0100
+++ b/OrthancServer/OrthancRestApi.cpp	Fri Nov 30 16:09:24 2012 +0100
@@ -296,15 +296,19 @@
   static void GetSystemInformation(RestApi::GetCall& call)
   {
     RETRIEVE_CONTEXT(call);
+    Json::Value result = Json::objectValue;
 
-    Json::Value result = Json::objectValue;
     result["Version"] = ORTHANC_VERSION;
     result["Name"] = GetGlobalStringParameter("Name", "");
-    result["TotalCompressedSize"] = boost::lexical_cast<std::string>
-      (context.GetIndex().GetTotalCompressedSize());
-    result["TotalUncompressedSize"] = boost::lexical_cast<std::string>
-      (context.GetIndex().GetTotalUncompressedSize());
+
+    call.GetOutput().AnswerJson(result);
+  }
 
+  static void GetStatistics(RestApi::GetCall& call)
+  {
+    RETRIEVE_CONTEXT(call);
+    Json::Value result = Json::objectValue;
+    context.GetIndex().ComputeStatistics(result);
     call.GetOutput().AnswerJson(result);
   }
 
@@ -631,6 +635,7 @@
 
     Register("/", ServeRoot);
     Register("/system", GetSystemInformation);
+    Register("/statistics", GetStatistics);
     Register("/changes", GetChanges);
     Register("/exports", GetExports);
 
--- a/OrthancServer/ServerIndex.cpp	Fri Nov 30 15:45:15 2012 +0100
+++ b/OrthancServer/ServerIndex.cpp	Fri Nov 30 16:09:24 2012 +0100
@@ -350,17 +350,24 @@
   }
 
 
-  uint64_t ServerIndex::GetTotalCompressedSize()
+  void ServerIndex::ComputeStatistics(Json::Value& target)
   {
     boost::mutex::scoped_lock lock(mutex_);
-    return db_->GetTotalCompressedSize();
-  }
+    target = Json::objectValue;
 
-  uint64_t ServerIndex::GetTotalUncompressedSize()
-  {
-    boost::mutex::scoped_lock lock(mutex_);
-    return db_->GetTotalUncompressedSize();
-  }
+    uint64_t cs = db_->GetTotalCompressedSize();
+    uint64_t us = db_->GetTotalUncompressedSize();
+    target["TotalDiskSize"] = boost::lexical_cast<std::string>(cs);
+    target["TotalUncompressedSize"] = boost::lexical_cast<std::string>(us);
+    target["TotalDiskSizeMB"] = boost::lexical_cast<unsigned int>(cs / (1024llu * 1024llu));
+    target["TotalUncompressedSizeMB"] = boost::lexical_cast<unsigned int>(us / (1024llu * 1024llu));
+
+    target["CountPatients"] = static_cast<unsigned int>(db_->GetResourceCount(ResourceType_Patient));
+    target["CountStudies"] = static_cast<unsigned int>(db_->GetResourceCount(ResourceType_Study));
+    target["CountSeries"] = static_cast<unsigned int>(db_->GetResourceCount(ResourceType_Series));
+    target["CountInstances"] = static_cast<unsigned int>(db_->GetResourceCount(ResourceType_Instance));
+  }          
+
 
 
   SeriesStatus ServerIndex::GetSeriesStatus(int id)
--- a/OrthancServer/ServerIndex.h	Fri Nov 30 15:45:15 2012 +0100
+++ b/OrthancServer/ServerIndex.h	Fri Nov 30 16:09:24 2012 +0100
@@ -79,9 +79,7 @@
                       const Attachments& attachments,
                       const std::string& remoteAet);
 
-    uint64_t GetTotalCompressedSize();
-
-    uint64_t GetTotalUncompressedSize();
+    void ComputeStatistics(Json::Value& target);                        
 
     bool LookupResource(Json::Value& result,
                         const std::string& publicId,