Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 80 additions & 33 deletions app/server/pkg/community/github/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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
}

Expand All @@ -66,35 +81,67 @@ 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 {
return fmt.Errorf("error while getting experiment count, err :%s", err)
}
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
}
8 changes: 6 additions & 2 deletions app/server/pkg/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
33 changes: 25 additions & 8 deletions app/server/pkg/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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
}