From 4eaaf8958bef79528d395607ac977a69cbcfef35 Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Sun, 12 May 2019 12:38:09 +0200
Subject: [PATCH] [enh] overview now displays UID

---
 back/webmin/graphql/schema.graphqls |  1 +
 back/webmin/queries/gql-uid.ts      | 11 +++++++++++
 back/webmin/webmin.ts               |  5 ++++-
 src/components/ForkNode.vue         | 23 +++++++++++++++++++++--
 src/lib/services/webmin.service.ts  | 16 ++++++++++++++++
 src/views/home/Overview.vue         |  8 ++++----
 6 files changed, 57 insertions(+), 7 deletions(-)
 create mode 100644 back/webmin/queries/gql-uid.ts

diff --git a/back/webmin/graphql/schema.graphqls b/back/webmin/graphql/schema.graphqls
index 7334a8c..6b30b11 100644
--- a/back/webmin/graphql/schema.graphqls
+++ b/back/webmin/graphql/schema.graphqls
@@ -244,5 +244,6 @@ type Query {
   stopAndResetData: Boolean!
   startNode: Boolean!
   synchronize(url: String!): Boolean
+  uid(pub: String!): String
 }
 
diff --git a/back/webmin/queries/gql-uid.ts b/back/webmin/queries/gql-uid.ts
new file mode 100644
index 0000000..fd4e771
--- /dev/null
+++ b/back/webmin/queries/gql-uid.ts
@@ -0,0 +1,11 @@
+import {Server} from 'duniter/server'
+
+export function gqlUid(server: Server) {
+  return async (_: any, params: { pub: string }) => {
+    const idty = await server.dal.getWrittenIdtyByPubkey(params.pub)
+    if (idty) {
+      return idty.uid
+    }
+    return null
+  }
+}
diff --git a/back/webmin/webmin.ts b/back/webmin/webmin.ts
index 11be942..795a2ec 100644
--- a/back/webmin/webmin.ts
+++ b/back/webmin/webmin.ts
@@ -10,6 +10,7 @@ import {Querable, querablep} from 'duniter/app/lib/common-libs/querable'
 import {NodeState, SoftVersions} from '../../common/types'
 import {SyncProgress, SyncEnding} from '../../common/dto'
 import {WS2PConnection} from 'duniter/app/modules/ws2p/lib/WS2PConnection'
+import {gqlUid} from './queries/gql-uid'
 
 export const pubsub = new PubSub();
 
@@ -232,7 +233,9 @@ export function plugModule(server: Server, startServices: () => Promise<void>, s
           })
 
           syncPromise = querablep(sync.syncPromise)
-        }
+        },
+
+        uid: gqlUid(server)
       },
 
       Subscription: {
diff --git a/src/components/ForkNode.vue b/src/components/ForkNode.vue
index 366fe1b..bec265e 100644
--- a/src/components/ForkNode.vue
+++ b/src/components/ForkNode.vue
@@ -35,16 +35,35 @@
   import Vue from 'vue';
   import {Prop} from 'vue-property-decorator';
 
+  const LABEL_LENGTH = 8
+
   @Component({})
   export default class extends Vue {
-    @Prop(String) label
+
+    @Prop(String) pubkey
     @Prop(Number) type
     @Prop(Number) number
     @Prop(Number) forkLevel
 
+    uid = ''
+
     get bgColor() {
-      const rgb = Math.min(240, 120 + this.forkLevel * 1)
+      const rgb = Math.min(240, 90 + this.forkLevel * 10)
       return 'rgb(' + rgb + ',' + rgb + ',' + rgb + ')'
     }
+
+    async mounted() {
+      this.uid = await this.$webmin.uid(this.pubkey)
+    }
+
+    get label() {
+      if (this.uid) {
+        const overflow = this.uid.length - LABEL_LENGTH
+        return overflow >= 0
+          ? this.uid.substr(0, LABEL_LENGTH - 2) + '..'
+          : this.uid.padEnd(LABEL_LENGTH, ' ')
+      }
+      return '*' + this.pubkey.substr(0, LABEL_LENGTH - 1)
+    }
   }
 </script>
diff --git a/src/lib/services/webmin.service.ts b/src/lib/services/webmin.service.ts
index e45b9fb..343d803 100644
--- a/src/lib/services/webmin.service.ts
+++ b/src/lib/services/webmin.service.ts
@@ -276,4 +276,20 @@ export class WebminService {
     })
     .result()
   }
+
+  async uid(pub: string) {
+    const res = await this.getApollo()
+    .watchQuery({
+      query: gql`
+          query ($pub: String!){
+              uid(pub: $pub)
+          }
+      `,
+      variables: {
+        pub
+      },
+    })
+    .result()
+    return res.data.uid
+  }
 }
diff --git a/src/views/home/Overview.vue b/src/views/home/Overview.vue
index 83fe01f..f09024d 100644
--- a/src/views/home/Overview.vue
+++ b/src/views/home/Overview.vue
@@ -38,8 +38,8 @@
               </div>
               <div class="col">
                 <ForkNode
-                    v-for="node in networkForkView" v-bind:key="node.pubkey"
-                    :label="node.label"
+                    v-for="node in networkForkView" v-bind:key="node.pub"
+                    :pubkey="node.pub"
                     :type="node.fork"
                     :number="node.forkNumber"
                     :forkLevel="node.forkLevel"
@@ -144,7 +144,7 @@
     currentSecond: { bs: string, count: number }
     netViewKeys: string[] = []
     netPubkeys: string[] = []
-    networkForkView: { label: string, fork: number }[] = []
+    networkForkView: { pub: string, fork: number }[] = []
     theNetView: { [bs: string]: number } = {}
 
     nodes: { name: string }[] = Array.from({ length: 30 }).map((v, i) => ({ name: `Node#${i}` }))
@@ -236,7 +236,7 @@
 
       this.networkForkView = this.netPubkeys.map(pub => {
         return {
-          label: pub.substr(0, 8),
+          pub,
           fork: this.headsByNode[pub] === this.currentBest.bs ? 0 : (this.headsByNode[pub] === this.currentSecond.bs ? 1 : 2),
           forkLevel: parseInt(String(this.currentBest.bs)) - parseInt(String(this.headsByNode[pub])),
           forkNumber: parseInt(String(this.headsByNode[pub]))
-- 
GitLab