private async Task <DetectionResult> detectAsync(AnomalyDetectorClient client, string datasource, Guid model_id, DateTimeOffset start_time, DateTimeOffset end_time)
        {
            try
            {
                Console.WriteLine("Start detect...");
                Response <Model> get_response = await client.GetMultivariateModelAsync(model_id).ConfigureAwait(false);

                DetectionRequest detectionRequest = new DetectionRequest(datasource, start_time, end_time);
                Response         result_response  = await client.DetectAnomalyAsync(model_id, detectionRequest).ConfigureAwait(false);

                var  ok        = result_response.Headers.TryGetValue("Location", out string result_id_path);
                Guid result_id = Guid.Parse(result_id_path.Split('/').LastOrDefault());
                // get detection result
                Response <DetectionResult> result = await client.GetDetectionResultAsync(result_id).ConfigureAwait(false);

                while (result.Value.Summary.Status != DetectionStatus.Ready & result.Value.Summary.Status != DetectionStatus.Failed)
                {
                    System.Threading.Thread.Sleep(2000);
                    result = await client.GetDetectionResultAsync(result_id).ConfigureAwait(false);
                }

                if (result.Value.Summary.Status != DetectionStatus.Ready)
                {
                    Console.WriteLine(String.Format("Inference failed."));
                    IReadOnlyList <ErrorResponse> errors = result.Value.Summary.Errors;
                    foreach (ErrorResponse error in errors)
                    {
                        Console.WriteLine(String.Format("Error code: {0}.", error.Code));
                        Console.WriteLine(String.Format("Error message: {0}.", error.Message));
                    }
                    return(null);
                }

                return(result.Value);
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Detection error. {0}", e.Message));
                throw new Exception(e.Message);
            }
        }
Beispiel #2
0
        private async Task <DetectionResult> DetectAsync(AnomalyDetectorClient client, string datasource, Guid model_id, DateTimeOffset start_time, DateTimeOffset end_time, int max_tryout = 500)
        {
            try
            {
                Console.WriteLine("Start detect...");
                Response <Model> get_response = await client.GetMultivariateModelAsync(model_id).ConfigureAwait(false);

                DetectionRequest detectionRequest = new DetectionRequest(datasource, start_time, end_time);
                Response         result_response  = await client.DetectAnomalyAsync(model_id, detectionRequest).ConfigureAwait(false);

                var  ok        = result_response.Headers.TryGetValue("Location", out string result_id_path);
                Guid result_id = Guid.Parse(result_id_path.Split('/').LastOrDefault());
                // get detection result
                Response <DetectionResult> result = await client.GetDetectionResultAsync(result_id).ConfigureAwait(false);

                int tryout_count = 0;
                while (result.Value.Summary.Status != DetectionStatus.Ready & tryout_count < max_tryout)
                {
                    System.Threading.Thread.Sleep(2000);
                    result = await client.GetDetectionResultAsync(result_id).ConfigureAwait(false);

                    tryout_count += 1;
                }

                if (result.Value.Summary.Status != DetectionStatus.Ready)
                {
                    Console.WriteLine(String.Format("Request timeout after {0} tryouts", max_tryout));
                    return(null);
                }

                return(result.Value);
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Detection error. {0}", e.Message));
                throw;
            }
        }