From c7f28c0032163075bd21f899c37ae992e3eece80 Mon Sep 17 00:00:00 2001 From: jan Date: Sun, 27 Mar 2016 19:46:17 +0200 Subject: temporaer laufzeiten der module (dashboard data) loggen, views angepasst, dashboard in goroutines aufgeteilt, weniger daten fuers listendashboard holen (reduktion von 6s -> 300ms insgesamt) diff --git a/dashboard.go b/dashboard.go index 0ca38ef..ad3c565 100644 --- a/dashboard.go +++ b/dashboard.go @@ -3,7 +3,10 @@ package main import ( "fagott.pw/grilist/grilist" "github.com/julienschmidt/httprouter" + "log" "net/http" + "sync" + "time" ) func viewDashboard(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { @@ -11,13 +14,24 @@ func viewDashboard(w http.ResponseWriter, r *http.Request, _ httprouter.Params) var categories []grilist.DashboardCategory + t1 := time.Now() + var wg sync.WaitGroup for _, module := range app.Modules { - for _, category := range module.ProvideDashboardData(user) { - if len(category.Cards) > 0 { - categories = append(categories, category) + wg.Add(1) + go func(module grilist.Module) { + t2 := time.Now() + for _, category := range module.ProvideDashboardData(user) { + if len(category.Cards) > 0 { + categories = append(categories, category) + } } - } + log.Printf("%s dashboard_data: %dms", module.Name(), time.Since(t2).Nanoseconds()/1000000) + wg.Done() + }(module) } + wg.Wait() + + log.Printf("dashboard_data: %dms", time.Since(t1).Nanoseconds()/1000000) data := make(map[string]interface{}) 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 ( "regexp" "strconv" "strings" + "time" "fagott.pw/charakterin" "fagott.pw/grilist/frontend" @@ -96,12 +97,14 @@ func (m *GrilsModule) GetListsOfGril(gril *Gril) error { func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory { var categories []grilist.DashboardCategory + t1 := time.Now() rows, err := m.g.DB.Query(`SELECT id FROM grilist.grils ORDER BY updated_at DESC LIMIT 5`) if err != nil { log.Println(err) return categories } defer rows.Close() + log.Printf("get_newest_grils: %dms", time.Since(t1).Nanoseconds()/1000000) cat := grilist.DashboardCategory{ Title: "Neue Grils", @@ -114,11 +117,13 @@ func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.Das continue } + t2 := time.Now() gril, err := m.FromID(id) if err != nil { log.Println(err) continue } + log.Printf("get_gril_from_id_in_newest_grils: %dms", time.Since(t2).Nanoseconds()/1000000) cat.Cards = append(cat.Cards, frontend.Card{ Title: gril.RomajiName, @@ -133,6 +138,7 @@ func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.Das }) } categories = append(categories, cat) + log.Printf("get_newest_grils_overall: %dms", time.Since(t1).Nanoseconds()/1000000) return categories } @@ -195,36 +201,36 @@ func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprou data := m.g.Renderer.DefaultData() data["user"] = user data["gril"] = gril - - // ähnliche grils holen - rows, err := m.g.DB.Query(`SELECT gril_id FROM ( + + // ähnliche grils holen + rows, err := m.g.DB.Query(`SELECT gril_id FROM ( SELECT DISTINCT gril_id FROM gril_appearance WHERE appearance_id IN (SELECT appearance_id FROM gril_appearance WHERE gril_id = $1) ) as t ORDER BY RANDOM() LIMIT 4;`, id) - if err != nil { - log.Println("could not get similar grils:", err) - http.Error(w, "500", http.StatusInternalServerError) - return - } - - defer rows.Close() - var similar []*Gril - for rows.Next() { - var id int - if err := rows.Scan(&id); err != nil { - log.Println("error scanning for similar gril:", err) - continue - } - - g, err := m.FromID(id) - if err != nil { - log.Println("invalid similar gril:", err) - continue - } - similar = append(similar, g) - } - data["SimilarGrils"] = similar + if err != nil { + log.Println("could not get similar grils:", err) + http.Error(w, "500", http.StatusInternalServerError) + return + } + + defer rows.Close() + var similar []*Gril + for rows.Next() { + var id int + if err := rows.Scan(&id); err != nil { + log.Println("error scanning for similar gril:", err) + continue + } + + g, err := m.FromID(id) + if err != nil { + log.Println("invalid similar gril:", err) + continue + } + similar = append(similar, g) + } + data["SimilarGrils"] = similar m.g.Renderer.RenderPage("gril", w, data) } 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 { var cards []frontend.Card for _, list := range lists { - s := "s" - if len(list.Grils) == 1 { - s = "" - } cards = append(cards, frontend.Card{ Title: list.Name, Description: list.Description, @@ -194,7 +190,7 @@ func ListsToCards(lists []*List) []frontend.Card { Link: fmt.Sprintf("/list/%d", list.ID), }, frontend.Action{ - Name: fmt.Sprintf(`%d gril%s`, len(list.Grils), s), + Name: fmt.Sprintf(`von %s`, list.Owner.ID, list.Owner.GetName(), list.Owner.GetName()), Link: "#", Disabled: true, }, @@ -214,11 +210,11 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar log.Println(err) return categories } - for _, list := range lists { - if err := m.getListGrils(list); err != nil { - log.Println(err) - } - } + //for _, list := range lists { + //if err := m.getListGrils(list); err != nil { + // log.Println(err) + //} + //} categories = append(categories, grilist.DashboardCategory{ Title: "Neueste Listen", @@ -230,7 +226,7 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar } // Listen des Benutzers - lists = m.GetUserLists(user, true) + lists = m.GetUserLists(user, false) categories = append(categories, grilist.DashboardCategory{ 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 @@ {{ if $action.Disabled }} {{ $action.Name | html }} {{ else }} - {{ $action.Name | html }} + {{ $action.Name | html }} {{ end }} {{ end }} -- cgit v0.10.1