🧮 Return total count in get metrics query (#2230)
* Return total count in get metrics query * Fix RPC issue * Fix vendor checks * Update version
This commit is contained in:
parent
d50fee6bb2
commit
beacc3233c
|
@ -28,6 +28,11 @@ type AppMetricValidationError struct {
|
|||
Errors []gojsonschema.ResultError
|
||||
}
|
||||
|
||||
type Page struct {
|
||||
AppMetrics []AppMetric
|
||||
TotalCount int
|
||||
}
|
||||
|
||||
const (
|
||||
// status-react navigation events
|
||||
NavigateTo AppMetricEventType = "navigate-to"
|
||||
|
@ -140,13 +145,18 @@ func (db *Database) SaveAppMetrics(appMetrics []AppMetric, sessionID string) (er
|
|||
return
|
||||
}
|
||||
|
||||
func (db *Database) GetAppMetrics(limit int, offset int) (appMetrics []AppMetric, err error) {
|
||||
rows, err := db.db.Query("SELECT event, value, app_version, operating_system, session_id, created_at FROM app_metrics LIMIT ? OFFSET ?", limit, offset)
|
||||
func (db *Database) GetAppMetrics(limit int, offset int) (page Page, err error) {
|
||||
countErr := db.db.QueryRow("SELECT count(*) FROM app_metrics").Scan(&page.TotalCount)
|
||||
if countErr != nil {
|
||||
return page, countErr
|
||||
}
|
||||
|
||||
rows, err := db.db.Query("SELECT event, value, app_version, operating_system, session_id, created_at FROM app_metrics LIMIT ? OFFSET ?", limit, offset)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return page, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
metric := AppMetric{}
|
||||
err := rows.Scan(
|
||||
|
@ -155,9 +165,9 @@ func (db *Database) GetAppMetrics(limit int, offset int) (appMetrics []AppMetric
|
|||
&metric.SessionID, &metric.CreatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return page, err
|
||||
}
|
||||
appMetrics = append(appMetrics, metric)
|
||||
page.AppMetrics = append(page.AppMetrics, metric)
|
||||
}
|
||||
return appMetrics, nil
|
||||
return page, nil
|
||||
}
|
||||
|
|
|
@ -37,11 +37,14 @@ func TestSaveAppMetrics(t *testing.T) {
|
|||
err := db.SaveAppMetrics(appMetrics, sessionID)
|
||||
require.NoError(t, err)
|
||||
|
||||
res, err := db.GetAppMetrics(10, 0)
|
||||
appMetricsPage, err := db.GetAppMetrics(10, 0)
|
||||
res := appMetricsPage.AppMetrics
|
||||
count := appMetricsPage.TotalCount
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, appMetrics[0].Event, res[0].Event)
|
||||
require.Equal(t, appMetrics[0].Value, res[0].Value)
|
||||
require.Equal(t, appMetrics[0].OS, res[0].OS)
|
||||
require.Equal(t, appMetrics[0].AppVersion, res[0].AppVersion)
|
||||
require.NotNil(t, res[0].CreatedAt)
|
||||
require.Equal(t, count, 1)
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ func (api *API) SaveAppMetrics(ctx context.Context, appMetrics []appmetrics.AppM
|
|||
return api.db.SaveAppMetrics(appMetrics, api.sessionID)
|
||||
}
|
||||
|
||||
func (api *API) GetAppMetrics(ctx context.Context, limit int, offset int) ([]appmetrics.AppMetric, error) {
|
||||
func (api *API) GetAppMetrics(ctx context.Context, limit int, offset int) (appmetrics.Page, error) {
|
||||
log.Debug("[AppMetricsAPI::GetAppMetrics]")
|
||||
return api.db.GetAppMetrics(limit, offset)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue