-
Notifications
You must be signed in to change notification settings - Fork 0
/
legislation.go
193 lines (175 loc) · 4.06 KB
/
legislation.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
package main
import (
"fmt"
"html/template"
"sort"
"strconv"
"strings"
"time"
"github.com/jehiah/legislator/db"
)
type LegislationList []Legislation
type Legislation struct {
db.Legislation
}
// FileID returns the file number without the "Int " or "Res " prefix
func (ll Legislation) FileID() string {
_, c, _ := strings.Cut(ll.File, " ")
return c
}
// FileNumber returns the File prefix as a number (without the session year)
func (ll Legislation) FileNumber() int {
s, _, ok := strings.Cut(ll.FileID(), "-")
n, err := strconv.Atoi(s)
if err != nil || !ok {
return 0
}
return n
}
// FileYear returns the session year of the legislation
func (ll Legislation) FileYear() int {
f := ll.FileID()
// some older entries have "Int 0349-1998-A"
if strings.Count(f, "-") == 2 {
f = strings.Join(strings.Split(f, "-")[:2], "-")
}
_, c, _ := strings.Cut(f, "-")
year, _ := strconv.Atoi(c)
return year
}
func (ll Legislation) Session() Session {
return FindSession(ll.FileYear())
}
func (ll Legislation) IntroLink() template.URL {
f := ll.FileID()
// some older entries have "Int 0349-1998-A"
if strings.Count(f, "-") == 2 {
f = strings.Join(strings.Split(f, "-")[:2], "-")
}
return template.URL("/" + f)
}
func (ll Legislation) IntroLinkText() string {
return "intro.nyc" + string(ll.IntroLink())
}
func (ll Legislation) NumberSponsors() int {
return len(ll.Sponsors)
}
func (ll Legislation) PrimarySponsor() db.PersonReference {
return ll.Sponsors[0]
}
func (ll Legislation) SponsoredBy(id int) bool {
for _, s := range ll.Sponsors {
if s.ID == id {
return true
}
}
return false
}
func (ll Legislation) Hearings() []db.History {
var o []db.History
for _, h := range ll.History {
switch h.Action {
case "Hearing Held by Committee", "Hearing on P-C Item by Comm":
o = append(o, h)
}
}
return o
}
func (ll Legislation) Votes() []History {
var o []History
for _, h := range ll.History {
switch h.Action {
case "Approved by Committee", "Approved by Council":
o = append(o, History{h})
}
}
return o
}
type History struct {
db.History
}
func (h History) VotePassed() bool {
ayes, nayes, _ := h.getVotes()
return ayes > nayes
}
func (h History) VoteSummary() string {
ayes, nays, abstains := h.getVotes()
return fmt.Sprintf("%d:%d:%d", ayes, abstains, nays)
}
func (h History) getVotes() (ayes int, nays int, abstains int) {
for _, v := range h.Votes {
switch v.Vote {
case "Affirmative":
ayes++
case "Negative":
nays++
case "Abstain":
abstains++
}
}
return
}
func (ll Legislation) RecentAction() (string, time.Time) {
// walk in reverse
for i := len(ll.History) - 1; i >= 0; i-- {
h := ll.History[i]
switch h.Action {
case "Introduced by Council",
"Amended by Committee",
"Approved by Committee",
"Approved by Council",
"Hearing Held by Committee",
"Withdrawn",
"Vetoed by Mayor",
"City Charter Rule Adopted":
return h.Action, h.Date
}
}
return "", time.Unix(0, 0)
}
func (ll Legislation) RecentDate() time.Time {
_, dt := ll.RecentAction()
return dt
}
func (ll Legislation) IsRecent() bool {
_, dt := ll.RecentAction()
return time.Now().Add(time.Hour * 24 * -14).Before(dt)
}
func (l LegislationList) Number() int {
return len(l)
}
func (l LegislationList) FilterPrimarySponsor(sponsor int) LegislationList {
var o []Legislation
for _, ll := range l {
if len(ll.Sponsors) > 0 && ll.Sponsors[0].ID == sponsor {
o = append(o, ll)
}
}
return LegislationList(o)
}
func (l LegislationList) FilterSecondarySponsor(sponsor int) LegislationList {
var o []Legislation
for _, ll := range l {
if len(ll.Sponsors) > 1 {
for _, s := range ll.Sponsors[1:] {
if s.ID == sponsor {
o = append(o, ll)
}
}
}
}
return LegislationList(o)
}
func (l LegislationList) Recent(d time.Duration) []RecentLegislation {
cut := time.Now().In(americaNewYork).Add(-1 * d)
var r []RecentLegislation
for _, ll := range l {
rr := NewRecentLegislation(ll)
if rr.Date.Before(cut) {
continue
}
r = append(r, rr)
}
sort.Slice(r, func(i, j int) bool { return r[i].Date.Before(r[j].Date) })
return r
}