protected TimeSeriesAggregationBase(AggregationType type, string name)
        {
            Aggregation = type;
            Name        = name ?? Aggregation.ToString();

            _values = new List <double>();
        }
Exemple #2
0
        private IWithMetricsQueryExecute CreateMetricsQuery(AggregationType metricAggregation, TimeSpan metricsInterval, string metricFilter, string metricDimension,
                                                            int?metricLimit, IMetricDefinition metricDefinition, DateTime recordDateTime)
        {
            var historyStartingFromInHours = _azureMonitorIntegrationConfiguration.Value.History.StartingFromInHours;
            var metricQuery = metricDefinition.DefineQuery()
                              .StartingFrom(recordDateTime.AddHours(-historyStartingFromInHours))
                              .EndsBefore(recordDateTime)
                              .WithAggregation(metricAggregation.ToString())
                              .WithInterval(metricsInterval);

            var queryLimit = metricLimit ?? Defaults.MetricDefaults.Limit;

            if (string.IsNullOrWhiteSpace(metricFilter) == false)
            {
                var filter = metricFilter.Replace("/", "%2F");
                metricQuery.WithOdataFilter(filter);
                metricQuery.SelectTop(queryLimit);
            }

            if (string.IsNullOrWhiteSpace(metricDimension) == false)
            {
                metricQuery.WithOdataFilter($"{metricDimension} eq '*'");
                metricQuery.SelectTop(queryLimit);
            }

            return(metricQuery);
        }
Exemple #3
0
        public static async Task <List <Measurement> > GetMetricAsync(
            this IMonitorManagementClient monitorManagementClient,
            string resourceUri,
            MetricName metricName,
            DateTime startTimeUtc, DateTime endTimeUtc,
            TimeSpan samplingInterval,
            AggregationType aggregation,
            CancellationToken cancellationToken = default)
        {
            Contract.Requires(aggregation != AggregationType.None);

            // Unfortunately, the Azure Metrics API is pretty bad and lacking documentation, so we do our best here to
            // get things working. However, API may fail anyways if an invalid set of sampling frequency vs time length
            // is input.
            var startTime = startTimeUtc.ToString("o").Replace("+", "%2b");
            var endTime   = endTimeUtc.ToString("o").Replace("+", "%2b");

            return(await RetryPolicy.ExecuteAsync(async() =>
            {
                var result = await monitorManagementClient.Metrics.ListAsync(
                    resourceUri: resourceUri,
                    metricnames: metricName,
                    timespan: $"{startTime}/{endTime}",
                    interval: samplingInterval,
                    aggregation: aggregation.ToString(),
                    cancellationToken: cancellationToken);

                var metric = result.Value[0];
                var extract = ExtractMetric[aggregation];
                var timeSeries = metric.Timeseries[0];

                return timeSeries.Data.Select(metricValue => new Measurement(metricValue.TimeStamp, extract(metricValue))).ToList();
            }, cancellationToken));
        }
Exemple #4
0
        private IWithMetricsQueryExecute CreateMetricsQuery(AggregationType metricAggregation, TimeSpan metricsInterval, string metricFilter,
                                                            IMetricDefinition metricDefinition, DateTime recordDateTime)
        {
            var metricQuery = metricDefinition.DefineQuery()
                              .StartingFrom(recordDateTime.AddDays(-5))
                              .EndsBefore(recordDateTime)
                              .WithAggregation(metricAggregation.ToString())
                              .WithInterval(metricsInterval);

            if (string.IsNullOrWhiteSpace(metricFilter) == false)
            {
                metricQuery.WithOdataFilter(metricFilter);
            }

            return(metricQuery);
        }
