rpc_statistics.rs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2025 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use std::vec::Vec;
  19. use log::error;
  20. use tinyjson::JsonValue;
  21. use darkfi::rpc::jsonrpc::{
  22. ErrorCode::{InternalError, InvalidParams},
  23. JsonError, JsonResponse, JsonResult,
  24. };
  25. use crate::Explorerd;
  26. impl Explorerd {
  27. // RPCAPI:
  28. // Queries the database to retrieve current basic statistics.
  29. // Returns the readable transaction upon success.
  30. //
  31. // **Params:**
  32. // * `None`
  33. //
  34. // **Returns:**
  35. // * `BaseStatistics` encoded into a JSON.
  36. //
  37. // --> {"jsonrpc": "2.0", "method": "statistics.get_basic_statistics", "params": [], "id": 1}
  38. // <-- {"jsonrpc": "2.0", "result": {...}, "id": 1}
  39. pub async fn statistics_get_basic_statistics(&self, id: u16, params: JsonValue) -> JsonResult {
  40. // Validate to ensure parameters are empty
  41. let params = params.get::<Vec<JsonValue>>().unwrap();
  42. if !params.is_empty() {
  43. return JsonError::new(InvalidParams, None, id).into()
  44. }
  45. // Fetch `BaseStatistics`, transform to `JsonResult`, and return results
  46. match self.service.get_base_statistics() {
  47. Ok(Some(statistics)) => JsonResponse::new(statistics.to_json_array(), id).into(),
  48. Ok(None) => JsonResponse::new(JsonValue::Array(vec![]), id).into(),
  49. Err(e) => {
  50. error!(
  51. target: "explorerd::rpc_statistics::statistics_get_basic_statistics",
  52. "Failed fetching basic statistics: {}", e
  53. );
  54. JsonError::new(InternalError, None, id).into()
  55. }
  56. }
  57. }
  58. // RPCAPI:
  59. // Queries the database to retrieve all metrics statistics.
  60. // Returns a collection of metric statistics upon success.
  61. //
  62. // **Params:**
  63. // * `None`
  64. //
  65. // **Returns:**
  66. // * `MetricsStatistics` array encoded into a JSON.
  67. //
  68. // --> {"jsonrpc": "2.0", "method": "statistics.get_metric_statistics", "params": [], "id": 1}
  69. // <-- {"jsonrpc": "2.0", "result": {...}, "id": 1}
  70. pub async fn statistics_get_metric_statistics(&self, id: u16, params: JsonValue) -> JsonResult {
  71. // Validate to ensure parameters are empty
  72. let params = params.get::<Vec<JsonValue>>().unwrap();
  73. if !params.is_empty() {
  74. return JsonError::new(InvalidParams, None, id).into()
  75. }
  76. // Fetch metric statistics and return results
  77. let metrics = match self.service.get_metrics_statistics().await {
  78. Ok(v) => v,
  79. Err(e) => {
  80. error!(target: "explorerd::rpc_statistics::statistics_get_metric_statistics", "Failed fetching metric statistics: {}", e);
  81. return JsonError::new(InternalError, None, id).into()
  82. }
  83. };
  84. // Transform statistics to JsonResponse and return result
  85. let metrics_json: Vec<JsonValue> = metrics.iter().map(|m| m.to_json_array()).collect();
  86. JsonResponse::new(JsonValue::Array(metrics_json), id).into()
  87. }
  88. // RPCAPI:
  89. // Queries the database to retrieve latest metric statistics.
  90. // Returns the readable metric statistics upon success.
  91. //
  92. // **Params:**
  93. // * `None`
  94. //
  95. // **Returns:**
  96. // * `MetricsStatistics` encoded into a JSON.
  97. //
  98. // --> {"jsonrpc": "2.0", "method": "statistics.get_latest_metric_statistics", "params": [], "id": 1}
  99. // <-- {"jsonrpc": "2.0", "result": {...}, "id": 1}
  100. pub async fn statistics_get_latest_metric_statistics(
  101. &self,
  102. id: u16,
  103. params: JsonValue,
  104. ) -> JsonResult {
  105. // Validate to ensure parameters are empty
  106. let params = params.get::<Vec<JsonValue>>().unwrap();
  107. if !params.is_empty() {
  108. return JsonError::new(InvalidParams, None, id).into()
  109. }
  110. // Fetch metric statistics and return results
  111. let metrics = match self.service.get_latest_metrics_statistics().await {
  112. Ok(v) => v,
  113. Err(e) => {
  114. error!(target: "explorerd::rpc_statistics::statistics_get_latest_metric_statistics", "Failed fetching metric statistics: {}", e);
  115. return JsonError::new(InternalError, None, id).into()
  116. }
  117. };
  118. // Transform statistics to JsonResponse and return result
  119. JsonResponse::new(metrics.to_json_array(), id).into()
  120. }
  121. }