/
trend.go
122 lines (106 loc) · 3.39 KB
/
trend.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package cmd
import (
"fmt"
"sort"
"strconv"
"time"
"github.com/spf13/cobra"
)
const GITHUB_TIMESTAMP_LAYOUT = "2006-01-02T15:04:05Z"
var trendCmd = &cobra.Command{
Use: "trend [<org>]",
Short: "Analyze the trend of programming languages used in repos across an organization over time",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
return runTrend(cmd, args)
},
}
func runTrend(cmd *cobra.Command, args []string) (err error) {
var org string
if len(args) > 0 {
org = args[0]
} else {
return fmt.Errorf(Red("No organization specified."))
}
fmt.Printf(Yellow("Analyzing organization: %s\n"), org)
language, _ := cmd.Flags().GetString("language")
top, _ := cmd.Flags().GetInt("top")
repoLimit, _ := cmd.Flags().GetInt("limit")
if repoLimit > 0 {
fmt.Printf(Yellow("Limiting to %d repositories.\n"), repoLimit)
} else {
fmt.Print(Red("Please select a repository limit greater than 0.\n"))
return
}
if language != "" {
fmt.Printf(Yellow("Filtering on language: %s\n"), language)
} else {
if top > 0 {
fmt.Printf(Yellow("Returning the top %d languages.\n"), top)
} else {
fmt.Print(Red("Please select a top languages value greater than 0.\n"))
return
}
}
repos, err := getAllRepos(org, language, repoLimit)
// Create a mapping between year and count of each language
languageMapPerYear := make(map[int]map[string]int)
for _, repo := range repos {
t, err := time.Parse(GITHUB_TIMESTAMP_LAYOUT, repo.CreatedAt)
if err != nil {
fmt.Print(Red("Error parsing CreatedAt date. Skipping repo: " + repo.NameWithOwner + "\n"))
continue
}
year := t.Year()
if _, ok := languageMapPerYear[year]; !ok {
languageMapPerYear[year] = make(map[string]int)
}
for _, language := range repo.Languages {
languageMapPerYear[year][language.Node.Name]++
}
}
// Extract the keys (years) into a slice
years := make([]int, 0, len(languageMapPerYear))
for year := range languageMapPerYear {
years = append(years, year)
}
// Sort the slice in ascending order
sort.Ints(years)
// Reverse the slice to get it in descending order
for i, j := 0, len(years)-1; i < j; i, j = i+1, j-1 {
years[i], years[j] = years[j], years[i]
}
for _, year := range years {
// convert the map to a slice
languages := make([]languageCount, 0, len(languageMapPerYear[year]))
for name, count := range languageMapPerYear[year] {
languages = append(languages, languageCount{name, count})
}
// sort the slice by count in descending order
sort.Slice(languages, func(i, j int) bool {
return languages[i].count > languages[j].count
})
if language_flag == "" {
fmt.Printf(Green("Repos created in year: %d\n"), year)
// print out the top N languages, along with the percent frequency
counter := 0
for _, lang := range languages {
if counter >= top {
break
}
fmt.Printf(" %*d repos (%*d%%) that include the language: %s\n", len(strconv.Itoa(len(repos))), lang.count, 2, (lang.count*100)/len(repos), lang.name)
counter++
}
// if a language was specified, only print that result:
} else {
for _, lang := range languages {
if lang.name == language_flag {
fmt.Printf(Green("Repos created in year: %d\n"), year)
fmt.Printf(" %*d repos (%*d%%) that include the language: %s\n", len(strconv.Itoa(len(repos))), lang.count, 2, (lang.count*100)/len(repos), lang.name)
break
}
}
}
}
return
}