Exemple #5
0
        public async Task <object> GetAggregatedPositionsAsync(string isin, DateTime date, AggregationType aggregationType)
        {
            Func <IEnumerable <Position>, IEnumerable <PortfolioPositionAggregatedDto> > aggregationFunction;

            switch (aggregationType)
            {
            case AggregationType.Country:
                aggregationFunction = AggregatePositionByCountry;
                break;

            case AggregationType.Currency:
                aggregationFunction = AggregatePositionByCurrency;
                break;

            case AggregationType.Type:
                aggregationFunction = AggregatePositionByType;
                break;

            default:
                aggregationFunction = AggregatePositionByCurrency;
                break;
            }

            var aggregatedPositions = await portfolioRepository.GetFirstOrDefaultAsync(
                predicate : p => p.ISIN == isin && p.Date == date.Date,
                include : source => source.Include(p => p.Positions),
                selector : portfolio => aggregationFunction(portfolio.Positions).ToList());

            JArray array = new JArray();

            aggregatedPositions.ForEach(t => array.Add($"{t.Key}: {t.Value}"));

            JObject typeResult = new JObject();

            typeResult[aggregationType.ToString()] = array;

            string json = typeResult.ToString();

            return(json);
        }
Exemple #6
0
        private async Task <double> QueryAzureMonitorAsync(MonitorManagementClient monitoringClient, string resourceId, string queueName, string metricName, AggregationType metricAggregation)
        {
            var metricDefinitions = await monitoringClient.MetricDefinitions.ListAsync(resourceId);

            if (metricDefinitions.FirstOrDefault(
                    metric => string.Equals(metric.Name.Value, metricName, StringComparison.InvariantCultureIgnoreCase)) == null)
            {
                Console.WriteLine(value: "Invalid metric");
            }

            var odataFilterMetrics = new ODataQuery <MetadataValue>($"EntityName eq '{queueName}'");

            var metrics = await monitoringClient.Metrics.ListAsync(resourceId, metricnames : metricName, odataQuery : odataFilterMetrics, aggregation : metricAggregation.ToString(), interval : TimeSpan.FromMinutes(value: 1));

            var foundMetric = metrics.Value.FirstOrDefault();

            if (foundMetric == null)
            {
                throw new ScrapingException(metricName, $"No metric was found for '{metricName}'");
            }

            var metricData = ExtractMostRecentMetricData(foundMetric, metricAggregation);

            return(metricData);
        }
