diff options
Diffstat (limited to 'modules/grils')
-rw-r--r-- | modules/grils/grils.go | 152 |
1 files changed, 72 insertions, 80 deletions
diff --git a/modules/grils/grils.go b/modules/grils/grils.go index 116b689..135398d 100644 --- a/modules/grils/grils.go +++ b/modules/grils/grils.go | |||
@@ -6,20 +6,20 @@ import ( | |||
6 | "net/http" | 6 | "net/http" |
7 | "regexp" | 7 | "regexp" |
8 | "strconv" | 8 | "strconv" |
9 | "time" | ||
10 | "strings" | 9 | "strings" |
10 | "time" | ||
11 | 11 | ||
12 | "fagott.pw/charakterin" | 12 | "fagott.pw/charakterin" |
13 | "fagott.pw/grilist/cache" | ||
13 | "fagott.pw/grilist/frontend" | 14 | "fagott.pw/grilist/frontend" |
14 | "fagott.pw/grilist/grilist" | 15 | "fagott.pw/grilist/grilist" |
15 | "fagott.pw/grilist/cache" | ||
16 | 16 | ||
17 | "github.com/julienschmidt/httprouter" | 17 | "github.com/julienschmidt/httprouter" |
18 | ) | 18 | ) |
19 | 19 | ||
20 | type CachedGril struct { | 20 | type CachedGril struct { |
21 | Created time.Time | 21 | Created time.Time |
22 | Gril *Gril | 22 | Gril *Gril |
23 | } | 23 | } |
24 | 24 | ||
25 | var ( | 25 | var ( |
@@ -38,7 +38,7 @@ func findIdx() { | |||
38 | 38 | ||
39 | type GrilsModule struct { | 39 | type GrilsModule struct { |
40 | g *grilist.Grilist | 40 | g *grilist.Grilist |
41 | c *cache.Cache | 41 | c *cache.Cache |
42 | } | 42 | } |
43 | 43 | ||
44 | func (m *GrilsModule) Name() string { | 44 | func (m *GrilsModule) Name() string { |
@@ -50,8 +50,8 @@ func (m *GrilsModule) Init(g *grilist.Grilist) { | |||
50 | m.g = g | 50 | m.g = g |
51 | m.g.Router.GET("/gril/:id", m.viewGril) | 51 | m.g.Router.GET("/gril/:id", m.viewGril) |
52 | m.g.Router.GET("/gril/:id/*rest", m.viewGril) | 52 | m.g.Router.GET("/gril/:id/*rest", m.viewGril) |
53 | 53 | ||
54 | m.c = cache.New() | 54 | m.c = cache.New() |
55 | } | 55 | } |
56 | 56 | ||
57 | func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*Gril, error) { | 57 | func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*Gril, error) { |
@@ -105,43 +105,35 @@ func (m *GrilsModule) GetListsOfGril(gril *Gril) error { | |||
105 | 105 | ||
106 | func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory { | 106 | func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory { |
107 | var categories []grilist.DashboardCategory | 107 | var categories []grilist.DashboardCategory |
108 | |||
109 | t1 := time.Now() | 108 | t1 := time.Now() |
110 | rows, err := m.g.DB.Query(`SELECT id FROM grilist.grils ORDER BY updated_at DESC LIMIT 5`) | 109 | |
110 | rows, err := m.g.DB.Query( | ||
111 | `SELECT id, romaji_name, kanji_name FROM grilist.get_recently_updated_grils();`) | ||
111 | if err != nil { | 112 | if err != nil { |
112 | log.Println(err) | 113 | log.Println(err) |
113 | return categories | 114 | return categories |
114 | } | 115 | } |
115 | defer rows.Close() | 116 | defer rows.Close() |
116 | log.Printf("get_newest_grils: %dms", time.Since(t1).Nanoseconds()/1000000) | ||
117 | 117 | ||
118 | cat := grilist.DashboardCategory{ | 118 | cat := grilist.DashboardCategory{ |
119 | Title: "Neue Grils", | 119 | Title: "Neue Grils", |
120 | } | 120 | } |
121 | 121 | ||
122 | for rows.Next() { | 122 | for rows.Next() { |
123 | var id int | 123 | var g Gril |
124 | if err := rows.Scan(&id); err != nil { | 124 | if err := rows.Scan(&g.ID, &g.RomajiName, &g.KanjiName); err != nil { |
125 | log.Println(err) | ||
126 | continue | ||
127 | } | ||
128 | |||
129 | t2 := time.Now() | ||
130 | gril, err := m.FromID(id) | ||
131 | if err != nil { | ||
132 | log.Println(err) | 125 | log.Println(err) |
133 | continue | 126 | continue |
134 | } | 127 | } |
135 | log.Printf("get_gril_from_id_in_newest_grils: %dms", time.Since(t2).Nanoseconds()/1000000) | ||
136 | 128 | ||
137 | cat.Cards = append(cat.Cards, frontend.Card{ | 129 | cat.Cards = append(cat.Cards, frontend.Card{ |
138 | Title: gril.RomajiName, | 130 | Title: g.RomajiName, |
139 | Description: gril.KanjiName, | 131 | Description: g.KanjiName, |
140 | Size: "medium", | 132 | Size: "medium", |
141 | Actions: []frontend.Action{ | 133 | Actions: []frontend.Action{ |
142 | frontend.Action{ | 134 | frontend.Action{ |
143 | Name: "anguckieren", | 135 | Name: "anguckieren", |
144 | Link: "/gril/" + gril.Slug(), | 136 | Link: "/gril/" + g.Slug(), |
145 | }, | 137 | }, |
146 | }, | 138 | }, |
147 | }) | 139 | }) |
@@ -152,77 +144,77 @@ func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.Das | |||
152 | } | 144 | } |
153 | 145 | ||
154 | func (m *GrilsModule) FromID(id int) (*Gril, error) { | 146 | func (m *GrilsModule) FromID(id int) (*Gril, error) { |
155 | if g, ok := m.c.Get(id); ok { | 147 | if g, ok := m.c.Get(id); ok { |
156 | return g.(*Gril), nil | 148 | return g.(*Gril), nil |
157 | } | 149 | } |
158 | 150 | ||
159 | gril := &Gril{ | 151 | gril := &Gril{ |
160 | ID: id, | 152 | ID: id, |
161 | } | 153 | } |
162 | t1 := time.Now() | 154 | t1 := time.Now() |
163 | err := m.g.DB.QueryRow(`SELECT updated_at, age, birthday FROM grilist.grils WHERE id = $1`, id).Scan(&gril.UpdatedAt, &gril.Age, &gril.Birthday) | 155 | err := m.g.DB.QueryRow(`SELECT updated_at, age, birthday FROM grilist.grils WHERE id = $1`, id).Scan(&gril.UpdatedAt, &gril.Age, &gril.Birthday) |
164 | log.Printf("get_gril_from_id_raw: %dms", time.Since(t1).Nanoseconds()/1000000) | 156 | log.Printf("get_gril_from_id_raw: %dms", time.Since(t1).Nanoseconds()/1000000) |
165 | 157 | ||
166 | if err != nil { | 158 | if err != nil { |
167 | return nil, err | 159 | return nil, err |
168 | } | 160 | } |
169 | 161 | ||
170 | // Namen rausholen | 162 | // Namen rausholen |
171 | rows, err := m.g.DB.Query(`SELECT name, name_type FROM grilist.gril_names WHERE gril_id = $1`, id) | 163 | rows, err := m.g.DB.Query(`SELECT name, name_type FROM grilist.gril_names WHERE gril_id = $1`, id) |
172 | if err != nil { | 164 | if err != nil { |
173 | return nil, err | 165 | return nil, err |
174 | } | 166 | } |
175 | log.Printf("get_gril_from_id_names: %dms", time.Since(t1).Nanoseconds()/1000000) | 167 | log.Printf("get_gril_from_id_names: %dms", time.Since(t1).Nanoseconds()/1000000) |
176 | defer rows.Close() | 168 | defer rows.Close() |
177 | 169 | ||
178 | for rows.Next() { | 170 | for rows.Next() { |
179 | var name string | 171 | var name string |
180 | var name_type int | 172 | var name_type int |
181 | if err := rows.Scan(&name, &name_type); err != nil { | 173 | if err := rows.Scan(&name, &name_type); err != nil { |
182 | return nil, err | 174 | return nil, err |
183 | } | 175 | } |
184 | 176 | ||
185 | switch name_type { | 177 | switch name_type { |
186 | case 0: | 178 | case 0: |
187 | gril.KanjiName = name | 179 | gril.KanjiName = name |
188 | break | 180 | break |
189 | case 1: | 181 | case 1: |
190 | gril.RomajiName = name | 182 | gril.RomajiName = name |
191 | default: | 183 | default: |
192 | gril.OtherNames = append(gril.OtherNames, name) | 184 | gril.OtherNames = append(gril.OtherNames, name) |
193 | } | 185 | } |
194 | } | ||
195 | /*rows, err := m.g.DB.Query(`SELECT name FROM grilist.gril_names WHERE gril_id = $1 AND name_type > 1`, id) | ||
196 | if err != nil { | ||
197 | return nil, err | ||
198 | } | ||
199 | defer rows.Close() | ||
200 | |||
201 | for rows.Next() { | ||
202 | var name string | ||
203 | if err := rows.Scan(&name); err != nil { | ||
204 | return nil, err | ||
205 | } | ||
206 | gril.OtherNames = append(gril.OtherNames, name) | ||
207 | } | ||
208 | rows.Close() | ||
209 | |||
210 | // Tags holen | ||
211 | rows, err := m.g.DB.Query(`SELECT name FROM grilist.tags WHERE id IN (SELECT tag_id FROM grilist.grils_tags WHERE gril_id = $1)`, id) | ||
212 | if err != nil { | ||
213 | return nil, err | ||
214 | } | 186 | } |
215 | defer rows.Close() | 187 | /*rows, err := m.g.DB.Query(`SELECT name FROM grilist.gril_names WHERE gril_id = $1 AND name_type > 1`, id) |
216 | 188 | if err != nil { | |
217 | for rows.Next() { | 189 | return nil, err |
218 | var name string | 190 | } |
219 | if err := rows.Scan(&name); err != nil { | 191 | defer rows.Close() |
220 | return nil, err | 192 | |
221 | } | 193 | for rows.Next() { |
222 | gril.Tags = append(gril.Tags, name) | 194 | var name string |
223 | }*/ | 195 | if err := rows.Scan(&name); err != nil { |
224 | 196 | return nil, err | |
225 | m.c.Insert(id, gril) | 197 | } |
198 | gril.OtherNames = append(gril.OtherNames, name) | ||
199 | } | ||
200 | rows.Close() | ||
201 | |||
202 | // Tags holen | ||
203 | rows, err := m.g.DB.Query(`SELECT name FROM grilist.tags WHERE id IN (SELECT tag_id FROM grilist.grils_tags WHERE gril_id = $1)`, id) | ||
204 | if err != nil { | ||
205 | return nil, err | ||
206 | } | ||
207 | defer rows.Close() | ||
208 | |||
209 | for rows.Next() { | ||
210 | var name string | ||
211 | if err := rows.Scan(&name); err != nil { | ||
212 | return nil, err | ||
213 | } | ||
214 | gril.Tags = append(gril.Tags, name) | ||
215 | }*/ | ||
216 | |||
217 | m.c.Insert(id, gril) | ||
226 | log.Printf("get_gril_from_id: %dms", time.Since(t1).Nanoseconds()/1000000) | 218 | log.Printf("get_gril_from_id: %dms", time.Since(t1).Nanoseconds()/1000000) |
227 | return gril, nil | 219 | return gril, nil |
228 | } | 220 | } |