예제 #1
0
        private IEnumerable <IAggregatedEvent> FetchAllTimeSeriesAggregatedEvents(TimeSeries timeSeries)
        {
            List <AggregatedEvent> result = new List <AggregatedEvent>();
            var query = new TableQuery <TimeSeriesValueEntity>().Where(
                TableQuery.GenerateFilterCondition(
                    nameof(TimeSeriesValueEntity.RowKey),
                    QueryComparisons.Equal,
                    timeSeries.ToString())
                );
            IEnumerable <TimeSeriesValueEntity> entities = _cacheTable.ExecuteQuery(query);

            foreach (var entity in entities)
            {
                AggregatedEvent aggregatedEvent = new AggregatedEvent()
                {
                    AggregatedEventSum    = entity.TimeSeriesTotal,
                    AggregationTimeSeries = entity.TimeSeries,
                    Id = entity.ArtistId,
                    AggregatedEventSumSource = entity.TimeSeriesValues.Select(x => new AggregatedEventSource()
                    {
                        Timestamp = x.Timestamp, Value = x.Value
                    })
                };
                result.Add(aggregatedEvent);
            }
            return(result);
        }
        public async Task <List <ItemCount> > GetSongPlayedOverTime(AggregateTimeRange timeRange, int artistWorkId)
        {
            string           localStorageKey = LOCALSTORAGEKEY_ARTISTWORKPLAYEDOVERTIME + artistWorkId + "-" + timeRange;
            List <ItemCount> result;
            TimeCachedObject <List <ItemCount> > cachedObject = await _localStorageService.GetItemAsync <TimeCachedObject <List <ItemCount> > >(localStorageKey);


            if (cachedObject == null || cachedObject.NextUpdateHour < DateTimeOffset.UtcNow)
            {
                ArtistWorkAggregatedEventsRequest request = new ArtistWorkAggregatedEventsRequest()
                {
                    ArtistWorkIds = new List <int>()
                    {
                        artistWorkId
                    }, TimeSeries = timeRange
                };
                HttpResponseMessage responseMessage = await _httpClient.PostAsJsonAsync(_endpointAddress + "ArtistWorkAggregatedEvents", request);

                string responseBody = await responseMessage.Content.ReadAsStringAsync();

                AggregatedEvent  temp  = JsonConvert.DeserializeObject <List <AggregatedEvent> >(responseBody)?.FirstOrDefault();
                List <ItemCount> items = temp.AggregatedEventSumSource.Select((x, i) => new ItemCount()
                {
                    Count = x.Value, ItemId = artistWorkId, Name = x.Timestamp.ToString()
                }).ToList();
                Dictionary <string, (ItemCount itemCount, DateTimeOffset sortOrder)> buckets = GetItemCountBuckets(timeRange, artistWorkId);
                foreach (var item in items)
                {
                    if (buckets.ContainsKey(item.Name))
                    {
                        buckets[item.Name].itemCount.Count = item.Count;
                    }
                }
                result = buckets.Values.OrderBy(x => x.sortOrder).Select(x => x.itemCount).ToList();
                DateTimeOffset nextUpdate = TimeCachedObject <object> .CalculateNextUpdateHour();

                cachedObject = new TimeCachedObject <List <ItemCount> >()
                {
                    CachedObject   = result,
                    NextUpdateHour = nextUpdate
                };
                await _localStorageService.SetItemAsync(localStorageKey, cachedObject);
            }
            else
            {
                result = cachedObject.CachedObject;
            }
            return(result);
        }
예제 #3
0
        public async Task <IAggregatedEvent> FetchTimeSeriesAggregatedEventAsync(int artistId, TimeSeries timeSeries)
        {
            var operation = TableOperation.Retrieve <TimeSeriesValueEntity>(artistId.ToString(), timeSeries.ToString());
            TimeSeriesValueEntity entity          = (TimeSeriesValueEntity)(await _cacheTable.ExecuteAsync(operation)).Result;
            AggregatedEvent       aggregatedEvent = new AggregatedEvent()
            {
                AggregatedEventSum    = entity.TimeSeriesTotal,
                AggregationTimeSeries = entity.TimeSeries,
                Id = entity.ArtistId,
                AggregatedEventSumSource = entity.TimeSeriesValues.Select(x => new AggregatedEventSource()
                {
                    Timestamp = x.Timestamp, Value = x.Value
                })
            };

            return(aggregatedEvent);
        }
