Exemplo n.º 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);
            }
        }