diff --git a/app/server/pkg/community/github/analytics.go b/app/server/pkg/community/github/analytics.go index f66c5113..f1ea5cae 100644 --- a/app/server/pkg/community/github/analytics.go +++ b/app/server/pkg/community/github/analytics.go @@ -26,17 +26,24 @@ var Github GithubData // Handler is responsible for the looping the UpdateGithubData() func Handler() { - for true { - log.Infof("Updating github litmus repository data...") - err := updateGithubStars() - if err != nil { - log.Error(err) - } - err = updateExpCount() - if err != nil { - log.Error(err) - } - log.Infof("Github litmus repository data updated...") + for { + func() { + defer func() { + if r := recover(); r != nil { + log.Errorf("Recovered from panic in community github Handler: %v", r) + } + }() + log.Infof("Updating github litmus repository data...") + err := updateGithubStars() + if err != nil { + log.Error(err) + } + err = updateExpCount() + if err != nil { + log.Error(err) + } + log.Infof("Github litmus repository data updated...") + }() time.Sleep(timeInterval) } } @@ -47,16 +54,24 @@ func updateGithubStars() error { if err != nil { return fmt.Errorf("error while getting github star data, err :%s", err) } - data, error := ioutil.ReadAll(response.Body) - if error != nil { - return fmt.Errorf("error while getting github star data, err :%s", error) + defer response.Body.Close() + if response.StatusCode != http.StatusOK { + return fmt.Errorf("GitHub API returned non-OK status: %d", response.StatusCode) + } + data, err := ioutil.ReadAll(response.Body) + if err != nil { + return fmt.Errorf("error while getting github star data, err :%s", err) } var githubD map[string]interface{} err = json.Unmarshal(data, &githubD) if err != nil { return fmt.Errorf("error while getting github star data, err :%s", err) } - Github.Stars = fmt.Sprintf("%v", githubD["stargazers_count"]) + starCount, ok := githubD["stargazers_count"] + if !ok { + return fmt.Errorf("stargazers_count field not found in GitHub API response") + } + Github.Stars = fmt.Sprintf("%v", starCount) return nil } @@ -66,7 +81,14 @@ func updateExpCount() error { if err != nil { return fmt.Errorf("error while getting experiment count, err :%s", err) } - data, _ := ioutil.ReadAll(response.Body) + defer response.Body.Close() + if response.StatusCode != http.StatusOK { + return fmt.Errorf("GitHub API returned non-OK status: %d", response.StatusCode) + } + data, err := ioutil.ReadAll(response.Body) + if err != nil { + return fmt.Errorf("error while reading response body, err :%s", err) + } var dir []map[string]interface{} err = json.Unmarshal(data, &dir) if err != nil { @@ -74,27 +96,52 @@ func updateExpCount() error { } count := 0 for _, dirD := range dir { - if dirD["type"].(string) == "dir" { - response, err = http.Get(githubApi + organization + "/chaos-charts/contents/faults/" + dirD["name"].(string)) - if err != nil { - return fmt.Errorf("error while getting experiment count, err :%s", err) - } - data, error := ioutil.ReadAll(response.Body) - if error != nil { - return fmt.Errorf("error while getting experiment count, err :%s", error) - } - var exp []map[string]interface{} - err = json.Unmarshal(data, &exp) + typeVal, typeOk := dirD["type"].(string) + nameVal, nameOk := dirD["name"].(string) + if !typeOk || !nameOk { + continue + } + if typeVal == "dir" { + expCount, err := getExperimentCountForDir(nameVal) if err != nil { - return fmt.Errorf("error while getting experiment count, err :%s", err) - } - for _, expD := range exp { - if expD["type"].(string) == "dir" && expD["name"].(string) != "icons" { - count++ - } + return err } + count += expCount } } Github.ExperimentCount = fmt.Sprintf("%v", count) return nil } + +// getExperimentCountForDir fetches and counts experiments in a specific fault directory +func getExperimentCountForDir(dirName string) (int, error) { + response, err := http.Get(githubApi + organization + "/chaos-charts/contents/faults/" + dirName) + if err != nil { + return 0, fmt.Errorf("error while getting experiment count, err :%s", err) + } + defer response.Body.Close() + if response.StatusCode != http.StatusOK { + return 0, fmt.Errorf("GitHub API returned non-OK status: %d", response.StatusCode) + } + data, err := ioutil.ReadAll(response.Body) + if err != nil { + return 0, fmt.Errorf("error while reading response body, err :%s", err) + } + var exp []map[string]interface{} + err = json.Unmarshal(data, &exp) + if err != nil { + return 0, fmt.Errorf("error while getting experiment count, err :%s", err) + } + count := 0 + for _, expD := range exp { + typeVal, typeOk := expD["type"].(string) + nameVal, nameOk := expD["name"].(string) + if !typeOk || !nameOk { + continue + } + if typeVal == "dir" && nameVal != "icons" { + count++ + } + } + return count, nil +} diff --git a/app/server/pkg/docker/docker.go b/app/server/pkg/docker/docker.go index 915198e6..c0623b5b 100644 --- a/app/server/pkg/docker/docker.go +++ b/app/server/pkg/docker/docker.go @@ -11,9 +11,13 @@ func FetchDockerPullsDetails() ([]byte, error) { if err != nil { return nil, fmt.Errorf("Error while getting docker pull data, err :%s", err) } + defer response.Body.Close() + if response.StatusCode != http.StatusOK { + return nil, fmt.Errorf("Docker Hub API returned non-OK status: %d", response.StatusCode) + } data, err := ioutil.ReadAll(response.Body) if err != nil { - return nil, fmt.Errorf("Error while getting docker pull data, err :%s", err) + return nil, fmt.Errorf("Error while reading docker pull data, err :%s", err) } - return data, err + return data, nil } diff --git a/app/server/pkg/github/github.go b/app/server/pkg/github/github.go index a2c44e1d..7dc8c291 100644 --- a/app/server/pkg/github/github.go +++ b/app/server/pkg/github/github.go @@ -19,12 +19,19 @@ var basePath = os.Getenv("GOPATH") + "/src/github.com/litmuschaos/charthub.litmu // Handler is responsible for the looping the UpdateGithubData() func Handler() { - for true { - log.Infof("Updating Github Litmus Repo Data ...") - err := UpdateGithubData() - if err != nil { - log.Error(err) - } + for { + func() { + defer func() { + if r := recover(); r != nil { + log.Errorf("Recovered from panic in github Handler: %v", r) + } + }() + log.Infof("Updating Github Litmus Repo Data ...") + err := UpdateGithubData() + if err != nil { + log.Error(err) + } + }() time.Sleep(timeInterval) } } @@ -39,12 +46,22 @@ func UpdateGithubData() error { if err != nil { return fmt.Errorf("Error while getting github repo data, err :%s", err) } - data, _ := ioutil.ReadAll(response.Body) + defer response.Body.Close() + if response.StatusCode != http.StatusOK { + return fmt.Errorf("GitHub API returned non-OK status: %d", response.StatusCode) + } + data, err := ioutil.ReadAll(response.Body) + if err != nil { + return fmt.Errorf("Error while reading response body, err :%s", err) + } file, err := os.Create(basePath + "githubRepoData.json") if err != nil { return fmt.Errorf("Error saving github data, err :%s", err) } - file.WriteString(string(data)) defer file.Close() + _, err = file.WriteString(string(data)) + if err != nil { + return fmt.Errorf("Error writing github data, err :%s", err) + } return nil }