From 8cf5aa13c21715e357423932aaff8f2803840a30 Mon Sep 17 00:00:00 2001 From: Rok Novosel Date: Fri, 22 Jul 2022 09:33:12 +0200 Subject: [PATCH] Revert "[Admin Analytics] add fetcher tests" (#39287) --- internal/adminanalytics/fetcher.go | 30 +-- internal/adminanalytics/notebooks.go | 8 +- internal/adminanalytics/notebooks_test.go | 297 ---------------------- 3 files changed, 13 insertions(+), 322 deletions(-) delete mode 100644 internal/adminanalytics/notebooks_test.go diff --git a/internal/adminanalytics/fetcher.go b/internal/adminanalytics/fetcher.go index a598f5db462dc..ddaaeaaa3ed7f 100644 --- a/internal/adminanalytics/fetcher.go +++ b/internal/adminanalytics/fetcher.go @@ -17,7 +17,6 @@ type AnalyticsFetcher struct { nodesQuery *sqlf.Query summaryQuery *sqlf.Query cache bool - noSetCache *bool } type AnalyticsNodeData struct { @@ -66,7 +65,11 @@ func (f *AnalyticsFetcher) Nodes(ctx context.Context) ([]*AnalyticsNode, error) nodes = append(nodes, &AnalyticsNode{data}) } - now := bod(time.Now()) + if _, err := setArrayToCache(cacheKey, nodes); err != nil { + return nil, err + } + + now := time.Now() to := now daysOffset := 1 from, err := getFromDate(f.dateRange, now) @@ -75,7 +78,7 @@ func (f *AnalyticsFetcher) Nodes(ctx context.Context) ([]*AnalyticsNode, error) } if f.dateRange == "LAST_THREE_MONTHS" { - to = sow(now) + to = now.AddDate(0, 0, -int(now.Weekday())+1) // monday of current week daysOffset = 7 } @@ -85,7 +88,7 @@ func (f *AnalyticsFetcher) Nodes(ctx context.Context) ([]*AnalyticsNode, error) var node *AnalyticsNode for _, n := range nodes { - if date.Equal(bod(n.Data.Date)) { + if bod(date).Equal(bod(n.Data.Date)) { node = n break } @@ -105,26 +108,15 @@ func (f *AnalyticsFetcher) Nodes(ctx context.Context) ([]*AnalyticsNode, error) allNodes = append(allNodes, node) } - if f.noSetCache == nil || *f.noSetCache != true { - if _, err := setArrayToCache(cacheKey, allNodes); err != nil { - return nil, err - } - } - return allNodes, nil + } -// beginning of day func bod(t time.Time) time.Time { year, month, day := t.Date() return time.Date(year, month, day, 0, 0, 0, 0, t.Location()) } -// start of week (monday) -func sow(t time.Time) time.Time { - return t.AddDate(0, 0, -int(t.Weekday())+1) -} - type AnalyticsSummaryData struct { TotalCount float64 TotalUniqueUsers float64 @@ -157,10 +149,8 @@ func (f *AnalyticsFetcher) Summary(ctx context.Context) (*AnalyticsSummary, erro summary := &AnalyticsSummary{data} - if f.noSetCache == nil || *f.noSetCache != true { - if _, err := setItemToCache(cacheKey, summary); err != nil { - return nil, err - } + if _, err := setItemToCache(cacheKey, summary); err != nil { + return nil, err } return summary, nil diff --git a/internal/adminanalytics/notebooks.go b/internal/adminanalytics/notebooks.go index f15f510a0b6a1..c5ee0d9784af8 100644 --- a/internal/adminanalytics/notebooks.go +++ b/internal/adminanalytics/notebooks.go @@ -7,10 +7,9 @@ import ( ) type Notebooks struct { - DateRange string - DB database.DB - Cache bool - NoSetCache *bool + DateRange string + DB database.DB + Cache bool } func (s *Notebooks) Creations() (*AnalyticsFetcher, error) { @@ -26,7 +25,6 @@ func (s *Notebooks) Creations() (*AnalyticsFetcher, error) { summaryQuery: summaryQuery, group: "Notebooks:Creations", cache: s.Cache, - noSetCache: s.NoSetCache, }, nil } diff --git a/internal/adminanalytics/notebooks_test.go b/internal/adminanalytics/notebooks_test.go deleted file mode 100644 index a84a4d7c01244..0000000000000 --- a/internal/adminanalytics/notebooks_test.go +++ /dev/null @@ -1,297 +0,0 @@ -package adminanalytics - -import ( - "context" - "fmt" - "testing" - "time" - - "github.com/google/go-cmp/cmp" - - "github.com/sourcegraph/log/logtest" - - "github.com/sourcegraph/sourcegraph/internal/database" - "github.com/sourcegraph/sourcegraph/internal/database/dbtest" -) - -func TestNotebooksCreationsLastWeek(t *testing.T) { - ctx := context.Background() - logger := logtest.Scoped(t) - db := database.NewDB(logger, dbtest.NewDB(logger, t)) - now := bod(time.Now()) - - _, err := db.ExecContext(context.Background(), ` -INSERT INTO event_logs - (id, name, argument, url, user_id, anonymous_user_id, source, version, timestamp) -VALUES - (1, 'SearchNotebookCreated', '{}', '', 1, '420657f0-d443-4d16-ac7d-003d8cdc91ea', 'WEB', 'version', $1::timestamp - interval '1 day'), - (2, 'SearchNotebookCreated', '{}', '', 2, '420657f0-d443-4d16-ac7d-003d8cdc91eb', 'WEB', 'version', $1::timestamp - interval '2 day'), - (3, 'SearchNotebookCreated', '{}', '', 0, '420657f0-d443-4d16-ac7d-003d8cdc91ec', 'WEB', 'version', $1::timestamp - interval '2 day'), - (4, 'SearchNotebookCreated', '{}', '', 0, '420657f0-d443-4d16-ac7d-003d8cdc91ec', 'WEB', 'version', $1::timestamp - interval '20 day'), - (5, 'SearchNotebookCreated', '{}', '', 0, '420657f0-d443-4d16-ac7d-003d8cdc91ec', 'WEB', 'version', $1::timestamp + interval '1 day') - `, now) - if err != nil { - t.Fatal(err) - } - - noSetCache := true - store := Notebooks{ - DateRange: "LAST_WEEK", - DB: db, - Cache: false, - NoSetCache: &noSetCache, - } - - fetcher, err := store.Creations() - if err != nil { - t.Fatal(err) - } - - results, err := fetcher.Nodes(ctx) - if err != nil { - t.Fatal(err) - } - - if len(results) != 8 { - t.Fatal(fmt.Sprintf("%d nodes returned rather than 8", len(results))) - } - - nodes := []*AnalyticsNode{ - { - Data: AnalyticsNodeData{ - Date: now.AddDate(0, 0, -1), - Count: 1, - UniqueUsers: 1, - RegisteredUsers: 1, - }, - }, - { - Data: AnalyticsNodeData{ - Date: now.AddDate(0, 0, -2), - Count: 2, - UniqueUsers: 2, - RegisteredUsers: 1, - }, - }, - } - - for _, node := range nodes { - var found *AnalyticsNode - - for _, result := range results { - if bod(node.Data.Date).Equal(bod(result.Data.Date)) { - found = result - } - } - - if diff := cmp.Diff(node, found); diff != "" { - t.Fatal(diff) - } - } - - summaryResult, err := fetcher.Summary(ctx) - if err != nil { - t.Fatal(err) - } - - summary := &AnalyticsSummary{ - Data: AnalyticsSummaryData{ - TotalCount: 3, - TotalUniqueUsers: 3, - TotalRegisteredUsers: 2, - }, - } - - if diff := cmp.Diff(summary, summaryResult); diff != "" { - t.Fatal(diff) - } -} - -func TestNotebooksCreationsLastMonth(t *testing.T) { - ctx := context.Background() - logger := logtest.Scoped(t) - db := database.NewDB(logger, dbtest.NewDB(logger, t)) - now := bod(time.Now()) - - _, err := db.ExecContext(context.Background(), ` -INSERT INTO event_logs - (id, name, argument, url, user_id, anonymous_user_id, source, version, timestamp) -VALUES - (1, 'SearchNotebookCreated', '{}', '', 1, '420657f0-d443-4d16-ac7d-003d8cdc91ea', 'WEB', 'version', $1::timestamp - interval '1 day'), - (2, 'SearchNotebookCreated', '{}', '', 2, '420657f0-d443-4d16-ac7d-003d8cdc91eb', 'WEB', 'version', $1::timestamp - interval '10 day'), - (3, 'SearchNotebookCreated', '{}', '', 0, '420657f0-d443-4d16-ac7d-003d8cdc91ec', 'WEB', 'version', $1::timestamp - interval '10 day'), - (4, 'SearchNotebookCreated', '{}', '', 0, '420657f0-d443-4d16-ac7d-003d8cdc91ec', 'WEB', 'version', $1::timestamp - interval '100 day'), - (5, 'SearchNotebookCreated', '{}', '', 0, '420657f0-d443-4d16-ac7d-003d8cdc91ec', 'WEB', 'version', $1::timestamp + interval '1 day') - `, now) - if err != nil { - t.Fatal(err) - } - - noSetCache := true - store := Notebooks{ - DateRange: "LAST_MONTH", - DB: db, - Cache: false, - NoSetCache: &noSetCache, - } - - fetcher, err := store.Creations() - if err != nil { - t.Fatal(err) - } - - results, err := fetcher.Nodes(ctx) - if err != nil { - t.Fatal(err) - } - - if len(results) < 28 { - t.Fatal(fmt.Sprintf("only %d nodes returned", len(results))) - } - - nodes := []*AnalyticsNode{ - { - Data: AnalyticsNodeData{ - Date: now.AddDate(0, 0, -1), - Count: 1, - UniqueUsers: 1, - RegisteredUsers: 1, - }, - }, - { - Data: AnalyticsNodeData{ - Date: now.AddDate(0, 0, -10), - Count: 2, - UniqueUsers: 2, - RegisteredUsers: 1, - }, - }, - } - - for _, node := range nodes { - var found *AnalyticsNode - - for _, result := range results { - if bod(node.Data.Date).Equal(bod(result.Data.Date)) { - found = result - } - } - - if diff := cmp.Diff(node, found); diff != "" { - t.Fatal(diff) - } - } - - summaryResult, err := fetcher.Summary(ctx) - if err != nil { - t.Fatal(err) - } - - summary := &AnalyticsSummary{ - Data: AnalyticsSummaryData{ - TotalCount: 3, - TotalUniqueUsers: 3, - TotalRegisteredUsers: 2, - }, - } - - if diff := cmp.Diff(summary, summaryResult); diff != "" { - t.Fatal(diff) - } -} - -func TestNotebooksCreationsLastThreeMonths(t *testing.T) { - ctx := context.Background() - logger := logtest.Scoped(t) - db := database.NewDB(logger, dbtest.NewDB(logger, t)) - now := bod(time.Now()) - - _, err := db.ExecContext(context.Background(), ` -INSERT INTO event_logs - (id, name, argument, url, user_id, anonymous_user_id, source, version, timestamp) -VALUES - (1, 'SearchNotebookCreated', '{}', '', 1, '420657f0-d443-4d16-ac7d-003d8cdc91ea', 'WEB', 'version', $1::timestamp), - (2, 'SearchNotebookCreated', '{}', '', 2, '420657f0-d443-4d16-ac7d-003d8cdc91eb', 'WEB', 'version', $1::timestamp), - (3, 'SearchNotebookCreated', '{}', '', 0, '420657f0-d443-4d16-ac7d-003d8cdc91ec', 'WEB', 'version', $1::timestamp), - (4, 'SearchNotebookCreated', '{}', '', 1, '420657f0-d443-4d16-ac7d-003d8cdc91ea', 'WEB', 'version', $1::timestamp - interval '40 day'), - (5, 'SearchNotebookCreated', '{}', '', 2, '420657f0-d443-4d16-ac7d-003d8cdc91eb', 'WEB', 'version', $1::timestamp - interval '40 day'), - (6, 'SearchNotebookCreated', '{}', '', 0, '420657f0-d443-4d16-ac7d-003d8cdc91ec', 'WEB', 'version', $1::timestamp - interval '40 day'), - (7, 'SearchNotebookCreated', '{}', '', 0, '420657f0-d443-4d16-ac7d-003d8cdc91ec', 'WEB', 'version', $1::timestamp - interval '100 day'), - (8, 'SearchNotebookCreated', '{}', '', 0, '420657f0-d443-4d16-ac7d-003d8cdc91ec', 'WEB', 'version', $1::timestamp + interval '1 day') - `, now) - if err != nil { - t.Fatal(err) - } - - noSetCache := true - store := Notebooks{ - DateRange: "LAST_THREE_MONTHS", - DB: db, - Cache: false, - NoSetCache: &noSetCache, - } - - fetcher, err := store.Creations() - if err != nil { - t.Fatal(err) - } - - results, err := fetcher.Nodes(ctx) - if err != nil { - t.Fatal(err) - } - - if len(results) < 12 { - t.Fatal(fmt.Sprintf("only %d nodes returned", len(results))) - } - - nodes := []*AnalyticsNode{ - { - Data: AnalyticsNodeData{ - Date: sow(now), - Count: 3, - UniqueUsers: 3, - RegisteredUsers: 2, - }, - }, - { - Data: AnalyticsNodeData{ - Date: sow(now.AddDate(0, 0, -40)), - Count: 3, - UniqueUsers: 3, - RegisteredUsers: 2, - }, - }, - } - - for _, node := range nodes { - var found *AnalyticsNode - - for _, result := range results { - if bod(node.Data.Date).Equal(bod(result.Data.Date)) { - found = result - } - } - - if diff := cmp.Diff(node, found); diff != "" { - t.Fatal(diff) - } - } - - summaryResult, err := fetcher.Summary(ctx) - if err != nil { - t.Fatal(err) - } - - summary := &AnalyticsSummary{ - Data: AnalyticsSummaryData{ - TotalCount: 6, - TotalUniqueUsers: 3, - TotalRegisteredUsers: 2, - }, - } - - if diff := cmp.Diff(summary, summaryResult); diff != "" { - t.Fatal(diff) - } -}