-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added blocks query for endpoint
- Loading branch information
1 parent
5f37242
commit b62eec7
Showing
6 changed files
with
120 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
mod db_item; | ||
mod packets; | ||
pub mod queryable; | ||
mod record_impl; | ||
pub mod subjects; | ||
pub mod types; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
use std::{fmt::Display, str::FromStr}; | ||
|
||
use chrono::{DateTime, TimeZone, Utc}; | ||
use fuel_streams_subject::subject::*; | ||
use fuel_streams_types::*; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::types::*; | ||
|
||
#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq, Eq)] | ||
pub enum TimeRange { | ||
#[serde(rename = "1h")] | ||
OneHour, | ||
#[serde(rename = "12h")] | ||
TwelveHours, | ||
#[serde(rename = "1d")] | ||
OneDay, | ||
#[serde(rename = "7d")] | ||
SevenDays, | ||
#[serde(rename = "30d")] | ||
ThirtyDays, | ||
#[serde(rename = "90d")] | ||
NinetyDays, | ||
#[serde(rename = "180d")] | ||
OneEightyDays, | ||
#[serde(rename = "1y")] | ||
OneYear, | ||
#[default] | ||
#[serde(rename = "all")] | ||
All, | ||
} | ||
|
||
impl Display for TimeRange { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
TimeRange::OneHour => write!(f, "1h"), | ||
TimeRange::TwelveHours => write!(f, "12h"), | ||
TimeRange::OneDay => write!(f, "1d"), | ||
TimeRange::SevenDays => write!(f, "7d"), | ||
TimeRange::ThirtyDays => write!(f, "30d"), | ||
TimeRange::NinetyDays => write!(f, "90d"), | ||
TimeRange::OneEightyDays => write!(f, "180d"), | ||
TimeRange::OneYear => write!(f, "1y"), | ||
TimeRange::All => write!(f, "all"), | ||
} | ||
} | ||
} | ||
|
||
impl FromStr for TimeRange { | ||
type Err = String; | ||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s { | ||
"1h" => Ok(TimeRange::OneHour), | ||
"12h" => Ok(TimeRange::TwelveHours), | ||
"1d" => Ok(TimeRange::OneDay), | ||
"7d" => Ok(TimeRange::SevenDays), | ||
"30d" => Ok(TimeRange::ThirtyDays), | ||
"90d" => Ok(TimeRange::NinetyDays), | ||
"180d" => Ok(TimeRange::OneEightyDays), | ||
"1y" => Ok(TimeRange::OneYear), | ||
"all" => Ok(TimeRange::All), | ||
_ => Err(format!("Invalid TimeRange: {}", s)), | ||
} | ||
} | ||
} | ||
|
||
#[derive( | ||
Subject, Debug, Clone, Default, Serialize, Deserialize, Eq, PartialEq, | ||
)] | ||
#[subject(id = "blocks")] | ||
#[subject(entity = "Block")] | ||
#[subject(query_all = "blocks.>")] | ||
#[subject(format = "blocks.{producer}.{height}")] | ||
pub struct BlocksQuery { | ||
#[subject( | ||
sql_column = "producer_address", | ||
description = "The address of the producer that created the block" | ||
)] | ||
pub producer: Option<Address>, | ||
#[subject( | ||
sql_column = "block_height", | ||
description = "The height of the block as unsigned 64 bit integer" | ||
)] | ||
pub height: Option<BlockHeight>, | ||
#[subject( | ||
sql_column = "timestamp", | ||
description = "The timestamp at which the block was added" | ||
)] | ||
pub timestamp: Option<DateTime<Utc>>, | ||
#[subject(description = "The timestamp at which the block was added")] | ||
pub time_range: Option<TimeRange>, | ||
} | ||
|
||
impl From<&Block> for BlocksQuery { | ||
fn from(block: &Block) -> Self { | ||
BlocksQuery { | ||
producer: Some(block.producer.to_owned()), | ||
height: Some(block.height.to_owned()), | ||
timestamp: Some( | ||
Utc.timestamp_opt(block.header.time.clone().to_unix(), 0) | ||
.unwrap(), | ||
), | ||
time_range: Some(TimeRange::default()), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters