예제 #1
0
        public Task <SmartSignalResult> AnalyzeResourcesAsync(AnalysisRequest analysisRequest, ITracer tracer, CancellationToken cancellationToken)
        {
            SmartSignalResult smartSignalResult = new SmartSignalResult();

            smartSignalResult.ResultItems.Add(new TestSignalResultItem("test title", analysisRequest.TargetResources.First()));
            return(Task.FromResult(smartSignalResult));
        }
            public Task <SmartSignalResult> AnalyzeResourcesAsync(AnalysisRequest analysisRequest, ITracer tracer, CancellationToken cancellationToken)
            {
                SmartSignalResult smartSignalResult = new SmartSignalResult();

                smartSignalResult.ResultItems.Add(new TestResultItem(typeof(T).Name, analysisRequest.TargetResources.Single()));
                return(Task.FromResult(smartSignalResult));
            }
        private async Task TestLoadSignalSimple(Type signalType, string expectedTitle = "test test test")
        {
            ISmartSignalLoader  loader   = new SmartSignalLoader(this.tracerMock.Object);
            SmartSignalManifest manifest = new SmartSignalManifest("3", "simple", "description", Version.Parse("1.0"), signalType.Assembly.GetName().Name, signalType.FullName, new List <ResourceType>()
            {
                ResourceType.Subscription
            }, new List <int> {
                60
            });
            SmartSignalPackage package = new SmartSignalPackage(manifest, this.assemblies["3"]);
            ISmartSignal       signal  = loader.LoadSignal(package);

            Assert.IsNotNull(signal, "Signal is NULL");

            var resource        = new ResourceIdentifier(ResourceType.VirtualMachine, "someSubscription", "someGroup", "someVM");
            var analysisRequest = new AnalysisRequest(
                new List <ResourceIdentifier> {
                resource
            },
                DateTime.UtcNow.AddDays(-1),
                TimeSpan.FromDays(1),
                new Mock <IAnalysisServicesFactory>().Object);
            SmartSignalResult signalResult = await signal.AnalyzeResourcesAsync(analysisRequest, this.tracerMock.Object, default(CancellationToken));

            Assert.AreEqual(1, signalResult.ResultItems.Count, "Incorrect number of result items returned");
            Assert.AreEqual(expectedTitle, signalResult.ResultItems.Single().Title, "Result item title is wrong");
            Assert.AreEqual(resource, signalResult.ResultItems.Single().ResourceIdentifier, "Result item resource identifier is wrong");
        }
예제 #4
0
        public Task <SmartSignalResult> AnalyzeResourcesAsync(AnalysisRequest analysisRequest, ITracer tracer, CancellationToken cancellationToken)
        {
            int[]             obj               = { 1, 2, 3 };
            var               dependent         = new DependentClass();
            SmartSignalResult smartSignalResult = new SmartSignalResult();

            smartSignalResult.ResultItems.Add(new TestSignalResultItem(
                                                  "test title - " + dependent.GetString() + " - " + dependent.ObjectToString(obj),
                                                  analysisRequest.TargetResources.First()));
            return(Task.FromResult(smartSignalResult));
        }
        /// <summary>
        /// Runs the smart signal.
        /// </summary>
        /// <param name="resources">The resources which the signal should run on.</param>
        /// <param name="analysisCadence">The analysis cadence.</param>
        /// <returns>A task that runs the smart signal.</returns>
        public async Task RunAsync(List <ResourceIdentifier> resources, TimeSpan analysisCadence)
        {
            this.cancellationTokenSource = new CancellationTokenSource();
            this.Results.Clear();
            var analysisRequest = new AnalysisRequest(resources, null, analysisCadence, this.analysisServicesFactory);

            try
            {
                // Run Signal
                this.IsSignalRunning = true;

                SmartSignalResult signalResults = await this.smartSignal.AnalyzeResourcesAsync(
                    analysisRequest,
                    this.Tracer,
                    this.cancellationTokenSource.Token);

                // Create signal result items
                List <SignalResultItem> signalResultItems = new List <SignalResultItem>();
                foreach (var resultItem in signalResults.ResultItems)
                {
                    // Create result item presentation
                    var resourceIds          = resources.Select(resource => resource.ResourceName).ToList();
                    var smartSignalsSettings = new SmartSignalSettings();
                    var smartSignalRequest   = new SmartSignalRequest(resourceIds, this.smartSignalManifes.Id, null, analysisCadence, smartSignalsSettings);
                    SmartSignalResultItemQueryRunInfo queryRunInfo = await this.queryRunInfoProvider.GetQueryRunInfoAsync(new List <ResourceIdentifier>() { resultItem.ResourceIdentifier }, this.cancellationTokenSource.Token);

                    SmartSignalResultItemPresentation resultItemPresentation = SmartSignalResultItemPresentation.CreateFromResultItem(
                        smartSignalRequest, this.smartSignalManifes.Name, resultItem, queryRunInfo);

                    // Create Azure resource identifier
                    ResourceIdentifier resourceIdentifier = ResourceIdentifier.CreateFromResourceId(resultItemPresentation.ResourceId);

                    signalResultItems.Add(new SignalResultItem(resultItemPresentation, resourceIdentifier));
                }

                this.Results = new ObservableCollection <SignalResultItem>(signalResultItems);
                this.tracer.TraceInformation($"Found {this.Results.Count} results");
            }
            catch (OperationCanceledException)
            {
                this.Tracer.TraceError("Signal run was canceled.");
            }
            catch (Exception e)
            {
                this.Tracer.ReportException(e);
            }
            finally
            {
                this.IsSignalRunning = false;
                this.cancellationTokenSource?.Dispose();
            }
        }
