Skip to content

Commit

Permalink
new: xally node statistics, remove: private ipv4 from dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
hayzamjs committed May 9, 2024
1 parent f250423 commit 0ae3043
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 35 deletions.
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ func main() {

app.Get("/", func(c *fiber.Ctx) error {
return c.Render("views/index.jet", fiber.Map{
"Title": fmt.Sprintf("Dashboard - %s", cfg.Node),
"NodeIP": cfg.IPv4,
"Title": fmt.Sprintf("Dashboard - %s", cfg.Node),
"NodeIP": cfg.IPv4,
"NodeType": cfg.Node,
})
})

Expand Down
Binary file added public/img/logo/xally-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions public/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function attachTippy() {
},
],
},
placement: (nodeType === "Xally") ? 'top' : undefined,
flip: false,
content: document.getElementById('activity-bar').getAttribute('tooltip-data'),
allowHTML: true,
Expand All @@ -29,6 +30,26 @@ document.addEventListener("htmx:confirm", function(e) {
e.detail.issueRequest(true);
return;
}

console.log(e.detail.question.includes("node"), nodeType, e.detail.question)

if (e.detail.question.includes("node") && nodeType === "Xally") {
//toast on the bottom saying Xally doesn't support node restart
Swal.fire({
title: "Xally does not support restarting",
icon: "error",
toast: true,
position: "bottom",
showConfirmButton: false,
timer: 3000,
width: 400,
timerProgressBar: true,
});

return;
}


Swal.fire({
title: "Are you sure?",
text: `${e.detail.question}`,
Expand Down
21 changes: 15 additions & 6 deletions services/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"fmt"
"github.com/NodeboxHQ/node-dashboard/utils/logger"
"github.com/common-nighthawk/go-figure"
"io/ioutil"
"io"
"net"
"net/http"
"os"
"strings"
"time"
)

const version = "1.0.8"
const version = "1.0.9"

type Config struct {
Node string `json:"node"`
Expand Down Expand Up @@ -88,12 +89,17 @@ func getIPAddresses() (privateIPv4, publicIPv4, publicIPv6 string, err error) {
}

func fetchPublicIP(url string) (string, error) {
resp, err := http.Get(url)
client := &http.Client{
Timeout: 10 * time.Second,
}

resp, err := client.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
ip, err := ioutil.ReadAll(resp.Body)

ip, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -124,9 +130,12 @@ func LoadConfig() (*Config, error) {
} else if strings.Contains(hostname, "-babylon") {
logger.Info("Babylon node detected")
config.Node = "Babylon"
} else if strings.Contains(hostname, "-xcally") || strings.Contains(hostname, "-xally") {
logger.Info("Xally node detected")
config.Node = "Xally"
} else {
logger.Error("Unknown node detected, defaulting to Babylon")
config.Node = "Babylon"
logger.Error("Unknown node detected, defaulting to Xally")
config.Node = "Xally"
badHostname = true
}

Expand Down
83 changes: 75 additions & 8 deletions services/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/NodeboxHQ/node-dashboard/services/dusk"
"github.com/NodeboxHQ/node-dashboard/services/linea"
"github.com/NodeboxHQ/node-dashboard/services/nulink"
"github.com/NodeboxHQ/node-dashboard/services/xally"
"github.com/NodeboxHQ/node-dashboard/utils"
"github.com/gofiber/fiber/v2"
"github.com/mackerelio/go-osstat/cpu"
Expand Down Expand Up @@ -55,6 +56,8 @@ func GetLogo(config *config.Config) fiber.Handler {
return c.SendString(fmt.Sprintf(`<img src="/assets/img/logo/nulink-logo.png?nodeip=%s" alt="logo-expanded" class="w-52 h-auto object-contain mx-auto block" />`, config.IPv4))
} else if config.Node == "Babylon" {
return c.SendString(fmt.Sprintf(`<img src="/assets/img/logo/babylon-logo.png?nodeip=%s" alt="logo-expanded" class="w-52 h-auto object-contain mx-auto block" />`, config.IPv4))
} else if config.Node == "Xally" {
return c.SendString(fmt.Sprintf(`<img src="/assets/img/logo/xally-logo.png?nodeip=%s" alt="logo-expanded" class="w-52 h-auto object-contain mx-auto block" />`, config.IPv4))
} else {
return c.SendString("")
}
Expand All @@ -64,7 +67,7 @@ func GetLogo(config *config.Config) fiber.Handler {
func GetCPUUsage(ipv4 string) fiber.Handler {
return func(c *fiber.Ctx) error {
cpuUsageTemplate := `
<div class="items-center py-2.5 px-5 border backdrop-blur-md border-cardBackgroundColor rounded-[20px] bg-cardBackgroundColor w-full shadow-md" hx-get="/data/cpu?nodeip=%s" hx-trigger="load" hx-swap="outerHTML transition:true">
<div class="items-center py-2.5 px-5 border backdrop-blur-md border-cardBackgroundColor rounded-[20px] bg-cardBackgroundColor w-full shadow-md" hx-get="/data/cpu?nodeip=%s" hx-trigger="load" hx-swap="outerHTML">
<h3 class="mb-2.5 text-center text-cardTitleColor text-lg font-semibold">CPU</h3>
<div class="flex flex-col">
<div class="flex content-between items-center gap-1.5 justify-around flex-col">
Expand Down Expand Up @@ -104,7 +107,7 @@ func GetCPUUsage(ipv4 string) fiber.Handler {
func GetRAMUsage(ipv4 string) fiber.Handler {
return func(c *fiber.Ctx) error {
ramUsageTemplate := `
<div class="items-center py-2.5 px-5 border backdrop-blur-md border-cardBackgroundColor rounded-[20px] bg-cardBackgroundColor w-full shadow-md" hx-get="/data/ram?nodeip=%s" hx-trigger="every 1s" hx-swap="outerHTML transition:true">
<div class="items-center py-2.5 px-5 border backdrop-blur-md border-cardBackgroundColor rounded-[20px] bg-cardBackgroundColor w-full shadow-md" hx-get="/data/ram?nodeip=%s" hx-trigger="every 1s" hx-swap="outerHTML">
<h3 class="mb-2.5 text-center text-cardTitleColor text-lg font-semibold">RAM</h3>
<div class="flex flex-col">
<div class="flex content-between items-center gap-1.5 justify-around flex-col">
Expand Down Expand Up @@ -136,7 +139,7 @@ func GetRAMUsage(ipv4 string) fiber.Handler {
func GetDiskUsage(ipv4 string) fiber.Handler {
return func(c *fiber.Ctx) error {
diskUsageTemplate := `
<div class="items-center py-2.5 px-5 border backdrop-blur-md border-cardBackgroundColor rounded-[20px] bg-cardBackgroundColor w-full shadow-md" hx-get="/data/disk?nodeip=%s" hx-trigger="every 1s" hx-swap="outerHTML transition:true">
<div class="items-center py-2.5 px-5 border backdrop-blur-md border-cardBackgroundColor rounded-[20px] bg-cardBackgroundColor w-full shadow-md" hx-get="/data/disk?nodeip=%s" hx-trigger="every 1s" hx-swap="outerHTML">
<h3 class="mb-2.5 text-center text-cardTitleColor text-lg font-semibold">Disk Usage</h3>
<div class="flex flex-col">
<div class="flex content-between items-center gap-1.5 justify-around flex-col">
Expand Down Expand Up @@ -189,7 +192,7 @@ func GetDiskUsage(ipv4 string) fiber.Handler {
func GetSystemUptime(ipv4 string) fiber.Handler {
return func(c *fiber.Ctx) error {
uptimeTemplate := `
<div class="items-center py-2.5 px-5 border backdrop-blur-md border-cardBackgroundColor rounded-[20px] bg-cardBackgroundColor w-full shadow-md" hx-get="/data/uptime?nodeip=%s" hx-trigger="every 1s" hx-swap="outerHTML transition:true">
<div class="items-center py-2.5 px-5 border backdrop-blur-md border-cardBackgroundColor rounded-[20px] bg-cardBackgroundColor w-full shadow-md" hx-get="/data/uptime?nodeip=%s" hx-trigger="every 1s" hx-swap="outerHTML">
<h3 class="mb-2.5 text-center text-cardTitleColor text-lg font-semibold">Uptime</h3>
<div class="flex flex-col">
<div class="flex content-between items-center gap-1.5 justify-around flex-col">
Expand Down Expand Up @@ -240,7 +243,7 @@ func GetActivity(config *config.Config) fiber.Handler {
adjective = "Online"
}

tippyContent := fmt.Sprintf("<b>Node</b> - %s <br> <b>Owner</b> - %s<br> <b>Private IPv4</b> - %s <br> <b>Public IPv4</b> - %s <br> <b>Public IPv6</b> - %s <br>", config.Node, config.Owner, config.PrivateIPv4, config.IPv4, config.IPv6)
tippyContent := fmt.Sprintf("<b>Node</b> - %s <br> <b>Owner</b> - %s<br> <b>Public IPv4</b> - %s <br> <b>Public IPv6</b> - %s <br>", config.Node, config.Owner, config.IPv4, config.IPv6)

needBr := ""

Expand Down Expand Up @@ -280,7 +283,7 @@ func GetActivity(config *config.Config) fiber.Handler {
adjective = "Online"
}

tippyContent := fmt.Sprintf("<b>Node</b> - %s <br> <b>Version</b> - %s <br> <b>Owner</b> - %s<br> <b>Private IPv4</b> - %s <br> <b>Public IPv4</b> - %s <br> <b>Public IPv6</b> - %s <br>", config.Node, status.Version, config.Owner, config.PrivateIPv4, config.IPv4, config.IPv6)
tippyContent := fmt.Sprintf("<b>Node</b> - %s <br> <b>Version</b> - %s <br> <b>Owner</b> - %s<br> <b>Public IPv4</b> - %s <br> <b>Public IPv6</b> - %s <br>", config.Node, status.Version, config.Owner, config.IPv4, config.IPv6)

needBr := ""

Expand Down Expand Up @@ -315,7 +318,7 @@ func GetActivity(config *config.Config) fiber.Handler {
adjective = "Offline"
}

tippyContent := fmt.Sprintf("<b>Node</b> - %s <br> <b>Owner</b> - %s<br> <b>Private IPv4</b> - %s <br> <b>Public IPv4</b> - %s <br> <b>Public IPv6</b> - %s", config.Node, config.Owner, config.PrivateIPv4, config.IPv4, config.IPv6)
tippyContent := fmt.Sprintf("<b>Node</b> - %s <br> <b>Owner</b> - %s<br> <b>Public IPv4</b> - %s <br> <b>Public IPv6</b> - %s", config.Node, config.Owner, config.IPv4, config.IPv6)

tippyContent = tippyContent + fmt.Sprintf("<br> <b>Dashboard Version</b> - %s", config.NodeboxDashboardVersion)
activityTemplate = strings.Replace(activityTemplate, "ALPINE_TOOLTIP", fmt.Sprintf(`tooltip-data="%s"`, tippyContent), -1)
Expand Down Expand Up @@ -344,7 +347,7 @@ func GetActivity(config *config.Config) fiber.Handler {
adjective = "Online"
}

tippyContent := fmt.Sprintf("<b>Node</b> - %s <br> <b>Owner</b> - %s<br> <b>Private IPv4</b> - %s <br> <b>Public IPv4</b> - %s <br> <b>Public IPv6</b> - %s <br>", config.Node, config.Owner, config.PrivateIPv4, config.IPv4, config.IPv6)
tippyContent := fmt.Sprintf("<b>Node</b> - %s <br> <b>Owner</b> - %s<br> <b>Public IPv4</b> - %s <br> <b>Public IPv6</b> - %s <br>", config.Node, config.Owner, config.IPv4, config.IPv6)

needBr := ""

Expand All @@ -356,6 +359,70 @@ func GetActivity(config *config.Config) fiber.Handler {
tippyContent = tippyContent + fmt.Sprintf("%s <b>Dashboard Version</b> - %s", needBr, config.NodeboxDashboardVersion)
activityTemplate = strings.Replace(activityTemplate, "ALPINE_TOOLTIP", fmt.Sprintf(`tooltip-data="%s"`, tippyContent), -1)

return c.SendString(fmt.Sprintf(activityTemplate, color, adjective))
} else if config.Node == "Xally" {
activityTemplate := `
<div class="w-64 h-7 mt-5 rounded-full overflow-hidden relative m-0" hx-get="/data/activity?nodeip=NODE_IP" hx-trigger="every 1s" hx-swap="outerHTML" id="activity-bar" ALPINE_TOOLTIP>
<div class="absolute top-0 left-0 w-full z-0 h-full bg-progressBarBackgroundColor rounded-full"></div>
<div class="absolute top-0 left-0 h-full rounded-[10px] transition-[width] w-full z-10 %s"></div>
<div class="items-center text-sm font-bold text-textColor absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 z-20"> Node %s </div>
</div>
`

status, _ := xally.FetchNodeData()

color := ""
adjective := ""
totalRunTime := 0.0
accumulatedGXally := 0.0
lastUpdate := ""

var keyIDs []string
var nodeIDs string

activityTemplate = strings.Replace(activityTemplate, "NODE_IP", config.IPv4, -1)

if len(status) == 0 {
color = "bg-red-500"
adjective = "Offline"
} else {
color = "bg-green-500"
adjective = "Online"

for _, node := range status {
totalRunTime += node.RunningTime
accumulatedGXally += node.Point
keyIDs = append(keyIDs, node.KeyID)

lastCheckTS := node.LastCheckTS
lastCheckTime := time.Unix(lastCheckTS, 0)
duration := time.Since(lastCheckTime).Round(time.Second)
minutes := duration / time.Minute
seconds := (duration % time.Minute) / time.Second

if minutes > 0 {
lastUpdate = fmt.Sprintf("%dm %ds ago", minutes, seconds)
} else {
lastUpdate = fmt.Sprintf("%ds ago", seconds)
}
}

nodeIDs = strings.Join(keyIDs, ", ")
}

tippyContent := fmt.Sprintf("<b>Node</b> - %s <br> <b>Owner</b> - %s<br> <b>Public IPv4</b> - %s <br> <b>Public IPv6</b> - %s", config.Node, config.Owner, config.IPv4, config.IPv6)

if len(status) == 0 {
color = "bg-red-500"
adjective = "Offline"
tippyContent += fmt.Sprintf("<br> <b>Dashboard Version</b> - %s", config.NodeboxDashboardVersion)
} else {
color = "bg-green-500"
adjective = "Online"
tippyContent += fmt.Sprintf("<br> <b>Total Nodes</b> - %d (%s) <br> <b>Total Running Time</b> - %.2f <br> <b>Total gXally</b> - %.2f <br> <b>Last Data Update</b> - %s <br> <b>Dashboard Version</b> - %s", len(status), nodeIDs, totalRunTime, accumulatedGXally, lastUpdate, config.NodeboxDashboardVersion)
}

activityTemplate = strings.Replace(activityTemplate, "ALPINE_TOOLTIP", fmt.Sprintf(`tooltip-data="%s"`, tippyContent), -1)
return c.SendString(fmt.Sprintf(activityTemplate, color, adjective))
} else {
return c.SendString("")
Expand Down
Loading

0 comments on commit 0ae3043

Please sign in to comment.