paper_client/stats.rs
1/*
2 * Copyright (c) Kia Shakiba
3 *
4 * This source code is licensed under the GNU AGPLv3 license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8use crate::policy::PaperPolicy;
9
10#[derive(Debug)]
11pub struct Stats {
12 max_size: u64,
13 used_size: u64,
14 num_objects: u64,
15
16 total_gets: u64,
17 total_sets: u64,
18 total_dels: u64,
19
20 miss_ratio: f64,
21
22 policies: Vec<PaperPolicy>,
23 policy: PaperPolicy,
24 is_auto_policy: bool,
25
26 uptime: u64,
27}
28
29impl Stats {
30 /// Creates a new instance of the cache's stats.
31 ///
32 /// # Examples
33 /// ```
34 /// use paper_client::{Stats, PaperPolicy};
35 ///
36 /// let stats = Stats::new(0, 0, 0, 0, 0, 0, 0.0, vec![PaperPolicy::Lru], PaperPolicy::Lru, false, 0);
37 /// ```
38 #[allow(clippy::too_many_arguments)]
39 pub fn new(
40 max_size: u64,
41 used_size: u64,
42 num_objects: u64,
43
44 total_gets: u64,
45 total_sets: u64,
46 total_dels: u64,
47
48 miss_ratio: f64,
49
50 policies: Vec<PaperPolicy>,
51 policy: PaperPolicy,
52 is_auto_policy: bool,
53
54 uptime: u64,
55 ) -> Self {
56 Stats {
57 max_size,
58 used_size,
59 num_objects,
60
61 total_gets,
62 total_sets,
63 total_dels,
64
65 miss_ratio,
66
67 policies,
68 policy,
69 is_auto_policy,
70
71 uptime,
72 }
73 }
74
75 /// Returns the cache's maximum size in bytes.
76 ///
77 /// # Examples
78 /// ```
79 /// use paper_client::{Stats, PaperPolicy};
80 ///
81 /// let stats = Stats::new(1000, 0, 0, 0, 0, 0, 0.0, vec![PaperPolicy::Lru], PaperPolicy::Lru, false, 0);
82 ///
83 /// assert_eq!(stats.get_max_size(), 1000);
84 /// ```
85 pub fn get_max_size(&self) -> u64 {
86 self.max_size
87 }
88
89 /// Returns the cache's used size in bytes.
90 ///
91 /// # Examples
92 /// ```
93 /// use paper_client::{Stats, PaperPolicy};
94 ///
95 /// let stats = Stats::new(1000, 500, 0, 0, 0, 0, 0.0, vec![PaperPolicy::Lru], PaperPolicy::Lru, false, 0);
96 ///
97 /// assert_eq!(stats.get_used_size(), 500);
98 /// ```
99 pub fn get_used_size(&self) -> u64 {
100 self.used_size
101 }
102
103 /// Returns the number of objects in the cache.
104 ///
105 /// # Examples
106 /// ```
107 /// use paper_client::{Stats, PaperPolicy};
108 ///
109 /// let stats = Stats::new(1000, 500, 10, 0, 0, 0, 0.0, vec![PaperPolicy::Lru], PaperPolicy::Lru, false, 0);
110 ///
111 /// assert_eq!(stats.get_num_objects(), 10);
112 /// ```
113 pub fn get_num_objects(&self) -> u64 {
114 self.num_objects
115 }
116
117 /// Returns the cache's total number of gets.
118 ///
119 /// # Examples
120 /// ```
121 /// use paper_client::{Stats, PaperPolicy};
122 ///
123 /// let stats = Stats::new(0, 0, 0, 10, 0, 0, 0.0, vec![PaperPolicy::Lru], PaperPolicy::Lru, false, 0);
124 ///
125 /// assert_eq!(stats.get_total_gets(), 10);
126 /// ```
127 pub fn get_total_gets(&self) -> u64 {
128 self.total_gets
129 }
130
131 /// Returns the cache's total number of sets.
132 ///
133 /// # Examples
134 /// ```
135 /// use paper_client::{Stats, PaperPolicy};
136 ///
137 /// let stats = Stats::new(0, 0, 0, 0, 10, 0, 0.0, vec![PaperPolicy::Lru], PaperPolicy::Lru, false, 0);
138 ///
139 /// assert_eq!(stats.get_total_sets(), 10);
140 /// ```
141 pub fn get_total_sets(&self) -> u64 {
142 self.total_sets
143 }
144
145 /// Returns the cache's total number of dels.
146 ///
147 /// # Examples
148 /// ```
149 /// use paper_client::{Stats, PaperPolicy};
150 ///
151 /// let stats = Stats::new(0, 0, 0, 0, 0, 10, 0.0, vec![PaperPolicy::Lru], PaperPolicy::Lru, false, 0);
152 ///
153 /// assert_eq!(stats.get_total_dels(), 10);
154 /// ```
155 pub fn get_total_dels(&self) -> u64 {
156 self.total_dels
157 }
158
159 /// Returns the cache's miss ratio.
160 ///
161 /// # Examples
162 /// ```
163 /// use paper_client::{Stats, PaperPolicy};
164 ///
165 /// let stats = Stats::new(0, 0, 0, 0, 0, 0, 1.0, vec![PaperPolicy::Lru], PaperPolicy::Lru, false, 0);
166 ///
167 /// assert_eq!(stats.get_miss_ratio(), 1.0);
168 /// ```
169 pub fn get_miss_ratio(&self) -> f64 {
170 self.miss_ratio
171 }
172
173 /// Returns the cache's configured eviction policies.
174 ///
175 /// # Examples
176 /// ```
177 /// use paper_client::{Stats, PaperPolicy};
178 ///
179 /// let stats = Stats::new(0, 0, 0, 0, 0, 0, 0.0, vec![PaperPolicy::Lru], PaperPolicy::Lru, false, 0);
180 ///
181 /// assert_eq!(stats.get_policies(), &[PaperPolicy::Lru]);
182 /// ```
183 pub fn get_policies(&self) -> &[PaperPolicy] {
184 &self.policies
185 }
186
187 /// Returns the cache's eviction policy.
188 ///
189 /// # Examples
190 /// ```
191 /// use paper_client::{Stats, PaperPolicy};
192 ///
193 /// let stats = Stats::new(0, 0, 0, 0, 0, 0, 0.0, vec![PaperPolicy::Lru], PaperPolicy::Lru, false, 0);
194 ///
195 /// assert_eq!(stats.get_policy(), &PaperPolicy::Lru);
196 /// ```
197 pub fn get_policy(&self) -> &PaperPolicy {
198 &self.policy
199 }
200
201 /// Returns `true` if the cache is configured to the [`PaperPolicy::Auto`]
202 /// eviction policy.
203 ///
204 /// # Examples
205 /// ```
206 /// use paper_client::{Stats, PaperPolicy};
207 ///
208 /// let stats = Stats::new(0, 0, 0, 0, 0, 0, 0.0, vec![PaperPolicy::Lru], PaperPolicy::Lru, true, 0);
209 ///
210 /// assert!(stats.is_auto_policy());
211 /// ```
212 pub fn is_auto_policy(&self) -> bool {
213 self.is_auto_policy
214 }
215
216 /// Returns the cache's uptime.
217 ///
218 /// # Examples
219 /// ```
220 /// use paper_client::{Stats, PaperPolicy};
221 ///
222 /// let stats = Stats::new(0, 0, 0, 0, 0, 0, 0.0, vec![PaperPolicy::Lru], PaperPolicy::Lru, false, 1);
223 ///
224 /// assert_eq!(stats.get_uptime(), 1);
225 /// ```
226 pub fn get_uptime(&self) -> u64 {
227 self.uptime
228 }
229}