Exemple #1
0
        private async Task <Guid?> TrainAsync(AnomalyDetectorClient client, string datasource, DateTimeOffset start_time, DateTimeOffset end_time, int max_tryout = 500)
        {
            try
            {
                Console.WriteLine("Training new model...");

                int model_number = await GetModelNumberAsync(client, false).ConfigureAwait(false);

                Console.WriteLine(String.Format("{0} available models before training.", model_number));

                ModelInfo data_feed       = new ModelInfo(datasource, start_time, end_time);
                Response  response_header = client.TrainMultivariateModel(data_feed);
                response_header.Headers.TryGetValue("Location", out string trained_model_id_path);
                Guid trained_model_id = Guid.Parse(trained_model_id_path.Split('/').LastOrDefault());
                Console.WriteLine(trained_model_id);

                // Wait until the model is ready. It usually takes several minutes
                Response <Model> get_response = await client.GetMultivariateModelAsync(trained_model_id).ConfigureAwait(false);

                ModelStatus?model_status = null;
                int         tryout_count = 0;
                while (tryout_count < max_tryout & model_status != ModelStatus.Ready)
                {
                    System.Threading.Thread.Sleep(10000);
                    get_response = await client.GetMultivariateModelAsync(trained_model_id).ConfigureAwait(false);

                    ModelInfo model_info = get_response.Value.ModelInfo;
                    Console.WriteLine(String.Format("model_id: {0}, createdTime: {1}, lastUpdateTime: {2}, status: {3}.", get_response.Value.ModelId, get_response.Value.CreatedTime, get_response.Value.LastUpdatedTime, model_info.Status));

                    if (model_info != null)
                    {
                        model_status = model_info.Status;
                    }
                    tryout_count += 1;
                }
                ;
                get_response = await client.GetMultivariateModelAsync(trained_model_id).ConfigureAwait(false);

                if (model_status != ModelStatus.Ready)
                {
                    Console.WriteLine(String.Format("Request timeout after {0} tryouts", max_tryout));
                }

                model_number = await GetModelNumberAsync(client).ConfigureAwait(false);

                Console.WriteLine(String.Format("{0} available models after training.", model_number));
                return(trained_model_id);
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Train error. {0}", e.Message));
                throw;
            }
        }
        private async Task <Guid?> trainAsync(AnomalyDetectorClient client, string datasource, DateTimeOffset start_time, DateTimeOffset end_time)
        {
            try
            {
                Console.WriteLine("Training new model...");

                int model_number = await getModelNumberAsync(client, false).ConfigureAwait(false);

                Console.WriteLine(String.Format("{0} available models before training.", model_number));

                ModelInfo data_feed       = new ModelInfo(datasource, start_time, end_time);
                Response  response_header = client.TrainMultivariateModel(data_feed);
                response_header.Headers.TryGetValue("Location", out string trained_model_id_path);
                Guid trained_model_id = Guid.Parse(trained_model_id_path.Split('/').LastOrDefault());
                Console.WriteLine(trained_model_id);

                // Wait until the model is ready. It usually takes several minutes
                Response <Model> get_response = await client.GetMultivariateModelAsync(trained_model_id).ConfigureAwait(false);

                while (get_response.Value.ModelInfo.Status != ModelStatus.Ready & get_response.Value.ModelInfo.Status != ModelStatus.Failed)
                {
                    System.Threading.Thread.Sleep(10000);
                    get_response = await client.GetMultivariateModelAsync(trained_model_id).ConfigureAwait(false);

                    Console.WriteLine(String.Format("model_id: {0}, createdTime: {1}, lastUpdateTime: {2}, status: {3}.", get_response.Value.ModelId, get_response.Value.CreatedTime, get_response.Value.LastUpdatedTime, get_response.Value.ModelInfo.Status));
                }

                if (get_response.Value.ModelInfo.Status != ModelStatus.Ready)
                {
                    Console.WriteLine(String.Format("Trainig failed."));
                    IReadOnlyList <ErrorResponse> errors = get_response.Value.ModelInfo.Errors;
                    foreach (ErrorResponse error in errors)
                    {
                        Console.WriteLine(String.Format("Error code: {0}.", error.Code));
                        Console.WriteLine(String.Format("Error message: {0}.", error.Message));
                    }
                    throw new Exception("Training failed.");
                }

                model_number = await getModelNumberAsync(client).ConfigureAwait(false);

                Console.WriteLine(String.Format("{0} available models after training.", model_number));
                return(trained_model_id);
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Train error. {0}", e.Message));
                throw new Exception(e.Message);
            }
        }
        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);
            }
        }
Exemple #4
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;
            }
        }