Exemple #7
0
        /*
         * <propertyDescription name="Microsoft.SDKSample.DirectoryLevel" formatID="{581CF603-2925-4acf-BB5A-3D3EB39EACD3}" propID="3">
         *  <description>Number of directory levels to this item.</description>
         *  <searchInfo inInvertedIndex="false" isColumn="false,
         *  <typeInfo canStackBy="false" type="Int32"/>
         *  <labelInfo label="Directory Level"/>
         * </propertyDescription>
         */
        internal XmlElement GetXmlPropertyDescription(XmlDocument doc)
        {
            var desc = doc.CreateElement("propertyDescription");

            desc.SetAttribute("name", CanonicalName);
            desc.SetAttribute("formatID", FormatId.ToString("B").ToUpper());
            desc.SetAttribute("propID", PropertyId.ToString());

            var search = doc.CreateElement("searchInfo");

            search.SetAttribute("inInvertedIndex", InInvertedIndex.ToString());
            if (IsColumn)
            {
                search.SetAttribute("isColumn", IsColumn.ToString());
                search.SetAttribute("isColumnSparse", IsColumnSparse.ToString());
                search.SetAttribute("columnIndexType", ColumnIndexType.ToString());
            }
            if (MaxSize != 512)
            {
                search.SetAttribute("maxSize", MaxSize.ToString());
            }
            if (Mnemonics != null && Mnemonics.Length > 0)
            {
                search.SetAttribute("mnemonics", Mnemonics);
            }
            desc.AppendChild(search);

            var label = doc.CreateElement("labelInfo");

            label.SetAttribute("label", DisplayName);
            label.SetAttribute("sortDescription", SortDescription.ToString());
            if (EditInvitation != null && EditInvitation.Length > 0)
            {
                label.SetAttribute("invitationText", EditInvitation);
            }
            if (HideLabel)
            {
                label.SetAttribute("hideLabel", HideLabel.ToString());
            }
            desc.AppendChild(label);

            var type = doc.CreateElement("typeInfo");

            type.SetAttribute("type", Type.ToString());
            if (GroupingRange != PropertyGroupingRange.Discrete)
            {
                type.SetAttribute("groupingRange", GroupingRange.ToString());
            }
            if (IsInnate)
            {
                type.SetAttribute("isInnate", IsInnate.ToString());
            }
            if (IsInnate && CanBePurged)
            {
                type.SetAttribute("canBePurged", CanBePurged.ToString());
            }
            //if (MultipleValues)
            type.SetAttribute("multipleValues", MultipleValues.ToString());
            if (IsGroup)
            {
                type.SetAttribute("isGroup", IsGroup.ToString());
            }
            if (AggregationType != PropertyAggregationType.Default)
            {
                type.SetAttribute("aggregationType", AggregationType.ToString());
            }
            if (IsTreeProperty)
            {
                type.SetAttribute("isTreeProperty", IsTreeProperty.ToString());
            }
            if (IsViewable)
            {
                type.SetAttribute("isViewable", IsViewable.ToString());
            }
            if (SearchRawValue)
            {
                type.SetAttribute("searchRawValue", SearchRawValue.ToString());
            }
            if (ConditionType != PropertyConditionType.String)
            {
                type.SetAttribute("conditionType", ConditionType.ToString());
            }
            if (ConditionOperation != ConditionOperationConfigured.Equal)
            {
                type.SetAttribute("defaultOperation", ConditionOperation.ToString());
            }
            desc.AppendChild(type);

            var display = doc.CreateElement("displayInfo");

            display.SetAttribute("displayType", DisplayType.ToString());
            if (DisplayType == PropertyDisplayType.String && StringFormat != StringFormat.General)
            {
                var format = doc.CreateElement("stringFormat");
                format.SetAttribute("formatAs", StringFormat.ToString());
                display.AppendChild(format);
            }
            if (DisplayType == PropertyDisplayType.Boolean && BooleanFormat != BooleanFormat.YesNo)
            {
                var format = doc.CreateElement("booleanFormat");
                format.SetAttribute("formatAs", BooleanFormat.ToString());
                display.AppendChild(format);
            }
            if (DisplayType == PropertyDisplayType.Number && NumberFormat != NumberFormat.General)
            {
                var format = doc.CreateElement("numberFormat");
                format.SetAttribute("formatAs", NumberFormat.ToString());
                display.AppendChild(format);
            }
            if (DisplayType == PropertyDisplayType.DateTime && DateTimeFormat != DateTimeFormat.General)
            {
                var format = doc.CreateElement("dateTimeFormat");
                format.SetAttribute("formatAs", DateTimeFormat.ToString());
                display.AppendChild(format);
            }



            if (DefaultColumnWidth != 20)
            {
                display.SetAttribute("defaultColumnWidth", DefaultColumnWidth.ToString());
            }
            if (Alignment != PropertyAlignmentType.Left)
            {
                display.SetAttribute("alignment", Alignment.ToString());
            }
            //if (RelativeDescriptionType != RelativeDescriptionType.General)
            //  display.SetAttribute("relativeDescriptionType", RelativeDescriptionType.ToString());
            if (DefaultSortDirection != SortDirection.Ascending)
            {
                display.SetAttribute("defaultSortDirection", DefaultSortDirection.ToString());
            }
            if (EditControl != EditControl.Default)
            {
                var edit = doc.CreateElement("editControl");
                edit.SetAttribute("control", EditControl.ToString());
                display.AppendChild(edit);
            }
            desc.AppendChild(display);

            return(desc);
        }