From cea48a80a50abb074b4c71e31ade0d43d3e950c8 Mon Sep 17 00:00:00 2001 From: Moses Narrow Date: Thu, 25 Jul 2024 13:16:37 -0500 Subject: [PATCH] combine files --- cmd/skywire-cli/commands/rewards/calc.go | 161 +++++++++++++++++- cmd/skywire-cli/commands/rewards/svccheck.go | 163 ------------------- 2 files changed, 156 insertions(+), 168 deletions(-) delete mode 100644 cmd/skywire-cli/commands/rewards/svccheck.go diff --git a/cmd/skywire-cli/commands/rewards/calc.go b/cmd/skywire-cli/commands/rewards/calc.go index 7cd145ed15..21e35ae8e2 100644 --- a/cmd/skywire-cli/commands/rewards/calc.go +++ b/cmd/skywire-cli/commands/rewards/calc.go @@ -2,16 +2,21 @@ package clirewards import ( + "encoding/json" "fmt" "os" + "reflect" "sort" "strconv" "strings" "time" "github.com/bitfield/script" + "github.com/fatih/color" "github.com/spf13/cobra" + "github.com/tidwall/pretty" + "github.com/skycoin/skywire-utilities/pkg/cipher" "github.com/skycoin/skywire-utilities/pkg/logging" tgbot "github.com/skycoin/skywire/cmd/skywire-cli/commands/rewards/tgbot" ) @@ -21,7 +26,6 @@ const yearlyTotalRewards int = 408000 var ( yearlyTotal int hwSurveyPath string - dmsghttpConfig string wdate = time.Now().AddDate(0, 0, -1).Format("2006-01-02") wDate time.Time utfile string @@ -33,7 +37,10 @@ var ( pubkey string logLvl string log *logging.Logger - svcConfig string + sConf []byte + dConf []byte + sConfig string + dConfig string ) type nodeinfo struct { @@ -72,8 +79,8 @@ func init() { RootCmd.Flags().IntVarP(&yearlyTotal, "year", "y", yearlyTotalRewards, "yearly total rewards") RootCmd.Flags().StringVarP(&utfile, "utfile", "u", "ut.txt", "uptime tracker data file") RootCmd.Flags().StringVarP(&hwSurveyPath, "lpath", "p", "log_collecting", "path to the surveys") - RootCmd.Flags().StringVarP(&svcConfig, "svcconf", "f", "/opt/skywire/services-config.json", "path to the services-config.json") - RootCmd.Flags().StringVarP(&dmsghttpConfig, "dmsghttpconf", "g", "/opt/skywire/dmsghttp-config.json", "path to the dmsghttp-config.json") + RootCmd.Flags().StringVarP(&sConfig, "svcconf", "f", "/opt/skywire/services-config.json", "path to the services-config.json") + RootCmd.Flags().StringVarP(&dConfig, "dmsghttpconf", "g", "/opt/skywire/dmsghttp-config.json", "path to the dmsghttp-config.json") RootCmd.Flags().BoolVarP(&h0, "h0", "0", false, "hide statistical data") RootCmd.Flags().BoolVarP(&h1, "h1", "1", false, "hide survey csv data") RootCmd.Flags().BoolVarP(&h2, "h2", "2", false, "hide reward csv data") @@ -97,7 +104,8 @@ Fetch uptimes: skywire-cli ut > ut.txt`, logging.SetLevel(lvl) } } - + mustExist(sConfig) + mustExist(dConfig) wDate, err = time.Parse("2006-01-02", wdate) if err != nil { log.Fatal("Error parsing date:", err) @@ -347,3 +355,146 @@ Fetch uptimes: skywire-cli ut > ut.txt`, } }, } + +func mustExist(path string) { + _, err := os.Stat(path) + if os.IsNotExist(err) { + log.Fatal("the path to the file does not exist: ", path, "\n", err) + } + if err != nil { + log.Fatal("error on os.Stat(", path, "):\n", err) + } +} + +var ( + misconfigured bool + nodeInfo []byte +) + +func init() { + RootCmd.AddCommand( + testCmd, + ) + testCmd.Flags().SortFlags = false + testCmd.Flags().StringVarP(&logLvl, "loglvl", "s", "info", "[ debug | warn | error | fatal | panic | trace ] \u001b[0m*") + testCmd.Flags().StringVarP(&pubkey, "pk", "k", pubkey, "verify services in survey for pubkey") + testCmd.Flags().StringVarP(&hwSurveyPath, "lpath", "p", "log_collecting", "path to the surveys") + testCmd.Flags().StringVarP(&sConfig, "svcconf", "f", "/opt/skywire/services-config.json", "path to the services-config.json") + testCmd.Flags().StringVarP(&dConfig, "dmsghttpconf", "g", "/opt/skywire/dmsghttp-config.json", "path to the dmsghttp-config.json") +} + +var testCmd = &cobra.Command{ + Use: "svc", + Short: "verify services in survey", + Run: func(cmd *cobra.Command, args []string) { + var err error + if log == nil { + log = logging.MustGetLogger("rewards") + } + if logLvl != "" { + if lvl, err := logging.LevelFromString(logLvl); err == nil { + logging.SetLevel(lvl) + } + } + + var pk1 cipher.PubKey + err = pk1.Set(pubkey) + if err != nil { + log.Fatal("invalid public key\n", err) + } + + mustExist(hwSurveyPath) + mustExist(fmt.Sprintf("%s/%s/node-info.json", hwSurveyPath, pubkey)) + mustExist(sConfig) + mustExist(dConfig) + + //stun_servers does not currently match between conf.skywire.skycoin.com & https://github.com/skycoin/skywire/blob/develop/services-config.json ; omit checking them until next version + nodeInfo, err = script.File(fmt.Sprintf("%s/%s/node-info.json", hwSurveyPath, pubkey)).JQ(`.services | del(.stun_servers)`).Bytes() + if err != nil { + log.Fatal("error parsing json with jq:\n", err) + } + + sConf, err := script.File(sConfig).JQ(`.prod | del(.stun_servers)`).Bytes() + if err != nil { + log.Fatal("error parsing json with jq:\n", err) + } + + // dConf, err := script.File(dConfig).JQ(`.prod`).Bytes() + // if err != nil { + // log.Fatal("error parsing json with jq:\n", err) + // } + + // Pretty print the original files for reference + // fmt.Printf("%s\n", pretty.Color(pretty.Pretty(nodeInfo), nil)) + // fmt.Printf("%s\n", pretty.Color(pretty.Pretty(sConf), nil)) + // fmt.Printf("%s\n", pretty.Color(pretty.Pretty(dConf), nil)) + + compareAndPrintDiffs(nodeInfo, sConf) + }, +} + +func compareAndPrintDiffs(nodeInfoData, svcConfigData []byte) { + var nodeInfoServices map[string]interface{} + var svcConfigServices map[string]interface{} + + if err := json.Unmarshal(nodeInfoData, &nodeInfoServices); err != nil { + log.Fatal("error unmarshalling nodeInfoData: ", err) + } + if err := json.Unmarshal(svcConfigData, &svcConfigServices); err != nil { + log.Fatal("error unmarshalling svcConfigData: ", err) + } + + compareMaps(nodeInfoServices, svcConfigServices) +} + +func compareMaps(nodeInfoServices, svcConfigServices map[string]interface{}) { + for key, value1 := range nodeInfoServices { + if value2, ok := svcConfigServices[key]; ok { + if reflect.TypeOf(value1).Kind() == reflect.Slice && reflect.TypeOf(value2).Kind() == reflect.Slice { + slice1 := value1.([]interface{}) + slice2 := value2.([]interface{}) + if !sliceContains(slice1, slice2) { + printDifference(key, value1, value2) + misconfigured = true + } + } else if !reflect.DeepEqual(value1, value2) { + printDifference(key, value1, value2) + misconfigured = true + } + } + } + if !misconfigured { + log.Info("services are configured correctly") + fmt.Printf("%s\n", pretty.Color(pretty.Pretty(nodeInfo), nil)) + return + } +} + +func sliceContains(slice1, slice2 []interface{}) bool { + for _, v2 := range slice2 { + found := false + for _, v1 := range slice1 { + if reflect.DeepEqual(v1, v2) { + found = true + break + } + } + if !found { + return false + } + } + return true +} + +func toJSON(value interface{}) string { + jsonData, err := json.MarshalIndent(value, "", " ") + if err != nil { + return fmt.Sprintf("%v", value) + } + return string(jsonData) +} + +func printDifference(key string, value1, value2 interface{}) { + red := color.New(color.FgRed).SprintFunc() + fmt.Printf("%s: %s != %s\n", key, red(toJSON(value1)), red(toJSON(value2))) +} diff --git a/cmd/skywire-cli/commands/rewards/svccheck.go b/cmd/skywire-cli/commands/rewards/svccheck.go deleted file mode 100644 index 6c879fb7dd..0000000000 --- a/cmd/skywire-cli/commands/rewards/svccheck.go +++ /dev/null @@ -1,163 +0,0 @@ -package clirewards - -import ( - "encoding/json" - "fmt" - "os" - "reflect" - - "github.com/bitfield/script" - "github.com/fatih/color" - "github.com/spf13/cobra" - "github.com/tidwall/pretty" - - "github.com/skycoin/skywire-utilities/pkg/cipher" - "github.com/skycoin/skywire-utilities/pkg/logging" -) - -var ( - misconfigured bool - nodeInfo []byte - sConf []byte - dConf []byte - sConfig string - dConfig string -) - -func init() { - RootCmd.AddCommand( - testCmd, - ) - testCmd.Flags().SortFlags = false - testCmd.Flags().StringVarP(&logLvl, "loglvl", "s", "info", "[ debug | warn | error | fatal | panic | trace ] \u001b[0m*") - testCmd.Flags().StringVarP(&pubkey, "pk", "k", pubkey, "verify services in survey for pubkey") - testCmd.Flags().StringVarP(&hwSurveyPath, "lpath", "p", "log_collecting", "path to the surveys") - testCmd.Flags().StringVarP(&sConfig, "svcconf", "f", "/opt/skywire/services-config.json", "path to the services-config.json") - testCmd.Flags().StringVarP(&dConfig, "dmsghttpconf", "g", "/opt/skywire/dmsghttp-config.json", "path to the dmsghttp-config.json") -} - -var testCmd = &cobra.Command{ - Use: "svc", - Short: "verify services in survey", - Run: func(cmd *cobra.Command, args []string) { - var err error - if log == nil { - log = logging.MustGetLogger("rewards") - } - if logLvl != "" { - if lvl, err := logging.LevelFromString(logLvl); err == nil { - logging.SetLevel(lvl) - } - } - - var pk1 cipher.PubKey - err = pk1.Set(pubkey) - if err != nil { - log.Fatal("invalid public key\n", err) - } - - mustExist(hwSurveyPath) - mustExist(fmt.Sprintf("%s/%s/node-info.json", hwSurveyPath, pubkey)) - mustExist(sConfig) - mustExist(dConfig) - - //stun_servers does not currently match between conf.skywire.skycoin.com & https://github.com/skycoin/skywire/blob/develop/services-config.json ; omit checking them until next version - nodeInfo, err = script.File(fmt.Sprintf("%s/%s/node-info.json", hwSurveyPath, pubkey)).JQ(`.services | del(.stun_servers)`).Bytes() - if err != nil { - log.Fatal("error parsing json with jq:\n", err) - } - - sConf, err := script.File(sConfig).JQ(`.prod | del(.stun_servers)`).Bytes() - if err != nil { - log.Fatal("error parsing json with jq:\n", err) - } - - // dConf, err := script.File(dConfig).JQ(`.prod`).Bytes() - // if err != nil { - // log.Fatal("error parsing json with jq:\n", err) - // } - - // Pretty print the original files for reference - // fmt.Printf("%s\n", pretty.Color(pretty.Pretty(nodeInfo), nil)) - // fmt.Printf("%s\n", pretty.Color(pretty.Pretty(sConf), nil)) - // fmt.Printf("%s\n", pretty.Color(pretty.Pretty(dConf), nil)) - - compareAndPrintDiffs(nodeInfo, sConf) - }, -} - -func mustExist(path string) { - _, err := os.Stat(path) - if os.IsNotExist(err) { - log.Fatal("the path to the file does not exist: ", path, "\n", err) - } - if err != nil { - log.Fatal("error on os.Stat(", path, "):\n", err) - } -} - -func compareAndPrintDiffs(nodeInfoData, svcConfigData []byte) { - var nodeInfoServices map[string]interface{} - var svcConfigServices map[string]interface{} - - if err := json.Unmarshal(nodeInfoData, &nodeInfoServices); err != nil { - log.Fatal("error unmarshalling nodeInfoData: ", err) - } - if err := json.Unmarshal(svcConfigData, &svcConfigServices); err != nil { - log.Fatal("error unmarshalling svcConfigData: ", err) - } - - compareMaps(nodeInfoServices, svcConfigServices) -} - -func compareMaps(nodeInfoServices, svcConfigServices map[string]interface{}) { - for key, value1 := range nodeInfoServices { - if value2, ok := svcConfigServices[key]; ok { - if reflect.TypeOf(value1).Kind() == reflect.Slice && reflect.TypeOf(value2).Kind() == reflect.Slice { - slice1 := value1.([]interface{}) - slice2 := value2.([]interface{}) - if !sliceContains(slice1, slice2) { - printDifference(key, value1, value2) - misconfigured = true - } - } else if !reflect.DeepEqual(value1, value2) { - printDifference(key, value1, value2) - misconfigured = true - } - } - } - if !misconfigured { - log.Info("services are configured correctly") - fmt.Printf("%s\n", pretty.Color(pretty.Pretty(nodeInfo), nil)) - return - } -} - -func sliceContains(slice1, slice2 []interface{}) bool { - for _, v2 := range slice2 { - found := false - for _, v1 := range slice1 { - if reflect.DeepEqual(v1, v2) { - found = true - break - } - } - if !found { - return false - } - } - return true -} - -func toJSON(value interface{}) string { - jsonData, err := json.MarshalIndent(value, "", " ") - if err != nil { - return fmt.Sprintf("%v", value) - } - return string(jsonData) -} - -func printDifference(key string, value1, value2 interface{}) { - red := color.New(color.FgRed).SprintFunc() - fmt.Printf("%s: %s != %s\n", key, red(toJSON(value1)), red(toJSON(value2))) -}