Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

receive by param the number of api keys to generate #808

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions analytics/demo/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))]
}
Expand All @@ -153,15 +154,16 @@ 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
// If we are generating future data, we want to start at the current date and create data for the next X days
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)
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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,
Expand Down
10 changes: 9 additions & 1 deletion analytics/demo/demo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestGenerateDemoData(t *testing.T) {
recordsPerHour int
trackPath bool
futureData bool
apikeysCount int
}

tests := []struct {
Expand All @@ -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) {
},
},
Expand All @@ -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) {},
},
},
Expand All @@ -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) {},
},
},
Expand All @@ -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) {},
},
},
Expand All @@ -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) {},
},
},
Expand All @@ -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) {},
},
},
Expand All @@ -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) {},
},
},
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion pumps/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion pumps/mongo_aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 20 additions & 20 deletions serializer/serializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
Loading