Added ability to filter results for pool on .last / .shares / .payout

This commit is contained in:
DataHoarder 2023-04-28 21:06:06 +02:00
parent ab823b04ad
commit 36dd97927e
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk

View file

@ -10,7 +10,9 @@ import (
"git.gammaspectra.live/P2Pool/p2pool-observer/types"
"git.gammaspectra.live/P2Pool/p2pool-observer/utils"
hbot "github.com/whyrusleeping/hellabot"
"golang.org/x/exp/slices"
"regexp"
"strings"
"time"
)
@ -30,16 +32,25 @@ func isNickAllowed(nick string) error {
return nil
}
func filterEntriesForChannel(bot *hbot.Bot, message *hbot.Message, entries []*channelEntry) (result []*channelEntry) {
func filterEntriesForChannel(bot *hbot.Bot, message *hbot.Message, entries []*channelEntry, exactMatches ...string) (result []*channelEntry) {
var actualEntry *channelEntry
var exactMatchesResult []*channelEntry
for _, e := range entries {
if e.Channel == message.To {
actualEntry = e
}
if slices.ContainsFunc(exactMatches, func(s string) bool {
return s == strings.ToLower(e.Name)
}) {
exactMatchesResult = append(exactMatchesResult, e)
}
if e.ApiEndpoint != "" && (message.To == bot.Nick || e.Channel == message.To) {
result = append(result, e)
}
}
if len(exactMatchesResult) > 0 {
return exactMatchesResult
}
if len(result) == 0 && actualEntry != nil && actualEntry.ApiEndpoint == "" {
for _, e := range entries {
if e.ApiEndpoint != "" {
@ -52,12 +63,12 @@ func filterEntriesForChannel(bot *hbot.Bot, message *hbot.Message, entries []*ch
var commands = []command{
{
Help: ".last",
Help: ".last [pool name]",
Description: "Displays the last time each pool found a Monero block, and other metrics.",
Match: regexp.MustCompile("(?i)^\\.(last|block|lastblock|pool)[ \t]*"),
Match: regexp.MustCompile("(?i)^\\.(last|block|lastblock|pool)[ \t]*([a-z ]+)?[ \t]*"),
Handle: func(db *DB, entries []*channelEntry, bot *hbot.Bot, message *hbot.Message, replyTo string, matches ...string) bool {
for _, e := range filterEntriesForChannel(bot, message, entries) {
for _, e := range filterEntriesForChannel(bot, message, entries, strings.TrimSpace(strings.ToLower(matches[2]))) {
func(e *channelEntry) {
e.ChainLock.RLock()
defer e.ChainLock.RUnlock()
@ -99,9 +110,9 @@ var commands = []command{
},
},
{
Help: ".status",
Help: ".status [pool name]",
Description: "Displays your pool and share status across all subscriptions to your nick",
Match: regexp.MustCompile("(?i)^\\.(status|shares)[ \t]*"),
Match: regexp.MustCompile("(?i)^\\.(status|shares)[ \t]*([a-z ]+)?[ \t]*"),
Handle: func(db *DB, entries []*channelEntry, bot *hbot.Bot, message *hbot.Message, replyTo string, matches ...string) bool {
subs := db.GetByNick(message.Name)
if len(subs) == 0 {
@ -126,7 +137,7 @@ var commands = []command{
var hasResults bool
var results []*result
for _, e := range filterEntriesForChannel(bot, message, entries) {
for _, e := range filterEntriesForChannel(bot, message, entries, strings.TrimSpace(strings.ToLower(matches[2]))) {
func(e *channelEntry) {
e.ChainLock.RLock()
defer e.ChainLock.RUnlock()
@ -197,9 +208,9 @@ var commands = []command{
},
},
{
Help: ".payout",
Help: ".payout [pool name]",
Description: "Displays your last payout details across all pools",
Match: regexp.MustCompile("(?i)^\\.(payouts?|payments?|last-payments?)[ \t]*"),
Match: regexp.MustCompile("(?i)^\\.(payouts?|payments?|last-payments?)[ \t]*([a-z ]+)?[ \t]*"),
Handle: func(db *DB, entries []*channelEntry, bot *hbot.Bot, message *hbot.Message, replyTo string, matches ...string) bool {
subs := db.GetByNick(message.Name)
if len(subs) == 0 {
@ -217,7 +228,7 @@ var commands = []command{
var results []*result
for _, e := range filterEntriesForChannel(bot, message, entries) {
for _, e := range filterEntriesForChannel(bot, message, entries, strings.TrimSpace(strings.ToLower(matches[2]))) {
func(e *channelEntry) {
for _, sub := range subs {
payouts := getSliceFromAPI[*index.Payout](e.ApiEndpoint, fmt.Sprintf("/api/payouts/%s", sub.Address.ToBase58()))