mirror of
https://github.com/pezkuwichain/pezkuwi-telemetry.git
synced 2026-06-12 07:41:09 +00:00
Fix sorting (#77)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import * as React from 'react';
|
||||
import { Types } from '@dotstats/common';
|
||||
import { Types, SortedCollection } from '@dotstats/common';
|
||||
import { Chains, Chain, Ago, OfflineIndicator } from './components';
|
||||
import { Connection } from './Connection';
|
||||
import { PersistentObject, PersistentSet } from './persist';
|
||||
@@ -36,13 +36,11 @@ export default class App extends React.Component<{}, State> {
|
||||
);
|
||||
|
||||
this.pins = new PersistentSet<Types.NodeName>('pinned_names', (pins) => {
|
||||
const { nodes, sortedNodes } = this.state;
|
||||
const { nodes } = this.state;
|
||||
|
||||
for (const node of nodes.values()) {
|
||||
node.setPinned(pins.has(node.name));
|
||||
}
|
||||
nodes.mutEachAndSort((node) => node.setPinned(pins.has(node.name)));
|
||||
|
||||
this.setState({ nodes, pins, sortedNodes: sortedNodes.sort(Node.compare) });
|
||||
this.setState({ nodes, pins });
|
||||
});
|
||||
|
||||
this.state = {
|
||||
@@ -53,8 +51,7 @@ export default class App extends React.Component<{}, State> {
|
||||
timeDiff: 0 as Types.Milliseconds,
|
||||
subscribed: null,
|
||||
chains: new Map(),
|
||||
nodes: new Map(),
|
||||
sortedNodes: [],
|
||||
nodes: new SortedCollection(Node.compare),
|
||||
settings: this.settings.raw(),
|
||||
pins: this.pins.get(),
|
||||
};
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { VERSION, timestamp, FeedMessage, Types, Maybe, sleep } from '@dotstats/common';
|
||||
import { sortedInsert, sortedIndexOf } from '@dotstats/common';
|
||||
import { State, Update, Node } from './state';
|
||||
import { PersistentSet } from './persist';
|
||||
import { getHashData, setHashData } from './utils';
|
||||
@@ -83,10 +82,7 @@ export class Connection {
|
||||
|
||||
public handleMessages = (messages: FeedMessage.Message[]) => {
|
||||
const { nodes, chains } = this.state;
|
||||
let { sortedNodes } = this.state;
|
||||
|
||||
// TODO: boolean flags are code smell, find a cleaner way to do this
|
||||
let dirty = false;
|
||||
const ref = nodes.ref();
|
||||
|
||||
for (const message of messages) {
|
||||
switch (message.action) {
|
||||
@@ -107,7 +103,7 @@ export class Connection {
|
||||
case Actions.BestBlock: {
|
||||
const [best, blockTimestamp, blockAverage] = message.payload;
|
||||
|
||||
nodes.forEach((node) => node.newBestBlock());
|
||||
nodes.mutEach((node) => node.newBestBlock());
|
||||
|
||||
this.state = this.update({ best, blockTimestamp, blockAverage });
|
||||
|
||||
@@ -119,90 +115,47 @@ export class Connection {
|
||||
const pinned = this.pins.has(nodeDetails[0]);
|
||||
const node = new Node(pinned, id, nodeDetails, nodeStats, nodeHardware, blockDetails, location);
|
||||
|
||||
nodes.set(id, node);
|
||||
sortedInsert(node, sortedNodes, Node.compare);
|
||||
|
||||
if (nodes.size !== sortedNodes.length) {
|
||||
console.error('Node count in sorted array is wrong!');
|
||||
sortedNodes = Array.from(nodes.values()).sort(Node.compare);
|
||||
}
|
||||
|
||||
dirty = true;
|
||||
nodes.add(node);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case Actions.RemovedNode: {
|
||||
const id = message.payload;
|
||||
const node = nodes.get(id);
|
||||
|
||||
if (node) {
|
||||
nodes.delete(id);
|
||||
const index = sortedIndexOf(node, sortedNodes, Node.compare);
|
||||
sortedNodes.splice(index, 1);
|
||||
|
||||
if (nodes.size !== sortedNodes.length) {
|
||||
console.error('Node count in sorted array is wrong!');
|
||||
sortedNodes = Array.from(nodes.values()).sort(Node.compare);
|
||||
}
|
||||
}
|
||||
|
||||
dirty = true;
|
||||
nodes.remove(id);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case Actions.LocatedNode: {
|
||||
const [id, lat, lon, city] = message.payload;
|
||||
const node = nodes.get(id);
|
||||
|
||||
if (!node) {
|
||||
break;
|
||||
}
|
||||
|
||||
node.updateLocation([lat, lon, city]);
|
||||
nodes.mut(id, (node) => node.updateLocation([lat, lon, city]));
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case Actions.ImportedBlock: {
|
||||
const [id, blockDetails] = message.payload;
|
||||
const node = nodes.get(id);
|
||||
|
||||
if (!node) {
|
||||
break;
|
||||
}
|
||||
|
||||
node.updateBlock(blockDetails);
|
||||
sortedNodes = sortedNodes.sort(Node.compare);
|
||||
|
||||
dirty = true;
|
||||
nodes.mutAndSort(id, (node) => node.updateBlock(blockDetails));
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case Actions.NodeStats: {
|
||||
const [id, nodeStats] = message.payload;
|
||||
const node = nodes.get(id);
|
||||
|
||||
if (!node) {
|
||||
break;
|
||||
}
|
||||
|
||||
node.updateStats(nodeStats);
|
||||
nodes.mut(id, (node) => node.updateStats(nodeStats));
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case Actions.NodeHardware: {
|
||||
const [id, nodeHardware] = message.payload;
|
||||
const node = nodes.get(id);
|
||||
|
||||
if (!node) {
|
||||
return;
|
||||
}
|
||||
|
||||
node.updateHardware(nodeHardware);
|
||||
nodes.mut(id, (node) => node.updateHardware(nodeHardware));
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -219,7 +172,7 @@ export class Connection {
|
||||
const [label, nodeCount] = message.payload;
|
||||
chains.set(label, nodeCount);
|
||||
|
||||
dirty = true;
|
||||
this.state = this.update({ chains });
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -229,22 +182,16 @@ export class Connection {
|
||||
|
||||
if (this.state.subscribed === message.payload) {
|
||||
nodes.clear();
|
||||
sortedNodes = [];
|
||||
this.state = this.update({ subscribed: null, nodes, chains, sortedNodes });
|
||||
this.state = this.update({ subscribed: null, nodes, chains });
|
||||
}
|
||||
|
||||
dirty = true;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case Actions.SubscribedTo: {
|
||||
nodes.clear();
|
||||
sortedNodes = [];
|
||||
|
||||
this.state = this.update({ subscribed: message.payload, nodes, sortedNodes });
|
||||
|
||||
dirty = true;
|
||||
this.state = this.update({ subscribed: message.payload, nodes });
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -252,11 +199,9 @@ export class Connection {
|
||||
case Actions.UnsubscribedFrom: {
|
||||
if (this.state.subscribed === message.payload) {
|
||||
nodes.clear();
|
||||
sortedNodes = [];
|
||||
this.state = this.update({ subscribed: null, nodes, sortedNodes });
|
||||
}
|
||||
|
||||
dirty = true;
|
||||
this.state = this.update({ subscribed: null, nodes });
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -273,8 +218,8 @@ export class Connection {
|
||||
}
|
||||
}
|
||||
|
||||
if (dirty) {
|
||||
this.state = this.update({ nodes, chains, sortedNodes });
|
||||
if (nodes.hasChangedSince(ref)) {
|
||||
this.state = this.update({ nodes });
|
||||
}
|
||||
|
||||
this.autoSubscribe();
|
||||
@@ -283,9 +228,13 @@ export class Connection {
|
||||
private bindSocket() {
|
||||
this.ping();
|
||||
|
||||
if (this.state) {
|
||||
const { nodes } = this.state;
|
||||
nodes.clear();
|
||||
}
|
||||
|
||||
this.state = this.update({
|
||||
status: 'online',
|
||||
nodes: new Map()
|
||||
});
|
||||
|
||||
if (this.state.subscribed) {
|
||||
|
||||
@@ -201,7 +201,7 @@ export class Chain extends React.Component<Chain.Props, Chain.State> {
|
||||
}
|
||||
|
||||
private nodes(): NodeState[] {
|
||||
return this.props.appState.sortedNodes;
|
||||
return this.props.appState.nodes.sorted();
|
||||
}
|
||||
|
||||
private pixelPosition(lat: Types.Latitude, lon: Types.Longitude): Node.Location.Position {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
.Sparkline {
|
||||
fill: currentcolor; /* rgba(255,255,255,0.5); */
|
||||
fill-opacity: 0.5;
|
||||
fill: currentcolor;
|
||||
fill-opacity: 0.35;
|
||||
stroke: currentcolor;
|
||||
margin: 0 -1px -3px -1px;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Types, Maybe } from '@dotstats/common';
|
||||
import { Types, Maybe, SortedCollection } from '@dotstats/common';
|
||||
|
||||
export class Node {
|
||||
public static compare(a: Node, b: Node): number {
|
||||
@@ -166,8 +166,7 @@ export interface State {
|
||||
timeDiff: Types.Milliseconds;
|
||||
subscribed: Maybe<Types.ChainLabel>;
|
||||
chains: Map<Types.ChainLabel, Types.NodeCount>;
|
||||
nodes: Map<Types.NodeId, Node>;
|
||||
sortedNodes: Node[];
|
||||
nodes: SortedCollection<Types.NodeId, Node>;
|
||||
settings: Readonly<State.Settings>;
|
||||
pins: Readonly<Set<Types.NodeName>>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user