Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add usage instead of used flag #663

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions farmerbot/internal/farmerbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func (f *FarmerBot) iterateOnNodes(ctx context.Context, subConn Substrate) error
node := f.state.nodes[nodeID]

if node.powerState == off && (node.neverShutDown || node.hasActiveRentContract) {
log.Debug().Uint32("nodeID", nodeID).Msg("Power on node because it is set to never shutdown")
log.Debug().Uint32("nodeID", nodeID).Msg("Power on node because it is set to never shutdown or has a rent contract")
err := f.powerOn(subConn, nodeID)
if err != nil {
log.Error().Err(err).Send()
Expand All @@ -301,7 +301,12 @@ func (f *FarmerBot) iterateOnNodes(ctx context.Context, subConn Substrate) error
}
}

if f.shouldWakeUp(subConn, node, roundStart) && wakeUpCalls < f.config.Power.PeriodicWakeUpLimit {
if f.shouldWakeUp(subConn, &node, roundStart, wakeUpCalls) {
err = f.state.updateNode(node)
if err != nil {
log.Error().Err(err).Send()
}

err := f.powerOn(subConn, nodeID)
if err != nil {
log.Error().Err(err).Uint32("nodeID", nodeID).Msg("failed to power on node")
Expand Down Expand Up @@ -354,8 +359,10 @@ func (f *FarmerBot) addOrUpdateNode(ctx context.Context, subConn Substrate, node
return nil
}

func (f *FarmerBot) shouldWakeUp(sub Substrate, node node, roundStart time.Time) bool {
if node.powerState != off || time.Since(node.lastTimePowerStateChanged) < periodicWakeUpDuration {
func (f *FarmerBot) shouldWakeUp(sub Substrate, node *node, roundStart time.Time, wakeUpCalls uint8) bool {
if node.powerState != off ||
time.Since(node.lastTimePowerStateChanged) < periodicWakeUpDuration ||
wakeUpCalls >= f.config.Power.PeriodicWakeUpLimit {
return false
}

Expand All @@ -364,6 +371,7 @@ func (f *FarmerBot) shouldWakeUp(sub Substrate, node node, roundStart time.Time)
// we wake up the node if the periodic wake up start time has started and only if the last time the node was awake
// was before the periodic wake up start of that day
log.Info().Uint32("nodeID", uint32(node.ID)).Msg("Periodic wake up")
node.lastTimePeriodicWakeUp = time.Now()
return true
}

Expand All @@ -381,6 +389,7 @@ func (f *FarmerBot) shouldWakeUp(sub Substrate, node node, roundStart time.Time)
// we also do not go through the code if we have woken up too many nodes at once => subtract (10 * (n-1))/min(periodic_wake up_limit, amount_of_nodes) from 8460
// now we can divide that by 10 and randomly generate a number in that range, if it's 0 we do the random wake up
log.Info().Uint32("nodeID", uint32(node.ID)).Msg("Random wake up")
node.timesRandomWakeUps++
return true
}

Expand Down
35 changes: 30 additions & 5 deletions farmerbot/internal/report.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package internal

import (
"encoding/json"
"fmt"
"time"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/rs/zerolog/log"
)

// NodeReport is a report for some node info
Expand All @@ -13,14 +16,17 @@ type NodeReport struct {
HasActiveRentContract bool `json:"rented"`
Dedicated bool `json:"dedicated"`
PublicConfig bool `json:"public_config"`
Used bool `json:"used"`
UsagePercentage uint8 `json:"usage_percentage"`
TimesRandomWakeUps int `json:"random_wakeups"`
SincePowerStateChanged time.Duration `json:"since_power_state_changed"`
SinceLastTimeAwake time.Duration `json:"since_last_time_awake"`
LastTimePeriodicWakeUp time.Time `json:"last_time_periodic_wakeup"`
UntilClaimedResourcesTimeout time.Duration `json:"until_claimed_resources_timeout"`
}

func createNodeReport(n node) NodeReport {
nodeID := uint32(n.ID)

var state string
switch n.powerState {
case on:
Expand Down Expand Up @@ -48,16 +54,23 @@ func createNodeReport(n node) NodeReport {
sinceLastTimeAwake = time.Since(n.lastTimeAwake)
}

var usage uint8
used, total := calculateResourceUsage(map[uint32]node{nodeID: n})
if total != 0 {
usage = uint8(100 * used / total)
}

return NodeReport{
ID: uint32(n.ID),
ID: nodeID,
State: state,
HasActiveRentContract: n.hasActiveRentContract,
Dedicated: n.dedicated,
PublicConfig: n.PublicConfig.HasValue,
Used: !n.isUnused(),
UsagePercentage: usage,
TimesRandomWakeUps: n.timesRandomWakeUps,
SincePowerStateChanged: sincePowerStateChanged,
SinceLastTimeAwake: sinceLastTimeAwake,
LastTimePeriodicWakeUp: n.lastTimePeriodicWakeUp,
UntilClaimedResourcesTimeout: untilClaimedResourcesTimeout,
}
}
Expand All @@ -72,8 +85,9 @@ func (f *FarmerBot) report() string {
"Rented",
"Dedicated",
"public config",
"Used",
"Usage",
"Random wake-ups",
"Periodic wake-up",
"last time state changed",
"last time awake",
"Claimed resources timeout",
Expand All @@ -82,14 +96,25 @@ func (f *FarmerBot) report() string {
for _, node := range f.nodes {
nodeReport := createNodeReport(node)

periodicWakeup := "-"
// if the node wakes up today
if nodeReport.LastTimePeriodicWakeUp.Day() == time.Now().Day() {
periodicWakeupTime, err := json.Marshal(wakeUpDate(nodeReport.LastTimePeriodicWakeUp))
if err != nil {
log.Error().Err(err).Uint32("nodeID", nodeReport.ID).Msg("failed to marshal wake up time")
}
periodicWakeup = string(periodicWakeupTime)
}

t.AppendRow([]interface{}{
nodeReport.ID,
nodeReport.State,
nodeReport.HasActiveRentContract,
nodeReport.Dedicated,
nodeReport.PublicConfig,
nodeReport.Used,
fmt.Sprintf("%d%%", nodeReport.UsagePercentage),
nodeReport.TimesRandomWakeUps,
periodicWakeup,
nodeReport.SincePowerStateChanged,
nodeReport.SinceLastTimeAwake,
nodeReport.UntilClaimedResourcesTimeout,
Expand Down
2 changes: 2 additions & 0 deletions farmerbot/internal/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ type node struct {
lastTimePowerStateChanged time.Time
lastTimeAwake time.Time
timesRandomWakeUps int
// set the time the node wakes up every day
lastTimePeriodicWakeUp time.Time
}

// NodeFilterOption represents the options to find a node
Expand Down
Loading