Skip to content
Snippets Groups Projects
Commit db1ea3d3 authored by Hugo Trentesaux's avatar Hugo Trentesaux
Browse files

improve online smith command

parent cbe4cd67
No related branches found
No related tags found
1 merge request!25indexer refac
This commit is part of merge request !25. Comments created here will be created in the context of that merge request.
......@@ -4,8 +4,8 @@ query IdentityNameByIndex($index: Int!) {
}
}
query NamesByIndexes($idtyids: [Int!]) {
identities(where: {index_in: $idtyids}) {
query NamesByIndexes($indexes: [Int!]!) {
identities(where: {index_in: $indexes}) {
index
name
}
......
......@@ -3,6 +3,7 @@ use crate::*;
use commands::identity::try_get_idty_index_by_name;
#[cfg(feature = "gdev")]
use runtime::runtime_types::gdev_runtime::opaque::SessionKeys as RuntimeSessionKeys;
use std::collections::HashMap;
use std::ops::Deref;
type SessionKeys = [u8; 128];
......@@ -68,7 +69,7 @@ pub enum Subcommand {
/// handle smith commands
pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliError> {
let mut data = data.build_client().await?;
let mut data = data.build_client().await?.build_indexer().await?;
match command {
Subcommand::GoOnline => {
go_online(&data).await?;
......@@ -222,6 +223,51 @@ pub async fn online(data: &Data) -> Result<(), anyhow::Error> {
.await?
.unwrap_or_default();
if let Some(indexer) = &data.indexer {
let mut names = HashMap::<IdtyId, String>::new();
indexer
.names_by_indexes(&online_authorities)
.await
.into_iter()
.for_each(|e| {
names.insert(e.0, e.1);
});
println!("Online:");
println!(
"{}",
online_authorities
.iter()
.map(|i| &names
.get(i)
.expect("panic! found authorities whith no name")[..])
.collect::<Vec<&str>>()
.join(", ")
);
println!("Incoming:");
println!(
"{}",
incoming_authorities
.iter()
.map(|i| &names
.get(i)
.expect("panic! found authorities whith no name")[..])
.collect::<Vec<&str>>()
.join(", ")
);
println!("Outgoing:");
println!(
"{}",
outgoing_authorities
.iter()
.map(|i| &names
.get(i)
.expect("panic! found authorities whith no name")[..])
.collect::<Vec<&str>>()
.join(", ")
);
} else {
println!("Online:");
println!("{online_authorities:?}");
......@@ -230,6 +276,7 @@ pub async fn online(data: &Data) -> Result<(), anyhow::Error> {
println!("Outgoing:");
println!("{outgoing_authorities:?}");
}
Ok(())
}
......
......@@ -15,7 +15,7 @@ pub struct Indexer {
}
impl Indexer {
// query
/// graphql query without error management
async fn query<T: GraphQLQuery>(
&self,
var: <T as GraphQLQuery>::Variables,
......@@ -38,6 +38,18 @@ impl Indexer {
.map(|idty| idty.name)
}
/// index → name (multiple)
pub async fn names_by_indexes(&self, indexes: &[IdtyId]) -> Vec<(IdtyId, String)> {
self.query::<NamesByIndexes>(names_by_indexes::Variables {
indexes: indexes.iter().map(|i| *i as i64).collect(),
})
.await
.identities
.into_iter()
.map(|idty| (idty.index as IdtyId, idty.name))
.collect()
}
/// pubkey → name
pub async fn username_by_pubkey(&self, pubkey: &str) -> Option<String> {
self.query::<IdentityNameByPubkey>(identity_name_by_pubkey::Variables {
......
use graphql_client::GraphQLQuery;
use sp_core::Bytes;
// index → identity
#[derive(GraphQLQuery)]
#[graphql(
schema_path = "res/indexer-schema.json",
......@@ -9,7 +8,6 @@ use sp_core::Bytes;
)]
pub struct IdentityNameByIndex;
// index → identity info
#[derive(GraphQLQuery)]
#[graphql(
schema_path = "res/indexer-schema.json",
......@@ -17,7 +15,6 @@ pub struct IdentityNameByIndex;
)]
pub struct IdentityInfo;
// pubkey → identity
#[derive(GraphQLQuery)]
#[graphql(
schema_path = "res/indexer-schema.json",
......@@ -25,7 +22,6 @@ pub struct IdentityInfo;
)]
pub struct IdentityNameByPubkey;
// pubkey → wasidentity
#[derive(GraphQLQuery)]
#[graphql(
schema_path = "res/indexer-schema.json",
......@@ -53,3 +49,10 @@ pub struct BlockByNumber;
query_path = "res/indexer-queries.graphql"
)]
pub struct GenesisHash;
#[derive(GraphQLQuery, Debug)]
#[graphql(
schema_path = "res/indexer-schema.json",
query_path = "res/indexer-queries.graphql"
)]
pub struct NamesByIndexes;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment