private EstablishmentViews UpdateView(bool force = false)
        {
            lock (UpdateLock)
            {
                _isUpdating = true;
                var existing = _viewManager.Get <EstablishmentViews>();
                if (existing != null && !force)
                {
                    return(existing);
                }

                var entities = _entities.Query <Establishment>()
                               .EagerLoad(_entities, new Expression <Func <Establishment, object> >[]
                {
                    x => x.Names.Select(y => y.TranslationToLanguage),
                    x => x.Urls,
                    x => x.Location.Places.Select(y => y.GeoPlanetPlace),
                    x => x.Parent,
                    x => x.Type,
                })
                               .OrderBy(x => x.RevisionId)
                ;

                var view = new List <EstablishmentView>();
                foreach (var entity in entities)
                {
                    view.Add(new EstablishmentView(entity));
                }
                _viewManager.Set <EstablishmentViews>(view.ToArray());
                _isUpdating = false;
                return(_viewManager.Get <EstablishmentViews>());
            }
        }
Beispiel #2
0
 internal void Set(EmployeeActivityCountsView view, int establishmentId)
 {
     Lock.EnterWriteLock();
     try
     {
         _views.Set <EmployeeActivityCountsView>(view, establishmentId);
     }
     finally
     {
         Lock.ExitWriteLock();
     }
 }
