Skip to content

Commit

Permalink
feat: Adds 4444s filtering, statistics, and visualizations
Browse files Browse the repository at this point in the history
  • Loading branch information
mrferris committed Feb 15, 2024
1 parent 98984c0 commit 361d1fb
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 28 deletions.
12 changes: 12 additions & 0 deletions entity/src/audit_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct Model {
pub success_rate_latest: f32,
pub success_rate_random: f32,
pub success_rate_oldest: f32,
pub success_rate_premerge: f32,
pub success_rate_all_headers: f32,
pub success_rate_all_bodies: f32,
pub success_rate_all_receipts: f32,
Expand All @@ -25,6 +26,9 @@ pub struct Model {
pub success_rate_random_headers: f32,
pub success_rate_random_bodies: f32,
pub success_rate_random_receipts: f32,
pub success_rate_premerge_headers: f32,
pub success_rate_premerge_bodies: f32,
pub success_rate_premerge_receipts: f32,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
Expand All @@ -40,6 +44,7 @@ pub async fn create(
success_rate_latest: f32,
success_rate_random: f32,
success_rate_oldest: f32,
success_rate_premerge: f32,
success_rate_all_headers: f32,
success_rate_all_bodies: f32,
success_rate_all_receipts: f32,
Expand All @@ -49,6 +54,9 @@ pub async fn create(
success_rate_random_headers: f32,
success_rate_random_bodies: f32,
success_rate_random_receipts: f32,
success_rate_premerge_headers: f32,
success_rate_premerge_bodies: f32,
success_rate_premerge_receipts: f32,
conn: &DatabaseConnection,
) -> Result<Model> {
let audit_stats = ActiveModel {
Expand All @@ -59,6 +67,7 @@ pub async fn create(
success_rate_latest: Set(success_rate_latest),
success_rate_random: Set(success_rate_random),
success_rate_oldest: Set(success_rate_oldest),
success_rate_premerge: Set(success_rate_premerge),
success_rate_all_headers: Set(success_rate_all_headers),
success_rate_all_bodies: Set(success_rate_all_bodies),
success_rate_all_receipts: Set(success_rate_all_receipts),
Expand All @@ -68,6 +77,9 @@ pub async fn create(
success_rate_random_headers: Set(success_rate_random_headers),
success_rate_random_bodies: Set(success_rate_random_bodies),
success_rate_random_receipts: Set(success_rate_random_receipts),
success_rate_premerge_headers: Set(success_rate_premerge_headers),
success_rate_premerge_bodies: Set(success_rate_premerge_bodies),
success_rate_premerge_receipts: Set(success_rate_premerge_receipts),
};
Ok(audit_stats.insert(conn).await?)
}
Expand Down
104 changes: 84 additions & 20 deletions glados-audit/src/stats.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
use chrono::Utc;
use entity::audit_stats;
use glados_core::stats::{
filter_audits, get_audit_stats, AuditFilters, ContentTypeFilter, Period, StrategyFilter,
SuccessFilter,
filter_audits, get_audit_stats, AuditFilters, ChainSegmentFilter, ContentTypeFilter, Period,
StrategyFilter, SuccessFilter,
};
use sea_orm::{DatabaseConnection, DbErr};
use tokio::time::{interval, Duration};
use tracing::{debug, error, info};
use tracing::{debug, error};

/// Loops indefinitely, periodically recording audit stats to the database.
pub async fn periodically_record_stats(period: Duration, conn: DatabaseConnection) -> ! {
debug!("initializing task for logging audit stats");
let mut interval = interval(period);

loop {
interval.tick().await;
record_current_stats(&conn).await.unwrap_or_else(|e| {
error!("failed to record audit stats: {e}");
});
info!("Successfully recorded audit stats");
interval.tick().await;
}
}

Expand All @@ -31,6 +30,7 @@ async fn record_current_stats(conn: &DatabaseConnection) -> Result<(), DbErr> {
latest,
random,
oldest,
premerge,
all_headers,
all_bodies,
all_receipts,
Expand All @@ -40,12 +40,16 @@ async fn record_current_stats(conn: &DatabaseConnection) -> Result<(), DbErr> {
random_headers,
random_bodies,
random_receipts,
premerge_headers,
premerge_bodies,
premerge_receipts,
) = tokio::join!(
get_audit_stats(
filter_audits(AuditFilters {
strategy: StrategyFilter::All,
content_type: ContentTypeFilter::All,
success: SuccessFilter::All
success: SuccessFilter::All,
chain_segment: ChainSegmentFilter::All,
}),
Period::Hour,
conn
Expand All @@ -54,7 +58,8 @@ async fn record_current_stats(conn: &DatabaseConnection) -> Result<(), DbErr> {
filter_audits(AuditFilters {
strategy: StrategyFilter::Latest,
content_type: ContentTypeFilter::All,
success: SuccessFilter::All
success: SuccessFilter::All,
chain_segment: ChainSegmentFilter::All,
}),
Period::Hour,
conn
Expand All @@ -63,7 +68,8 @@ async fn record_current_stats(conn: &DatabaseConnection) -> Result<(), DbErr> {
filter_audits(AuditFilters {
strategy: StrategyFilter::Random,
content_type: ContentTypeFilter::All,
success: SuccessFilter::All
success: SuccessFilter::All,
chain_segment: ChainSegmentFilter::All,
}),
Period::Hour,
conn
Expand All @@ -72,7 +78,18 @@ async fn record_current_stats(conn: &DatabaseConnection) -> Result<(), DbErr> {
filter_audits(AuditFilters {
strategy: StrategyFilter::Oldest,
content_type: ContentTypeFilter::All,
success: SuccessFilter::All
success: SuccessFilter::All,
chain_segment: ChainSegmentFilter::All,
}),
Period::Hour,
conn
),
get_audit_stats(
filter_audits(AuditFilters {
strategy: StrategyFilter::All,
content_type: ContentTypeFilter::All,
success: SuccessFilter::All,
chain_segment: ChainSegmentFilter::PreMerge,
}),
Period::Hour,
conn
Expand All @@ -81,7 +98,8 @@ async fn record_current_stats(conn: &DatabaseConnection) -> Result<(), DbErr> {
filter_audits(AuditFilters {
strategy: StrategyFilter::All,
content_type: ContentTypeFilter::Headers,
success: SuccessFilter::All
success: SuccessFilter::All,
chain_segment: ChainSegmentFilter::All,
}),
Period::Hour,
conn
Expand All @@ -90,7 +108,8 @@ async fn record_current_stats(conn: &DatabaseConnection) -> Result<(), DbErr> {
filter_audits(AuditFilters {
strategy: StrategyFilter::All,
content_type: ContentTypeFilter::Bodies,
success: SuccessFilter::All
success: SuccessFilter::All,
chain_segment: ChainSegmentFilter::All,
}),
Period::Hour,
conn
Expand All @@ -99,7 +118,8 @@ async fn record_current_stats(conn: &DatabaseConnection) -> Result<(), DbErr> {
filter_audits(AuditFilters {
strategy: StrategyFilter::All,
content_type: ContentTypeFilter::Receipts,
success: SuccessFilter::All
success: SuccessFilter::All,
chain_segment: ChainSegmentFilter::All,
}),
Period::Hour,
conn
Expand All @@ -108,7 +128,8 @@ async fn record_current_stats(conn: &DatabaseConnection) -> Result<(), DbErr> {
filter_audits(AuditFilters {
strategy: StrategyFilter::Latest,
content_type: ContentTypeFilter::Headers,
success: SuccessFilter::All
success: SuccessFilter::All,
chain_segment: ChainSegmentFilter::All,
}),
Period::Hour,
conn
Expand All @@ -117,7 +138,8 @@ async fn record_current_stats(conn: &DatabaseConnection) -> Result<(), DbErr> {
filter_audits(AuditFilters {
strategy: StrategyFilter::Latest,
content_type: ContentTypeFilter::Bodies,
success: SuccessFilter::All
success: SuccessFilter::All,
chain_segment: ChainSegmentFilter::All,
}),
Period::Hour,
conn
Expand All @@ -126,7 +148,8 @@ async fn record_current_stats(conn: &DatabaseConnection) -> Result<(), DbErr> {
filter_audits(AuditFilters {
strategy: StrategyFilter::Latest,
content_type: ContentTypeFilter::Receipts,
success: SuccessFilter::All
success: SuccessFilter::All,
chain_segment: ChainSegmentFilter::All,
}),
Period::Hour,
conn
Expand All @@ -135,7 +158,8 @@ async fn record_current_stats(conn: &DatabaseConnection) -> Result<(), DbErr> {
filter_audits(AuditFilters {
strategy: StrategyFilter::Random,
content_type: ContentTypeFilter::Headers,
success: SuccessFilter::All
success: SuccessFilter::All,
chain_segment: ChainSegmentFilter::All,
}),
Period::Hour,
conn
Expand All @@ -144,27 +168,60 @@ async fn record_current_stats(conn: &DatabaseConnection) -> Result<(), DbErr> {
filter_audits(AuditFilters {
strategy: StrategyFilter::Random,
content_type: ContentTypeFilter::Bodies,
success: SuccessFilter::All
success: SuccessFilter::All,
chain_segment: ChainSegmentFilter::All,
}),
Period::Hour,
conn
),
get_audit_stats(
filter_audits(AuditFilters {
strategy: StrategyFilter::Random,
strategy: StrategyFilter::All,
content_type: ContentTypeFilter::Receipts,
success: SuccessFilter::All
success: SuccessFilter::All,
chain_segment: ChainSegmentFilter::All,
}),
Period::Hour,
conn
)
),
get_audit_stats(
filter_audits(AuditFilters {
strategy: StrategyFilter::All,
content_type: ContentTypeFilter::Headers,
success: SuccessFilter::All,
chain_segment: ChainSegmentFilter::PreMerge,
}),
Period::Hour,
conn
),
get_audit_stats(
filter_audits(AuditFilters {
strategy: StrategyFilter::All,
content_type: ContentTypeFilter::Bodies,
success: SuccessFilter::All,
chain_segment: ChainSegmentFilter::PreMerge,
}),
Period::Hour,
conn
),
get_audit_stats(
filter_audits(AuditFilters {
strategy: StrategyFilter::All,
content_type: ContentTypeFilter::Receipts,
success: SuccessFilter::All,
chain_segment: ChainSegmentFilter::PreMerge,
}),
Period::Hour,
conn
),
);

// Handle errors and get success rates.
let success_rate_all = all?.pass_percent;
let success_rate_latest = latest?.pass_percent;
let success_rate_random = random?.pass_percent;
let success_rate_oldest = oldest?.pass_percent;
let success_rate_premerge = premerge?.pass_percent;
let success_rate_all_headers = all_headers?.pass_percent;
let success_rate_all_bodies = all_bodies?.pass_percent;
let success_rate_all_receipts = all_receipts?.pass_percent;
Expand All @@ -174,6 +231,9 @@ async fn record_current_stats(conn: &DatabaseConnection) -> Result<(), DbErr> {
let success_rate_random_headers = random_headers?.pass_percent;
let success_rate_random_bodies = random_bodies?.pass_percent;
let success_rate_random_receipts = random_receipts?.pass_percent;
let success_rate_premerge_headers = premerge_headers?.pass_percent;
let success_rate_premerge_bodies = premerge_bodies?.pass_percent;
let success_rate_premerge_receipts = premerge_receipts?.pass_percent;

// Record the values.
match audit_stats::create(
Expand All @@ -183,6 +243,7 @@ async fn record_current_stats(conn: &DatabaseConnection) -> Result<(), DbErr> {
success_rate_latest,
success_rate_random,
success_rate_oldest,
success_rate_premerge,
success_rate_all_headers,
success_rate_all_bodies,
success_rate_all_receipts,
Expand All @@ -192,6 +253,9 @@ async fn record_current_stats(conn: &DatabaseConnection) -> Result<(), DbErr> {
success_rate_random_headers,
success_rate_random_bodies,
success_rate_random_receipts,
success_rate_premerge_headers,
success_rate_premerge_bodies,
success_rate_premerge_receipts,
conn,
)
.await
Expand Down
32 changes: 31 additions & 1 deletion glados-core/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use chrono::{DateTime, Duration, Utc};
use entity::{
content::{self, SubProtocol},
content_audit::{self, AuditResult, SelectionStrategy},
execution_metadata,
};
use sea_orm::sea_query::Alias;
use sea_orm::{
sea_query::{Expr, IntoCondition},
ColumnTrait, DatabaseConnection, DbErr, EntityTrait, JoinType, PaginatorTrait, QueryFilter,
Expand Down Expand Up @@ -43,7 +45,7 @@ pub fn filter_audits(filters: AuditFilters) -> Select<content_audit::Entity> {
}
};
// Content type filters
match filters.content_type {
let audits = match filters.content_type {
ContentTypeFilter::All => audits,
ContentTypeFilter::Headers => audits.join(
JoinType::InnerJoin,
Expand Down Expand Up @@ -84,6 +86,27 @@ pub fn filter_audits(filters: AuditFilters) -> Select<content_audit::Entity> {
.into_condition()
}),
),
};

match filters.chain_segment {
ChainSegmentFilter::PreMerge => {
// Alias content table for this join because the content table was already joined above.
let content_alias = Alias::new("content_table");
let aliased_content_metadata_relation = {
let mut rel = content::Relation::ExecutionMetadata.def();
rel.from_tbl = rel.from_tbl.alias(content_alias.clone());
rel
};
audits
.join_as(
JoinType::InnerJoin,
content_audit::Relation::Content.def(),
content_alias,
)
.join(JoinType::InnerJoin, aliased_content_metadata_relation)
.filter(execution_metadata::Column::BlockNumber.lt(15_537_392))
}
ChainSegmentFilter::All => audits,
}
}

Expand Down Expand Up @@ -192,6 +215,7 @@ pub struct AuditFilters {
pub strategy: StrategyFilter,
pub content_type: ContentTypeFilter,
pub success: SuccessFilter,
pub chain_segment: ChainSegmentFilter,
}

#[derive(Deserialize)]
Expand All @@ -216,3 +240,9 @@ pub enum ContentTypeFilter {
Bodies,
Receipts,
}

#[derive(Deserialize)]
pub enum ChainSegmentFilter {
All,
PreMerge,
}
Loading

0 comments on commit 361d1fb

Please sign in to comment.