Skip to content

Commit

Permalink
feat: adds 4444s stats and graphing
Browse files Browse the repository at this point in the history
  • Loading branch information
mrferris committed Feb 21, 2024
1 parent df1260c commit 17c165d
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 6 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_four_fours: 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_four_fours_headers: f32,
pub success_rate_four_fours_bodies: f32,
pub success_rate_four_fours_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_four_fours: 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_four_fours_headers: f32,
success_rate_four_fours_bodies: f32,
success_rate_four_fours_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_four_fours: Set(success_rate_four_fours),
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_four_fours_headers: Set(success_rate_four_fours_headers),
success_rate_four_fours_bodies: Set(success_rate_four_fours_bodies),
success_rate_four_fours_receipts: Set(success_rate_four_fours_receipts),
};
Ok(audit_stats.insert(conn).await?)
}
Expand Down
51 changes: 49 additions & 2 deletions glados-audit/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use glados_core::stats::{
};
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) -> ! {
Expand All @@ -17,7 +17,6 @@ pub async fn periodically_record_stats(period: Duration, conn: DatabaseConnectio
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,
fourfours,
all_headers,
all_bodies,
all_receipts,
Expand All @@ -40,6 +40,9 @@ async fn record_current_stats(conn: &DatabaseConnection) -> Result<(), DbErr> {
random_headers,
random_bodies,
random_receipts,
fourfours_headers,
fourfours_bodies,
fourfours_receipts,
) = tokio::join!(
get_audit_stats(
filter_audits(AuditFilters {
Expand Down Expand Up @@ -77,6 +80,15 @@ async fn record_current_stats(conn: &DatabaseConnection) -> Result<(), DbErr> {
Period::Hour,
conn
),
get_audit_stats(
filter_audits(AuditFilters {
strategy: StrategyFilter::FourFours,
content_type: ContentTypeFilter::All,
success: SuccessFilter::All
}),
Period::Hour,
conn
),
get_audit_stats(
filter_audits(AuditFilters {
strategy: StrategyFilter::All,
Expand Down Expand Up @@ -157,6 +169,33 @@ async fn record_current_stats(conn: &DatabaseConnection) -> Result<(), DbErr> {
}),
Period::Hour,
conn
),
get_audit_stats(
filter_audits(AuditFilters {
strategy: StrategyFilter::FourFours,
content_type: ContentTypeFilter::Headers,
success: SuccessFilter::All
}),
Period::Hour,
conn
),
get_audit_stats(
filter_audits(AuditFilters {
strategy: StrategyFilter::FourFours,
content_type: ContentTypeFilter::Bodies,
success: SuccessFilter::All
}),
Period::Hour,
conn
),
get_audit_stats(
filter_audits(AuditFilters {
strategy: StrategyFilter::FourFours,
content_type: ContentTypeFilter::Receipts,
success: SuccessFilter::All
}),
Period::Hour,
conn
)
);

Expand All @@ -165,6 +204,7 @@ async fn record_current_stats(conn: &DatabaseConnection) -> Result<(), DbErr> {
let success_rate_latest = latest?.pass_percent;
let success_rate_random = random?.pass_percent;
let success_rate_oldest = oldest?.pass_percent;
let success_rate_fourfours = fourfours?.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 +214,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_fourfours_headers = fourfours_headers?.pass_percent;
let success_rate_fourfours_bodies = fourfours_bodies?.pass_percent;
let success_rate_fourfours_receipts = fourfours_receipts?.pass_percent;

// Record the values.
match audit_stats::create(
Expand All @@ -183,6 +226,7 @@ async fn record_current_stats(conn: &DatabaseConnection) -> Result<(), DbErr> {
success_rate_latest,
success_rate_random,
success_rate_oldest,
success_rate_fourfours,
success_rate_all_headers,
success_rate_all_bodies,
success_rate_all_receipts,
Expand All @@ -192,6 +236,9 @@ async fn record_current_stats(conn: &DatabaseConnection) -> Result<(), DbErr> {
success_rate_random_headers,
success_rate_random_bodies,
success_rate_random_receipts,
success_rate_fourfours_headers,
success_rate_fourfours_bodies,
success_rate_fourfours_receipts,
conn,
)
.await
Expand Down
4 changes: 4 additions & 0 deletions glados-core/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pub fn filter_audits(filters: AuditFilters) -> Select<content_audit::Entity> {
StrategyFilter::Oldest => audits.filter(
content_audit::Column::StrategyUsed.eq(SelectionStrategy::SelectOldestUnaudited),
),
StrategyFilter::FourFours => {
audits.filter(content_audit::Column::StrategyUsed.eq(SelectionStrategy::FourFours))
}
};
// Success filters
let audits = match filters.success {
Expand Down Expand Up @@ -200,6 +203,7 @@ pub enum StrategyFilter {
Random,
Latest,
Oldest,
FourFours,
}

#[derive(Deserialize)]
Expand Down
15 changes: 12 additions & 3 deletions glados-web/assets/js/stats_history.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ function createMultiLineChart(height, width, dataSets) {
}

// Select all '.legend' group elements and click on all but the first three
let selectedIndexes = [0, 4, 14, 15, 16]
svg.selectAll(".legend")
.each(function(d, i) {
if (i >= 3) { // Skip the first three
.each(function (d, i) {
if (!selectedIndexes.includes(i)) { // Skip the first three
dispatchClick(this);
}
});
Expand All @@ -113,7 +114,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 +136,7 @@ function convertDataForChart(data) {
'success_rate_latest',
'success_rate_random',
'success_rate_oldest',
'success_rate_four_fourss',
'success_rate_all_headers',
'success_rate_all_bodies',
'success_rate_all_receipts',
Expand All @@ -140,6 +146,9 @@ function convertDataForChart(data) {
'success_rate_random_headers',
'success_rate_random_bodies',
'success_rate_random_receipts',
'success_rate_four_fours_headers',
'success_rate_four_fours_bodies',
'success_rate_four_fours_receipts'
];

return successRateKeys.map(key =>
Expand Down
4 changes: 3 additions & 1 deletion glados-web/templates/audit_dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ <h1 class="header text-center">Audit Dashboard</h1>
type="button">Random</button>
<button id="oldest-button" filter="Oldest" class="btn btn-outline-secondary" type="button">Oldest
Unaudited</button>
<button id="fourfours-button" filter="FourFours" class="btn btn-outline-secondary"
type="button">4444s</button>
</div>
<div id="success-buttons" class="btn-group mx-1 " role="group">
<button id="all-success-button" filter="All" class="btn btn-outline-secondary"
Expand All @@ -41,7 +43,7 @@ <h1 class="header text-center">Audit Dashboard</h1>
</div>
</div>
</div>
<br/>
<br />
<div id="audit-table"></div>
<style>
.btn-outline-secondary.active {
Expand Down
2 changes: 2 additions & 0 deletions migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod m20230511_104830_create_content_audit;
mod m20230511_104838_create_execution_metadata;
mod m20230511_104937_create_key_value;
mod m20231107_004843_create_audit_stats;
mod m20240213_190221_add_fourfours_stats;

pub struct Migrator;

Expand All @@ -25,6 +26,7 @@ impl MigratorTrait for Migrator {
Box::new(m20230511_104937_create_key_value::Migration),
Box::new(m20230508_111707_create_census_tables::Migration),
Box::new(m20231107_004843_create_audit_stats::Migration),
Box::new(m20240213_190221_add_fourfours_stats::Migration),
]
}
}
60 changes: 60 additions & 0 deletions migration/src/m20240213_190221_add_fourfours_stats.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.alter_table(
Table::alter()
.table(AuditStats::Table)
.add_column_if_not_exists(
ColumnDef::new(AuditStats::SuccessRateFourFours)
.float()
.default(0.0),
)
.add_column_if_not_exists(
ColumnDef::new(AuditStats::SuccessRateFourFoursHeaders)
.float()
.default(0.0),
)
.add_column_if_not_exists(
ColumnDef::new(AuditStats::SuccessRateFourFoursBodies)
.float()
.default(0.0),
)
.add_column_if_not_exists(
ColumnDef::new(AuditStats::SuccessRateFourFoursReceipts)
.float()
.default(0.0),
)
.to_owned(),
)
.await
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.alter_table(
Table::alter()
.table(AuditStats::Table)
.drop_column(AuditStats::SuccessRateFourFours)
.drop_column(AuditStats::SuccessRateFourFoursHeaders)
.drop_column(AuditStats::SuccessRateFourFoursBodies)
.drop_column(AuditStats::SuccessRateFourFoursReceipts)
.to_owned(),
)
.await
}
}

#[derive(Iden)]
enum AuditStats {
Table,
SuccessRateFourFours,
SuccessRateFourFoursHeaders,
SuccessRateFourFoursBodies,
SuccessRateFourFoursReceipts,
}

0 comments on commit 17c165d

Please sign in to comment.