public async Task ExampleUsage1() { // Create an analytics system and fill it with feature usage events: var analytics = CreateLocalAnalyticsSystem(); var t = new MockDateTimeV2(); IoC.inject.SetSingleton <DateTimeV2>(t); string featureId = "feature1"; { var startDate = DateTimeV2.ParseUtc("01.01.2011"); var endDate = DateTimeV2.ParseUtc("19.01.2011"); SimulateUsage(featureId, 100, t, startDate, endDate); await AssertFeatureUsageDetected(analytics, featureId, 100); } t.mockUtcNow = DateTimeV2.ParseUtc("01.02.2011"); await TestAllRules1(analytics, featureId); { // Simulate more usage in the next 5 days: var startDate = DateTimeV2.ParseUtc("20.01.2011"); var endDate = DateTimeV2.ParseUtc("25.01.2011"); SimulateUsage(featureId, 100, t, startDate, endDate); await AssertFeatureUsageDetected(analytics, featureId, 200); } // Simulate a month without any usage: t.mockUtcNow = DateTimeV2.ParseUtc("01.03.2011"); await TestAllRules2(analytics, featureId); { // Simulate that a usage notification is shown and test the related rule: var notificationId = "notification1"; var daysAgo = 20; t.mockUtcNow = DateTimeV2.ParseUtc("01.03.2011"); // Simulate that notification1 is shown to the user (e.g. by the usageRule system): AppFlow.TrackEvent(EventConsts.catUsage, EventConsts.SHOW + "_" + notificationId); t.mockUtcNow = DateTimeV2.ParseUtc("02.03.2011"); // Simulate a day passing by UsageRule notificationMinXDaysOld = analytics.NewNotificationMinXDaysOldRule(notificationId, daysAgo); Assert.False(await notificationMinXDaysOld.isTrue()); Assert.False(await notificationMinXDaysOld.IsNotificationMinXDaysOld(analytics)); t.mockUtcNow = DateTimeV2.ParseUtc("25.03.2011"); // Simulate more time passing by Assert.True(await notificationMinXDaysOld.IsNotificationMinXDaysOld(analytics)); Assert.True(await notificationMinXDaysOld.isTrue()); // Simulate a second show of the notification: AppFlow.TrackEvent(EventConsts.catUsage, EventConsts.SHOW + "_" + notificationId); Assert.True(await notificationMinXDaysOld.IsNotificationMinXDaysOld(analytics)); Assert.True(await notificationMinXDaysOld.isTrue()); } }
void SimulateUsage(string featureId, int evCount, MockDateTimeV2 t, DateTime start, DateTime end) { double s = start.ToUnixTimestampUtc(); double durationInMs = (end - start).TotalMilliseconds; Assert.NotEqual(0, durationInMs); for (int i = 0; i < evCount; i++) { double percent = i / (double)evCount; Assert.InRange(percent, 0, 1); t.mockUtcNow = DateTimeV2.NewDateTimeFromUnixTimestamp((long)(s + percent * durationInMs)); AppFlow.TrackEvent(featureId, EventConsts.START); } }