summaryrefslogtreecommitdiffstats
path: root/src/Model.php
blob: a21aebebeade0783d623b9741ec563741c538eb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php

namespace Drupal\ml_engine;

use Drupal\Component\Utility\Xss;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\ml_engine\MLEngineBase;

class Model extends MLEngineBase{

  public $config;

  public function __construct() {
      parent::__construct();
      $this->config = \Drupal::configFactory()->getEditable('ml_engine.test.model');
  }

  public static function create(ContainerInterface $container) {
     return new static();
  }

  public function UpdateModelList() {
      $service = $this->create_service();
      $response = $service->projects_models->listProjectsModels($this->project_name);
      $models = $response->__get('models');

      $models_array = [];
      
      for ($i=0; $i<count($models); $i++){
          $models_array[$i] = (array) $models[$i];
      }

      $this->config->clear('list')->save();
      $this->config->set('list',$models_array)->save();
      return $models_array;
  }

  public function delete($name){
      $model_full_name = $this->project_name."/models/".$name;
      $service = $this->create_service();

      try{
        $response = $service->projects_models->delete($model_full_name);
        $response = (array) $response;
        $response['message'] = "Successfully deleted model ". $name;

        return array( "success" => 1, "response" => $response );
      }catch (\Google_Service_Exception $ex){
        $error = json_decode($ex->getMessage(), true)['error'];
        return array( "success" => 0, "response" => $error);
      }
  }

  public function get($name){
      $model_full_name = $this->project_name."/models/".$name;
      $service = $this->create_service();

      try{
        $response = $service->projects_models->get($model_full_name);
        $response = (array) $response;
        $response['message'] = "Successfully got model ". $name;

        return array( "success" => 1, "response" => $response );
      }catch (\Google_Service_Exception $ex){
        $error = json_decode($ex->getMessage(), true)['error'];
        return array( "success" => 0, "response" => $error);
      }
  }

  private function createModelObject(array $para){
      $model = new \Google_Service_CloudMachineLearningEngine_GoogleCloudMlV1Model();
      $model->setName($para['name']);
      $model->setDescription($para['description']);
      $model->setRegions($para['region']);
      return $model;
  }

  public function ModelCreate(array $para){
      $model = $this->createModelObject($para);
      $service = $this->create_service();

      try{
        $response = $service->projects_models->create($this->project_name,$model);
        $response = (array) $response;
        $response['message'] = "Successfully created model ". $para['name'];
        
        return array( "success" => 1, "response" => $response );
      }catch (\Google_Service_Exception $ex){
        $error = json_decode($ex->getMessage(), true)['error'];
        return array( "success" => 0, "response" => $error);
      }
  }

}