1 | // Copyright 2018 Fedir RYKHTIK. All rights reserved. |
||
2 | // Use of this source code is governed by the GNU GPL 3.0 |
||
3 | // license that can be found in the LICENSE file. |
||
4 | |||
5 | package main |
||
6 | |||
7 | import ( |
||
8 | "encoding/csv" |
||
9 | "fmt" |
||
10 | "log" |
||
11 | "os" |
||
12 | "path/filepath" |
||
13 | "reflect" |
||
14 | ) |
||
15 | |||
16 | func clearHTTPCacheFolder(tmpFolderPath string, dryRun bool) error { |
||
17 | d, err := os.Open(tmpFolderPath) |
||
18 | if err != nil { |
||
19 | log.Fatalf("Could not open %s", tmpFolderPath) |
||
20 | } |
||
21 | defer d.Close() |
||
22 | names, err := d.Readdirnames(-1) |
||
23 | if err != nil { |
||
24 | log.Fatalf("Could not read from %s", tmpFolderPath) |
||
25 | } |
||
26 | for _, name := range names { |
||
27 | fp := filepath.Join(tmpFolderPath, name) |
||
28 | if dryRun { |
||
29 | fmt.Printf("Deleted %s\n", fp) |
||
30 | } else { |
||
31 | err = os.RemoveAll(fp) |
||
32 | if err != nil { |
||
33 | log.Fatalf("Could not remove %s", fp) |
||
34 | } |
||
35 | fmt.Printf("Deleted %s\n", fp) |
||
36 | } |
||
37 | } |
||
38 | return nil |
||
39 | } |
||
40 | |||
41 | func writeCSVStatistics(ghData []Repository, csvFilePath string) { |
||
42 | 1 | var csvData [][]string |
|
43 | 1 | csvData = append(csvData, headersFromStructTags()) |
|
44 | 1 | for _, r := range ghData { |
|
45 | 1 | csvData = append(csvData, formatRepositoryDataForCSV(r)) |
|
46 | } |
||
47 | 1 | writeCsv(csvFilePath, csvData) |
|
48 | } |
||
49 | |||
50 | func formatRepositoryDataForCSV(r Repository) []string { |
||
51 | 1 | ghProjectData := []string{ |
|
52 | r.Name, |
||
53 | fmt.Sprintf("%s", r.URL), |
||
54 | fmt.Sprintf("%s", r.Author), |
||
55 | fmt.Sprintf("%s", r.AuthorLocation), |
||
56 | fmt.Sprintf("%s", r.MainLanguage), |
||
57 | fmt.Sprintf("%s", r.AllLanguages), |
||
58 | fmt.Sprintf("%s", r.Description), |
||
59 | fmt.Sprintf("%d", r.TotalCodeSize), |
||
60 | fmt.Sprintf("%s", r.License), |
||
61 | fmt.Sprintf("%d", r.AuthorsFollowers), |
||
62 | fmt.Sprintf("%d", r.Top10ContributorsFollowers), |
||
63 | fmt.Sprintf("%d/%02d", r.CreatedAt.Year(), r.CreatedAt.Month()), |
||
64 | fmt.Sprintf("%d", r.Age), |
||
65 | fmt.Sprintf("%d", r.TotalCommits), |
||
66 | fmt.Sprintf("%d", r.TotalAdditions), |
||
67 | fmt.Sprintf("%d", r.TotalDeletions), |
||
68 | fmt.Sprintf("%d", r.TotalCodeChanges), |
||
69 | fmt.Sprintf(r.LastCommitDate.Format("2006-01-02 15:04:05")), |
||
70 | fmt.Sprintf("%.4f", r.CommitsByDay), |
||
71 | fmt.Sprintf("%d", r.AverageContributionPeriod), |
||
72 | fmt.Sprintf("%d", r.MediCommitSize), |
||
73 | fmt.Sprintf("%d", r.TotalTags), |
||
74 | fmt.Sprintf("%d", r.Watchers), |
||
75 | fmt.Sprintf("%d", r.Forks), |
||
76 | fmt.Sprintf("%d", r.Contributors), |
||
77 | fmt.Sprintf("%.2f", r.ActiveForkersPercentage), |
||
78 | fmt.Sprintf("%d", r.ReturningContributors), |
||
79 | fmt.Sprintf("%d", r.OpenIssues), |
||
80 | fmt.Sprintf("%d", r.ClosedIssues), |
||
81 | fmt.Sprintf("%d", r.TotalIssues), |
||
82 | fmt.Sprintf("%.4f", r.IssueByDay), |
||
83 | fmt.Sprintf("%.2f", r.ClosedIssuesPercentage), |
||
84 | fmt.Sprintf("%d", r.PlacementPopularity), |
||
85 | fmt.Sprintf("%d", r.PlacementAge), |
||
86 | fmt.Sprintf("%d", r.PlacementTotalCommits), |
||
87 | fmt.Sprintf("%d", r.PlacementTotalTags), |
||
88 | fmt.Sprintf("%d", r.PlacementTop10ContributorsFollowers), |
||
89 | fmt.Sprintf("%d", r.PlacementClosedIssuesPercentage), |
||
90 | fmt.Sprintf("%d", r.PlacementCommitsByDay), |
||
91 | fmt.Sprintf("%d", r.PlacementActiveForkersColumn), |
||
92 | fmt.Sprintf("%d", r.PlacementOverall), |
||
93 | } |
||
94 | 1 | return ghProjectData |
|
95 | } |
||
96 | |||
97 | func headersFromStructTags() []string { |
||
98 | 1 | r := new(Repository) |
|
99 | 1 | return r.reflectRepositoryHeaders() |
|
100 | } |
||
101 | |||
102 | func (f *Repository) reflectRepositoryHeaders() []string { |
||
103 | 1 | var headers []string |
|
104 | 1 | val := reflect.ValueOf(f).Elem() |
|
105 | 1 | for i := 0; i < val.NumField(); i++ { |
|
106 | 1 | headers = append(headers, val.Type().Field(i).Tag.Get("header")) |
|
107 | } |
||
108 | 1 | return headers |
|
109 | } |
||
110 | |||
111 | func writeCsv(csvFilePath string, ghDataCSV [][]string) { |
||
112 | 1 | file, err := os.Create(csvFilePath) |
|
113 | 1 | if err != nil { |
|
114 | log.Fatal("Cannot create file", err) |
||
115 | } |
||
116 | 1 | defer file.Close() |
|
117 | 1 | writer := csv.NewWriter(file) |
|
118 | 1 | defer writer.Flush() |
|
119 | 1 | for _, value := range ghDataCSV { |
|
120 | 1 | err := writer.Write(value) |
|
121 | 1 | if err != nil { |
|
122 | log.Fatal("Cannot write to file", err) |
||
123 | } |
||
124 | } |
||
125 | } |
||
126 |