summaryrefslogtreecommitdiff
path: root/misc/dashboard/codereview/dashboard/front.go
blob: 1ef769365801e9acc575f4cd8cd4bd287ca452d1 (plain)
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package dashboard

// This file handles the front page.

import (
	"bytes"
	"html/template"
	"io"
	"net/http"
	"strings"
	"sync"
	"time"

	"appengine"
	"appengine/datastore"
	"appengine/user"
)

func init() {
	http.HandleFunc("/", handleFront)
	http.HandleFunc("/favicon.ico", http.NotFound)
}

// maximum number of active CLs to show in person-specific tables.
const maxCLs = 100

func handleFront(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	data := &frontPageData{
		Reviewers: personList,
		User:      user.Current(c).Email,
		IsAdmin:   user.IsAdmin(c),
	}
	var currentPerson string
	u := data.User
	you := "you"
	if e := r.FormValue("email"); e != "" {
		u = e
		you = e
	}
	currentPerson, data.UserIsReviewer = emailToPerson[u]

	var wg sync.WaitGroup
	errc := make(chan error, 10)
	activeCLs := datastore.NewQuery("CL").
		Filter("Closed =", false).
		Order("-Modified")

	tableFetch := func(index int, f func(tbl *clTable) error) {
		wg.Add(1)
		go func() {
			defer wg.Done()
			start := time.Now()
			if err := f(&data.Tables[index]); err != nil {
				errc <- err
			}
			data.Timing[index] = time.Now().Sub(start)
		}()
	}

	if data.UserIsReviewer {
		tableFetch(0, func(tbl *clTable) error {
			q := activeCLs.Filter("Reviewer =", currentPerson).Limit(maxCLs)
			tbl.Title = "CLs assigned to " + you + " for review"
			tbl.Assignable = true
			_, err := q.GetAll(c, &tbl.CLs)
			return err
		})
	}

	tableFetch(1, func(tbl *clTable) error {
		q := activeCLs.Filter("Author =", currentPerson).Limit(maxCLs)
		tbl.Title = "CLs sent by " + you
		tbl.Assignable = true
		_, err := q.GetAll(c, &tbl.CLs)
		return err
	})

	tableFetch(2, func(tbl *clTable) error {
		q := activeCLs.Limit(50)
		tbl.Title = "Other active CLs"
		tbl.Assignable = true
		if _, err := q.GetAll(c, &tbl.CLs); err != nil {
			return err
		}
		// filter
		if data.UserIsReviewer {
			for i := len(tbl.CLs) - 1; i >= 0; i-- {
				cl := tbl.CLs[i]
				if cl.Author == currentPerson || cl.Reviewer == currentPerson {
					// Preserve order.
					copy(tbl.CLs[i:], tbl.CLs[i+1:])
					tbl.CLs = tbl.CLs[:len(tbl.CLs)-1]
				}
			}
		}
		return nil
	})

	tableFetch(3, func(tbl *clTable) error {
		q := datastore.NewQuery("CL").
			Filter("Closed =", true).
			Order("-Modified").
			Limit(10)
		tbl.Title = "Recently closed CLs"
		tbl.Assignable = false
		_, err := q.GetAll(c, &tbl.CLs)
		return err
	})

	// Not really a table fetch.
	tableFetch(0, func(_ *clTable) error {
		var err error
		data.LogoutURL, err = user.LogoutURL(c, "/")
		return err
	})

	wg.Wait()

	select {
	case err := <-errc:
		c.Errorf("%v", err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	default:
	}

	var b bytes.Buffer
	if err := frontPage.ExecuteTemplate(&b, "front", &data); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	io.Copy(w, &b)
}

type frontPageData struct {
	Tables [4]clTable
	Timing [4]time.Duration

	Reviewers      []string
	UserIsReviewer bool

	User, LogoutURL string // actual logged in user
	IsAdmin         bool
}

type clTable struct {
	Title      string
	Assignable bool
	CLs        []*CL
}

var frontPage = template.Must(template.New("front").Funcs(template.FuncMap{
	"selected": func(a, b string) string {
		if a == b {
			return "selected"
		}
		return ""
	},
	"shortemail": func(s string) string {
		if i := strings.Index(s, "@"); i >= 0 {
			s = s[:i]
		}
		return s
	},
}).Parse(`
<!doctype html>
<html>
  <head>
    <title>Go code reviews</title>
    <link rel="icon" type="image/png" href="/static/icon.png" />
    <style type="text/css">
      body {
        font-family: Helvetica, sans-serif;
      }
      img#gopherstamp {
        float: right;
	height: auto;
	width: 250px;
      }
      h1, h2, h3 {
        color: #777;
	margin-bottom: 0;
      }
      table {
        border-spacing: 0;
      }
      td {
        vertical-align: top;
        padding: 2px 5px;
      }
      tr.unreplied td.email {
        border-left: 2px solid blue;
      }
      tr.pending td {
        background: #fc8;
      }
      tr.failed td {
        background: #f88;
      }
      tr.saved td {
        background: #8f8;
      }
      .cls {
        margin-top: 0;
      }
      a {
        color: blue;
	text-decoration: none;  /* no link underline */
      }
      address {
        font-size: 10px;
	text-align: right;
      }
      .email {
        font-family: monospace;
      }
    </style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  <head>
  <body>

<img id="gopherstamp" src="/static/gopherstamp.jpg" />
<h1>Go code reviews</h1>

<table class="cls">
{{range $i, $tbl := .Tables}}
<tr><td colspan="5"><h3>{{$tbl.Title}}</h3></td></tr>
{{if .CLs}}
{{range $cl := .CLs}}
  <tr id="cl-{{$cl.Number}}" class="{{if not $i}}{{if not .Reviewed}}unreplied{{end}}{{end}}">
    <td class="email">{{$cl.DisplayOwner}}</td>
    <td>
    {{if $tbl.Assignable}}
    <select id="cl-rev-{{$cl.Number}}" {{if not $.UserIsReviewer}}disabled{{end}}>
      <option></option>
      {{range $.Reviewers}}
      <option {{selected . $cl.Reviewer}}>{{.}}</option>
      {{end}}
    </select>
    <script type="text/javascript">
    $(function() {
      $('#cl-rev-{{$cl.Number}}').change(function() {
        var r = $(this).val();
        var row = $('tr#cl-{{$cl.Number}}');
        row.addClass('pending');
        $.post('/assign', {
          'cl': '{{$cl.Number}}',
          'r': r
        }).success(function() {
          row.removeClass('pending');
          row.addClass('saved');
        }).error(function() {
          row.removeClass('pending');
          row.addClass('failed');
        });
      });
    });
    </script>
    {{end}}
    </td>
    <td>
      <a href="http://codereview.appspot.com/{{.Number}}/" title="{{ printf "%s" .Description}}">{{.Number}}: {{.FirstLineHTML}}</a>
      {{if and .LGTMs $tbl.Assignable}}<br /><span style="font-size: smaller;">LGTMs: {{.LGTMHTML}}</span>{{end}}
      {{if and .NotLGTMs $tbl.Assignable}}<br /><span style="font-size: smaller; color: #f74545;">NOT LGTMs: {{.NotLGTMHTML}}</span>{{end}}
      {{if .LastUpdateBy}}<br /><span style="font-size: smaller; color: #777777;">(<span title="{{.LastUpdateBy}}">{{.LastUpdateBy | shortemail}}</span>) {{.LastUpdate}}</span>{{end}}
    </td>
    <td title="Last modified">{{.ModifiedAgo}}</td>
    <td>{{if $.IsAdmin}}<a href="/update-cl?cl={{.Number}}" title="Update this CL">&#x27f3;</a>{{end}}</td>
  </tr>
{{end}}
{{else}}
<tr><td colspan="5"><em>none</em></td></tr>
{{end}}
{{end}}
</table>

<hr />
<address>
You are <span class="email">{{.User}}</span> &middot; <a href="{{.LogoutURL}}">logout</a><br />
datastore timing: {{range .Timing}} {{.}}{{end}}
</address>

  </body>
</html>
`))