fsinfo.cc 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include <hdfspp/fsinfo.h>
  19. #include <sstream>
  20. #include <iomanip>
  21. namespace hdfs {
  22. FsInfo::FsInfo()
  23. : capacity(0),
  24. used(0),
  25. remaining(0),
  26. under_replicated(0),
  27. corrupt_blocks(0),
  28. missing_blocks(0),
  29. missing_repl_one_blocks(0),
  30. blocks_in_future(0) {
  31. }
  32. std::string FsInfo::str(const std::string fs_name) const {
  33. std::string fs_name_label = "Filesystem";
  34. std::string size = std::to_string(capacity);
  35. std::string size_label = "Size";
  36. std::string used = std::to_string(this->used);
  37. std::string used_label = "Used";
  38. std::string available = std::to_string(remaining);
  39. std::string available_label = "Available";
  40. std::string use_percentage = std::to_string(this->used * 100 / capacity) + "%";
  41. std::string use_percentage_label = "Use%";
  42. std::stringstream ss;
  43. ss << std::left << std::setw(std::max(fs_name.size(), fs_name_label.size())) << fs_name_label
  44. << std::right << std::setw(std::max(size.size(), size_label.size()) + 2) << size_label
  45. << std::right << std::setw(std::max(used.size(), used_label.size()) + 2) << used_label
  46. << std::right << std::setw(std::max(available.size(), available_label.size()) + 2) << available_label
  47. << std::right << std::setw(std::max(use_percentage.size(), use_percentage_label.size()) + 2) << use_percentage_label
  48. << std::endl
  49. << std::left << std::setw(std::max(fs_name.size(), fs_name_label.size())) << fs_name
  50. << std::right << std::setw(std::max(size.size(), size_label.size()) + 2) << size
  51. << std::right << std::setw(std::max(used.size(), used_label.size()) + 2) << used
  52. << std::right << std::setw(std::max(available.size(), available_label.size()) + 2) << available
  53. << std::right << std::setw(std::max(use_percentage.size(), use_percentage_label.size()) + 2) << use_percentage;
  54. return ss.str();
  55. }
  56. }