]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
rgw: start moving usage management functions
authorYehuda Sadeh <yehuda@inktank.com>
Wed, 19 Sep 2012 22:02:49 +0000 (15:02 -0700)
committerYehuda Sadeh <yehuda@inktank.com>
Mon, 8 Oct 2012 17:57:48 +0000 (10:57 -0700)
Move usage management functionality to its own place.

Signed-off-by: Yehuda Sadeh <yehuda@inktank.com>
src/Makefile.am
src/rgw/rgw_admin.cc
src/rgw/rgw_usage.cc [new file with mode: 0644]
src/rgw/rgw_usage.h [new file with mode: 0644]

index 2cd7b45fd476e459d72622ad1783d9d6b33c2550..0329a0fcf9d5f9ff214e670d14ca7675944bf54b 100644 (file)
@@ -313,6 +313,7 @@ librgw_a_SOURCES =  \
        rgw/rgw_client_io.cc \
        rgw/rgw_fcgi.cc \
        rgw/rgw_xml.cc \
+       rgw/rgw_usage.cc \
        rgw/rgw_user.cc \
        rgw/rgw_tools.cc \
        rgw/rgw_rados.cc \
@@ -1716,6 +1717,7 @@ noinst_HEADERS = \
        rgw/rgw_rest_swift.h\
        rgw/rgw_rest_s3.h\
        rgw/rgw_tools.h\
+       rgw/rgw_usage.h\
        rgw/rgw_user.h\
        sample.ceph.conf\
        tools/common.h\
index fcbeed78f46839424072cb97f476461600ad9484..339f044bcea7830fc072f1470d9e3bb25bcfdbac 100644 (file)
@@ -21,6 +21,7 @@ using namespace std;
 #include "rgw_acl_s3.h"
 #include "rgw_log.h"
 #include "rgw_formats.h"
+#include "rgw_usage.h"
 #include "auth/Crypto.h"
 
 #define dout_subsys ceph_subsys_rgw
@@ -648,25 +649,6 @@ static int remove_bucket(rgw_bucket& bucket, bool delete_children)
   return ret;
 }
 
