Skip to content

Commit

Permalink
feat: adds network selection for glados views
Browse files Browse the repository at this point in the history
  • Loading branch information
mrferris committed Sep 18, 2024
1 parent b517b77 commit 68b0f7c
Show file tree
Hide file tree
Showing 8 changed files with 385 additions and 381 deletions.
20 changes: 18 additions & 2 deletions entity/src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use ethportal_api::OverlayContentKey;
use sea_orm::{entity::prelude::*, ActiveValue::NotSet, Set};

/// Portal network sub-protocol. History, state, transactions etc.
#[derive(Debug, Clone, Eq, PartialEq, EnumIter, DeriveActiveEnum)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, EnumIter, DeriveActiveEnum)]
#[sea_orm(rs_type = "i32", db_type = "Integer")]
pub enum SubProtocol {
History = 0,
Expand All @@ -26,6 +26,22 @@ impl SubProtocol {
}
}

#[derive(Debug, PartialEq)]
pub struct InvalidSubProtocolError;

impl TryFrom<&String> for SubProtocol {
type Error = InvalidSubProtocolError;

fn try_from(value: &String) -> Result<Self, Self::Error> {
match value.to_lowercase().as_str() {
"history" => Ok(SubProtocol::History),
"state" => Ok(SubProtocol::State),
"beacon" => Ok(SubProtocol::Beacon),
_ => Err(InvalidSubProtocolError),
}
}
}

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "content")]
pub struct Model {
Expand Down Expand Up @@ -70,7 +86,7 @@ pub async fn get_or_create<T: OverlayContentKey>(
) -> Result<Model> {
// First try to lookup an existing entry.
if let Some(content_key_model) = Entity::find()
.filter(Column::ProtocolId.eq(sub_protocol.clone()))
.filter(Column::ProtocolId.eq(sub_protocol))
.filter(Column::ContentKey.eq(content_key.to_bytes()))
.one(conn)
.await?
Expand Down
4 changes: 2 additions & 2 deletions entity/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ async fn test_content_table_unique_constraints() {
id: NotSet,
content_id: Set(id_a.clone()),
content_key: Set(key_a.clone()),
protocol_id: Set(protocol_a.clone()),
protocol_id: Set(protocol_a),
first_available_at: Set(Utc::now()),
};
action_a.clone().insert(&conn).await.unwrap();
Expand Down Expand Up @@ -318,7 +318,7 @@ async fn test_content_table_unique_constraints() {
id: NotSet,
content_id: Set(id_b),
content_key: Set(key_a),
protocol_id: Set(protocol_a.clone()),
protocol_id: Set(protocol_a),
first_available_at: Set(Utc::now()),
};
assert!(action_c
Expand Down
20 changes: 12 additions & 8 deletions glados-web/assets/js/censustimeseries.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ function createSquareChart(width, data) {
.attr("style", "max-width: 90%; height: 100%; height: intrinsic;");


let title = data.censuses.length > 0 ? `${nodes.length} nodes found during 24 hour period beginning at ${data.censuses[0].time}`
: `No censuses found during this 24 hour period.`;
const url = new URL(window.location);
const subprotocol = url.searchParams.get('network');
let title = data.censuses.length > 0 ? `${nodes.length} ${subprotocol} nodes found during 24 hour period beginning at ${data.censuses[0].time}`
: `No ${subprotocol} censuses found during this 24 hour period.`;

// Append the title
svg.append("text")
Expand Down Expand Up @@ -360,9 +362,9 @@ function getClientStringFromDecodedEnr(decodedEnr) {
}

// Fetch the census node records from the API.
async function getCensusTimeSeriesData(numDaysAgo) {
async function getCensusTimeSeriesData(numDaysAgo, subprotocol) {

const baseUrl = `census-node-timeseries-data/?days-ago=${numDaysAgo}`;
const baseUrl = `census-node-timeseries-data/?days-ago=${numDaysAgo}&network=${subprotocol}`;
return fetch(`${baseUrl}`)
.then(response => {
if (!response.ok) {
Expand All @@ -381,18 +383,20 @@ async function censusTimeSeriesChart(daysAgo) {
svgElement.remove();
});

const data = await getCensusTimeSeriesData(daysAgo);
const url = new URL(window.location);
subprotocol = url.searchParams.get('network');

const data = await getCensusTimeSeriesData(daysAgo, subprotocol);
console.log('Census data from glados API:');
console.log(data);
if (data) {
document.getElementById('census-timeseries-graph').appendChild(createSquareChart(1700, data));
if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
alert('This page does not display well on Firefox, Chrome or Safari are recommended.');
}
} else {
console.log('No data available to plot the census chart');
}
}

// Sort nodes by nickname, latestClientString, and nodeId
function sortNodes(a, b) {

// Check for "bootnode" in nodeNickName
Expand Down
2 changes: 0 additions & 2 deletions glados-web/assets/js/stats_history.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,8 @@ function getStatsRecords(weeksAgo) {

async function updateChart(weeksAgo) {
const data = await getStatsRecords(weeksAgo);
console.log(data);

let dataSets = convertDataForChart(data);
console.log(dataSets);

// Clear the existing chart
d3.select("#stats-history-graph").html("");
Expand Down
2 changes: 1 addition & 1 deletion glados-web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub async fn run_glados_web(config: Arc<State>) -> Result<()> {

// setup router
let app = Router::new()
.route("/", get(routes::root))
.route("/", get(routes::network_overview))
.route("/census/census-list/", get(routes::census_explorer_list))
.route("/census/", get(routes::single_census_view))
.route("/census/explorer", get(routes::census_explorer))
Expand Down
Loading

0 comments on commit 68b0f7c

Please sign in to comment.