protected async Task <IActionResult> GetSystemInvoker(TResource resource, string detectorId, string invokerId, string dataSource, string timeRange)
        {
            Dictionary <string, dynamic> systemContext = PrepareSystemContext(resource, detectorId, dataSource, timeRange);

            await this._sourceWatcherService.Watcher.WaitForFirstCompletion();

            var dataProviders = new DataProviders.DataProviders((DataProviderContext)this.HttpContext.Items[HostConstants.DataProviderContextKey]);
            var invoker       = this._invokerCache.GetSystemInvoker(invokerId);

            if (invoker == null)
            {
                return(null);
            }

            Response res = new Response
            {
                Metadata = invoker.EntryPointDefinitionAttribute
            };

            var response = (Response)await invoker.Invoke(new object[] { dataProviders, systemContext, res });

            List <DataProviderMetadata> dataProvidersMetadata = GetDataProvidersMetadata(dataProviders);

            return(response == null ? (IActionResult)NotFound() : Ok(DiagnosticApiResponse.FromCsxResponse(response, dataProvidersMetadata)));
        }
Esempio n. 2
0
        private DiagnosticApiResponse CreateQueryExceptionResponse(Exception ex, Definition detectorDefinition, bool isInternal, List <DataProviderMetadata> dataProvidersMetadata)
        {
            Response response = new Response()
            {
                Metadata = RemovePIIFromDefinition(detectorDefinition, isInternal)
            };

            response.AddMarkdownView($"<pre><code>{ex.ToString()}</code></pre>", "Detector Runtime Exception");
            return(DiagnosticApiResponse.FromCsxResponse(response, dataProvidersMetadata));
        }
Esempio n. 3
0
        protected async Task <IActionResult> GetDetector(TResource resource, string detectorId, string startTime, string endTime, string timeGrain)
        {
            if (!DateTimeHelper.PrepareStartEndTimeWithTimeGrain(startTime, endTime, timeGrain, out DateTime startTimeUtc, out DateTime endTimeUtc, out TimeSpan timeGrainTimeSpan, out string errorMessage))
            {
                return(BadRequest(errorMessage));
            }

            OperationContext <TResource> cxt = PrepareContext(resource, startTimeUtc, endTimeUtc);
            var detectorResponse             = await GetDetectorInternal(detectorId, cxt);

            return(detectorResponse == null ? (IActionResult)NotFound() : Ok(DiagnosticApiResponse.FromCsxResponse(detectorResponse.Item1, detectorResponse.Item2)));
        }
Esempio n. 4
0
        protected async Task <IActionResult> ExecuteQuery <TPostBodyResource>(TResource resource, CompilationBostBody <TPostBodyResource> jsonBody, string startTime, string endTime, string timeGrain)
        {
            if (jsonBody == null)
            {
                return(BadRequest("Missing body"));
            }

            if (string.IsNullOrWhiteSpace(jsonBody.Script))
            {
                return(BadRequest("Missing script in body"));
            }

            if (!DateTimeHelper.PrepareStartEndTimeWithTimeGrain(startTime, endTime, timeGrain, out DateTime startTimeUtc, out DateTime endTimeUtc, out TimeSpan timeGrainTimeSpan, out string errorMessage))
            {
                return(BadRequest(errorMessage));
            }

            await this._sourceWatcherService.Watcher.WaitForFirstCompletion();

            EntityMetadata metaData      = new EntityMetadata(jsonBody.Script);
            var            dataProviders = new DataProviders.DataProviders(_dataSourcesConfigService.Config);

            QueryResponse <DiagnosticApiResponse> queryRes = new QueryResponse <DiagnosticApiResponse>
            {
                InvocationOutput = new DiagnosticApiResponse()
            };

            Assembly tempAsm = null;

            this.Request.Headers.TryGetValue(HeaderConstants.RequestIdHeaderName, out StringValues requestIds);
            var compilerResponse = await _compilerHostClient.GetCompilationResponse(jsonBody.Script, requestIds.FirstOrDefault() ?? string.Empty);

            queryRes.CompilationOutput = compilerResponse;

            if (queryRes.CompilationOutput.CompilationSucceeded)
            {
                byte[] asmData = Convert.FromBase64String(compilerResponse.AssemblyBytes);
                byte[] pdbData = Convert.FromBase64String(compilerResponse.PdbBytes);

                tempAsm = Assembly.Load(asmData, pdbData);

                using (var invoker = new EntityInvoker(metaData, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports()))
                {
                    invoker.InitializeEntryPoint(tempAsm);

                    // Verify Detector with other detectors in the system in case of conflicts
                    if (!VerifyEntity(invoker, ref queryRes))
                    {
                        return(Ok(queryRes));
                    }
                    OperationContext <TResource> cxt = PrepareContext(resource, startTimeUtc, endTimeUtc);
                    List <DataProviderMetadata>  dataProvidersMetadata = null;

                    try
                    {
                        var responseInput = new Response()
                        {
                            Metadata = RemovePIIFromDefinition(invoker.EntryPointDefinitionAttribute, cxt.IsInternalCall)
                        };
                        var invocationResponse = (Response)await invoker.Invoke(new object[] { dataProviders, cxt, responseInput });

                        invocationResponse.UpdateDetectorStatusFromInsights();

                        if (cxt.IsInternalCall)
                        {
                            dataProvidersMetadata = GetDataProvidersMetadata(dataProviders);
                        }

                        queryRes.RuntimeSucceeded = true;
                        queryRes.InvocationOutput = DiagnosticApiResponse.FromCsxResponse(invocationResponse, dataProvidersMetadata);
                    }
                    catch (Exception ex)
                    {
                        if (cxt.IsInternalCall)
                        {
                            queryRes.RuntimeSucceeded = false;
                            queryRes.InvocationOutput = CreateQueryExceptionResponse(ex, invoker.EntryPointDefinitionAttribute, cxt.IsInternalCall, GetDataProvidersMetadata(dataProviders));
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            }

            return(Ok(queryRes));
        }