asic_rs/data/
hashrate.rs

1use measurements::Power;
2use serde::{Deserialize, Serialize};
3use std::{
4    fmt::{Display, Formatter},
5    ops::Div,
6};
7
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
9pub enum HashRateUnit {
10    Hash,
11    KiloHash,
12    MegaHash,
13    GigaHash,
14    TeraHash,
15    PetaHash,
16    ExaHash,
17    ZettaHash,
18    YottaHash,
19}
20
21impl HashRateUnit {
22    fn to_multiplier(&self) -> f64 {
23        match self {
24            HashRateUnit::Hash => 1e0,
25            HashRateUnit::KiloHash => 1e3,
26            HashRateUnit::MegaHash => 1e6,
27            HashRateUnit::GigaHash => 1e9,
28            HashRateUnit::TeraHash => 1e12,
29            HashRateUnit::PetaHash => 1e15,
30            HashRateUnit::ExaHash => 1e18,
31            HashRateUnit::ZettaHash => 1e21,
32            HashRateUnit::YottaHash => 1e24,
33        }
34    }
35}
36
37impl Display for HashRateUnit {
38    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
39        match self {
40            HashRateUnit::Hash => write!(f, "H/s"),
41            HashRateUnit::KiloHash => write!(f, "KH/s"),
42            HashRateUnit::MegaHash => write!(f, "MH/s"),
43            HashRateUnit::GigaHash => write!(f, "GH/s"),
44            HashRateUnit::TeraHash => write!(f, "TH/s"),
45            HashRateUnit::PetaHash => write!(f, "PH/s"),
46            HashRateUnit::ExaHash => write!(f, "EH/s"),
47            HashRateUnit::ZettaHash => write!(f, "ZH/s"),
48            HashRateUnit::YottaHash => write!(f, "YH/s"),
49        }
50    }
51}
52
53#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
54pub struct HashRate {
55    /// The current amount of hashes being computed
56    pub value: f64,
57    /// The unit of the hashes in value
58    pub unit: HashRateUnit,
59    /// The algorithm of the computed hashes
60    pub algo: String,
61}
62
63impl HashRate {
64    pub fn as_unit(self, unit: HashRateUnit) -> Self {
65        let base = self.value * self.unit.to_multiplier(); // Convert to base unit (e.g., bytes)
66
67        Self {
68            value: base / unit.clone().to_multiplier(),
69            unit,
70            algo: self.algo,
71        }
72    }
73}
74
75impl Display for HashRate {
76    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
77        write!(f, "{} {}", self.value, self.unit)
78    }
79}
80impl Div<HashRate> for Power {
81    type Output = f64;
82
83    fn div(self, hash_rate: HashRate) -> Self::Output {
84        self.as_watts() / hash_rate.value
85    }
86}