Last active
February 26, 2023 14:28
-
-
Save yukimochi/4a2057b3795ec0b680183671127563df to your computer and use it in GitHub Desktop.
Check All subscriber in Activity-Relay
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"os" | |
"strings" | |
"time" | |
"github.com/go-redis/redis" | |
) | |
type instance struct { | |
Domain string `json:"domain"` | |
Limited bool `json:"limited"` | |
Failed bool `json:"failed"` | |
} | |
type result struct { | |
Subscribers []instance `json:"subscribers"` | |
Followers []instance `json:"followers"` | |
JobCount int64 `json:"job_count"` | |
ReviewCount int `json:"review_count"` | |
LastUpdated int64 `json:"last_updated"` | |
} | |
func contains(list []string, key string, prefix string) bool { | |
for _, value := range list { | |
if value == prefix+key { | |
return true | |
} | |
} | |
return false | |
} | |
func main() { | |
executedTime := time.Now().Unix() | |
client := redis.NewClient(&redis.Options{ | |
Addr: os.Getenv("REDIS_URL"), | |
}) | |
jobCount, _ := client.HLen("relay").Result() | |
reviews, _ := client.Keys("relay:pending:*").Result() | |
reviewCount := len(reviews) | |
limitedDomains, _ := client.HKeys("relay:config:limitedDomain").Result() | |
failedDomains, _ := client.Keys("relay:statistics:*").Result() | |
// Check All Subscribers. | |
var Subscribers = []instance{} | |
subscribers, _ := client.Keys("relay:subscription:*").Result() | |
for _, subscriber := range subscribers { | |
domain := strings.Replace(subscriber, "relay:subscription:", "", 1) | |
limited := contains(limitedDomains, domain, "") | |
failed := contains(failedDomains, domain, "relay:statistics:") | |
Subscribers = append(Subscribers, instance{ | |
Domain: domain, | |
Limited: limited, | |
Failed: failed, | |
}) | |
} | |
// Check All Follower. | |
var Followers = []instance{} | |
followers, _ := client.Keys("relay:follower:*").Result() | |
for _, follower := range followers { | |
domain := strings.Replace(follower, "relay:follower:", "", 1) | |
limited := contains(limitedDomains, domain, "") | |
failed := contains(failedDomains, domain, "relay:statistics:") | |
Followers = append(Followers, instance{ | |
Domain: domain, | |
Limited: limited, | |
Failed: failed, | |
}) | |
} | |
var result = result{} | |
result.Subscribers = Subscribers | |
result.Followers = Followers | |
result.LastUpdated = executedTime | |
result.JobCount = jobCount | |
result.ReviewCount = reviewCount | |
js, err := json.Marshal(&result) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(string(js)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment