changeset 3177:053e72ff9fc5

new metrics: orthanc_jobs_[pending|running|completed]
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 30 Jan 2019 10:24:12 +0100
parents 784bbb03fb54
children 6d558598d713
files Core/JobsEngine/JobsRegistry.cpp Core/JobsEngine/JobsRegistry.h OrthancServer/OrthancRestApi/OrthancRestSystem.cpp
diffstat 3 files changed, 54 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/Core/JobsEngine/JobsRegistry.cpp	Tue Jan 29 18:07:41 2019 +0100
+++ b/Core/JobsEngine/JobsRegistry.cpp	Wed Jan 30 10:24:12 2019 +0100
@@ -1398,4 +1398,44 @@
       }
     }
   }
+
+
+  void JobsRegistry::GetStatistics(unsigned int& pending,
+                                   unsigned int& running,
+                                   unsigned int& completed)
+  {
+    boost::mutex::scoped_lock lock(mutex_);
+    CheckInvariants();
+
+    pending = 0;
+    running = 0;
+    completed = 0;
+    
+    for (JobsIndex::const_iterator it = jobsIndex_.begin();
+         it != jobsIndex_.end(); ++it)
+    {
+      JobHandler& job = *it->second;
+
+      switch (job.GetState())
+      {
+        case JobState_Retry:
+        case JobState_Paused:
+        case JobState_Pending:
+          pending ++;
+          break;
+
+        case JobState_Running:
+          running ++;
+          break;
+          
+        case JobState_Success:
+        case JobState_Failure:
+          completed ++;
+          break;
+
+        default:
+          throw OrthancException(ErrorCode_InternalError);
+      }
+    }    
+  }
 }
--- a/Core/JobsEngine/JobsRegistry.h	Tue Jan 29 18:07:41 2019 +0100
+++ b/Core/JobsEngine/JobsRegistry.h	Wed Jan 30 10:24:12 2019 +0100
@@ -199,6 +199,10 @@
 
     void ResetObserver();
 
+    void GetStatistics(unsigned int& pending,
+                       unsigned int& running,
+                       unsigned int& completed);
+
     class RunningJob : public boost::noncopyable
     {
     private:
--- a/OrthancServer/OrthancRestApi/OrthancRestSystem.cpp	Tue Jan 29 18:07:41 2019 +0100
+++ b/OrthancServer/OrthancRestApi/OrthancRestSystem.cpp	Wed Jan 30 10:24:12 2019 +0100
@@ -407,22 +407,27 @@
   
   static void GetMetricsPrometheus(RestApiGetCall& call)
   {
-    static const uint64_t MEGA_BYTES = 1024 * 1024;
+    static const float MEGA_BYTES = 1024 * 1024;
 
     ServerContext& context = OrthancRestApi::GetContext(call);
 
-    MetricsRegistry& registry = context.GetMetricsRegistry();
-
     uint64_t diskSize, uncompressedSize, countPatients, countStudies, countSeries, countInstances;
     context.GetIndex().GetGlobalStatistics(diskSize, uncompressedSize, countPatients, 
                                            countStudies, countSeries, countInstances);
 
-    registry.SetValue("orthanc_disk_size_mb", static_cast<unsigned int>(diskSize / MEGA_BYTES));
-    registry.SetValue("orthanc_uncompressed_size_mb", static_cast<unsigned int>(diskSize / MEGA_BYTES));
+    unsigned int jobsPending, jobsRunning, jobsCompleted;
+    context.GetJobsEngine().GetRegistry().GetStatistics(jobsPending, jobsRunning, jobsCompleted);
+
+    MetricsRegistry& registry = context.GetMetricsRegistry();
+    registry.SetValue("orthanc_disk_size_mb", static_cast<float>(diskSize) / MEGA_BYTES);
+    registry.SetValue("orthanc_uncompressed_size_mb", static_cast<float>(diskSize) / MEGA_BYTES);
     registry.SetValue("orthanc_count_patients", static_cast<unsigned int>(countPatients));
     registry.SetValue("orthanc_count_studies", static_cast<unsigned int>(countStudies));
     registry.SetValue("orthanc_count_series", static_cast<unsigned int>(countSeries));
     registry.SetValue("orthanc_count_instances", static_cast<unsigned int>(countInstances));
+    registry.SetValue("orthanc_jobs_pending", jobsPending);
+    registry.SetValue("orthanc_jobs_running", jobsRunning);
+    registry.SetValue("orthanc_jobs_completed", jobsCompleted);
     
     std::string s;
     registry.ExportPrometheusText(s);