From b9430ddf2b37b435892a26a7f1422f7076b82d34 Mon Sep 17 00:00:00 2001
From: Michael Davis <michael.davis@cern.ch>
Date: Thu, 10 Jan 2019 16:02:24 +0100
Subject: [PATCH] [cta-admin] Output summary of failed requests

---
 cmdline/CtaAdminCmd.cpp                 | 19 +++++++--
 xroot_plugins/XrdCtaFailedRequestLs.hpp | 55 ++++++++++++++++---------
 xrootd-ssi-protobuf-interface           |  2 +-
 3 files changed, 51 insertions(+), 25 deletions(-)

diff --git a/cmdline/CtaAdminCmd.cpp b/cmdline/CtaAdminCmd.cpp
index 09337e79dd..b2da936286 100644
--- a/cmdline/CtaAdminCmd.cpp
+++ b/cmdline/CtaAdminCmd.cpp
@@ -62,6 +62,8 @@ void IStreamBuffer<cta::xrd::Data>::DataCallback(cta::xrd::Data record) const
       switch(record.data_case()) {
          case Data::kAflsItem:      std::cout << Log::DumpProtobuf(&record.afls_item());    break;
          case Data::kAflsSummary:   std::cout << Log::DumpProtobuf(&record.afls_summary()); break;
+         case Data::kFrlsItem:      std::cout << Log::DumpProtobuf(&record.frls_item());    break;
+         case Data::kFrlsSummary:   std::cout << Log::DumpProtobuf(&record.frls_summary()); break;
          case Data::kLpaItem:       std::cout << Log::DumpProtobuf(&record.lpa_item());     break;
          case Data::kLpaSummary:    std::cout << Log::DumpProtobuf(&record.lpa_summary());  break;
          case Data::kLprItem:       std::cout << Log::DumpProtobuf(&record.lpr_item());     break;
@@ -75,6 +77,8 @@ void IStreamBuffer<cta::xrd::Data>::DataCallback(cta::xrd::Data record) const
    else switch(record.data_case()) {
          case Data::kAflsItem:      CtaAdminCmd::print(record.afls_item());    break;
          case Data::kAflsSummary:   CtaAdminCmd::print(record.afls_summary()); break;
+         case Data::kFrlsItem:      CtaAdminCmd::print(record.frls_item());    break;
+         case Data::kFrlsSummary:   CtaAdminCmd::print(record.frls_summary()); break;
          case Data::kLpaItem:       CtaAdminCmd::print(record.lpa_item());     break;
          case Data::kLpaSummary:    CtaAdminCmd::print(record.lpa_summary());  break;
          case Data::kLprItem:       CtaAdminCmd::print(record.lpr_item());     break;
@@ -464,15 +468,22 @@ void CtaAdminCmd::print(const cta::admin::FailedRequestLsItem &frls_item)
 void CtaAdminCmd::printFrLsSummaryHeader()
 {
    std::cout << TEXT_RED
-             << std::setfill(' ') << std::setw(11) << std::right << "request type"   << ' '
-             << std::setfill(' ') << std::setw(13) << std::right << "total files" << ' '
-             << std::setfill(' ') << std::setw(12) << std::right << "total size"  << ' '
+             << std::setfill(' ') << std::setw(12) << std::right << "request type"        << ' '
+             << std::setfill(' ') << std::setw(13) << std::right << "total files"         << ' '
+             << std::setfill(' ') << std::setw(20) << std::right << "total size (bytes)"  << ' '
              << TEXT_NORMAL << std::endl;
 }
 
 void CtaAdminCmd::print(const cta::admin::FailedRequestLsSummary &frls_summary)
 {
-   throw std::runtime_error("Not implemented.");
+   std::string request_type =
+      frls_summary.request_type() == cta::admin::RequestType::ARCHIVE_REQUEST  ? "archive" :
+      frls_summary.request_type() == cta::admin::RequestType::RETRIEVE_REQUEST ? "retrieve" : "total";
+
+   std::cout << std::setfill(' ') << std::setw(11) << std::right << request_type               << ' '
+             << std::setfill(' ') << std::setw(13) << std::right << frls_summary.total_files() << ' '
+             << std::setfill(' ') << std::setw(20) << std::right << frls_summary.total_size()  << ' '
+             << std::endl;
 }
 
 void CtaAdminCmd::printLpaHeader()
diff --git a/xroot_plugins/XrdCtaFailedRequestLs.hpp b/xroot_plugins/XrdCtaFailedRequestLs.hpp
index faea338cde..f7ceca7291 100644
--- a/xroot_plugins/XrdCtaFailedRequestLs.hpp
+++ b/xroot_plugins/XrdCtaFailedRequestLs.hpp
@@ -19,6 +19,8 @@
 #pragma once
 
 #include <XrdSsiPbOStreamBuffer.hpp>
+#include <objectstore/ArchiveQueue.hpp>
+#include <objectstore/RetrieveQueue.hpp>
 #include <scheduler/Scheduler.hpp>
 #include <scheduler/RetrieveJob.hpp>
 
@@ -48,7 +50,6 @@ public:
       XrdSsiPb::Log::Msg(XrdSsiPb::Log::DEBUG, LOG_SUFFIX, "~FailedRequestLsStream() destructor");
    }
 
-#if 0
    /*!
     * Synchronously obtain data from an active stream
     *
@@ -74,7 +75,7 @@ public:
       XrdSsiPb::OStreamBuffer<Data> *streambuf;
 
       try {
-         if(!m_isSummary && !m_archiveFileItor.hasMore()) {
+         if(!m_isSummary && true) {
             // Nothing more to send, close the stream
             last = true;
             return nullptr;
@@ -96,17 +97,8 @@ public:
    auto retrieveJobFailedList = m_scheduler.getRetrieveJobsFailedSummary(m_lc);
    cmdlineOutput << "Failed retrieve jobs: " << retrieveJobFailedList.size() << std::endl;
 #endif
-#if 0
-   // failed archive jobs
-   auto archive_summary = m_scheduler.getRetrieveJobsFailedSummary(m_lc);
-   responseTable.push_back({ "archive", std::to_string(archive_summary.candidateFiles), std::to_string(archive_summary.candidateBytes) });
-
-   // failed retrieve jobs
-   auto retrieve_summary = m_scheduler.getRetrieveJobsFailedSummary(m_lc);
-   responseTable.push_back({ "retrieve", std::to_string(retrieve_summary.candidateFiles), std::to_string(retrieve_summary.candidateBytes) });
-#endif
-
 
+#if 0
          for(bool is_buffer_full = false; m_archiveFileItor.hasMore() && !is_buffer_full; )
          {
             const cta::common::dataStructures::FailedRequest archiveFile = m_archiveFileItor.next();
@@ -145,6 +137,7 @@ public:
                is_buffer_full = streambuf->Push(record);
             }
          }
+#endif
          dlen = streambuf->Size();
          XrdSsiPb::Log::Msg(XrdSsiPb::Log::DEBUG, LOG_SUFFIX, "GetBuff(): Returning buffer with ", dlen, " bytes of data.");
       } catch(cta::exception::Exception &ex) {
@@ -162,26 +155,48 @@ public:
       }
       return streambuf;
    }
-#endif
 
 #if 0
+   // failed archive jobs
+   auto archive_summary = m_scheduler.getRetrieveJobsFailedSummary(m_lc);
+   responseTable.push_back({ "archive", std::to_string(archive_summary.candidateFiles), std::to_string(archive_summary.candidateBytes) });
+
+   // failed retrieve jobs
+   auto retrieve_summary = m_scheduler.getRetrieveJobsFailedSummary(m_lc);
+   responseTable.push_back({ "retrieve", std::to_string(retrieve_summary.candidateFiles), std::to_string(retrieve_summary.candidateBytes) });
+#endif
    void GetBuffSummary(XrdSsiPb::OStreamBuffer<Data> *streambuf) {
-      common::dataStructures::FailedRequestSummary summary = m_catalogue.getTapeFileSummary(m_searchCriteria);
 
-      Data record;
+      cta::objectstore::ArchiveQueue::CandidateJobList archive_summary;
+      cta::objectstore::RetrieveQueue::CandidateJobList retrieve_summary;
 
-      // Summary statistics
-      record.mutable_afls_summary()->set_total_files(summary.totalFiles);
-      record.mutable_afls_summary()->set_total_size(summary.totalBytes);
+      Data record;
 
-      streambuf->Push(record);
+      if(m_isArchive) {
+         record.mutable_frls_summary()->set_request_type(cta::admin::RequestType::ARCHIVE_REQUEST);
+         record.mutable_frls_summary()->set_total_files(archive_summary.candidateFiles);
+         record.mutable_frls_summary()->set_total_size(archive_summary.candidateBytes);
+         streambuf->Push(record);
+      }
+      if(m_isRetrieve) {
+         record.mutable_frls_summary()->set_request_type(cta::admin::RequestType::RETRIEVE_REQUEST);
+         record.mutable_frls_summary()->set_total_files(retrieve_summary.candidateFiles);
+         record.mutable_frls_summary()->set_total_size(retrieve_summary.candidateBytes);
+         streambuf->Push(record);
+      }
+      if(m_isArchive && m_isRetrieve) {
+         record.mutable_frls_summary()->set_request_type(cta::admin::RequestType::TOTAL);
+         record.mutable_frls_summary()->set_total_files(archive_summary.candidateFiles + retrieve_summary.candidateFiles);
+         record.mutable_frls_summary()->set_total_size(archive_summary.candidateBytes + retrieve_summary.candidateBytes);
+         streambuf->Push(record);
+      }
 
       m_isSummary = false;
    }
-#endif
 
 private:
    Scheduler &m_scheduler;                 //!< Reference to CTA Scheduler
+
    bool m_isArchive;                       //!< List failed archive requests
    bool m_isRetrieve;                      //!< List failed retrieve requests
    bool m_isLogEntries;                    //!< Show failure log messages (verbose)
diff --git a/xrootd-ssi-protobuf-interface b/xrootd-ssi-protobuf-interface
index 515045f34d..3c85295b46 160000
--- a/xrootd-ssi-protobuf-interface
+++ b/xrootd-ssi-protobuf-interface
@@ -1 +1 @@
-Subproject commit 515045f34d3fc26d8259b548b469e7d025468a61
+Subproject commit 3c85295b463f14a5b6959b826d01696b42892f27
-- 
GitLab