From 569fe3b1e0b87b3ec77f91f821ea9bbabf733205 Mon Sep 17 00:00:00 2001 From: Moses Narrow Date: Wed, 16 Oct 2024 18:16:01 -0500 Subject: [PATCH] continue refactoring reward system UI --- cmd/skywire-cli/commands/rewards/ui.go | 206 +++++-------------------- 1 file changed, 41 insertions(+), 165 deletions(-) diff --git a/cmd/skywire-cli/commands/rewards/ui.go b/cmd/skywire-cli/commands/rewards/ui.go index b1aef2afec..571b8ec2f9 100644 --- a/cmd/skywire-cli/commands/rewards/ui.go +++ b/cmd/skywire-cli/commands/rewards/ui.go @@ -5,7 +5,6 @@ import ( "bytes" "context" "encoding/base64" - "encoding/csv" "fmt" htmpl "html/template" "io" @@ -15,7 +14,6 @@ import ( "path/filepath" "regexp" "runtime" - "sort" "strconv" "strings" "sync" @@ -98,19 +96,6 @@ var ( var skyenvfile = os.Getenv("SKYENV") -func tploghtmlfunc() (l string) { - l = "Skywire Transport Bandwidth Logs By Day\n\n
"
-	l += navlinks
-	l += "

Blue = Verified Bandwidth

" - l += "

Yellow = Transport bandwidth inconsistent

" - l += "

Red = Error: sent or received is zero

" - tp, _ := script.Exec(`skywire cli log tp -d rewards/log_backups`).String() //nolint - l += fmt.Sprintf("%s\n", ansihtml.ConvertToHTML([]byte(tp))) - l += htmltoplink - l += htmlend - return l -} - func mainPage(c *gin.Context) { c.Writer.Header().Set("Server", "") tmpl0, err1 := tmpl.Clone() @@ -433,67 +418,6 @@ func server() { c.Writer.Flush() }) - r1.GET("/tpsn", func(c *gin.Context) { - c.Writer.Header().Set("Server", "") - c.Writer.Header().Set("Content-Type", "text/html;charset=utf-8") - c.Writer.Header().Set("Transfer-Encoding", "chunked") - c.Writer.WriteHeader(http.StatusOK) - c.Writer.Flush() - c.Writer.Write([]byte("Skywire Transport Setup-Node")) //nolint - c.Writer.Flush() - c.Writer.Write([]byte("\n\n
")) //nolint
-		c.Writer.Flush()
-		c.Writer.Write([]byte(navlinks)) //nolint
-		c.Writer.Flush()
-		tmpFile, err := os.CreateTemp(os.TempDir(), "*.sh")
-		if err != nil {
-			return
-		}
-		if err := tmpFile.Close(); err != nil {
-			return
-		}
-		_, _ = script.Exec(`chmod +x ` + tmpFile.Name()).String()                                         //nolint
-		_, _ = script.Echo(nextlogrun).WriteFile(tmpFile.Name())                                          //nolint
-		res, _ := script.Exec(`bash -c 'source ` + tmpFile.Name() + ` ; _nextskywireclilogrun'`).String() //nolint
-		os.Remove(tmpFile.Name())                                                                         //nolint
-		c.Writer.Write([]byte(fmt.Sprintf("%s\n", res)))                                                  //nolint
-		c.Writer.Flush()
-
-		// Initial line count
-		initialLineCount, _ := script.File("rewards/transport-setup-node.txt").CountLines() //nolint
-		// Read and print the initial lines
-		initialContent, _ := script.File("rewards/transport-setup-node.txt").First(initialLineCount).Bytes() //nolint
-		c.Writer.Write(ansihtml.ConvertToHTML(initialContent))                                               //nolint
-		c.Writer.Flush()
-		for {
-			select {
-			case <-c.Writer.CloseNotify():
-				return
-			default:
-			}
-			// Sleep for a short duration
-			time.Sleep(100 * time.Millisecond)
-			// Get the current line count
-			currentLineCount, _ := script.File("rewards/transport-setup-node.txt").CountLines() //nolint
-			// Check if there are new lines
-			if currentLineCount > initialLineCount {
-				newContent, _ := script.File("rewards/transport-setup-node.txt").Last(currentLineCount - initialLineCount).Bytes() //nolint
-				initialLineCount = currentLineCount
-				c.Writer.Write(ansihtml.ConvertToHTML(newContent)) //nolint
-				c.Writer.Flush()
-			}
-			finished, _ := script.Exec("bash -c 'systemctl is-active --quiet skywire-reward.service || echo true'").String() //nolint
-			if finished != "" {
-				break
-			}
-		}
-
-		c.Writer.Write([]byte(htmltoplink)) //nolint
-		c.Writer.Flush()
-		c.Writer.Write([]byte(htmlend)) //nolint
-		c.Writer.Flush()
-	})
-
 	r1.GET("/log-collection/tree", func(c *gin.Context) {
 		c.Writer.Header().Set("Server", "")
 		c.Writer.Header().Set("Transfer-Encoding", "chunked")
@@ -513,65 +437,6 @@ func server() {
 		c.Writer.Write([]byte(htmlend)) //nolint
 		c.Writer.Flush()
 	})
-	r1.GET("/proxy-test", func(c *gin.Context) {
-		c.Writer.Header().Set("Server", "")
-		c.Writer.Header().Set("Transfer-Encoding", "chunked")
-		c.Writer.WriteHeader(http.StatusOK)
-		c.Writer.Write([]byte("Proxy Tests\n\n
")) //nolint
-		c.Writer.Flush()
-		c.Writer.Write([]byte(navlinks)) //nolint
-		c.Writer.Flush()
-		c.Writer.Write([]byte("testproxies.sh\n")) //nolint
-		c.Writer.Flush()
-		proxytestcsvdata, _ := script.File("rewards/proxy_test/proxies.csv").Replace("00pk", "server_pk").String() //nolint
-		proxytestdata := ""
-		reader := csv.NewReader(strings.NewReader(proxytestcsvdata))
-		records, err := reader.ReadAll()
-		if err != nil {
-			c.Writer.Write([]byte(htmltoplink + htmlend)) //nolint
-			c.Writer.Flush()
-		}
-		maxWidths := make([]int, len(records[0]))
-		for _, record := range records {
-			for i, field := range record {
-				if len(field) > maxWidths[i] {
-					maxWidths[i] = len(field)
-				}
-			}
-		}
-		sort.Slice(records[1:], func(i, j int) bool {
-			if records[i+1][0] == "N/A" {
-				return true
-			}
-			if records[j+1][0] == "N/A" {
-				return false
-			}
-			if records[i+1][0] == "self_transport" {
-				return true
-			}
-			if records[j+1][0] == "self_transport" {
-				return false
-			}
-			val1, _ := strconv.ParseFloat(strings.TrimSuffix(records[i+1][2], "s"), 64) //nolint
-			val2, _ := strconv.ParseFloat(strings.TrimSuffix(records[j+1][2], "s"), 64) //nolint
-			return val1 < val2
-		})
-
-		for _, record := range records {
-			for i, field := range record {
-				proxytestdata += fmt.Sprintf("%*s ", maxWidths[i], field)
-			}
-			proxytestdata += "\n"
-		}
-		c.Writer.Write([]byte(proxytestdata)) //nolint
-		c.Writer.Flush()
-		c.Writer.Write([]byte(htmltoplink + htmlend)) //nolint
-		c.Writer.Flush()
-	})
-
-	r1.GET("/rewards/testproxies.sh", func(c *gin.Context) {
-		serveSyntaxHighlighted(c)
-	})
 
 	r1.GET("/log-collection/tree/:pk", func(c *gin.Context) {
 		c.Writer.Header().Set("Server", "")
@@ -615,7 +480,18 @@ func server() {
 	r1.GET("/log-collection/tplogs", func(c *gin.Context) {
 		c.Writer.Header().Set("Server", "")
 		c.Writer.WriteHeader(http.StatusOK)
-		c.Writer.Write([]byte([]byte(tploghtmlfunc()))) //nolint
+		c.Writer.Write([]byte(func() (l string) {
+				l = "Skywire Transport Bandwidth Logs By Day\n\n
"
+				l += navlinks
+				l += "

Blue = Verified Bandwidth

" + l += "

Yellow = Transport bandwidth inconsistent

" + l += "

Red = Error: sent or received is zero

" + tp, _ := script.Exec(`skywire cli log tp -d rewards/log_backups`).String() //nolint + l += fmt.Sprintf("%s\n", ansihtml.ConvertToHTML([]byte(tp))) + l += htmltoplink + l += htmlend + return l + }())) //nolint }) r1.GET("/skycoin-rewards", func(c *gin.Context) { @@ -668,35 +544,35 @@ func server() { l += "\n" rewardtxncsvs, _ := script.FindFiles(`rewards/hist`).MatchRegexp(regexp.MustCompile(".?.?.?.?-.?.?-.?.?_rewardtxn0.csv")).Replace("rewards/hist/", "").Replace("_rewardtxn0.csv", "").Slice() //nolint for i := len(rewardtxncsvs) - 1; i >= 0; i-- { - skycoinpershare, _ := script.File("rewards/hist/"+rewardtxncsvs[i]+"_stats.txt").Match("Skycoin Per Share: ").Replace("Skycoin Per Share: ", "").String() //nolint - skycoinpershare1 := "" - skycoinpershare2 := "" - if strings.TrimSpace(skycoinpershare) == "" { - skycoinpershare1, _ = script.File("rewards/hist/"+rewardtxncsvs[i]+"_stats.txt").Match("Skycoin Per Share (Pool 1): ").Replace("Skycoin Per Share (Pool 1): ", "").String() //nolint - skycoinpershare2, _ = script.File("rewards/hist/"+rewardtxncsvs[i]+"_stats.txt").Match("Skycoin Per Share (Pool 2): ").Replace("Skycoin Per Share (Pool 2): ", "").String() //nolint - skycoinpershare1 = strings.TrimSpace(skycoinpershare1) - skycoinpershare2 = strings.TrimSpace(skycoinpershare2) - } else { - skycoinpershare1 = strings.TrimSpace(skycoinpershare) - skycoinpershare2 = "" - } - - var distributedIcon string - if _, err := os.Stat("rewards/hist/" + rewardtxncsvs[i] + ".txt"); err == nil { - distributedIcon = "" - } else { - distributedIcon = "" - } - l += "\n" - l += "" + rewardtxncsvs[i] + "\n" - l += "" + skycoinpershare1 + "\n" - if skycoinpershare2 != "" { - l += "" + skycoinpershare2 + "\n" - } else { - l += "\n" - } - l += "" + distributedIcon + "\n" - l += "\n" + skycoinpershare, _ := script.File("rewards/hist/"+rewardtxncsvs[i]+"_stats.txt").Match("Skycoin Per Share: ").Replace("Skycoin Per Share: ", "").String() //nolint + skycoinpershare1 := "" + skycoinpershare2 := "" + if strings.TrimSpace(skycoinpershare) == "" { + skycoinpershare1, _ = script.File("rewards/hist/"+rewardtxncsvs[i]+"_stats.txt").Match("Skycoin Per Share (Pool 1): ").Replace("Skycoin Per Share (Pool 1): ", "").String() //nolint + skycoinpershare2, _ = script.File("rewards/hist/"+rewardtxncsvs[i]+"_stats.txt").Match("Skycoin Per Share (Pool 2): ").Replace("Skycoin Per Share (Pool 2): ", "").String() //nolint + skycoinpershare1 = strings.TrimSpace(skycoinpershare1) + skycoinpershare2 = strings.TrimSpace(skycoinpershare2) + } else { + skycoinpershare1 = strings.TrimSpace(skycoinpershare) + skycoinpershare2 = "" + } + + var distributedIcon string + if _, err := os.Stat("rewards/hist/" + rewardtxncsvs[i] + ".txt"); err == nil { + distributedIcon = "" + } else { + distributedIcon = "" + } + l += "\n" + l += "" + rewardtxncsvs[i] + "\n" + l += "" + skycoinpershare1 + "\n" + if skycoinpershare2 != "" { + l += "" + skycoinpershare2 + "\n" + } else { + l += "\n" + } + l += "" + distributedIcon + "\n" + l += "\n" } l += "\n\n" l += "
" + htmltoplink