-
Notifications
You must be signed in to change notification settings - Fork 0
/
report_resubmit.go
243 lines (216 loc) · 5.8 KB
/
report_resubmit.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
package main
import (
"fmt"
"log"
"net/http"
"os"
"sort"
"strings"
"time"
"cloud.google.com/go/storage"
"github.com/jehiah/legislator/db"
)
type ResubmitFile struct {
Resubmitted []db.ResubmitLegislation
}
// ReportReintroductions shows bills to be re-submitted
func (a *App) ReportReintroductions(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
template := "report_reintroductions.html"
t := newTemplate(a.templateFS, template)
type Row struct {
Legislation
NewLegislation *Legislation
}
// data := make(map[int]*Row)
type Page struct {
Page string
SubPage string
LastSync LastSync
Person db.Person
People []db.Person
Session Session
PreviousSession Session
Sessions []Session
Data []*Row
IsCurrentSession bool
ResubmittedOnly bool
FiledBills int
Resubmitted int
ResubmittPct float64
Sponsored int
Responsored int
ResponsoredPct float64
}
body := Page{
Page: "reports",
SubPage: "reintroduction",
Session: CurrentSession,
PreviousSession: Sessions[1],
IsCurrentSession: true,
Sessions: Sessions[:len(Sessions)-1], // skip the last one
ResubmittedOnly: true, // r.Form.Get("resubmitted") == "only",
}
for i, s := range body.Sessions {
if s.String() == r.Form.Get("session") {
body.Session = s
body.PreviousSession = Sessions[i+1]
body.IsCurrentSession = i == 0
}
}
// var metadata []PersonMetadata
// err = a.getJSONFile(r.Context(), "build/people_metadata.json", &metadata)
// if err != nil {
// log.Print(err)
// http.Error(w, "Internal Server Error", 500)
// return
// }
// metadataLookup := make(map[int]PersonMetadata)
// for _, m := range metadata {
// metadataLookup[m.ID] = metadataLookup[]
// }
var people []db.Person
err := a.getJSONFile(r.Context(), "build/people_all.json", &people)
if err != nil {
log.Print(err)
http.Error(w, "Internal Server Error", 500)
return
}
for _, p := range people {
var current, previous bool
for _, or := range p.OfficeRecords {
switch {
case or.BodyName != "City Council":
continue
case or.MemberType == "PRIMARY PUBLIC ADVOCATE":
continue
}
if body.Session.Overlaps(or.Start, or.End) {
current = true
}
if body.PreviousSession.Overlaps(or.Start, or.End) {
previous = true
}
}
if current && previous {
body.People = append(body.People, p)
if p.Slug == r.Form.Get("sponsor") {
body.Person = p
}
// data[p.ID] = &Row{Person: Person{Person: p}}
}
}
resubmittedFrom := make(map[string]db.ResubmitLegislation)
resubmittedTo := make(map[string]db.ResubmitLegislation)
var resubmitted []db.ResubmitLegislation
currentYear := time.Now().Year()
for year := body.Session.StartYear; year <= body.Session.EndYear && year <= currentYear; year++ {
var resubmitFile ResubmitFile
err := a.getJSONFile(r.Context(), fmt.Sprintf("build/resubmit_%d.json", year), &resubmitFile)
if err != nil {
if err == storage.ErrObjectNotExist || os.IsNotExist(err) {
continue
}
log.Print(err)
http.Error(w, "Internal Server Error", 500)
return
}
resubmitted = append(resubmitted, resubmitFile.Resubmitted...)
for _, r := range resubmitFile.Resubmitted {
resubmittedFrom[r.FromFile] = r
resubmittedTo[r.ToFile] = r
}
}
lookup := make(map[string]*Legislation)
for year := body.Session.StartYear; year <= body.Session.EndYear && year <= currentYear; year++ {
var l []Legislation
err := a.getJSONFile(r.Context(), fmt.Sprintf("build/%d.json", year), &l)
if err != nil {
if err == storage.ErrObjectNotExist || os.IsNotExist(err) {
continue
}
log.Print(err)
http.Error(w, "Internal Server Error", 500)
return
}
for _, ll := range l {
ll := ll
_, ok := resubmittedTo[ll.File]
if !ok {
continue
}
lookup[ll.File] = &ll
}
}
// get all the years for the previous legislative session
for year := body.PreviousSession.StartYear; year <= body.PreviousSession.EndYear; year++ {
var l []Legislation
err := a.getJSONFile(r.Context(), fmt.Sprintf("build/%d.json", year), &l)
if err != nil {
if err == storage.ErrObjectNotExist || os.IsNotExist(err) {
continue
}
log.Print(err)
http.Error(w, "Internal Server Error", 500)
return
}
for _, ll := range l {
switch ll.StatusName {
case "Withdrawn",
"Enacted (Charter Referendum)",
"Enacted (Mayor's Desk for Signature)",
"Enacted",
"City Charter Rule Adopted":
continue // no need for re-introduction
}
body.FiledBills++
r, ok := resubmittedFrom[ll.File]
if ok {
body.Resubmitted++
}
if body.ResubmittedOnly && !ok {
continue
}
new := lookup[r.ToFile]
if body.Person.ID > 0 && !ll.SponsoredBy(body.Person.ID) {
continue
}
if ll.SponsoredBy(body.Person.ID) {
body.Sponsored++
if new != nil && new.SponsoredBy(body.Person.ID) {
body.Responsored++
}
}
body.Data = append(body.Data, &Row{
Legislation: ll,
NewLegislation: new,
})
}
}
if body.Resubmitted > 0 {
body.ResubmittPct = (float64(body.Resubmitted) / float64(body.FiledBills)) * 100
}
if body.Responsored > 0 {
body.ResponsoredPct = (float64(body.Responsored) / float64(body.Sponsored)) * 100
}
if body.ResubmittedOnly {
sort.Slice(body.Data, func(i, j int) bool {
return strings.Compare(body.Data[i].NewLegislation.File, body.Data[j].NewLegislation.File) == -1
})
}
err = a.getJSONFile(r.Context(), "build/last_sync.json", &body.LastSync)
if err != nil {
log.Print(err)
http.Error(w, "Internal Server Error", 500)
return
}
w.Header().Set("content-type", "text/html")
cacheTTL := time.Minute * 15
a.addExpireHeaders(w, cacheTTL)
err = t.ExecuteTemplate(w, template, body)
if err != nil {
log.Print(err)
http.Error(w, "Internal Server Error", 500)
return
}
}