diff --git a/app/analytics.go b/app/analytics.go index 813a573dfe..6d4014633d 100644 --- a/app/analytics.go +++ b/app/analytics.go @@ -185,7 +185,13 @@ func (a *App) GetAnalytics(name string, teamId string) (model.AnalyticsRows, *mo rows[4] = &model.AnalyticsRow{Name: "command_count", Value: 0} rows[5] = &model.AnalyticsRow{Name: "session_count", Value: 0} - iHookChan := a.Srv.Store.Webhook().AnalyticsIncomingCount(teamId) + iHookChan := make(chan store.StoreResult, 1) + go func() { + c, err := a.Srv.Store.Webhook().AnalyticsIncomingCount(teamId) + iHookChan <- store.StoreResult{Data: c, Err: err} + close(iHookChan) + }() + oHookChan := a.Srv.Store.Webhook().AnalyticsOutgoingCount(teamId) commandChan := a.Srv.Store.Command().AnalyticsCommandCount(teamId) sessionChan := a.Srv.Store.Session().AnalyticsSessionCount() diff --git a/app/diagnostics.go b/app/diagnostics.go index a0c1485708..d53013d284 100644 --- a/app/diagnostics.go +++ b/app/diagnostics.go @@ -197,8 +197,8 @@ func (a *App) trackActivity() { slashCommandsCount = scc.Data.(int64) } - if iwc := <-a.Srv.Store.Webhook().AnalyticsIncomingCount(""); iwc.Err == nil { - incomingWebhooksCount = iwc.Data.(int64) + if c, err := a.Srv.Store.Webhook().AnalyticsIncomingCount(""); err == nil { + incomingWebhooksCount = c } if owc := <-a.Srv.Store.Webhook().AnalyticsOutgoingCount(""); owc.Err == nil { diff --git a/store/sqlstore/webhook_store.go b/store/sqlstore/webhook_store.go index 95c71376a3..b5a6488dd9 100644 --- a/store/sqlstore/webhook_store.go +++ b/store/sqlstore/webhook_store.go @@ -326,26 +326,25 @@ func (s SqlWebhookStore) UpdateOutgoing(hook *model.OutgoingWebhook) store.Store }) } -func (s SqlWebhookStore) AnalyticsIncomingCount(teamId string) store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - query := - `SELECT - COUNT(*) - FROM - IncomingWebhooks - WHERE - DeleteAt = 0` +func (s SqlWebhookStore) AnalyticsIncomingCount(teamId string) (int64, *model.AppError) { + query := + `SELECT + COUNT(*) + FROM + IncomingWebhooks + WHERE + DeleteAt = 0` - if len(teamId) > 0 { - query += " AND TeamId = :TeamId" - } + if len(teamId) > 0 { + query += " AND TeamId = :TeamId" + } - if v, err := s.GetReplica().SelectInt(query, map[string]interface{}{"TeamId": teamId}); err != nil { - result.Err = model.NewAppError("SqlWebhookStore.AnalyticsIncomingCount", "store.sql_webhooks.analytics_incoming_count.app_error", nil, "team_id="+teamId+", err="+err.Error(), http.StatusInternalServerError) - } else { - result.Data = v - } - }) + v, err := s.GetReplica().SelectInt(query, map[string]interface{}{"TeamId": teamId}) + if err != nil { + return 0, model.NewAppError("SqlWebhookStore.AnalyticsIncomingCount", "store.sql_webhooks.analytics_incoming_count.app_error", nil, "team_id="+teamId+", err="+err.Error(), http.StatusInternalServerError) + } + + return v, nil } func (s SqlWebhookStore) AnalyticsOutgoingCount(teamId string) store.StoreChannel { diff --git a/store/store.go b/store/store.go index 34bf9cf9f7..3fc11b9ee6 100644 --- a/store/store.go +++ b/store/store.go @@ -397,7 +397,7 @@ type WebhookStore interface { PermanentDeleteOutgoingByUser(userId string) StoreChannel UpdateOutgoing(hook *model.OutgoingWebhook) StoreChannel - AnalyticsIncomingCount(teamId string) StoreChannel + AnalyticsIncomingCount(teamId string) (int64, *model.AppError) AnalyticsOutgoingCount(teamId string) StoreChannel InvalidateWebhookCache(webhook string) ClearCaches() diff --git a/store/storetest/mocks/WebhookStore.go b/store/storetest/mocks/WebhookStore.go index 3c512d99e6..a0c512e25c 100644 --- a/store/storetest/mocks/WebhookStore.go +++ b/store/storetest/mocks/WebhookStore.go @@ -14,19 +14,26 @@ type WebhookStore struct { } // AnalyticsIncomingCount provides a mock function with given fields: teamId -func (_m *WebhookStore) AnalyticsIncomingCount(teamId string) store.StoreChannel { +func (_m *WebhookStore) AnalyticsIncomingCount(teamId string) (int64, *model.AppError) { ret := _m.Called(teamId) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(string) store.StoreChannel); ok { + var r0 int64 + if rf, ok := ret.Get(0).(func(string) int64); ok { r0 = rf(teamId) } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(store.StoreChannel) + r0 = ret.Get(0).(int64) + } + + var r1 *model.AppError + if rf, ok := ret.Get(1).(func(string) *model.AppError); ok { + r1 = rf(teamId) + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).(*model.AppError) } } - return r0 + return r0, r1 } // AnalyticsOutgoingCount provides a mock function with given fields: teamId diff --git a/store/storetest/webhook_store.go b/store/storetest/webhook_store.go index 85e4a6e418..d380dd12de 100644 --- a/store/storetest/webhook_store.go +++ b/store/storetest/webhook_store.go @@ -524,12 +524,13 @@ func testWebhookStoreCountIncoming(t *testing.T, ss store.Store) { _, _ = ss.Webhook().SaveIncoming(o1) - if r := <-ss.Webhook().AnalyticsIncomingCount(""); r.Err != nil { - t.Fatal(r.Err) - } else { - if r.Data.(int64) == 0 { - t.Fatal("should have at least 1 incoming hook") - } + c, err := ss.Webhook().AnalyticsIncomingCount("") + if err != nil { + t.Fatal(err) + } + + if c == 0 { + t.Fatal("should have at least 1 incoming hook") } }