/// <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();
            }
        }
예제 #2
0
        private string GetSmartSignalResultItemPresentation()
        {
            var signalResult = new SmartSignalResultItemPresentation(
                id: "someId",
                title: "someTitle",
                summary: new SmartSignalResultItemPresentationSummary(
                    "3980",
                    "Maximum Request Count for the application",
                    new SmartSignalResultItemPresentationProperty("Bar Chart", "Perf | where TimeGenerated >= ago(1h) | where CounterName == \'% Processor Time\'|", ResultItemPresentationSection.Chart, string.Empty)),
                resourceId: "/subscriptions/b4b7d4c1-8c25-4da3-bf1c-e50f647a8130/resourceGroups/asafst/providers/Microsoft.Insights/components/deepinsightsdailyreports",
                correlationHash: "93e9a62b1e1a0dca5d9d63cc7e9aae71edb9988aa6f1dfc3b85e71b0f57d2819",
                signalId: "SampleSignal",
                signalName: "SampleSignal",
                analysisTimestamp: DateTime.UtcNow,
                analysisWindowSizeInMinutes: 5,
                properties: new List <SmartSignalResultItemPresentationProperty>(),
                rawProperties: new Dictionary <string, string>(),
                queryRunInfo: null);

            return(JsonConvert.SerializeObject(signalResult));
        }
        private SmartSignalResultItemPresentation CreatePresentation(SmartSignalResultItem resultItem, bool nullQueryRunInfo = false)
        {
            SmartSignalResultItemQueryRunInfo queryRunInfo = null;

            if (!nullQueryRunInfo)
            {
                queryRunInfo = new SmartSignalResultItemQueryRunInfo(
                    resultItem.ResourceIdentifier.ResourceType == ResourceType.ApplicationInsights ? TelemetryDbType.ApplicationInsights : TelemetryDbType.LogAnalytics,
                    new List <string>()
                {
                    "resourceId1", "resourceId2"
                });
            }

            DateTime lastExecutionTime = DateTime.Now.Date.AddDays(-1);
            string   resourceId        = "resourceId";
            var      request           = new SmartSignalRequest(new List <string>()
            {
                resourceId
            }, "signalId", lastExecutionTime, TimeSpan.FromDays(1), new SmartSignalSettings());

            return(SmartSignalResultItemPresentation.CreateFromResultItem(request, SignalName, resultItem, queryRunInfo));
        }
        /// <summary>
        /// Loads the signal, runs it, and returns the generated result presentations
        /// </summary>
        /// <param name="request">The signal request</param>
        /// <param name="cancellationToken">The cancellation token</param>
        /// <returns>A <see cref="Task{TResult}"/>, returning the list of Smart Signal result item presentations generated by the signal</returns>
        public async Task <List <SmartSignalResultItemPresentation> > RunAsync(SmartSignalRequest request, CancellationToken cancellationToken)
        {
            // Read the signal's package
            this.tracer.TraceInformation($"Loading signal package for signal ID {request.SignalId}");
            SmartSignalPackage signalPackage = await this.smartSignalRepository.ReadSignalPackageAsync(request.SignalId, cancellationToken);

            SmartSignalManifest signalManifest = signalPackage.Manifest;

            this.tracer.TraceInformation($"Read signal package, ID {signalManifest.Id}, Version {signalManifest.Version}");

            // Load the signal
            ISmartSignal signal = this.smartSignalLoader.LoadSignal(signalPackage);

            this.tracer.TraceInformation($"Signal instance loaded successfully, ID {signalManifest.Id}");

            // Get the resources on which to run the signal
            List <ResourceIdentifier> resources = await this.GetResourcesForSignal(request.ResourceIds, signalManifest, cancellationToken);

            // Run the signal
            this.tracer.TraceInformation($"Started running signal ID {signalManifest.Id}, Name {signalManifest.Name}");
            SmartSignalResult signalResult;

            try
            {
                var analysisRequest = new AnalysisRequest(resources, request.LastExecutionTime, request.Cadence, this.analysisServicesFactory);
                signalResult = await signal.AnalyzeResourcesAsync(analysisRequest, this.tracer, cancellationToken);

                this.tracer.TraceInformation($"Completed running signal ID {signalManifest.Id}, Name {signalManifest.Name}, returning {signalResult.ResultItems.Count} result items");
            }
            catch (Exception e)
            {
                this.tracer.TraceInformation($"Failed running signal ID {signalManifest.Id}, Name {signalManifest.Name}: {e.Message}");
                throw new SmartSignalCustomException(e.GetType().ToString(), e.Message, e.StackTrace);
            }

            // Verify that each result item belongs to one of the types declared in the signal manifest
            foreach (SmartSignalResultItem resultItem in signalResult.ResultItems)
            {
                if (!signalManifest.SupportedResourceTypes.Contains(resultItem.ResourceIdentifier.ResourceType))
                {
                    throw new UnidentifiedResultItemResourceTypeException(resultItem.ResourceIdentifier);
                }
            }

            // Trace the number of result items of each type
            foreach (var resultItemType in signalResult.ResultItems.GroupBy(x => x.GetType().Name))
            {
                this.tracer.TraceInformation($"Got {resultItemType.Count()} Smart Signal result items of type '{resultItemType.Key}'");
                this.tracer.ReportMetric("SignalResultItemType", resultItemType.Count(), new Dictionary <string, string>()
                {
                    { "ResultItemType", resultItemType.Key }
                });
            }

            // Create results
            List <SmartSignalResultItemPresentation> results = new List <SmartSignalResultItemPresentation>();

            foreach (var resultItem in signalResult.ResultItems)
            {
                SmartSignalResultItemQueryRunInfo queryRunInfo = await this.queryRunInfoProvider.GetQueryRunInfoAsync(new List <ResourceIdentifier>() { resultItem.ResourceIdentifier }, cancellationToken);

                results.Add(SmartSignalResultItemPresentation.CreateFromResultItem(request, signalManifest.Name, resultItem, queryRunInfo));
            }

            this.tracer.TraceInformation($"Returning {results.Count} results");
            return(results);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="SignalResultItem"/> class
 /// </summary>
 /// <param name="smartSignalResultItemPresentation">The signal result item presentation object</param>
 /// <param name="resourceIdentifier">The signal result's resource identifier</param>
 public SignalResultItem(SmartSignalResultItemPresentation smartSignalResultItemPresentation, ResourceIdentifier resourceIdentifier)
 {
     this.ResultItemPresentation = smartSignalResultItemPresentation;
     this.ResourceIdentifier     = resourceIdentifier;
 }