예제 #4
0
        /// <summary>
        /// Publish an event
        /// </summary>
        /// <typeparam name="TEventType"></typeparam>
        /// <param name="eventToPublish"></param>
        public void PublishEvent <TEventType>(TEventType eventToPublish)
        {
            if (eventToPublish is AggregatedEvent)
            {
                AggregatedEvent aggEvent = eventToPublish as AggregatedEvent;
                if (aggEvent.IsHandled)
                {
                    return;
                }
            }

            var subsriberType = typeof(ISubscriber <>).MakeGenericType(typeof(TEventType));

            var subscribers = GetSubscriberList(subsriberType);

            List <WeakReference> subsribersToBeRemoved = new List <WeakReference>();

            foreach (var weakSubsriber in subscribers)
            {
                if (weakSubsriber.IsAlive)
                {
                    var subscriber = (ISubscriber <TEventType>)weakSubsriber.Target;

                    InvokeSubscriberEvent <TEventType>(eventToPublish, subscriber);
                }
                else
                {
                    subsribersToBeRemoved.Add(weakSubsriber);
                } //End-if-else (weakSubsriber.IsAlive)
            }     //End-for-each (var weakSubriber in subscribers)


            if (subsribersToBeRemoved.Any())
            {
                lock (lockSubscriberDictionary)
                {
                    foreach (var remove in subsribersToBeRemoved)
                    {
                        subscribers.Remove(remove);
                    } //End-for-each (var remove in subsribersToBeRemoved)
                }     //End-lock (lockSubscriberDictionary)
            }         //End-if (subsribersToBeRemoved.Any())
        }
예제 #5
0
        private void InvokeSubscriberEvent <TEventType>(TEventType eventToPublish, ISubscriber <TEventType> subscriber)
        {
            //Synchronize the invocation of method

            SynchronizationContext syncContext = SynchronizationContext.Current;

            if (syncContext == null)
            {
                syncContext = new SynchronizationContext();
            }//End-if (syncContext == null)

            if (eventToPublish is AggregatedEvent)
            {
                AggregatedEvent aggEvent = eventToPublish as AggregatedEvent;
                if (aggEvent.IsHandled)
                {
                    return;
                }
            }

            subscriber.OnEventHandler(eventToPublish);

            //syncContext.Post(s => subscriber.OnEventHandler(eventToPublish), null);
        }
        public IEnumerable <IAggregatedEvent> FetchArtistArtistWorkAggregatedEvents(int artistId, TimeSeries timeSeries)
        {
            //artistId == TimeSeriesValueEntity.ArtistId && timeSeries == TimeSeriesValueEntity.TimeSeries
            var query = new TableQuery <TimeSeriesValueEntity>().Where(
                TableQuery.CombineFilters(
                    TableQuery.GenerateFilterConditionForInt(
                        nameof(TimeSeriesValueEntity.ArtistId),
                        QueryComparisons.Equal,
                        artistId),
                    TableOperators.And,
                    TableQuery.GenerateFilterCondition(
                        nameof(TimeSeriesValueEntity.TimeSeries),
                        QueryComparisons.Equal,
                        timeSeries.ToString())));

            IEnumerable <TimeSeriesValueEntity> timeSeriesValueEntities =
                _cacheTable.ExecuteQuery(query);

            List <AggregatedEvent> aggregatedEvents = new List <AggregatedEvent>();

            foreach (var entity in timeSeriesValueEntities)
            {
                AggregatedEvent aggregatedEvent = new AggregatedEvent()
                {
                    AggregatedEventSum    = entity.TimeSeriesTotal,
                    AggregationTimeSeries = entity.TimeSeries,
                    Id = entity.ArtistWorkId,
                    AggregatedEventSumSource = entity.TimeSeriesValues.Select(x => new AggregatedEventSource()
                    {
                        Timestamp = x.Timestamp, Value = x.Value
                    })
                };
                aggregatedEvents.Add(aggregatedEvent);
            }
            return(aggregatedEvents);
        }