Beispiel #3
0
 internal void Set(IEnumerable <EmployeePlacesView> views, int establishmentId)
 {
     Lock.EnterWriteLock();
     try
     {
         _views.Set <IEnumerable <EmployeePlacesView> >(views, establishmentId);
     }
     finally
     {
         Lock.ExitWriteLock();
     }
 }
        public void BuildViews()
        {
#if DEBUG
            var buildTimer = new Stopwatch();
            buildTimer.Start();
#endif
            var entities = _entities.Query <Activity>();

            /* ------ Construct the general view. ----- */

            var view = new List <ActivityView>();
            Rwlock.EnterWriteLock();

            try
            {
                foreach (var entity in entities)
                {
                    /* Only public activities. Do not include edit-copy activities */
                    if ((entity.Mode == ActivityMode.Public) &&
                        (entity.EditSourceId == null))
                    {
                        view.Add(new ActivityView(entity));
                    }
                }

                _viewManager.Set <ICollection <ActivityView> >(view);
            }
#if DEBUG
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
#endif
            finally
            {
                Rwlock.ExitWriteLock();

                Debug.WriteLine(DateTime.Now + " ActivityView build complete with " + view.Count + " activities.");
            }


            /* ----- Construct the stats views ----- */

            StatsRwlock.EnterWriteLock();

            try
            {
                var establishmentsWithPeopleWithActivities =
                    _queries.Execute(new EstablishmentsWithPeopleWithActivities());

                /* ----- Global activity counts per establishment ----- */

                _globalActivityDictionary = _viewManager.Get <Dictionary <int, GlobalActivityCountView> >();
                if (_globalActivityDictionary == null)
                {
                    _viewManager.Set <Dictionary <int, GlobalActivityCountView> >(
                        new Dictionary <int, GlobalActivityCountView>());
                    _globalActivityDictionary = _viewManager.Get <Dictionary <int, GlobalActivityCountView> >();
                }
                _globalActivityDictionary.Clear();

                foreach (var establishment in establishmentsWithPeopleWithActivities)
                {
                    int establishmentId = establishment.RevisionId;
                    var stats           = new GlobalActivityCountView {
                        EstablishmentId = establishmentId
                    };

                    stats.TypeCounts  = new Collection <ActivityViewStats.TypeCount>();
                    stats.PlaceCounts = new Collection <ActivityViewStats.PlaceCount>();

                    var settings = _queries.Execute(new EmployeeModuleSettingsByEstablishmentId(establishmentId));

                    DateTime toDateUtc   = new DateTime(DateTime.UtcNow.Year + 1, 1, 1);
                    DateTime fromDateUtc = (settings != null) && (settings.ReportsDefaultYearRange.HasValue)
                                               ? toDateUtc.AddYears(-(settings.ReportsDefaultYearRange.Value + 1))
                                               : new DateTime(DateTime.MinValue.Year, 1, 1);

                    stats.CountOfPlaces = 0;
                    stats.Count         = 0;

                    IEnumerable <Place> places = _entities.Query <Place>()
                                                 .Where(p => p.IsCountry || p.IsWater || p.IsEarth);
                    foreach (var place in places)
                    {
                        int activityCount =
                            _queries.Execute(
                                new ActivityCountByPlaceIdsEstablishmentId(new int[] { place.RevisionId },
                                                                           establishmentId,
                                                                           fromDateUtc,
                                                                           toDateUtc,
                                                                           false, /* include undated */
                                                                           true /* include future */));

                        stats.PlaceCounts.Add(new ActivityViewStats.PlaceCount
                        {
                            PlaceId      = place.RevisionId,
                            CountryCode  = place.IsCountry ? place.GeoPlanetPlace.Country.Code : null,
                            OfficialName = place.OfficialName,
                            Count        = activityCount,
                            Lat          = place.Center.Latitude.HasValue ? place.Center.Latitude.Value : 0,
                            Lng          = place.Center.Longitude.HasValue ? place.Center.Longitude.Value : 0
                        });

                        stats.Count += activityCount;

                        if (activityCount > 0)
                        {
                            stats.CountOfPlaces += 1;
                        }

                        if ((settings != null) && settings.ActivityTypes.Any())
                        {
                            foreach (var type in settings.ActivityTypes)
                            {
                                int placeTypeCount = _queries.Execute(
                                    new ActivityCountByTypeIdPlaceIdsEstablishmentId(type.Id,
                                                                                     new int[] { place.RevisionId },
                                                                                     establishmentId,
                                                                                     fromDateUtc,
                                                                                     toDateUtc,
                                                                                     false, /* include undated */
                                                                                     true /* include future */));

                                var typeCount = stats.TypeCounts.SingleOrDefault(t => t.TypeId == type.Id);
                                if (typeCount != null)
                                {
                                    typeCount.Count += placeTypeCount;
                                }
                                else
                                {
                                    typeCount = new ActivityViewStats.TypeCount
                                    {
                                        TypeId = type.Id,
                                        Type   = type.Type,
                                        Count  = placeTypeCount,
                                        Rank   = type.Rank
                                    };

                                    stats.TypeCounts.Add(typeCount);
                                }
                            }

                            stats.TypeCounts = stats.TypeCounts.OrderBy(t => t.Rank).ToList();
                        }
                    }

                    _globalActivityDictionary.Remove(establishmentId);
                    _globalActivityDictionary.Add(establishmentId, stats);
                }

                _viewManager.Set <Dictionary <int, GlobalActivityCountView> >(_globalActivityDictionary);

                /* ----- Global people counts per establishment ----- */

                _globalPeopleDictionary = _viewManager.Get <Dictionary <int, GlobalPeopleCountView> >();
                if (_globalPeopleDictionary == null)
                {
                    _viewManager.Set <Dictionary <int, GlobalPeopleCountView> >(
                        new Dictionary <int, GlobalPeopleCountView>());
                    _globalPeopleDictionary = _viewManager.Get <Dictionary <int, GlobalPeopleCountView> >();
                }
                _globalPeopleDictionary.Clear();


                foreach (var establishment in establishmentsWithPeopleWithActivities)
                {
                    int establishmentId = establishment.RevisionId;
                    var stats           = new GlobalPeopleCountView {
                        EstablishmentId = establishmentId
                    };

                    stats.TypeCounts  = new Collection <ActivityViewStats.TypeCount>();
                    stats.PlaceCounts = new Collection <ActivityViewStats.PlaceCount>();

                    var settings = _queries.Execute(new EmployeeModuleSettingsByEstablishmentId(establishmentId));

                    DateTime toDateUtc   = new DateTime(DateTime.UtcNow.Year + 1, 1, 1);
                    DateTime fromDateUtc = (settings != null) && (settings.ReportsDefaultYearRange.HasValue)
                                               ? toDateUtc.AddYears(-(settings.ReportsDefaultYearRange.Value + 1))
                                               : new DateTime(DateTime.MinValue.Year, 1, 1);

                    stats.CountOfPlaces = 0;
                    stats.Count         = _queries.Execute(new PeopleWithActivitiesCountByEstablishmentId(establishmentId,
                                                                                                          fromDateUtc,
                                                                                                          toDateUtc));

                    IEnumerable <Place> places = _entities.Query <Place>()
                                                 .Where(p => p.IsCountry || p.IsWater || p.IsEarth);
                    foreach (var place in places)
                    {
                        int peopleCount =
                            _queries.Execute(
                                new PeopleWithActivitiesCountByPlaceIdsEstablishmentId(new int[] { place.RevisionId },
                                                                                       establishmentId,
                                                                                       fromDateUtc,
                                                                                       toDateUtc,
                                                                                       false, /* include undated */
                                                                                       true /* include future */));

                        stats.PlaceCounts.Add(new ActivityViewStats.PlaceCount
                        {
                            PlaceId      = place.RevisionId,
                            CountryCode  = place.IsCountry ? place.GeoPlanetPlace.Country.Code : null,
                            OfficialName = place.OfficialName,
                            Count        = peopleCount
                        });

                        if (peopleCount > 0)
                        {
                            stats.CountOfPlaces += 1;
                        }
                    }

                    if ((settings != null) && settings.ActivityTypes.Any())
                    {
                        foreach (var type in settings.ActivityTypes)
                        {
                            int globalTypeCount = _queries.Execute(
                                new PeopleWithActivitiesCountByTypeIdEstablishmentId(type.Id,
                                                                                     establishmentId,
                                                                                     fromDateUtc,
                                                                                     toDateUtc,
                                                                                     false, /* include undated */
                                                                                     true /* include future */));

                            var typeCount = stats.TypeCounts.SingleOrDefault(c => c.TypeId == type.Id);
                            if (typeCount != null)
                            {
                                typeCount.Count += globalTypeCount;
                            }
                            else
                            {
                                typeCount = new ActivityViewStats.TypeCount
                                {
                                    TypeId = type.Id,
                                    Type   = type.Type,
                                    Count  = globalTypeCount,
                                    Rank   = type.Rank
                                };

                                stats.TypeCounts.Add(typeCount);
                            }
                        }

                        stats.TypeCounts = stats.TypeCounts.OrderBy(t => t.Rank).ToList();
                    }

                    _globalPeopleDictionary.Remove(establishmentId);
                    _globalPeopleDictionary.Add(establishmentId, stats);
                }

                _viewManager.Set <Dictionary <int, GlobalPeopleCountView> >(_globalPeopleDictionary);
            }
#if DEBUG
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
#endif
            finally
            {
                StatsRwlock.ExitWriteLock();
                Debug.WriteLine(DateTime.Now + " Activity stats done building.");
            }

#if DEBUG
            buildTimer.Stop();
            Debug.WriteLine(DateTime.Now + " Activity views build time: " + buildTimer.Elapsed.TotalMinutes + " mins.");
#endif
        }