/
about.go
245 lines (210 loc) · 6.67 KB
/
about.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package main
import (
"bytes"
"net/http"
"time"
. "github.com/Deleplace/programming-idioms/idioms"
"context"
"google.golang.org/appengine/log"
)
// AboutFacade is the Facade for the About page.
type AboutFacade struct {
PageMeta PageMeta
UserProfile UserProfile
AllIdioms []*Idiom
Coverage CoverageFacade
AllLangs []string
}
// CoverageFacade is the facade for the Language Cover block of the About page.
type CoverageFacade struct {
IdiomIds []int
IdiomTitles []string
Languages []string
// Ex: Checked[123]["java"] == 957
Checked map[int]map[string]int
// How many impls does each language have?
// Ex : langImplCount["java"] == 52
LangImplCount map[string]int
// What is the cumulated "score" for this language?
// Including number of impls, doc URLs, and demo URLs.
// Ex : langImplScore["java"] == 81
LangImplScore map[string]int
}
// aboutPage makes a handler for the full About page, with a specific section
func aboutPage(templateName string) betterHandler {
return func(w http.ResponseWriter, r *http.Request) error {
data := AboutFacade{
PageMeta: PageMeta{
Toggles: toggles,
ExtraCss: []string{
hostPrefix() + themeDirectory() + "/css/docs.css",
hostPrefix() + themeDirectory() + "/css/pages/about.css",
},
ExtraJs: []string{hostPrefix() + themeDirectory() + "/js/pages/about.js"},
},
UserProfile: readUserProfile(r),
AllLangs: AllLanguages(),
}
return templates.ExecuteTemplate(w, templateName, data)
}
}
func ajaxAboutProject(w http.ResponseWriter, r *http.Request) error {
return templates.ExecuteTemplate(w, "block-about-project", nil)
}
func ajaxAboutSeeAlso(w http.ResponseWriter, r *http.Request) error {
return templates.ExecuteTemplate(w, "block-about-see-also", nil)
}
func ajaxAboutContact(w http.ResponseWriter, r *http.Request) error {
return templates.ExecuteTemplate(w, "block-about-contact", nil)
}
func ajaxAboutAllIdioms(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
if cachedHTML := htmlCacheRead(ctx, "/about-block-all-idioms"); cachedHTML != nil {
// Using the whole HTML block from Memcache
log.Debugf(ctx, "%s from memcache!", "/about-block-all-idioms")
_, err := w.Write(cachedHTML)
return err
}
log.Debugf(ctx, "retrieveAllIdioms start...")
allIdioms, err := retrieveAllIdioms(r, false)
if err != nil {
return err
}
log.Debugf(ctx, "retrieveAllIdioms end.")
data := AboutFacade{
PageMeta: PageMeta{
Toggles: toggles,
},
UserProfile: readUserProfile(r),
AllIdioms: allIdioms,
}
var buffer bytes.Buffer
log.Debugf(ctx, "block-about-all-idioms templating start...")
err = templates.ExecuteTemplate(&buffer, "block-about-all-idioms", data)
log.Debugf(ctx, "block-about-all-idioms templating end.")
if err != nil {
return PiErrorf(http.StatusInternalServerError, "%v", err)
}
_, err = w.Write(buffer.Bytes())
if err != nil {
return err
}
htmlCacheWrite(ctx, "/about-block-all-idioms", buffer.Bytes(), 12*time.Hour)
return nil
}
func ajaxAboutLanguageCoverage(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
favlangs := lookForFavoriteLanguages(r)
if len(favlangs) == 0 {
if coverageHTML := htmlCacheZipRead(ctx, "about-block-language-coverage"); coverageHTML != nil {
// Using the whole HTML block from Memcache
log.Debugf(ctx, "block-about-language-coverage from memcache!")
_, err := w.Write(coverageHTML)
return err
}
}
coverage, err := languageCoverage(ctx)
if err != nil {
log.Errorf(ctx, "Error generating language coverage: %v", err)
return PiErrorf(http.StatusInternalServerError, "Couldn't generate language coverage")
}
favoritesFirstWithOrder(coverage.Languages, favlangs, coverage.LangImplCount, coverage.LangImplScore)
data := AboutFacade{
PageMeta: PageMeta{
Toggles: toggles,
},
UserProfile: readUserProfile(r),
Coverage: coverage,
}
var buffer bytes.Buffer
log.Debugf(ctx, "block-about-language-coverage templating start...")
err = templates.ExecuteTemplate(&buffer, "block-about-language-coverage", data)
log.Debugf(ctx, "block-about-language-coverage templating end.")
if err != nil {
return err
}
if len(favlangs) == 0 {
// Caching may also be done in own goroutine, or defered as a task.
htmlCacheZipWrite(ctx, "about-block-language-coverage", buffer.Bytes(), 24*time.Hour)
}
_, err = w.Write(buffer.Bytes())
return err
}
func ajaxAboutRss(w http.ResponseWriter, r *http.Request) error {
return templates.ExecuteTemplate(w, "block-about-rss", nil)
}
type AboutCheatsheetsFacade struct {
UserProfile UserProfile
AllLangs []string
}
func ajaxAboutCheatsheets(w http.ResponseWriter, r *http.Request) error {
data := AboutCheatsheetsFacade{
UserProfile: readUserProfile(r),
AllLangs: AllLanguages(),
}
return templates.ExecuteTemplate(w, "block-about-cheatsheets", data)
}
type AboutBacklogsFacade struct {
UserProfile UserProfile
}
func ajaxAboutBacklogs(w http.ResponseWriter, r *http.Request) error {
data := AboutBacklogsFacade{
UserProfile: readUserProfile(r),
}
return templates.ExecuteTemplate(w, "block-about-backlogs", data)
}
func languageCoverage(ctx context.Context) (cover CoverageFacade, err error) {
checked := map[int]map[string]int{}
langImplCount := map[string]int{}
langImplScore := map[string]int{}
log.Debugf(ctx, "Loading full idiom list...")
_, idioms, err := dao.getAllIdioms(ctx, 799, "-ImplCount") // TODO change 799 ?!
if err != nil {
return cover, err
}
log.Debugf(ctx, "Full idiom list loaded.")
idiomIds := make([]int, len(idioms))
idiomTitles := make([]string, len(idioms))
log.Debugf(ctx, "Counting impls of each idiom...")
for i, idiom := range idioms {
idiomIds[i] = idiom.Id
idiomTitles[i] = idiom.Title
for _, impl := range idiom.Implementations {
if checked[idiom.Id] == nil {
checked[idiom.Id] = map[string]int{impl.LanguageName: impl.Id}
} else {
checked[idiom.Id][impl.LanguageName] = impl.Id
}
langImplCount[impl.LanguageName]++
// 1 point for existing
score := 1
langImplScore[impl.LanguageName]++
if len(impl.DocumentationURL) > 6 {
// 1 point for documentation URL
score++
}
if len(impl.DemoURL) > 6 {
// 1 point for online demo
score++
}
langImplScore[impl.LanguageName] += score
}
}
log.Debugf(ctx, "Impls of each idiom counted.")
cover = CoverageFacade{
IdiomIds: idiomIds,
IdiomTitles: idiomTitles,
Languages: AllLanguages(),
Checked: checked,
LangImplCount: langImplCount,
LangImplScore: langImplScore,
}
return cover, nil
}
func hiddenizeExtraColumns(j int, classBefore, classExtra string) string {
const maxCols = 11 // 12 lang columns 0..11 are shown
if j > maxCols {
return classExtra
}
return classBefore
}