diff --git a/scripts/compare_providers/main.go b/scripts/compare_providers/main.go new file mode 100644 index 000000000..beba1c34c --- /dev/null +++ b/scripts/compare_providers/main.go @@ -0,0 +1,90 @@ +package main + +import ( + "context" + "flag" + "fmt" + "log" + + finderhttpclient "github.com/ipni/storetheindex/api/v0/finder/client/http" + "github.com/ipni/storetheindex/api/v0/finder/model" + "github.com/libp2p/go-libp2p/core/peer" +) + +type compareProvidersStats struct { + unknown uint + latestAdMismatch uint + latestAdMatch uint + totalSourceProviders uint + totalTargetProviders uint +} + +func (s compareProvidersStats) print() { + fmt.Println("Stats:") + fmt.Printf(" OK: %d\n", s.latestAdMatch) + fmt.Printf(" Unknown by target: %d\n", s.unknown) + fmt.Printf(" Latest Ad Mismatch: %d\n", s.latestAdMismatch) + fmt.Println(" --------------------------") + fmt.Printf(" Total source providers: %d\n", s.totalSourceProviders) + fmt.Printf(" Total target providers: %d\n", s.totalTargetProviders) +} + +func main() { + source := flag.String("source", "", "Source indexer") + target := flag.String("target", "", "Target indexer") + + flag.Parse() + + if *source == "" || *target == "" { + log.Fatal("both indexer instances must be specified") + } + + sourceClient, err := finderhttpclient.New(*source) + if err != nil { + log.Fatal(err) + } + + targetClient, err := finderhttpclient.New(*target) + if err != nil { + log.Fatal(err) + } + ctx := context.Background() + + sourceProvs, err := sourceClient.ListProviders(ctx) + if err != nil { + log.Fatal(err) + } + + targetProvs, err := targetClient.ListProviders(ctx) + if err != nil { + log.Fatal(err) + } + + targets := make(map[peer.ID]*model.ProviderInfo) + for _, target := range targetProvs { + if target.AddrInfo.ID == "" { + continue + } + targets[target.AddrInfo.ID] = target + } + var stats compareProvidersStats + for _, p := range sourceProvs { + id := p.AddrInfo.ID + fmt.Printf("%s: ", id) + if other, exists := targets[id]; !exists { + fmt.Println("Unknown by target indexer.") + stats.unknown++ + } else if p.LastAdvertisement != other.LastAdvertisement { + fmt.Println("Mismatching latest ad") + // TODO implement diagnosis of which is ahead/behind and by how many ads. + stats.latestAdMismatch++ + } else { + fmt.Println("OK") + stats.latestAdMatch++ + } + } + stats.totalSourceProviders = uint(len(sourceProvs)) + stats.totalTargetProviders = uint(len(targetProvs)) + fmt.Println() + stats.print() +} diff --git a/scripts/peer_id_from_priv_key.go b/scripts/peer_id_from_priv_key/main.go similarity index 100% rename from scripts/peer_id_from_priv_key.go rename to scripts/peer_id_from_priv_key/main.go