diff options
author | jan <jan@ruken.pw> | 2016-03-27 17:46:17 (UTC) |
---|---|---|
committer | jan <jan@ruken.pw> | 2016-03-27 17:46:17 (UTC) |
commit | c7f28c0032163075bd21f899c37ae992e3eece80 (patch) | |
tree | 18c580c2f260e93ac97ddb4b4ec42bcae7e58254 | |
parent | 876530b3da8377e7072824bca5b3844c682eb37a (diff) |
temporaer laufzeiten der module (dashboard data) loggen, views angepasst, dashboard in goroutines aufgeteilt, weniger daten fuers listendashboard holen (reduktion von 6s -> 300ms insgesamt)
-rw-r--r-- | dashboard.go | 22 | ||||
-rw-r--r-- | modules/grils/grils.go | 58 | ||||
-rw-r--r-- | modules/lists/lists.go | 18 | ||||
-rw-r--r-- | views/includes/card.html | 2 |
4 files changed, 58 insertions, 42 deletions
diff --git a/dashboard.go b/dashboard.go index 0ca38ef..ad3c565 100644 --- a/dashboard.go +++ b/dashboard.go | |||
@@ -3,7 +3,10 @@ package main | |||
3 | import ( | 3 | import ( |
4 | "fagott.pw/grilist/grilist" | 4 | "fagott.pw/grilist/grilist" |
5 | "github.com/julienschmidt/httprouter" | 5 | "github.com/julienschmidt/httprouter" |
6 | "log" | ||
6 | "net/http" | 7 | "net/http" |
8 | "sync" | ||
9 | "time" | ||
7 | ) | 10 | ) |
8 | 11 | ||
9 | func viewDashboard(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { | 12 | func viewDashboard(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { |
@@ -11,13 +14,24 @@ func viewDashboard(w http.ResponseWriter, r *http.Request, _ httprouter.Params) | |||
11 | 14 | ||
12 | var categories []grilist.DashboardCategory | 15 | var categories []grilist.DashboardCategory |
13 | 16 | ||
17 | t1 := time.Now() | ||
18 | var wg sync.WaitGroup | ||
14 | for _, module := range app.Modules { | 19 | for _, module := range app.Modules { |
15 | for _, category := range module.ProvideDashboardData(user) { | 20 | wg.Add(1) |
16 | if len(category.Cards) > 0 { | 21 | go func(module grilist.Module) { |
17 | categories = append(categories, category) | 22 | t2 := time.Now() |
23 | for _, category := range module.ProvideDashboardData(user) { | ||
24 | if len(category.Cards) > 0 { | ||
25 | categories = append(categories, category) | ||
26 | } | ||
18 | } | 27 | } |
19 | } | 28 | log.Printf("%s dashboard_data: %dms", module.Name(), time.Since(t2).Nanoseconds()/1000000) |
29 | wg.Done() | ||
30 | }(module) | ||
20 | } | 31 | } |
32 | wg.Wait() | ||
33 | |||
34 | log.Printf("dashboard_data: %dms", time.Since(t1).Nanoseconds()/1000000) | ||
21 | 35 | ||
22 | data := make(map[string]interface{}) | 36 | data := make(map[string]interface{}) |
23 | data["categories"] = categories | 37 | data["categories"] = categories |
diff --git a/modules/grils/grils.go b/modules/grils/grils.go index e6aaa11..fe947bc 100644 --- a/modules/grils/grils.go +++ b/modules/grils/grils.go | |||
@@ -7,6 +7,7 @@ import ( | |||
7 | "regexp" | 7 | "regexp" |
8 | "strconv" | 8 | "strconv" |
9 | "strings" | 9 | "strings" |
10 | "time" | ||
10 | 11 | ||
11 | "fagott.pw/charakterin" | 12 | "fagott.pw/charakterin" |
12 | "fagott.pw/grilist/frontend" | 13 | "fagott.pw/grilist/frontend" |
@@ -96,12 +97,14 @@ func (m *GrilsModule) GetListsOfGril(gril *Gril) error { | |||
96 | func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory { | 97 | func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory { |
97 | var categories []grilist.DashboardCategory | 98 | var categories []grilist.DashboardCategory |
98 | 99 | ||
100 | t1 := time.Now() | ||
99 | rows, err := m.g.DB.Query(`SELECT id FROM grilist.grils ORDER BY updated_at DESC LIMIT 5`) | 101 | rows, err := m.g.DB.Query(`SELECT id FROM grilist.grils ORDER BY updated_at DESC LIMIT 5`) |
100 | if err != nil { | 102 | if err != nil { |
101 | log.Println(err) | 103 | log.Println(err) |
102 | return categories | 104 | return categories |
103 | } | 105 | } |
104 | defer rows.Close() | 106 | defer rows.Close() |
107 | log.Printf("get_newest_grils: %dms", time.Since(t1).Nanoseconds()/1000000) | ||
105 | 108 | ||
106 | cat := grilist.DashboardCategory{ | 109 | cat := grilist.DashboardCategory{ |
107 | Title: "Neue Grils", | 110 | Title: "Neue Grils", |
@@ -114,11 +117,13 @@ func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.Das | |||
114 | continue | 117 | continue |
115 | } | 118 | } |
116 | 119 | ||
120 | t2 := time.Now() | ||
117 | gril, err := m.FromID(id) | 121 | gril, err := m.FromID(id) |
118 | if err != nil { | 122 | if err != nil { |
119 | log.Println(err) | 123 | log.Println(err) |
120 | continue | 124 | continue |
121 | } | 125 | } |
126 | log.Printf("get_gril_from_id_in_newest_grils: %dms", time.Since(t2).Nanoseconds()/1000000) | ||
122 | 127 | ||
123 | cat.Cards = append(cat.Cards, frontend.Card{ | 128 | cat.Cards = append(cat.Cards, frontend.Card{ |
124 | Title: gril.RomajiName, | 129 | Title: gril.RomajiName, |
@@ -133,6 +138,7 @@ func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.Das | |||
133 | }) | 138 | }) |
134 | } | 139 | } |
135 | categories = append(categories, cat) | 140 | categories = append(categories, cat) |
141 | log.Printf("get_newest_grils_overall: %dms", time.Since(t1).Nanoseconds()/1000000) | ||
136 | return categories | 142 | return categories |
137 | } | 143 | } |
138 | 144 | ||
@@ -195,36 +201,36 @@ func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprou | |||
195 | data := m.g.Renderer.DefaultData() | 201 | data := m.g.Renderer.DefaultData() |
196 | data["user"] = user | 202 | data["user"] = user |
197 | data["gril"] = gril | 203 | data["gril"] = gril |
198 | 204 | ||
199 | // ähnliche grils holen | 205 | // ähnliche grils holen |
200 | rows, err := m.g.DB.Query(`SELECT gril_id FROM ( | 206 | rows, err := m.g.DB.Query(`SELECT gril_id FROM ( |
201 | SELECT DISTINCT gril_id FROM gril_appearance WHERE appearance_id IN (SELECT appearance_id FROM gril_appearance WHERE gril_id = $1) | 207 | SELECT DISTINCT gril_id FROM gril_appearance WHERE appearance_id IN (SELECT appearance_id FROM gril_appearance WHERE gril_id = $1) |
202 | ) as t | 208 | ) as t |
203 | ORDER BY RANDOM() | 209 | ORDER BY RANDOM() |
204 | LIMIT 4;`, id) | 210 | LIMIT 4;`, id) |
205 | if err != nil { | 211 | if err != nil { |
206 | log.Println("could not get similar grils:", err) | 212 | log.Println("could not get similar grils:", err) |
207 | http.Error(w, "500", http.StatusInternalServerError) | 213 | http.Error(w, "500", http.StatusInternalServerError) |
208 | return | 214 | return |
209 | } | 215 | } |
210 | 216 | ||
211 | defer rows.Close() | 217 | defer rows.Close() |
212 | var similar []*Gril | 218 | var similar []*Gril |
213 | for rows.Next() { | 219 | for rows.Next() { |
214 | var id int | 220 | var id int |
215 | if err := rows.Scan(&id); err != nil { | 221 | if err := rows.Scan(&id); err != nil { |
216 | log.Println("error scanning for similar gril:", err) | 222 | log.Println("error scanning for similar gril:", err) |
217 | continue | 223 | continue |
218 | } | 224 | } |
219 | 225 | ||
220 | g, err := m.FromID(id) | 226 | g, err := m.FromID(id) |
221 | if err != nil { | 227 | if err != nil { |
222 | log.Println("invalid similar gril:", err) | 228 | log.Println("invalid similar gril:", err) |
223 | continue | 229 | continue |
224 | } | 230 | } |
225 | similar = append(similar, g) | 231 | similar = append(similar, g) |
226 | } | 232 | } |
227 | data["SimilarGrils"] = similar | 233 | data["SimilarGrils"] = similar |
228 | 234 | ||
229 | m.g.Renderer.RenderPage("gril", w, data) | 235 | m.g.Renderer.RenderPage("gril", w, data) |
230 | } | 236 | } |
diff --git a/modules/lists/lists.go b/modules/lists/lists.go index 3febdba..836f6af 100644 --- a/modules/lists/lists.go +++ b/modules/lists/lists.go | |||
@@ -180,10 +180,6 @@ func ListsToCards(lists []*List) []frontend.Card { | |||
180 | var cards []frontend.Card | 180 | var cards []frontend.Card |
181 | 181 | ||
182 | for _, list := range lists { | 182 | for _, list := range lists { |
183 | s := "s" | ||
184 | if len(list.Grils) == 1 { | ||
185 | s = "" | ||
186 | } | ||
187 | cards = append(cards, frontend.Card{ | 183 | cards = append(cards, frontend.Card{ |
188 | Title: list.Name, | 184 | Title: list.Name, |
189 | Description: list.Description, | 185 | Description: list.Description, |
@@ -194,7 +190,7 @@ func ListsToCards(lists []*List) []frontend.Card { | |||
194 | Link: fmt.Sprintf("/list/%d", list.ID), | 190 | Link: fmt.Sprintf("/list/%d", list.ID), |
195 | }, | 191 | }, |
196 | frontend.Action{ | 192 | frontend.Action{ |
197 | Name: fmt.Sprintf(`<span class="chip">%d gril%s</span>`, len(list.Grils), s), | 193 | Name: fmt.Sprintf(`<span class="chip">von <a class="purple-text text-lighten-2" style="margin-right: 0px" href="/user/%d/%s">%s</a></span>`, list.Owner.ID, list.Owner.GetName(), list.Owner.GetName()), |
198 | Link: "#", | 194 | Link: "#", |
199 | Disabled: true, | 195 | Disabled: true, |
200 | }, | 196 | }, |
@@ -214,11 +210,11 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar | |||
214 | log.Println(err) | 210 | log.Println(err) |
215 | return categories | 211 | return categories |
216 | } | 212 | } |
217 | for _, list := range lists { | 213 | //for _, list := range lists { |
218 | if err := m.getListGrils(list); err != nil { | 214 | //if err := m.getListGrils(list); err != nil { |
219 | log.Println(err) | 215 | // log.Println(err) |
220 | } | 216 | //} |
221 | } | 217 | //} |
222 | 218 | ||
223 | categories = append(categories, grilist.DashboardCategory{ | 219 | categories = append(categories, grilist.DashboardCategory{ |
224 | Title: "Neueste Listen", | 220 | Title: "Neueste Listen", |
@@ -230,7 +226,7 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar | |||
230 | } | 226 | } |
231 | 227 | ||
232 | // Listen des Benutzers | 228 | // Listen des Benutzers |
233 | lists = m.GetUserLists(user, true) | 229 | lists = m.GetUserLists(user, false) |
234 | 230 | ||
235 | categories = append(categories, grilist.DashboardCategory{ | 231 | categories = append(categories, grilist.DashboardCategory{ |
236 | Title: "Meine Listen", | 232 | Title: "Meine Listen", |
diff --git a/views/includes/card.html b/views/includes/card.html index be28272..289c9d3 100644 --- a/views/includes/card.html +++ b/views/includes/card.html | |||
@@ -11,7 +11,7 @@ | |||
11 | {{ if $action.Disabled }} | 11 | {{ if $action.Disabled }} |
12 | <span>{{ $action.Name | html }}</span> | 12 | <span>{{ $action.Name | html }}</span> |
13 | {{ else }} | 13 | {{ else }} |
14 | <a href="{{ $action.Link }}">{{ $action.Name | html }}</a> | 14 | <a class="purple-text text-lighten-2" href="{{ $action.Link }}">{{ $action.Name | html }}</a> |
15 | {{ end }} | 15 | {{ end }} |
16 | {{ end }} | 16 | {{ end }} |
17 | </div> | 17 | </div> |