Skip to content

Commit 1f023e9

Browse files
author
Peter Pratscher
committed
Add peer map to charts
1 parent a2ee684 commit 1f023e9

File tree

6 files changed

+89
-6
lines changed

6 files changed

+89
-6
lines changed

handlers/charts.go

+32-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ package handlers
1818

1919
import (
2020
"coda-explorer/db"
21+
"coda-explorer/services"
2122
"coda-explorer/templates"
2223
"coda-explorer/types"
2324
"coda-explorer/version"
25+
"fmt"
26+
"github.com/lib/pq"
2427
"html/template"
28+
"net"
2529
"net/http"
2630
)
2731

@@ -33,14 +37,39 @@ func Charts(w http.ResponseWriter, r *http.Request) {
3337

3438
w.Header().Set("Content-Type", "text/html")
3539

36-
var stats []*types.Statistic
37-
err := db.DB.Select(&stats, "SELECT * FROM statistics WHERE value > 0 ORDER BY ts, indicator")
40+
pageData := &types.ChartsPageData{
41+
Peers: make(map[string]*types.PeerInfoPageData),
42+
}
43+
err := db.DB.Select(&pageData.Statistics, "SELECT * FROM statistics WHERE value > 0 ORDER BY ts, indicator")
3844
if err != nil {
3945
logger.Errorf("error retrieving statistcs data for route %v: %v", r.URL.String(), err)
4046
http.Error(w, "Internal server error", 503)
4147
return
4248
}
4349

50+
var peers pq.StringArray
51+
err = db.DB.Get(&peers, "SELECT peers FROM daemonstatus ORDER BY ts DESC LIMIT 1")
52+
53+
for _, peer := range peers {
54+
ip, _, err := net.SplitHostPort(peer)
55+
if err != nil {
56+
continue
57+
}
58+
59+
rec, err := services.GeoIpDb.GetRecord(ip)
60+
61+
if err != nil {
62+
continue
63+
}
64+
65+
geoKey := fmt.Sprintf("%v;%v", rec.Latitude, rec.Longitude)
66+
if pageData.Peers[geoKey] == nil {
67+
pageData.Peers[geoKey] = &types.PeerInfoPageData{}
68+
}
69+
pageData.Peers[geoKey].PeerCount++
70+
pageData.Peers[geoKey].Geo = &rec
71+
}
72+
4473
data := &types.PageData{
4574
Meta: &types.Meta{
4675
Title: "coda explorer",
@@ -49,7 +78,7 @@ func Charts(w http.ResponseWriter, r *http.Request) {
4978
},
5079
ShowSyncingMessage: false,
5180
Active: "charts",
52-
Data: stats,
81+
Data: pageData,
5382
Version: version.Version,
5483
}
5584

ip2location/IP2LOCATION-LITE-DB1.BIN

-1.84 MB
Binary file not shown.

ip2location/IP2LOCATION-LITE-DB5.BIN

64.1 MB
Binary file not shown.

services/services.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ var logger = logrus.New().WithField("module", "services")
3838
// Init will initialize the services
3939
func Init() {
4040

41-
db, err := ip2location.NewIP2Location("ip2location/IP2LOCATION-LITE-DB1.BIN")
41+
db, err := ip2location.NewIP2Location("ip2location/IP2LOCATION-LITE-DB5.BIN")
4242
if err != nil {
4343
logger.Fatalf("error opening ip2location database: %v", err)
4444
}

templates/charts.html

+45-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
{{ define "js"}}
2+
<script src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.min.js"></script>
3+
<script src="//cdnjs.cloudflare.com/ajax/libs/topojson/1.6.9/topojson.min.js"></script>
4+
<script src="https://cdnjs.cloudflare.com/ajax/libs/datamaps/0.5.9/datamaps.all.min.js" integrity="sha256-Lo7ue3BsvPrQ6MABnTPBDPysEwggoUeJiDKKAeKiDFY=" crossorigin="anonymous"></script>
5+
26
<script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
37
<script src="/js/chartHelper.js"></script>
48
<script>
@@ -14,7 +18,7 @@
1418
return obj
1519
}, {})
1620

17-
const data = {{.}}
21+
const data = {{.Statistics}}
1822
const chartData = arrayToObject(data, "indicator")
1923

2024
const charts = []
@@ -54,6 +58,40 @@
5458
return val;
5559
}))
5660

61+
const peers = {{.Peers}}
62+
console.log(peers)
63+
var map = new Datamap({
64+
element: document.getElementById('map-peers'),
65+
scope: 'world',
66+
geographyConfig: {
67+
popupOnHover: false,
68+
highlightOnHover: false
69+
},
70+
fills: {
71+
defaultFill: '#ABDDA4',
72+
PEER: 'blue',
73+
}
74+
});
75+
76+
77+
var bubbles = []
78+
for (var geoKey in peers) {
79+
bubbles.push({
80+
peerCount: peers[geoKey].PeerCount,
81+
country: peers[geoKey].Geo.CountryLong,
82+
city: peers[geoKey].Geo.City,
83+
radius: peers[geoKey].PeerCount * 1.5,
84+
latitude: peers[geoKey].Geo.Latitude,
85+
longitude: peers[geoKey].Geo.Longitude,
86+
fillKey: 'PEER',
87+
})
88+
}
89+
map.bubbles(bubbles, {
90+
popupTemplate: function(geo, data) {
91+
return '<div class="hoverinfo">' + data.country + ' - ' + data.city + ': ' + data.peerCount + ' peers</div>'
92+
}
93+
});
94+
5795
$('#toggleSwitch').on('change', function(e) {
5896
charts.forEach((chart) => {
5997
chart.updateOptions({
@@ -71,6 +109,9 @@
71109
{{end}}
72110

73111
{{ define "css"}}
112+
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"
113+
integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
114+
crossorigin=""/>
74115
{{end}}
75116

76117
{{ define "content"}}
@@ -97,7 +138,9 @@ <h1 class="h4 mb-1 mb-md-0">
97138
<div id="chart-snark-workers" class="border-bottom mb-2"></div>
98139
<div id="chart-snark-fees" class="border-bottom mb-2"></div>
99140
<div id="chart-total-staked" class="border-bottom mb-2"></div>
100-
<div id="chart-peers"></div>
141+
<div id="chart-peers" class="border-bottom mb-2"></div>
142+
<span class="ml-2" style="font-size: 14px; font-weight: 900; font-family: Helvetica, Arial, sans-serif; opacity: 1;">Peer Map</span>
143+
<div id="map-peers" class="border-bottom mb-2" style="position: relative; margin: 0 auto; width: 750px; height: 500px;"></div>
101144
</div>
102145
</div>
103146
{{end}}

types/templates.go

+11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package types
1818

1919
import (
2020
"github.com/lib/pq"
21+
"github.com/tankbusta/go-ip2location"
2122
"time"
2223
)
2324

@@ -94,3 +95,13 @@ type SnarkJobPageData struct {
9495
Height int `db:"height"`
9596
Epoch int `db:"epoch"`
9697
}
98+
99+
type ChartsPageData struct {
100+
Statistics []*Statistic
101+
Peers map[string]*PeerInfoPageData
102+
}
103+
104+
type PeerInfoPageData struct {
105+
PeerCount int
106+
Geo *ip2location.IP2LocationEntry
107+
}

0 commit comments

Comments
 (0)