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

improve online smith command

parent cbe4cd67
Branches
Tags
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!) { ...@@ -4,8 +4,8 @@ query IdentityNameByIndex($index: Int!) {
} }
} }
query NamesByIndexes($idtyids: [Int!]) { query NamesByIndexes($indexes: [Int!]!) {
identities(where: {index_in: $idtyids}) { identities(where: {index_in: $indexes}) {
index index
name name
} }
......
...@@ -3,6 +3,7 @@ use crate::*; ...@@ -3,6 +3,7 @@ use crate::*;
use commands::identity::try_get_idty_index_by_name; use commands::identity::try_get_idty_index_by_name;
#[cfg(feature = "gdev")] #[cfg(feature = "gdev")]
use runtime::runtime_types::gdev_runtime::opaque::SessionKeys as RuntimeSessionKeys; use runtime::runtime_types::gdev_runtime::opaque::SessionKeys as RuntimeSessionKeys;
use std::collections::HashMap;
use std::ops::Deref; use std::ops::Deref;
type SessionKeys = [u8; 128]; type SessionKeys = [u8; 128];
...@@ -68,7 +69,7 @@ pub enum Subcommand { ...@@ -68,7 +69,7 @@ pub enum Subcommand {
/// handle smith commands /// handle smith commands
pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliError> { 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 { match command {
Subcommand::GoOnline => { Subcommand::GoOnline => {
go_online(&data).await?; go_online(&data).await?;
...@@ -222,6 +223,51 @@ pub async fn online(data: &Data) -> Result<(), anyhow::Error> { ...@@ -222,6 +223,51 @@ pub async fn online(data: &Data) -> Result<(), anyhow::Error> {
.await? .await?
.unwrap_or_default(); .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:");
println!("{online_authorities:?}"); println!("{online_authorities:?}");
...@@ -230,6 +276,7 @@ pub async fn online(data: &Data) -> Result<(), anyhow::Error> { ...@@ -230,6 +276,7 @@ pub async fn online(data: &Data) -> Result<(), anyhow::Error> {
println!("Outgoing:"); println!("Outgoing:");
println!("{outgoing_authorities:?}"); println!("{outgoing_authorities:?}");
}
Ok(()) Ok(())
} }
......
...@@ -15,7 +15,7 @@ pub struct Indexer { ...@@ -15,7 +15,7 @@ pub struct Indexer {
} }
impl Indexer { impl Indexer {
// query /// graphql query without error management
async fn query<T: GraphQLQuery>( async fn query<T: GraphQLQuery>(
&self, &self,
var: <T as GraphQLQuery>::Variables, var: <T as GraphQLQuery>::Variables,
...@@ -38,6 +38,18 @@ impl Indexer { ...@@ -38,6 +38,18 @@ impl Indexer {
.map(|idty| idty.name) .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 /// pubkey → name
pub async fn username_by_pubkey(&self, pubkey: &str) -> Option<String> { pub async fn username_by_pubkey(&self, pubkey: &str) -> Option<String> {
self.query::<IdentityNameByPubkey>(identity_name_by_pubkey::Variables { self.query::<IdentityNameByPubkey>(identity_name_by_pubkey::Variables {
......
use graphql_client::GraphQLQuery; use graphql_client::GraphQLQuery;
use sp_core::Bytes; use sp_core::Bytes;
// index → identity
#[derive(GraphQLQuery)] #[derive(GraphQLQuery)]
#[graphql( #[graphql(
schema_path = "res/indexer-schema.json", schema_path = "res/indexer-schema.json",
...@@ -9,7 +8,6 @@ use sp_core::Bytes; ...@@ -9,7 +8,6 @@ use sp_core::Bytes;
)] )]
pub struct IdentityNameByIndex; pub struct IdentityNameByIndex;
// index → identity info
#[derive(GraphQLQuery)] #[derive(GraphQLQuery)]
#[graphql( #[graphql(
schema_path = "res/indexer-schema.json", schema_path = "res/indexer-schema.json",
...@@ -17,7 +15,6 @@ pub struct IdentityNameByIndex; ...@@ -17,7 +15,6 @@ pub struct IdentityNameByIndex;
)] )]
pub struct IdentityInfo; pub struct IdentityInfo;
// pubkey → identity
#[derive(GraphQLQuery)] #[derive(GraphQLQuery)]
#[graphql( #[graphql(
schema_path = "res/indexer-schema.json", schema_path = "res/indexer-schema.json",
...@@ -25,7 +22,6 @@ pub struct IdentityInfo; ...@@ -25,7 +22,6 @@ pub struct IdentityInfo;
)] )]
pub struct IdentityNameByPubkey; pub struct IdentityNameByPubkey;
// pubkey → wasidentity
#[derive(GraphQLQuery)] #[derive(GraphQLQuery)]
#[graphql( #[graphql(
schema_path = "res/indexer-schema.json", schema_path = "res/indexer-schema.json",
...@@ -53,3 +49,10 @@ pub struct BlockByNumber; ...@@ -53,3 +49,10 @@ pub struct BlockByNumber;
query_path = "res/indexer-queries.graphql" query_path = "res/indexer-queries.graphql"
)] )]
pub struct GenesisHash; 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