예제 #6
0
        /// <summary>
        /// The method runs the sample analysis calls
        /// </summary>
        /// <param name="analysisRequest">The analysis request object</param>
        /// <param name="tracer">used to save trace messages</param>
        /// <param name="cancellationToken">The cancellation token</param>
        /// <returns>The smart signal result containing the result items</returns>
        public async Task <SmartSignalResult> AnalyzeResourcesAsync(AnalysisRequest analysisRequest, ITracer tracer, CancellationToken cancellationToken)
        {
            SmartSignalResult smartSignalResult = new SmartSignalResult();

            // 1. Fetch from Application Insights client - name of an application with maximum request in the last 24h
            ITelemetryDataClient applicationInsightsDataClient = await analysisRequest.AnalysisServicesFactory.CreateApplicationInsightsTelemetryDataClientAsync(
                analysisRequest.TargetResources, cancellationToken);

            IList <DataTable> applicationInsightsResult = await applicationInsightsDataClient.RunQueryAsync(HighestAppApplicationInsightsQuery, cancellationToken);

            long   countReqByAppName = 0;
            string appName           = "N/A";

            if (applicationInsightsResult.Count > 0)
            {
                countReqByAppName = Convert.ToInt64(applicationInsightsResult[0].Rows[0]["countReqByAppName"]);
                appName           = Convert.ToString(applicationInsightsResult[0].Rows[0]["appName"]);
                tracer.TraceInformation($"App {appName} has high request count of {countReqByAppName}");
            }
            else
            {
                tracer.TraceError("Failed to perform the query in Application Insights");
            }

            // 2. Fetch from Log Analytics - time of highest CPU in the last 24h
            ITelemetryDataClient logAnalyticsDataClient = await analysisRequest.AnalysisServicesFactory.CreateLogAnalyticsTelemetryDataClientAsync(
                analysisRequest.TargetResources, cancellationToken);

            IList <DataTable> logAnalyticsResult = await logAnalyticsDataClient.RunQueryAsync(MaximumProcessorTimeLogAnalyticsQuery, cancellationToken);

            double   highestProcessorTimePercent = 0;
            DateTime timeOfHighestProcessorTime  = new DateTime();

            if (logAnalyticsResult.Count > 0)
            {
                highestProcessorTimePercent = Convert.ToDouble(logAnalyticsResult[0].Rows[0]["CounterValue"]);
                timeOfHighestProcessorTime  = Convert.ToDateTime(logAnalyticsResult[0].Rows[0]["TimeGenerated"]);
                tracer.TraceInformation($"The highest value of % Processor Time {highestProcessorTimePercent} appeared at {timeOfHighestProcessorTime}");
            }
            else
            {
                tracer.TraceError("Failed to perform the query in Log Analytics");
            }

            analysisRequest.TargetResources.ForEach(resourceIdentifier => smartSignalResult.ResultItems.Add(
                                                        new HighRequestCountSignalResultItem("High Processing Time Percentage(LA) and Request Count(AI)", appName, countReqByAppName, highestProcessorTimePercent, timeOfHighestProcessorTime, resourceIdentifier)));
            return(smartSignalResult);
        }
        private async Task TestLoadSignalFromDll(string signalId, string expectedTitle)
        {
            ISmartSignalLoader loader  = new SmartSignalLoader(this.tracerMock.Object);
            SmartSignalPackage package = new SmartSignalPackage(this.manifests[signalId], this.assemblies[signalId]);
            ISmartSignal       signal  = loader.LoadSignal(package);

            Assert.IsNotNull(signal, "Signal is NULL");

            var resource        = new ResourceIdentifier(ResourceType.VirtualMachine, "someSubscription", "someGroup", "someVM");
            var analysisRequest = new AnalysisRequest(
                new List <ResourceIdentifier> {
                resource
            },
                DateTime.UtcNow.AddDays(-1),
                TimeSpan.FromDays(1),
                new Mock <IAnalysisServicesFactory>().Object);
            SmartSignalResult signalResult = await signal.AnalyzeResourcesAsync(analysisRequest, this.tracerMock.Object, default(CancellationToken));

            Assert.AreEqual(1, signalResult.ResultItems.Count, "Incorrect number of result items returned");
            Assert.AreEqual(expectedTitle, signalResult.ResultItems.Single().Title, "Result item title is wrong");
            Assert.AreEqual(resource, signalResult.ResultItems.Single().ResourceIdentifier, "Result item resource identifier is wrong");
        }
            public async Task <SmartSignalResult> AnalyzeResourcesAsync(AnalysisRequest analysisRequest, ITracer tracer, CancellationToken cancellationToken)
            {
                this.IsRunning = true;

                Assert.IsNotNull(analysisRequest.TargetResources, "Resources list is null");
                Assert.AreEqual(1, analysisRequest.TargetResources.Count);
                Assert.AreEqual(this.ExpectedResourceType, analysisRequest.TargetResources.Single().ResourceType);

                if (this.ShouldStuck)
                {
                    try
                    {
                        await Task.Delay(int.MaxValue, cancellationToken);
                    }
                    catch (TaskCanceledException)
                    {
                        this.WasCanceled = true;
                        throw;
                    }
                }

                if (this.ShouldThrow)
                {
                    throw new DivideByZeroException();
                }

                if (this.ShouldThrowCustom)
                {
                    throw new CustomException();
                }

                SmartSignalResult smartSignalResult = new SmartSignalResult();

                smartSignalResult.ResultItems.Add(new TestSignalResultItem(analysisRequest.TargetResources.First()));
                return(await Task.FromResult(smartSignalResult));
            }