Last active
August 29, 2015 14:21
-
-
Save montanaflynn/36a37305569ac984efbc to your computer and use it in GitHub Desktop.
Kong Metrics
This file contains hidden or 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 ( | |
"flag" | |
"encoding/json" | |
"github.com/PuerkitoBio/goquery" | |
"io/ioutil" | |
"net/http" | |
"strconv" | |
) | |
type GitHub struct { | |
Stars int `json:"stargazers_count"` | |
Forks int `json:"forks_count"` | |
Watchers int `json:"subscribers_count"` | |
Downloads int `json:"download_count"` | |
Releases []Release `json:"releases"` | |
} | |
type Release struct { | |
Name string `json:"tag_name"` | |
Downloads int `json:"download_count"` | |
Assets []Asset `json:"assets"` | |
} | |
type Asset struct { | |
Name string `json:"name"` | |
Downloads int `json:"download_count"` | |
} | |
func getGitHubMetrics() GitHub { | |
mainRes, _ := http.Get("https://api.github.com/repos/mashape/kong") | |
defer mainRes.Body.Close() | |
mainBody, _ := ioutil.ReadAll(mainRes.Body) | |
var g GitHub | |
json.Unmarshal(mainBody, &g) | |
releaseRes, _ := http.Get("https://api.github.com/repos/mashape/kong/releases") | |
defer releaseRes.Body.Close() | |
releaseBody, _ := ioutil.ReadAll(releaseRes.Body) | |
var i []Release | |
json.Unmarshal(releaseBody, &i) | |
s, _ := json.Marshal(i) | |
json.Unmarshal(s, &g.Releases) | |
for key, value := range g.Releases { | |
releaseDownloadCount := 0 | |
for _, value := range value.Assets { | |
releaseDownloadCount = releaseDownloadCount + value.Downloads | |
} | |
g.Releases[key].Downloads = releaseDownloadCount | |
} | |
totalDownloadCount := 0 | |
for _, value := range g.Releases { | |
totalDownloadCount = totalDownloadCount + value.Downloads | |
} | |
g.Downloads = totalDownloadCount | |
return g | |
} | |
type LuaRocks struct { | |
Downloads int `json:"downloads"` | |
} | |
func getLuaRocksMetrics() *LuaRocks { | |
doc, _ := goquery.NewDocument("https://luarocks.org/search?q=kong") | |
downloadCount, _ := strconv.Atoi(doc.Find(".downloads .value").Text()) | |
d := &LuaRocks{ | |
Downloads: downloadCount, | |
} | |
return d | |
} | |
type Docker struct { | |
Stars int `json:"stars"` | |
Pulls int `json:"pulls"` | |
} | |
func getDockerMetrics() Docker { | |
resp, _ := http.Get("http://docker.cloudbrain.io/mashape/kong") | |
defer resp.Body.Close() | |
body, _ := ioutil.ReadAll(resp.Body) | |
var d Docker | |
json.Unmarshal(body, &d) | |
return d | |
} | |
type Container struct { | |
GitHub GitHub `json:"github"` | |
LuaRocks *LuaRocks `json:"luarocks"` | |
Docker Docker `json:"docker"` | |
} | |
func serveDocker(w http.ResponseWriter, r *http.Request) { | |
c := &Container{ | |
GitHub: getGitHubMetrics(), | |
LuaRocks: getLuaRocksMetrics(), | |
Docker: getDockerMetrics(), | |
} | |
j, _ := json.MarshalIndent(c, "", " ") | |
w.WriteHeader(http.StatusOK) | |
w.Write([]byte(string(j))) | |
} | |
func main() { | |
var port *string = flag.String("port", "7777", "Port to listen on") | |
flag.Parse() | |
http.HandleFunc("/", serveDocker) | |
http.ListenAndServe(":"+*port, nil) | |
} |
This file contains hidden or 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" | |
"github.com/PuerkitoBio/goquery" | |
"io/ioutil" | |
"net/http" | |
"strconv" | |
) | |
type GitHub struct { | |
Stars int `json:"stargazers_count"` | |
Forks int `json:"forks_count"` | |
Watchers int `json:"subscribers_count"` | |
Downloads int `json:"download_count"` | |
Releases []Release `json:"releases"` | |
} | |
type Release struct { | |
Name string `json:"tag_name"` | |
Downloads int `json:"download_count"` | |
Assets []Asset `json:"assets"` | |
} | |
type Asset struct { | |
Name string `json:"name"` | |
Downloads int `json:"download_count"` | |
} | |
func getGitHubMetrics() GitHub { | |
mainRes, _ := http.Get("https://api.github.com/repos/mashape/kong") | |
defer mainRes.Body.Close() | |
mainBody, _ := ioutil.ReadAll(mainRes.Body) | |
var g GitHub | |
json.Unmarshal(mainBody, &g) | |
releaseRes, _ := http.Get("https://api.github.com/repos/mashape/kong/releases") | |
defer releaseRes.Body.Close() | |
releaseBody, _ := ioutil.ReadAll(releaseRes.Body) | |
var i []Release | |
json.Unmarshal(releaseBody, &i) | |
s, _ := json.Marshal(i) | |
json.Unmarshal(s, &g.Releases) | |
for key, value := range g.Releases { | |
releaseDownloadCount := 0 | |
for _, value := range value.Assets { | |
releaseDownloadCount = releaseDownloadCount + value.Downloads | |
} | |
g.Releases[key].Downloads = releaseDownloadCount | |
} | |
totalDownloadCount := 0 | |
for _, value := range g.Releases { | |
totalDownloadCount = totalDownloadCount + value.Downloads | |
} | |
g.Downloads = totalDownloadCount | |
return g | |
} | |
type LuaRocks struct { | |
Downloads int `json:"downloads"` | |
} | |
func getLuaRocksMetrics() *LuaRocks { | |
doc, _ := goquery.NewDocument("https://luarocks.org/search?q=kong") | |
downloadCount, _ := strconv.Atoi(doc.Find(".downloads .value").Text()) | |
d := &LuaRocks{ | |
Downloads: downloadCount, | |
} | |
return d | |
} | |
type Docker struct { | |
Stars int `json:"stars"` | |
Pulls int `json:"pulls"` | |
} | |
func getDockerMetrics() Docker { | |
resp, _ := http.Get("http://docker.cloudbrain.io/mashape/kong") | |
defer resp.Body.Close() | |
body, _ := ioutil.ReadAll(resp.Body) | |
var d Docker | |
json.Unmarshal(body, &d) | |
return d | |
} | |
type Container struct { | |
GitHub GitHub `json:"github"` | |
LuaRocks *LuaRocks `json:"luarocks"` | |
Docker Docker `json:"docker"` | |
} | |
func main() { | |
c := &Container{ | |
GitHub: getGitHubMetrics(), | |
LuaRocks: getLuaRocksMetrics(), | |
Docker: getDockerMetrics(), | |
} | |
j, _ := json.MarshalIndent(c, "", " ") | |
fmt.Println(string(j)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment