Skip to content

Commit

Permalink
feat: Adds 4444s stat calculations, DB migrations, and visualizations
Browse files Browse the repository at this point in the history
  • Loading branch information
mrferris committed Feb 14, 2024
1 parent 98984c0 commit 742bafe
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 20 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
97 changes: 81 additions & 16 deletions glados-audit/src/stats.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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};
Expand Down Expand Up @@ -31,6 +31,7 @@ async fn record_current_stats(conn: &DatabaseConnection) -> Result<(), DbErr> {
latest,
random,
oldest,
premerge,
all_headers,
all_bodies,
all_receipts,
Expand All @@ -40,12 +41,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 +59,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 +69,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 +79,8 @@ 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
Expand All @@ -81,7 +89,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 +99,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 +109,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 +119,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 +129,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 +139,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 +149,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,7 +159,8 @@ 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
Expand All @@ -153,18 +169,60 @@ async fn record_current_stats(conn: &DatabaseConnection) -> Result<(), DbErr> {
filter_audits(AuditFilters {
strategy: StrategyFilter::Random,
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::Random,
content_type: ContentTypeFilter::All,
success: SuccessFilter::All,
chain_segment: ChainSegmentFilter::PreMerge,
}),
Period::Hour,
conn
),
get_audit_stats(
filter_audits(AuditFilters {
strategy: StrategyFilter::Random,
content_type: ContentTypeFilter::Headers,
success: SuccessFilter::All,
chain_segment: ChainSegmentFilter::PreMerge,
}),
Period::Hour,
conn
),
get_audit_stats(
filter_audits(AuditFilters {
strategy: StrategyFilter::Random,
content_type: ContentTypeFilter::Bodies,
success: SuccessFilter::All,
chain_segment: ChainSegmentFilter::PreMerge,
}),
Period::Hour,
conn
),
get_audit_stats(
filter_audits(AuditFilters {
strategy: StrategyFilter::Random,
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 +232,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 +244,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 +254,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
21 changes: 20 additions & 1 deletion glados-core/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use chrono::{DateTime, Duration, Utc};
use entity::{
content::{self, SubProtocol},
content_audit::{self, AuditResult, SelectionStrategy},
execution_metadata,
};
use sea_orm::{
sea_query::{Expr, IntoCondition},
Expand Down Expand Up @@ -43,7 +44,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 +85,17 @@ pub fn filter_audits(filters: AuditFilters) -> Select<content_audit::Entity> {
.into_condition()
}),
),
};

match filters.chain_segment {
ChainSegmentFilter::PreMerge => audits
.join(JoinType::InnerJoin, content_audit::Relation::Content.def())
.join(
JoinType::InnerJoin,
execution_metadata::Relation::Content.def(),
)
.filter(execution_metadata::Column::BlockNumber.lt(15_537_392)),
ChainSegmentFilter::All => audits,
}
}

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

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

#[derive(Deserialize)]
pub enum ChainSegmentFilter {
All,
PreMerge,
}
12 changes: 10 additions & 2 deletions glados-web/assets/js/stats_history.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function createMultiLineChart(height, width, dataSets) {

// Select all '.legend' group elements and click on all but the first three
svg.selectAll(".legend")
.each(function(d, i) {
.each(function (d, i) {
if (i >= 3) { // Skip the first three
dispatchClick(this);
}
Expand All @@ -113,7 +113,11 @@ function createMultiLineChart(height, width, dataSets) {
.attr("fill", (d, i) => colors[i % colors.length]);

// Add text to the legend.
const labels = ["All", "Latest", "Random", "Oldest", "All Headers", "All Bodies", "All Receipts", "Latest Headers", "Latest Bodies", "Latest Receipts", "Random Headers", "Random Bodies", "Random Receipts"];
const labels = ["All", "Latest", "Random", "Oldest", "4444s",
"All Headers", "All Bodies", "All Receipts",
"Latest Headers", "Latest Bodies", "Latest Receipts",
"Random Headers", "Random Bodies", "Random Receipts",
"4444s Headers", "4444s Bodies", "4444s Receipts"];
legend.append("text")
.attr("x", -24)
.attr("y", 9)
Expand All @@ -131,6 +135,7 @@ function convertDataForChart(data) {
'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 @@ -140,6 +145,9 @@ function convertDataForChart(data) {
'success_rate_random_headers',
'success_rate_random_bodies',
'success_rate_random_receipts',
'success_rate_premerge_headers',
'success_rate_premerge_bodies',
'success_rate_premerge_receipts'
];

return successRateKeys.map(key =>
Expand Down
Loading

0 comments on commit 742bafe

Please sign in to comment.