-void dump_usage_categories_info(Formatter *formatter, const rgw_usage_log_entry& entry, map<string, bool>& categories)
-{
-  formatter->open_array_section("categories");
-  map<string, rgw_usage_data>::const_iterator uiter;
-  for (uiter = entry.usage_map.begin(); uiter != entry.usage_map.end(); ++uiter) {
-    if (categories.size() && !categories.count(uiter->first))
-      continue;
-    const rgw_usage_data& usage = uiter->second;
-    formatter->open_object_section("entry");
-    formatter->dump_string("category", uiter->first);
-    formatter->dump_int("bytes_sent", usage.bytes_sent);
-    formatter->dump_int("bytes_received", usage.bytes_received);
-    formatter->dump_int("ops", usage.ops);
-    formatter->dump_int("successful_ops", usage.successful_ops);
-    formatter->close_section(); // entry
-  }
-  formatter->close_section(); // categories
-}
-
 int main(int argc, char **argv) 
 {
   vector<const char*> args;
@@ -1553,100 +1535,13 @@ next:
     parse_date(start_date, &start_epoch);
     parse_date(end_date, &end_epoch);
 
-    uint32_t max_entries = 1000;
-
-    bool is_truncated = true;
-
-    RGWUsageIter usage_iter;
-
-    map<rgw_user_bucket, rgw_usage_log_entry> usage;
-
-    formatter->open_object_section("usage");
-    if (show_log_entries) {
-      formatter->open_array_section("entries");
-    }
-    string last_owner;
-    bool user_section_open = false;
-    map<string, rgw_usage_log_entry> summary_map;
-    while (is_truncated) {
-      int ret = rgwstore->read_usage(user_id, start_epoch, end_epoch, max_entries,
-                                     &is_truncated, usage_iter, usage);
-
-      if (ret == -ENOENT) {
-        ret = 0;
-        is_truncated = false;
-      }
-
-      if (ret < 0) {
-        cerr << "ERROR: read_usage() returned ret=" << ret << std::endl;
-        break;
-      }
-
-      map<rgw_user_bucket, rgw_usage_log_entry>::iterator iter;
-      for (iter = usage.begin(); iter != usage.end(); ++iter) {
-        const rgw_user_bucket& ub = iter->first;
-        const rgw_usage_log_entry& entry = iter->second;
-
-        if (show_log_entries) {
-          if (ub.user.compare(last_owner) != 0) {
-            if (user_section_open) {
-              formatter->close_section();
-              formatter->close_section();
-            }
-            formatter->open_object_section("user");
-            formatter->dump_string("owner", ub.user);
-            formatter->open_array_section("buckets");
-            user_section_open = true;
-            last_owner = ub.user;
-          }
-          formatter->open_object_section("bucket");
-          formatter->dump_string("bucket", ub.bucket);
-          utime_t ut(entry.epoch, 0);
-          ut.gmtime(formatter->dump_stream("time"));
-          formatter->dump_int("epoch", entry.epoch);
-         dump_usage_categories_info(formatter, entry, categories);
-          formatter->close_section(); // bucket
-          formatter->flush(cout);
-        }
-
-        summary_map[ub.user].aggregate(entry, &categories);
-      }
-    }
-    if (show_log_entries) {
-      if (user_section_open) {
-        formatter->close_section(); // buckets
-        formatter->close_section(); //user
-      }
-      formatter->close_section(); // entries
-    }
-
-    if (show_log_sum) {
-      formatter->open_array_section("summary");
-      map<string, rgw_usage_log_entry>::iterator siter;
-      for (siter = summary_map.begin(); siter != summary_map.end(); ++siter) {
-        const rgw_usage_log_entry& entry = siter->second;
-        formatter->open_object_section("user");
-        formatter->dump_string("user", siter->first);
-       dump_usage_categories_info(formatter, entry, categories);
-       rgw_usage_data total_usage;
-       entry.sum(total_usage, categories);
-        formatter->open_object_section("total");
-        formatter->dump_int("bytes_sent", total_usage.bytes_sent);
-        formatter->dump_int("bytes_received", total_usage.bytes_received);
-        formatter->dump_int("ops", total_usage.ops);
-        formatter->dump_int("successful_ops", total_usage.successful_ops);
-        formatter->close_section(); // total
-
-        formatter->close_section(); // user
-
-        formatter->flush(cout);
-      }
-
-      formatter->close_section(); // summary
+    int ret = RGWUsage::show(rgwstore, user_id, start_epoch, end_epoch,
+                            show_log_entries, show_log_sum, &categories,
+                            formatter);
+    if (ret < 0) {
+      cerr << "ERROR: failed to show usage" << std::endl;
+      return 1;
     }
-
-    formatter->close_section(); // usage
-    formatter->flush(cout);
   }
 
   if (opt_cmd == OPT_USAGE_TRIM) {
@@ -1661,7 +1556,7 @@ next:
     parse_date(start_date, &start_epoch);
     parse_date(end_date, &end_epoch);
 
-    int ret = rgwstore->trim_usage(user_id, start_epoch, end_epoch);
+    int ret = RGWUsage::trim(rgwstore, user_id, start_epoch, end_epoch);
     if (ret < 0) {
       cerr << "ERROR: read_usage() returned ret=" << ret << std::endl;
       return 1;
diff --git a/src/rgw/rgw_usage.cc b/src/rgw/rgw_usage.cc
new file mode 100644 (file)
index 0000000..64cfe6b
--- /dev/null
@@ -0,0 +1,136 @@
+
+#include <string>
+#include <map>
+
+#include "rgw_rados.h"
+#include "rgw_usage.h"
+
+using namespace std;
+
+
+static void dump_usage_categories_info(Formatter *formatter, const rgw_usage_log_entry& entry, map<string, bool> *categories)
+{
+  formatter->open_array_section("categories");
+  map<string, rgw_usage_data>::const_iterator uiter;
+  for (uiter = entry.usage_map.begin(); uiter != entry.usage_map.end(); ++uiter) {
+    if (categories && categories->size() && !categories->count(uiter->first))
+      continue;
+    const rgw_usage_data& usage = uiter->second;
+    formatter->open_object_section("entry");
+    formatter->dump_string("category", uiter->first);
+    formatter->dump_int("bytes_sent", usage.bytes_sent);
+    formatter->dump_int("bytes_received", usage.bytes_received);
+    formatter->dump_int("ops", usage.ops);
+    formatter->dump_int("successful_ops", usage.successful_ops);
+    formatter->close_section(); // entry
+  }
+  formatter->close_section(); // categories
+}
+
+int RGWUsage::show(RGWRados *store, string& uid, uint64_t start_epoch,
+                  uint64_t end_epoch, bool show_log_entries, bool show_log_sum,
+                  map<string, bool> *categories,
+                  ceph::Formatter *formatter)
+{
+  uint32_t max_entries = 1000;
+
+  bool is_truncated = true;
+
+  RGWUsageIter usage_iter;
+
+  map<rgw_user_bucket, rgw_usage_log_entry> usage;
+
+  formatter->open_object_section("usage");
+  if (show_log_entries) {
+    formatter->open_array_section("entries");
+  }
+  string last_owner;
+  bool user_section_open = false;
+  map<string, rgw_usage_log_entry> summary_map;
+  while (is_truncated) {
+    int ret = rgwstore->read_usage(uid, start_epoch, end_epoch, max_entries,
+                                   &is_truncated, usage_iter, usage);
+
+    if (ret == -ENOENT) {
+      ret = 0;
+      is_truncated = false;
+    }
+
+    if (ret < 0) {
+      return ret;
+    }
+
+    map<rgw_user_bucket, rgw_usage_log_entry>::iterator iter;
+    for (iter = usage.begin(); iter != usage.end(); ++iter) {
+      const rgw_user_bucket& ub = iter->first;
+      const rgw_usage_log_entry& entry = iter->second;
+
+      if (show_log_entries) {
+        if (ub.user.compare(last_owner) != 0) {
+          if (user_section_open) {
+            formatter->close_section();
+            formatter->close_section();
+          }
+          formatter->open_object_section("user");
+          formatter->dump_string("owner", ub.user);
+          formatter->open_array_section("buckets");
+          user_section_open = true;
+          last_owner = ub.user;
+        }
+        formatter->open_object_section("bucket");
+        formatter->dump_string("bucket", ub.bucket);
+        utime_t ut(entry.epoch, 0);
+        ut.gmtime(formatter->dump_stream("time"));
+        formatter->dump_int("epoch", entry.epoch);
+        dump_usage_categories_info(formatter, entry, categories);
+        formatter->close_section(); // bucket
+        formatter->flush(cout);
+      }
+
+      summary_map[ub.user].aggregate(entry, categories);
+    }
+  }
+  if (show_log_entries) {
+    if (user_section_open) {
+      formatter->close_section(); // buckets
+      formatter->close_section(); //user
+    }
+    formatter->close_section(); // entries
+  }
+
+  if (show_log_sum) {
+    formatter->open_array_section("summary");
+    map<string, rgw_usage_log_entry>::iterator siter;
+    for (siter = summary_map.begin(); siter != summary_map.end(); ++siter) {
+      const rgw_usage_log_entry& entry = siter->second;
+      formatter->open_object_section("user");
+      formatter->dump_string("user", siter->first);
+      dump_usage_categories_info(formatter, entry, categories);
+      rgw_usage_data total_usage;
+      entry.sum(total_usage, *categories);
+      formatter->open_object_section("total");
+      formatter->dump_int("bytes_sent", total_usage.bytes_sent);
+      formatter->dump_int("bytes_received", total_usage.bytes_received);
+      formatter->dump_int("ops", total_usage.ops);
+      formatter->dump_int("successful_ops", total_usage.successful_ops);
+      formatter->close_section(); // total
+
+      formatter->close_section(); // user
+
+      formatter->flush(cout);
+    }
+
+    formatter->close_section(); // summary
+  }
+
+  formatter->close_section(); // usage
+  formatter->flush(cout);
+
+  return 0;
+}
+
+int RGWUsage::trim(RGWRados *store, string& uid, uint64_t start_epoch,
+                  uint64_t end_epoch)
+{
+  return store->trim_usage(uid, start_epoch, end_epoch);
+}
diff --git a/src/rgw/rgw_usage.h b/src/rgw/rgw_usage.h
new file mode 100644 (file)
index 0000000..1cf9ab8
--- /dev/null
@@ -0,0 +1,25 @@
+#ifndef CEPH_RGW_USAGE_H
+#define CEPH_RGW_USAGE_H
+
+#include <string>
+#include <map>
+
+#include "common/Formatter.h"
+
+class RGWRados;
+
+
+class RGWUsage
+{
+public:
+  static int show(RGWRados *store, std::string& uid, uint64_t start_epoch,
+                 uint64_t end_epoch, bool show_log_entries, bool show_log_sum,
+                 std::map<std::string, bool> *categories,
+                 ceph::Formatter *formatter);
+
+  static int trim(RGWRados *store, std::string& uid, uint64_t start_epoch,
+                 uint64_t end_epoch);
+};
+
+
+#endif