statistics.rs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 tinyjson::JsonValue;
  20. use darkfi::{rpc::jsonrpc::validate_empty_params, Result};
  21. use crate::Explorerd;
  22. impl Explorerd {
  23. // RPCAPI:
  24. // Queries the database to retrieve current basic statistics.
  25. // Returns the readable transaction upon success.
  26. //
  27. // **Params:**
  28. // * `None`
  29. //
  30. // **Returns:**
  31. // * `BaseStatistics` encoded into a JSON.
  32. //
  33. // **Example API Usage:**
  34. // --> {"jsonrpc": "2.0", "method": "statistics.get_basic_statistics", "params": [], "id": 1}
  35. // <-- {"jsonrpc": "2.0", "result": {...}, "id": 1}
  36. pub async fn statistics_get_basic_statistics(&self, params: &JsonValue) -> Result<JsonValue> {
  37. // Validate that no parameters are provided
  38. validate_empty_params(params)?;
  39. // Attempt to retrieve base statistics; if found, convert to a JSON array,
  40. // otherwise return an empty JSON array
  41. match self.service.get_base_statistics()? {
  42. Some(statistics) => Ok(statistics.to_json_array()),
  43. None => Ok(JsonValue::Array(vec![])),
  44. }
  45. }
  46. // RPCAPI:
  47. // Queries the database to retrieve all metrics statistics.
  48. // Returns a collection of metric statistics upon success.
  49. //
  50. // **Params:**
  51. // * `None`
  52. //
  53. // **Returns:**
  54. // * `MetricsStatistics` array encoded into a JSON.
  55. //
  56. // **Example API Usage:**
  57. // --> {"jsonrpc": "2.0", "method": "statistics.get_metric_statistics", "params": [], "id": 1}
  58. // <-- {"jsonrpc": "2.0", "result": {...}, "id": 1}
  59. pub async fn statistics_get_metric_statistics(&self, params: &JsonValue) -> Result<JsonValue> {
  60. // Validate that no parameters are provided
  61. validate_empty_params(params)?;
  62. // Retrieve metric statistics
  63. let statistics = self.service.get_metrics_statistics().await?;
  64. // Convert each metric statistic into a JSON array, returning the collected array
  65. let statistics_json: Vec<JsonValue> =
  66. statistics.iter().map(|m| m.to_json_array()).collect();
  67. Ok(JsonValue::Array(statistics_json))
  68. }
  69. // RPCAPI:
  70. // Queries the database to retrieve latest metric statistics.
  71. // Returns the readable metric statistics upon success.
  72. //
  73. // **Params:**
  74. // * `None`
  75. //
  76. // **Returns:**
  77. // * `MetricsStatistics` encoded into a JSON.
  78. //
  79. // **Example API Usage:**
  80. // --> {"jsonrpc": "2.0", "method": "statistics.get_latest_metric_statistics", "params": [], "id": 1}
  81. // <-- {"jsonrpc": "2.0", "result": {...}, "id": 1}
  82. pub async fn statistics_get_latest_metric_statistics(
  83. &self,
  84. params: &JsonValue,
  85. ) -> Result<JsonValue> {
  86. // Validate that no parameters are provided
  87. validate_empty_params(params)?;
  88. // Retrieve the latest statistics
  89. let statistics = self.service.get_latest_metrics_statistics().await?;
  90. // Convert the retrieved metrics into a JSON array and return it
  91. Ok(statistics.to_json_array())
  92. }
  93. }
  94. #[cfg(test)]
  95. /// Test module for validating the functionality of RPC methods related to explorer statistics.
  96. /// Focuses on ensuring proper error handling for invalid parameters across several use cases.
  97. mod tests {
  98. use crate::test_utils::{setup, validate_empty_rpc_parameters};
  99. /// Tests all RPC-related statistics calls when provided with empty parameters, ensuring they
  100. /// handle the input correctly and return appropriate validation responses.
  101. #[test]
  102. fn test_statistics_rpc_calls_for_empty_parameters() {
  103. smol::block_on(async {
  104. let explorerd = setup();
  105. let rpc_methods = [
  106. "statistics.get_latest_metric_statistics",
  107. "statistics.get_metric_statistics",
  108. "statistics.get_basic_statistics",
  109. ];
  110. for rpc_method in rpc_methods.iter() {
  111. validate_empty_rpc_parameters(&explorerd, rpc_method).await;
  112. }
  113. });
  114. }
  115. }