/// <summary>
 /// Initializes a new instance of the <see cref="PeerStatistics" /> class.
 /// </summary>
 /// <param name="maxLoggedEvents">The maximum number of logged events.</param>
 /// <param name="peerEndPoint">The peer end point.</param>
 public PeerStatistics(int maxLoggedEvents, IPEndPoint peerEndPoint)
 {
     this.PeerEndPoint = peerEndPoint;
     this.LatestEvents = new ConcurrentFixedSizeQueue <string>(maxLoggedEvents);
 }
Example #2
0
        public async Task RunGenerationAsync(DateTime?from, DateTime?to)
        {
            _keepWorking = true;
            _ctSource    = new CancellationTokenSource();

            var source = new TaskCompletionSource <bool>();

            _completionTask = source.Task;

            try
            {
                _frames = new ConcurrentFixedSizeQueue <List <HeatPoint> >(int.MaxValue);

                ProtectOriginalMetadataSet();

                if (from.HasValue)
                {
                    _metadataSet.FromDate = from;
                }
                if (to.HasValue)
                {
                    _metadataSet.ToDate = to;
                }

                await CountTagsTotalAsync();

                if (this.TotalCountProp.Value == 0)
                {
                    _lastImage = await GetFirstImageForMetadatasetAssetAsync(from);

                    ImageProp.Value = _lastImage.Data;
                    PertcantageProcessedUpdated?.Invoke(100);
                }

                var batchSize  = 100;
                var batchesNum = Math.Ceiling((double)TotalCountProp.Value / batchSize);
                batchesNum = Math.Min(batchesNum, _settings.NumberOfBatchesToProcess);

                var hyperIds = new List <HyperId>();

                for (int i = 0; i < batchesNum; i++)
                {
                    StatusProp.Value = $"Processing batch {(i + 1)} out of {batchesNum}...";

                    if (!_keepWorking || _ct.IsCancellationRequested)
                    {
                        return;
                    }

                    var skip = batchSize * i;

                    var findArgs = new FindHyperDocumentsArgs(typeof(HyperTag))
                    {
                        Skip  = skip,
                        Limit = batchSize
                    };

                    var conditions = await MetaDataSetHelper.GenerateFilterFromMetaDataSetAsync(_hyperStore, _metadataSet);

                    findArgs.DescriptorConditions.AddCondition(conditions.Result);

                    //hyper tag ids condition
                    var hyperTagIdscondition = new MultiScopeCondition(AndOr.Or);
                    foreach (var id in _metadataSet.HyperTagIds ?? new HyperDocumentId[] { })
                    {
                        hyperTagIdscondition.AddCondition("_id", id.Id);
                    }
                    if (hyperTagIdscondition.ConditionsCount > 0)
                    {
                        findArgs.DocumentConditions.AddCondition(hyperTagIdscondition);
                    }

                    var tagDocs = await _hyperStore.ExecuteAsync(findArgs) ?? new HyperDocument[0];

                    if (!_keepWorking || _ct.IsCancellationRequested)
                    {
                        return;
                    }

                    var tags            = tagDocs.Select(it => it.GetPayload <HyperTag>()).ToArray();
                    var grouppedBySlice = tags.GroupBy(it =>
                    {
                        var item = it.GetElement <IHyperTagHyperIds>();
                        return(item.HyperId);
                    }).ToArray();

                    if (!_keepWorking || _ct.IsCancellationRequested)
                    {
                        return;
                    }

                    if (_width == 0 || _height == 0)
                    {
                        var hyperId = grouppedBySlice.First().Key;

                        await UpdateImageDataAsync(hyperId);

                        _globalMatrix = new uint[_height, _width];

                        ImageProp.Value = _lastImage.Data;

                        if (!_keepWorking || _ct.IsCancellationRequested)
                        {
                            return;
                        }
                    }

                    grouppedBySlice = grouppedBySlice.OrderBy(x => x.Key).ToArray();

                    // Group by asset first?

                    var groupedByFragment = grouppedBySlice.GroupBy(x => x.Key.FragmentId.Value).ToArray();

                    foreach (var group in groupedByFragment)
                    {
                        var sliceGroupsOfFragment = group.ToArray();

                        foreach (var sliceTags in sliceGroupsOfFragment)
                        {
                            await _pauseResetEvent.WaitAsync();

                            var framePoints = new List <HeatPoint>();

                            foreach (var tag in sliceTags)
                            {
                                // TODO: Reintegrate RealImage processing logic
                                if (_settings.RenderingMode == RenderingMode.Masks || _settings.RenderingMode == RenderingMode.RealImage)
                                {
                                    var tagExtraction = ProcessMaskedTag(tag);
                                    if (tagExtraction != null)
                                    {
                                        ProcessMaskOverlapsIntoMatrix(tagExtraction.Image, tagExtraction.Rect);
                                    }
                                }
                                else if (_settings.RenderingMode == RenderingMode.LowerPointOfGeometry)
                                {
                                    ProcessGeometryTag(tag, framePoints);
                                }
                            }

                            _frames.Enqueue(framePoints);
                        }

                        if (!_keepWorking || _ct.IsCancellationRequested)
                        {
                            return;
                        }
                    }

                    _lastHyperId = grouppedBySlice.Last().Key;

                    await RerenderAsync();

                    var tagDocsCount = tagDocs.Count();
                    ItemsProcessed.Value  += tagDocsCount;
                    PertcantageLabel.Value = ((double)100 * ItemsProcessed.Value / TotalCountProp.Value).ToString("0.00", System.Globalization.CultureInfo.InvariantCulture) + "%";
                    PertcantageProcessedUpdated?.Invoke(100 * tagDocsCount / (double)TotalCountProp.Value);
                }
            }
            catch (Exception ex)
            {
                Logger.Instance.Error(this.GetType(), nameof(RunGenerationAsync), ex.Message);
            }
            finally
            {
                source.SetResult(true);
            }
        }