Skip to content

Commit 5ee63da

Browse files
author
4rkal
committed
show hashrate in mining tab
1 parent 64a4784 commit 5ee63da

File tree

4 files changed

+94
-3
lines changed

4 files changed

+94
-3
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
build:
2+
go build -o tmp/dmvp2p main.go
3+
4+
run:
5+
go run main.go

helpers/helpers.go

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ var (
1919
xmrigProcess *exec.Cmd
2020
)
2121

22+
type Hashrate struct {
23+
Total []float64 `json:"total"`
24+
Highest float64 `json:"highest"`
25+
}
26+
27+
type SummaryResponse struct {
28+
Hashrate Hashrate `json:"hashrate"`
29+
}
30+
2231
type User struct {
2332
Name string `json:"name"`
2433
GitHub string `json:"github"`
@@ -80,7 +89,13 @@ func startP2Pool(host, address, path string) error {
8089
}
8190

8291
func startXmrig(path string) error {
83-
xmrigProcess = exec.Command(path, "-o", "127.0.0.1:3333")
92+
xmrigProcess = exec.Command(path,
93+
"-o", "127.0.0.1:3333",
94+
"--http-host=127.0.0.1",
95+
"--http-port=9999",
96+
"--http-access-token=dmvp2p",
97+
"--api-worker-id=1",
98+
"--api-id=1")
8499

85100
if err := xmrigProcess.Start(); err != nil {
86101
return fmt.Errorf("failed to start xmrig: %w", err)
@@ -133,3 +148,56 @@ func SelectFileWithDialog(label *widget.Label, settingsPath *string) {
133148
*settingsPath = selectedPath
134149
label.SetText("Selected file: " + *settingsPath)
135150
}
151+
152+
func GetXmrigStats() Hashrate {
153+
url := "http://127.0.0.1:9999/1/summary"
154+
token := "dmvp2p"
155+
156+
client := &http.Client{}
157+
158+
req, err := http.NewRequest("GET", url, nil)
159+
if err != nil {
160+
fmt.Printf("Error creating request: %v\n", err)
161+
return Hashrate{}
162+
}
163+
164+
req.Header.Add("Authorization", "Bearer "+token)
165+
166+
resp, err := client.Do(req)
167+
if err != nil {
168+
fmt.Printf("Error making request: %v\n", err)
169+
return Hashrate{}
170+
}
171+
defer resp.Body.Close()
172+
173+
if resp.StatusCode != http.StatusOK {
174+
fmt.Printf("Received non-OK HTTP status: %s\n", resp.Status)
175+
return Hashrate{}
176+
}
177+
178+
body, err := ioutil.ReadAll(resp.Body)
179+
if err != nil {
180+
fmt.Printf("Error reading response body: %v\n", err)
181+
return Hashrate{}
182+
}
183+
184+
var summary SummaryResponse
185+
err = json.Unmarshal(body, &summary)
186+
if err != nil {
187+
fmt.Printf("Error parsing JSON response: %v\n", err)
188+
return Hashrate{}
189+
}
190+
191+
totalHashrate := 0.0
192+
if len(summary.Hashrate.Total) > 0 {
193+
totalHashrate = summary.Hashrate.Total[0]
194+
}
195+
196+
fmt.Printf("Total Hashrate: %.2f H/s\n", totalHashrate)
197+
fmt.Printf("Highest Hashrate: %.2f H/s\n", summary.Hashrate.Highest)
198+
199+
return Hashrate{
200+
Total: []float64{totalHashrate},
201+
Highest: summary.Hashrate.Highest,
202+
}
203+
}

helpers/users.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"x": "MoneroTalk",
3333
"website": "https://www.monerotalk.live/",
3434
"address": "47e6GvjL4in5Zy5vVHMb9PQtGXQAcFvWSCQn2fuwDYZoZRk3oFjefr51WBNDGG9EjF1YDavg7pwGDFSAVWC5K42CBcLLv5U",
35-
"description": "Monero Talk broadcasts weekly conversations w/ topical guests to understand what the Monero cryptocurrency is today \n & what it hopes to become"
35+
"description": "Broadcasts weekly conversations w/ topical guests to understand what the Monero cryptocurrency is today & what it hopes to become"
3636
}
3737

3838
]

main.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"image/color"
66
"net/url"
77
"strings"
8+
"time"
89

910
"fyne.io/fyne/v2"
1011
"fyne.io/fyne/v2/app"
@@ -183,6 +184,23 @@ func main() {
183184
infoLabel.SetText("Selected file: " + settings.P2poolPath)
184185
}
185186

187+
hashrate_label := canvas.NewText("No hashrate detected", color.White)
188+
hashrate_label.TextSize = 20
189+
hashrate_label.Alignment = fyne.TextAlignCenter
190+
191+
ticker := time.NewTicker(2 * time.Second)
192+
defer ticker.Stop()
193+
go func() {
194+
for range ticker.C {
195+
stats := helpers.GetXmrigStats()
196+
if stats.Highest != 0 && stats.Total != nil {
197+
hashrate_label.Text = fmt.Sprintf("%v h/s | Max %v h/s", stats.Total[0], stats.Highest)
198+
199+
}
200+
201+
}
202+
}()
203+
186204
selectFileButton := widget.NewButton("Select File", func() {
187205
helpers.SelectFileWithDialog(infoLabel, &settings.P2poolPath)
188206
})
@@ -207,7 +225,7 @@ func main() {
207225

208226
tabs := container.NewAppTabs(
209227
container.NewTabItemWithIcon("Home", theme.HomeIcon(), container.NewStack(userContainer)),
210-
container.NewTabItemWithIcon("Mining", theme.BrokenImageIcon(), container.NewVBox(text3)),
228+
container.NewTabItemWithIcon("Mining", theme.BrokenImageIcon(), container.NewVBox(text3, emtpy_line, hashrate_label)),
211229
container.NewTabItemWithIcon("Settings", theme.SettingsIcon(), container.NewVBox(text4, fullScreenButton, emtpy_line, p2pool_label, infoLabel, selectFileButton, xmrig_label, infoLabel2, selectFileButton2, emtpy_line, saveSettings)),
212230
)
213231

0 commit comments

Comments
 (0)