diff --git a/analytics/demo/demo.go b/analytics/demo/demo.go index bef7cc91c..331e7eadb 100644 --- a/analytics/demo/demo.go +++ b/analytics/demo/demo.go @@ -19,9 +19,10 @@ var ( log = logger.GetLogger() ) -func DemoInit(orgId, apiId, version string) { - apiID = apiId - GenerateAPIKeys(orgId) +// Start initializes the demo mode +func Start(orgID, newAAPIID, version string, apikeysCount int) { + apiID = newAAPIID + GenerateAPIKeys(orgID, apikeysCount) apiVersion = version if version == "" { apiVersion = "Default" @@ -120,26 +121,26 @@ func responseCode() int { return codes[rand.Intn(len(codes))] } -func GenerateAPIKeys(orgId string) { - set := make([]string, 50) +func GenerateAPIKeys(orgID string, apikeysQuantity int) { + set := make([]string, apikeysQuantity) for i := 0; i < len(set); i++ { - set[i] = generateAPIKey(orgId) + set[i] = generateAPIKey(orgID) } apiKeys = set } -func generateAPIKey(orgId string) string { +func generateAPIKey(orgID string) string { u1, err := uuid.NewV4() if err != nil { log.WithError(err).Error("failed to generate UUID") } id := strings.Replace(u1.String(), "-", "", -1) - return orgId + id + return orgID + id } -func getRandomKey(orgId string) string { +func getRandomKey(orgID string, apiKeysCount int) string { if len(apiKeys) == 0 { - GenerateAPIKeys(orgId) + GenerateAPIKeys(orgID, apiKeysCount) } return apiKeys[rand.Intn(len(apiKeys))] } @@ -153,7 +154,8 @@ func country() string { return codes[rand.Intn(len(codes))] } -func GenerateDemoData(days, recordsPerHour int, orgID string, demoFutureData, trackPath bool, writer func([]interface{}, *health.Job, time.Time, int)) { +// GenerateDemoData given the params for the demo mode +func GenerateDemoData(days, recordsPerHour int, orgID string, demoFutureData, trackPath bool, apiKeysCount int, writer func([]interface{}, *health.Job, time.Time, int)) { t := time.Now() start := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.UTC) count := 0 @@ -161,7 +163,7 @@ func GenerateDemoData(days, recordsPerHour int, orgID string, demoFutureData, tr if demoFutureData { for d := 0; d < days; d++ { for h := 0; h < 24; h++ { - WriteDemoData(start, d, h, recordsPerHour, orgID, trackPath, writer) + WriteDemoData(start, d, h, recordsPerHour, orgID, trackPath, apiKeysCount, writer) } count++ log.Infof("Finished %d of %d\n", count, days) @@ -172,14 +174,15 @@ func GenerateDemoData(days, recordsPerHour int, orgID string, demoFutureData, tr // Otherwise, we want to start at the (current date - X days) and create data until yesterday's date for d := days; d > 0; d-- { for h := 0; h < 24; h++ { - WriteDemoData(start, -d, h, recordsPerHour, orgID, trackPath, writer) + WriteDemoData(start, -d, h, recordsPerHour, orgID, trackPath, apiKeysCount, writer) } count++ log.Infof("Finished %d of %d\n", count, days) } } -func WriteDemoData(start time.Time, d, h, recordsPerHour int, orgID string, trackPath bool, writer func([]interface{}, *health.Job, time.Time, int)) { +// WriteDemoData writes the data generated in demo mode into store +func WriteDemoData(start time.Time, d, h, recordsPerHour int, orgID string, trackPath bool, apikeysCount int, writer func([]interface{}, *health.Job, time.Time, int)) { set := []interface{}{} ts := start.AddDate(0, 0, d) ts = ts.Add(time.Duration(h) * time.Hour) @@ -193,7 +196,7 @@ func WriteDemoData(start time.Time, d, h, recordsPerHour int, orgID string, trac timeDifference := 3600 / volume // this is the difference in seconds between each record nextTimestamp := ts // this is the timestamp of the next record for i := 0; i < volume; i++ { - r := GenerateRandomAnalyticRecord(orgID, trackPath) + r := GenerateRandomAnalyticRecord(orgID, trackPath, apikeysCount) r.Day = nextTimestamp.Day() r.Month = nextTimestamp.Month() r.Year = nextTimestamp.Year() @@ -207,7 +210,8 @@ func WriteDemoData(start time.Time, d, h, recordsPerHour int, orgID string, trac writer(set, nil, time.Now(), 10) } -func GenerateRandomAnalyticRecord(orgID string, trackPath bool) analytics.AnalyticsRecord { +// GenerateRandomAnalyticRecord generates one single analytics record for the demo mode +func GenerateRandomAnalyticRecord(orgID string, trackPath bool, apiKeysCount int) analytics.AnalyticsRecord { p := randomPath() api, apiID := randomAPI() ts := time.Now() @@ -222,7 +226,7 @@ func GenerateRandomAnalyticRecord(orgID string, trackPath bool) analytics.Analyt Year: ts.Year(), Hour: ts.Hour(), ResponseCode: responseCode(), - APIKey: getRandomKey(orgID), + APIKey: getRandomKey(orgID, apiKeysCount), TimeStamp: ts, APIVersion: apiVersion, APIName: api, diff --git a/analytics/demo/demo_test.go b/analytics/demo/demo_test.go index 2abd835ce..99004ba18 100644 --- a/analytics/demo/demo_test.go +++ b/analytics/demo/demo_test.go @@ -17,6 +17,7 @@ func TestGenerateDemoData(t *testing.T) { recordsPerHour int trackPath bool futureData bool + apikeysCount int } tests := []struct { @@ -31,6 +32,7 @@ func TestGenerateDemoData(t *testing.T) { orgID: "test", trackPath: false, futureData: true, + apikeysCount: 50, writer: func(data []interface{}, job *health.Job, ts time.Time, n int) { }, }, @@ -42,6 +44,7 @@ func TestGenerateDemoData(t *testing.T) { recordsPerHour: 1, orgID: "test", trackPath: true, + apikeysCount: 50, writer: func([]interface{}, *health.Job, time.Time, int) {}, }, }, @@ -52,6 +55,7 @@ func TestGenerateDemoData(t *testing.T) { recordsPerHour: 2, orgID: "test", trackPath: false, + apikeysCount: 50, writer: func([]interface{}, *health.Job, time.Time, int) {}, }, }, @@ -62,6 +66,7 @@ func TestGenerateDemoData(t *testing.T) { recordsPerHour: 2, orgID: "test", trackPath: true, + apikeysCount: 50, writer: func([]interface{}, *health.Job, time.Time, int) {}, }, }, @@ -72,6 +77,7 @@ func TestGenerateDemoData(t *testing.T) { recordsPerHour: 100, orgID: "test", trackPath: false, + apikeysCount: 50, writer: func([]interface{}, *health.Job, time.Time, int) {}, }, }, @@ -82,6 +88,7 @@ func TestGenerateDemoData(t *testing.T) { recordsPerHour: 0, orgID: "test", trackPath: true, + apikeysCount: 50, writer: func([]interface{}, *health.Job, time.Time, int) {}, }, }, @@ -92,6 +99,7 @@ func TestGenerateDemoData(t *testing.T) { recordsPerHour: 0, orgID: "test", trackPath: false, + apikeysCount: 50, writer: func([]interface{}, *health.Job, time.Time, int) {}, }, }, @@ -121,7 +129,7 @@ func TestGenerateDemoData(t *testing.T) { } } - GenerateDemoData(tt.args.days, tt.args.recordsPerHour, tt.args.orgID, tt.args.futureData, tt.args.trackPath, tt.args.writer) + GenerateDemoData(tt.args.days, tt.args.recordsPerHour, tt.args.orgID, tt.args.futureData, tt.args.trackPath, tt.args.apikeysCount, tt.args.writer) if tt.args.recordsPerHour == 0 { isValid := counter >= 300*tt.args.days || counter <= 500*tt.args.days assert.True(t, isValid) diff --git a/main.go b/main.go index 2dcd1ea5c..dbc307c24 100644 --- a/main.go +++ b/main.go @@ -47,6 +47,7 @@ var ( demoDays = kingpin.Flag("demo-days", "flag that determines the number of days for the analytics records").Default("30").Int() demoRecordsPerHour = kingpin.Flag("demo-records-per-hour", "flag that determines the number of records per hour for the analytics records").Default("0").Int() demoFutureData = kingpin.Flag("demo-future-data", "flag that determines if the demo data should be in the future").Default("false").Bool() + demoAPIKeysCount = kingpin.Flag("demo-apikeys-count", "flag that determines the quantity of apikeys to generate in demo mode").Default("50").Int() debugMode = kingpin.Flag("debug", "enable debug mode").Bool() //lint:ignore U1000 Function is used when version flag is passed in command line version = kingpin.Version(pumps.Version) @@ -483,8 +484,8 @@ func main() { if *demoMode != "" { log.Info("BUILDING DEMO DATA AND EXITING...") log.Warning("Starting from date: ", time.Now().AddDate(0, 0, -30)) - demo.DemoInit(*demoMode, *demoApiMode, *demoApiVersionMode) - demo.GenerateDemoData(*demoDays, *demoRecordsPerHour, *demoMode, *demoFutureData, *demoTrackPath, writeToPumps) + demo.Start(*demoMode, *demoApiMode, *demoApiVersionMode, *demoAPIKeysCount) + demo.GenerateDemoData(*demoDays, *demoRecordsPerHour, *demoMode, *demoFutureData, *demoTrackPath, *demoAPIKeysCount, writeToPumps) return } diff --git a/pumps/csv_test.go b/pumps/csv_test.go index 2fefa5462..be45091c6 100644 --- a/pumps/csv_test.go +++ b/pumps/csv_test.go @@ -201,7 +201,7 @@ func TestCSVPump_WriteData(t *testing.T) { var records []interface{} if !tt.wantErr { for i := 0; i < tt.args.numberOfRecords; i++ { - records = append(records, demo.GenerateRandomAnalyticRecord("orgid", false)) + records = append(records, demo.GenerateRandomAnalyticRecord("orgid", false, 50)) } } else { records = append(records, "invalid record") diff --git a/pumps/mongo_aggregate_test.go b/pumps/mongo_aggregate_test.go index 5a8a726a6..fcb83b704 100644 --- a/pumps/mongo_aggregate_test.go +++ b/pumps/mongo_aggregate_test.go @@ -315,7 +315,7 @@ func TestMongoAggregatePump_SelfHealing(t *testing.T) { var set []interface{} for { count++ - record := demo.GenerateRandomAnalyticRecord("org123", true) + record := demo.GenerateRandomAnalyticRecord("org123", true, 50) set = append(set, record) if count == 1000 { err := pmp1.WriteData(context.TODO(), set) diff --git a/serializer/serializer_test.go b/serializer/serializer_test.go index 5af8b72c8..8880459e4 100644 --- a/serializer/serializer_test.go +++ b/serializer/serializer_test.go @@ -109,16 +109,16 @@ func TestSerializer_GetSuffix(t *testing.T) { func BenchmarkProtobufEncoding(b *testing.B) { serializer := NewAnalyticsSerializer(PROTOBUF_SERIALIZER) records := []analytics.AnalyticsRecord{ - demo.GenerateRandomAnalyticRecord("org_1", true), - demo.GenerateRandomAnalyticRecord("org_1", true), - demo.GenerateRandomAnalyticRecord("org_1", true), - demo.GenerateRandomAnalyticRecord("org_1", true), - demo.GenerateRandomAnalyticRecord("org_1", true), - demo.GenerateRandomAnalyticRecord("org_2", true), - demo.GenerateRandomAnalyticRecord("org_2", true), - demo.GenerateRandomAnalyticRecord("org_2", true), - demo.GenerateRandomAnalyticRecord("org_2", true), - demo.GenerateRandomAnalyticRecord("org_2", true), + demo.GenerateRandomAnalyticRecord("org_1", true, 50), + demo.GenerateRandomAnalyticRecord("org_1", true, 50), + demo.GenerateRandomAnalyticRecord("org_1", true, 50), + demo.GenerateRandomAnalyticRecord("org_1", true, 50), + demo.GenerateRandomAnalyticRecord("org_1", true, 50), + demo.GenerateRandomAnalyticRecord("org_2", true, 50), + demo.GenerateRandomAnalyticRecord("org_2", true, 50), + demo.GenerateRandomAnalyticRecord("org_2", true, 50), + demo.GenerateRandomAnalyticRecord("org_2", true, 50), + demo.GenerateRandomAnalyticRecord("org_2", true, 50), } b.Helper() b.ReportAllocs() @@ -136,16 +136,16 @@ func BenchmarkProtobufEncoding(b *testing.B) { func BenchmarkMsgpEncoding(b *testing.B) { serializer := NewAnalyticsSerializer(MSGP_SERIALIZER) records := []analytics.AnalyticsRecord{ - demo.GenerateRandomAnalyticRecord("org_1", true), - demo.GenerateRandomAnalyticRecord("org_1", true), - demo.GenerateRandomAnalyticRecord("org_1", true), - demo.GenerateRandomAnalyticRecord("org_1", true), - demo.GenerateRandomAnalyticRecord("org_1", true), - demo.GenerateRandomAnalyticRecord("org_2", true), - demo.GenerateRandomAnalyticRecord("org_2", true), - demo.GenerateRandomAnalyticRecord("org_2", true), - demo.GenerateRandomAnalyticRecord("org_2", true), - demo.GenerateRandomAnalyticRecord("org_2", true), + demo.GenerateRandomAnalyticRecord("org_1", true, 50), + demo.GenerateRandomAnalyticRecord("org_1", true, 50), + demo.GenerateRandomAnalyticRecord("org_1", true, 50), + demo.GenerateRandomAnalyticRecord("org_1", true, 50), + demo.GenerateRandomAnalyticRecord("org_1", true, 50), + demo.GenerateRandomAnalyticRecord("org_2", true, 50), + demo.GenerateRandomAnalyticRecord("org_2", true, 50), + demo.GenerateRandomAnalyticRecord("org_2", true, 50), + demo.GenerateRandomAnalyticRecord("org_2", true, 50), + demo.GenerateRandomAnalyticRecord("org_2", true, 50), } b.Helper() b.ReportAllocs()