From a793eb865151b27f10fd8ff743e229535cc63865 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Thu, 22 Dec 2016 15:00:05 -0500 Subject: Skip intensive stat DB queries when more than a set number of users on the system (#4876) --- api/admin.go | 94 +++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 74 insertions(+), 20 deletions(-) (limited to 'api/admin.go') diff --git a/api/admin.go b/api/admin.go index 3fb6c31f8..6ec76caae 100644 --- a/api/admin.go +++ b/api/admin.go @@ -367,6 +367,19 @@ func getAnalytics(c *Context, w http.ResponseWriter, r *http.Request) { teamId := params["id"] name := params["name"] + skipIntensiveQueries := false + var systemUserCount int64 + if r := <-Srv.Store.User().AnalyticsUniqueUserCount(""); r.Err != nil { + c.Err = r.Err + return + } else { + systemUserCount = r.Data.(int64) + if systemUserCount > int64(*utils.Cfg.AnalyticsSettings.MaxUsersForStatistics) { + l4g.Debug("More than %v users on the system, intensive queries skipped", *utils.Cfg.AnalyticsSettings.MaxUsersForStatistics) + skipIntensiveQueries = true + } + } + if name == "standard" { var rows model.AnalyticsRows = make([]*model.AnalyticsRow, 8) rows[0] = &model.AnalyticsRow{"channel_open_count", 0} @@ -380,10 +393,18 @@ func getAnalytics(c *Context, w http.ResponseWriter, r *http.Request) { openChan := Srv.Store.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_OPEN) privateChan := Srv.Store.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_PRIVATE) - postChan := Srv.Store.Post().AnalyticsPostCount(teamId, false, false) - userChan := Srv.Store.User().AnalyticsUniqueUserCount(teamId) teamChan := Srv.Store.Team().AnalyticsTeamCount() + var userChan store.StoreChannel + if teamId != "" { + userChan = Srv.Store.User().AnalyticsUniqueUserCount(teamId) + } + + var postChan store.StoreChannel + if !skipIntensiveQueries { + postChan = Srv.Store.Post().AnalyticsPostCount(teamId, false, false) + } + if r := <-openChan; r.Err != nil { c.Err = r.Err return @@ -398,18 +419,26 @@ func getAnalytics(c *Context, w http.ResponseWriter, r *http.Request) { rows[1].Value = float64(r.Data.(int64)) } - if r := <-postChan; r.Err != nil { - c.Err = r.Err - return + if postChan == nil { + rows[2].Value = -1 } else { - rows[2].Value = float64(r.Data.(int64)) + if r := <-postChan; r.Err != nil { + c.Err = r.Err + return + } else { + rows[2].Value = float64(r.Data.(int64)) + } } - if r := <-userChan; r.Err != nil { - c.Err = r.Err - return + if userChan == nil { + rows[3].Value = float64(systemUserCount) } else { - rows[3].Value = float64(r.Data.(int64)) + if r := <-userChan; r.Err != nil { + c.Err = r.Err + return + } else { + rows[3].Value = float64(r.Data.(int64)) + } } if r := <-teamChan; r.Err != nil { @@ -449,6 +478,12 @@ func getAnalytics(c *Context, w http.ResponseWriter, r *http.Request) { w.Write([]byte(rows.ToJson())) } else if name == "post_counts_day" { + if skipIntensiveQueries { + rows := model.AnalyticsRows{&model.AnalyticsRow{"", -1}} + w.Write([]byte(rows.ToJson())) + return + } + if r := <-Srv.Store.Post().AnalyticsPostCountsByDay(teamId); r.Err != nil { c.Err = r.Err return @@ -456,6 +491,12 @@ func getAnalytics(c *Context, w http.ResponseWriter, r *http.Request) { w.Write([]byte(r.Data.(model.AnalyticsRows).ToJson())) } } else if name == "user_counts_with_posts_day" { + if skipIntensiveQueries { + rows := model.AnalyticsRows{&model.AnalyticsRow{"", -1}} + w.Write([]byte(rows.ToJson())) + return + } + if r := <-Srv.Store.Post().AnalyticsUserCountsWithPostsByDay(teamId); r.Err != nil { c.Err = r.Err return @@ -471,25 +512,38 @@ func getAnalytics(c *Context, w http.ResponseWriter, r *http.Request) { rows[4] = &model.AnalyticsRow{"command_count", 0} rows[5] = &model.AnalyticsRow{"session_count", 0} - fileChan := Srv.Store.Post().AnalyticsPostCount(teamId, true, false) - hashtagChan := Srv.Store.Post().AnalyticsPostCount(teamId, false, true) iHookChan := Srv.Store.Webhook().AnalyticsIncomingCount(teamId) oHookChan := Srv.Store.Webhook().AnalyticsOutgoingCount(teamId) commandChan := Srv.Store.Command().AnalyticsCommandCount(teamId) sessionChan := Srv.Store.Session().AnalyticsSessionCount() - if r := <-fileChan; r.Err != nil { - c.Err = r.Err - return + var fileChan store.StoreChannel + var hashtagChan store.StoreChannel + if !skipIntensiveQueries { + fileChan = Srv.Store.Post().AnalyticsPostCount(teamId, true, false) + hashtagChan = Srv.Store.Post().AnalyticsPostCount(teamId, false, true) + } + + if fileChan == nil { + rows[0].Value = -1 } else { - rows[0].Value = float64(r.Data.(int64)) + if r := <-fileChan; r.Err != nil { + c.Err = r.Err + return + } else { + rows[0].Value = float64(r.Data.(int64)) + } } - if r := <-hashtagChan; r.Err != nil { - c.Err = r.Err - return + if hashtagChan == nil { + rows[1].Value = -1 } else { - rows[1].Value = float64(r.Data.(int64)) + if r := <-hashtagChan; r.Err != nil { + c.Err = r.Err + return + } else { + rows[1].Value = float64(r.Data.(int64)) + } } if r := <-iHookChan; r.Err != nil { -- cgit v1.2.3-1-g7c22