public Nullable <DateTime> GetDateOfDeviceLastVisit(Guid deviceId, Guid applicationId) { try { IMongoQuery queryBase = Query.And ( Query <AppUsageSummary> .EQ <Guid>(mem => mem.ApplicationId, applicationId), Query.EQ("DevicesVisits.DeviceId", BsonValue.Create(deviceId)) ); MongoCursor <AppUsageSummary> appUsageSummaries = this.GetCollection <AppUsageSummary>().Find(queryBase); appUsageSummaries.SetSortOrder(SortBy <AppUsageSummary> .Descending(x => x.Date)); AppUsageSummary appUsageSummary = appUsageSummaries.FirstOrDefault(); if (appUsageSummary != null) { return(appUsageSummary.Date); } return(null); } catch (Exception ex) { throw new DataAccessLayerException(ex); } }
public void Save_AppUsageSummary_ValuesIncrement() { EventMapper eventMapper = new EventMapper(this.client, this.database); Guid applicationId = Guid.NewGuid(); Guid deviceId = Guid.NewGuid(); AppUsageSummary expected = new AppUsageSummary() { ApplicationId = applicationId, Count = 2, Date = date, PlatformId = platform, Version = version, TimeGroup = new TimeOfDayGroup() { _10 = 2 }, WeekGroup = new DayOfWeekGroup() { Wed = 2 }, FrequencyUsageGroup = new FrequencyOfUsageGroup(), DevicesVisits = new List <DeviceAppVisit>() { new DeviceAppVisit(deviceId) { Count = 2 } }, NewReturningGroup = new NewReturningGroup(2, 0) }; Event eventItem = new Event() { ApplicationId = applicationId, Date = date, Version = version, PlatformId = platform, DateCreatedOnDevice = dateCreatedOnDevice, DeviceId = deviceId, DateCreated = date }; AppUsageSummary summary = new AppUsageSummary(eventItem, null); eventMapper.Save(summary); eventMapper.Save(summary); IMongoQuery query = Query.And ( Query <AppUsageSummary> .EQ <DateTime>(mem => mem.Date, date), Query <AppUsageSummary> .EQ <Guid>(mem => mem.ApplicationId, applicationId), Query <AppUsageSummary> .EQ <string>(mem => mem.Version, version), Query <AppUsageSummary> .EQ <PlatformType>(mem => mem.PlatformId, platform) ); AppUsageSummary actual = this.GetCollection <AppUsageSummary>().FindOne(query); actual.ShouldHave().AllPropertiesBut(x => x.Id) .IncludingNestedObjects().EqualTo(expected); }
public void Save(AppUsageSummary summary) { this.eventMapper.Save(summary); }
public void Log(Event eventItem) { Application application = this.applicationRepository.Find(eventItem.ApplicationId); if (application != null) { DeviceInfo device = this.deviceRepository.Find(eventItem.DeviceId); if (device != null) { eventItem.PlatformId = device.PlatformType; switch (eventItem.EventTypeId) { case EventType.ApplicationOpen: Nullable <DateTime> lastDeviceVisit = this.eventRepository .GetDateOfDeviceLastVisit(eventItem.DeviceId, eventItem.ApplicationId); AppUsageSummary appUsageSummary = new AppUsageSummary(eventItem, lastDeviceVisit); this.eventRepository.Save(appUsageSummary); break; case EventType.ApplicationClose: AppUsageDurationSummary appUsageSum = new AppUsageDurationSummary(eventItem); this.eventRepository.Save(appUsageSum); DeviceAppLastScreen appLastScreen = this.eventRepository.GetDeviceAppLastScreenOneBy(eventItem.DeviceId, eventItem.ApplicationId); if (appLastScreen != null) { ScreenRouteSummary routeSum = new ScreenRouteSummary(eventItem, appLastScreen.ScreenName, string.Empty); this.eventRepository.Save(routeSum); this.eventRepository.Remove(eventItem.DeviceId, eventItem.ApplicationId); } break; case EventType.Event: EventSummary eventSum = new EventSummary(eventItem); this.eventRepository.Save(eventSum); break; case EventType.ScreenClose: ScreenSummary screenUsageSum = new ScreenSummary(eventItem); this.eventRepository.Save(screenUsageSum); this.eventRepository.Remove(eventItem.DeviceId, eventItem.ApplicationId); this.eventRepository.Save(new DeviceAppLastScreen(eventItem)); break; case EventType.ScreenOpen: DeviceAppLastScreen lastScreen = this.eventRepository.GetDeviceAppLastScreenOneBy(eventItem.DeviceId, eventItem.ApplicationId); ScreenRouteSummary routeSum2 = new ScreenRouteSummary(eventItem, string.Empty, eventItem.ScreenName); if (lastScreen != null) { if (lastScreen.SessionId == eventItem.SessionId) { routeSum2 = new ScreenRouteSummary(eventItem, lastScreen.ScreenName, eventItem.ScreenName); } this.eventRepository.Remove(eventItem.DeviceId, eventItem.ApplicationId); } this.eventRepository.Save(routeSum2); this.eventRepository.Save(new DeviceAppLastScreen(eventItem)); break; case EventType.ContentLoaded: ContentLoadSummary contentSum = new ContentLoadSummary(eventItem); this.eventRepository.Save(contentSum); break; } if (this.settings.DataLoggingRecordRaw) { this.eventRepository.Save(eventItem); } } else { throw new NoDeviceException(eventItem.DeviceId); } } else { throw new InactiveApplicationException(eventItem.ApplicationId); } }
public void Save(AppUsageSummary entity) { try { IMongoQuery queryBase = Query.And ( Query <AppUsageSummary> .EQ <DateTime>(mem => mem.Date, entity.Date), Query <AppUsageSummary> .EQ <Guid>(mem => mem.ApplicationId, entity.ApplicationId), Query <AppUsageSummary> .EQ <string>(mem => mem.Version, entity.Version), Query <AppUsageSummary> .EQ <PlatformType>(mem => mem.PlatformId, entity.PlatformId) ); IMongoUpdate updateExistCheck = Update <AppUsageSummary> .SetOnInsert(x => x.Version, entity.Version) .SetOnInsert(x => x.Date, entity.Date) .SetOnInsert(x => x.ApplicationId, entity.ApplicationId) .SetOnInsert(x => x.PlatformId, entity.PlatformId) .SetOnInsert(x => x.DevicesVisits, new List <DeviceAppVisit>()) .Inc(x => x.TimeGroup._0, entity.TimeGroup._0) .Inc(x => x.TimeGroup._1, entity.TimeGroup._1) .Inc(x => x.TimeGroup._2, entity.TimeGroup._2) .Inc(x => x.TimeGroup._3, entity.TimeGroup._3) .Inc(x => x.TimeGroup._4, entity.TimeGroup._4) .Inc(x => x.TimeGroup._5, entity.TimeGroup._5) .Inc(x => x.TimeGroup._6, entity.TimeGroup._6) .Inc(x => x.TimeGroup._7, entity.TimeGroup._7) .Inc(x => x.TimeGroup._8, entity.TimeGroup._8) .Inc(x => x.TimeGroup._9, entity.TimeGroup._9) .Inc(x => x.TimeGroup._10, entity.TimeGroup._10) .Inc(x => x.TimeGroup._11, entity.TimeGroup._11) .Inc(x => x.TimeGroup._12, entity.TimeGroup._12) .Inc(x => x.TimeGroup._13, entity.TimeGroup._13) .Inc(x => x.TimeGroup._14, entity.TimeGroup._14) .Inc(x => x.TimeGroup._15, entity.TimeGroup._15) .Inc(x => x.TimeGroup._16, entity.TimeGroup._16) .Inc(x => x.TimeGroup._17, entity.TimeGroup._17) .Inc(x => x.TimeGroup._18, entity.TimeGroup._18) .Inc(x => x.TimeGroup._19, entity.TimeGroup._19) .Inc(x => x.TimeGroup._20, entity.TimeGroup._20) .Inc(x => x.TimeGroup._21, entity.TimeGroup._21) .Inc(x => x.TimeGroup._22, entity.TimeGroup._22) .Inc(x => x.TimeGroup._23, entity.TimeGroup._23) .Inc(x => x.WeekGroup.Mon, entity.WeekGroup.Mon) .Inc(x => x.WeekGroup.Tue, entity.WeekGroup.Tue) .Inc(x => x.WeekGroup.Wed, entity.WeekGroup.Wed) .Inc(x => x.WeekGroup.Thu, entity.WeekGroup.Thu) .Inc(x => x.WeekGroup.Fri, entity.WeekGroup.Fri) .Inc(x => x.WeekGroup.Sat, entity.WeekGroup.Sat) .Inc(x => x.WeekGroup.Sun, entity.WeekGroup.Sun) .Inc(x => x.FrequencyUsageGroup._24hrs, entity.FrequencyUsageGroup._24hrs) .Inc(x => x.FrequencyUsageGroup._1day, entity.FrequencyUsageGroup._1day) .Inc(x => x.FrequencyUsageGroup._2days, entity.FrequencyUsageGroup._2days) .Inc(x => x.FrequencyUsageGroup._3days, entity.FrequencyUsageGroup._3days) .Inc(x => x.FrequencyUsageGroup._4days, entity.FrequencyUsageGroup._4days) .Inc(x => x.FrequencyUsageGroup._5days, entity.FrequencyUsageGroup._5days) .Inc(x => x.FrequencyUsageGroup._6days, entity.FrequencyUsageGroup._6days) .Inc(x => x.FrequencyUsageGroup._1wk, entity.FrequencyUsageGroup._1wk) .Inc(x => x.FrequencyUsageGroup._2wk, entity.FrequencyUsageGroup._2wk) .Inc(x => x.FrequencyUsageGroup._3wk, entity.FrequencyUsageGroup._3wk) .Inc(x => x.FrequencyUsageGroup._1mt, entity.FrequencyUsageGroup._1mt) .Inc(x => x.FrequencyUsageGroup.Over1Mt, entity.FrequencyUsageGroup.Over1Mt) .Inc(x => x.NewReturningGroup.New, entity.NewReturningGroup.New) .Inc(x => x.NewReturningGroup.Returning, entity.NewReturningGroup.Returning) .Inc(x => x.Count, 1); this.GetCollection <AppUsageSummary>().FindAndModify(queryBase, SortBy.Descending("Date", "DevicesVisits.DeviceId"), updateExistCheck, false, true); this.GetCollection <AppUsageDurationSummary>().EnsureIndex(IndexKeys.Descending("Date", "DevicesVisits.DeviceId")); IMongoQuery queryGetExistingValues = Query.And ( queryBase, Query.NE("DevicesVisits.DeviceId", BsonValue.Create(entity.DevicesVisits.First().DeviceId)) ); IMongoUpdate insertDeviceVisits = Update .Push("DevicesVisits", BsonValue.Create(entity.DevicesVisits.First().CopyOnlyKey().ToBsonDocument())); this.GetCollection <AppUsageSummary>().Update(queryGetExistingValues, insertDeviceVisits); IMongoQuery queryDeviceVisits = Query.And ( queryBase, Query.EQ("DevicesVisits.DeviceId", BsonValue.Create(entity.DevicesVisits.First().DeviceId)) ); IMongoUpdate updateDeviceVisits = Update .Inc("DevicesVisits.$.Count", 1); this.GetCollection <AppUsageSummary>().Update(queryDeviceVisits, updateDeviceVisits); } catch (Exception ex) { throw new DataAccessLayerException(ex); } }