-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
75 lines (57 loc) · 1.51 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"encoding/json"
"fmt"
"net/http"
"sort"
"github.com/davecgh/go-spew/spew"
"github.com/fbessez/octo-org/models"
)
const (
port = ":8090"
)
var (
availableSorting = [4]string{"additions", "deletions", "commits", "ratio"}
)
func statsHandler(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
enableCors(&w)
forceRefresh := getForceRefreshValue(req)
sortOption := req.URL.Query().Get("sort")
if sortOption == "" {
sortOption = "commits"
}
repoNames, err := getRepoNames(ctx, forceRefresh)
check(err)
orgStats, err := getOrgStats(ctx, forceRefresh, repoNames)
check(err)
orgStatsByUser, err := models.ConvertOrgStatsToOrgStatsByUser(*orgStats)
check(err)
writeUserStats(orgStatsByUser)
userCommits := getUserCommits(*orgStatsByUser)
switch sortOption {
case "additions":
sort.Slice(userCommits, func(i, j int) bool {
return userCommits[i].TotalAdditions > userCommits[j].TotalAdditions
})
case "deletions":
sort.Slice(userCommits, func(i, j int) bool {
return userCommits[i].TotalDeletions > userCommits[j].TotalDeletions
})
default:
sort.Slice(userCommits, func(i, j int) bool {
return userCommits[i].TotalCommits > userCommits[j].TotalCommits
})
}
json.NewEncoder(w).Encode(userCommits)
return
}
func enableCors(w *http.ResponseWriter) {
(*w).Header().Set("Access-Control-Allow-Origin", "*")
}
func main() {
spew.Dump(port)
http.HandleFunc("/stats", statsHandler)
fmt.Println("Listening on " + port)
http.ListenAndServe(port, nil)
}