summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorboaloysius2017-06-21 09:17:36 +0530
committerboaloysius2017-06-21 09:17:36 +0530
commit3564195c0c792b22626aa085079401da49230456 (patch)
treec0de59bbc6b55366aa2785e472dc6a930ebe3dae
parentefba21f280993fb797dae942081b237451beaca5 (diff)
Made major archetecture change. Added jobr, project etc services
-rw-r--r--ml_engine.links.action.yml5
-rw-r--r--ml_engine.links.menu.yml2
-rw-r--r--ml_engine.links.task.yml12
-rw-r--r--ml_engine.routing.yml32
-rw-r--r--ml_engine.services.yml16
-rw-r--r--src/Controller/JobController.php99
-rw-r--r--src/Form/TestCredential copy.php126
-rw-r--r--src/Form/TestCredential.php95
-rw-r--r--src/Form/TestJobCancel.php84
-rw-r--r--src/Form/TestJobCreate.php190
-rw-r--r--src/Form/TestJobGet.php76
-rw-r--r--src/Form/TestJobList.php114
-rw-r--r--src/Form/TestPredict.php87
-rw-r--r--src/Job.php110
-rw-r--r--src/MLEngineBase.php33
-rw-r--r--src/MLEngineService.php37
-rw-r--r--src/Predict.php32
-rw-r--r--src/Project.php63
18 files changed, 714 insertions, 499 deletions
diff --git a/ml_engine.links.action.yml b/ml_engine.links.action.yml
new file mode 100644
index 0000000..2010793
--- /dev/null
+++ b/ml_engine.links.action.yml
@@ -0,0 +1,5 @@
+ml_engine.feed_add:
+ route_name: ml_engine.test.job.list.refresh
+ title: 'Refresh'
+ appears_on:
+ - 'ml_engine.test.job.list'
diff --git a/ml_engine.links.menu.yml b/ml_engine.links.menu.yml
index ed37e5f..81456aa 100644
--- a/ml_engine.links.menu.yml
+++ b/ml_engine.links.menu.yml
@@ -2,7 +2,7 @@ ml_engine.settings:
title: 'Google ML Engine Settings'
parent: system.admin_config_media
description: 'Configure Google ML Engine.'
- route_name: ml_engine.settings
+ route_name: ml_engine.settings.form
ml_engine.test:
title: 'Google ML Engine Test'
diff --git a/ml_engine.links.task.yml b/ml_engine.links.task.yml
index 5523b8f..7bc1d87 100644
--- a/ml_engine.links.task.yml
+++ b/ml_engine.links.task.yml
@@ -4,22 +4,22 @@ ml_engine.test:
title: 'Credential'
ml_engine.test.prediction:
- route_name: ml_engine.test.prediction
+ route_name: ml_engine.test.prediction.form
base_route: ml_engine.test
title: 'Predict'
ml_engine.test.job:
- route_name: ml_engine.test.job.create
+ route_name: ml_engine.test.job.create.form
base_route: ml_engine.test
title: 'Job'
ml_engine.test.job.create:
- route_name: ml_engine.test.job.create
+ route_name: ml_engine.test.job.create.form
parent_id: ml_engine.test.job
title: 'Create'
ml_engine.test.job.get:
- route_name: ml_engine.test.job.get
+ route_name: ml_engine.test.job.get.form
parent_id: ml_engine.test.job
title: 'Get'
@@ -28,7 +28,7 @@ ml_engine.test.job.list:
parent_id: ml_engine.test.job
title: 'List'
-ml_engine.test.job.cancel:
- route_name: ml_engine.test.job.cancel
+ml_engine.test.job.cancel.form:
+ route_name: ml_engine.test.job.cancel.form
parent_id: ml_engine.test.job
title: 'Cancel' \ No newline at end of file
diff --git a/ml_engine.routing.yml b/ml_engine.routing.yml
index 28de93d..02a5d5b 100644
--- a/ml_engine.routing.yml
+++ b/ml_engine.routing.yml
@@ -1,4 +1,4 @@
-ml_engine.settings:
+ml_engine.settings.form:
path: '/admin/config/media/ml-engine'
defaults:
_form: 'Drupal\ml_engine\Form\SettingsForm'
@@ -15,7 +15,7 @@ ml_engine.test:
requirements:
_permission: 'administer ml_engine'
-ml_engine.test.prediction:
+ml_engine.test.prediction.form:
path: '/admin/config/media/ml_engine/test/predict'
defaults:
_title: 'ML Engine - Prediction Test'
@@ -33,7 +33,7 @@ ml_engine.test.job:
requirements:
_permission: 'administer ml_engine'
-ml_engine.test.job.create:
+ml_engine.test.job.create.form:
path: '/admin/config/media/ml_engine/test/job/create'
defaults:
_title: 'ML Engine - Job Create'
@@ -42,7 +42,7 @@ ml_engine.test.job.create:
requirements:
_permission: 'administer ml_engine'
-ml_engine.test.job.get:
+ml_engine.test.job.get.form:
path: '/admin/config/media/ml_engine/test/job/get'
defaults:
_title: 'ML Engine - Job Get'
@@ -56,15 +56,33 @@ ml_engine.test.job.list:
defaults:
_title: 'ML Engine - Job List'
_description: 'Try Job'
- _form: '\Drupal\ml_engine\Form\TestJobList'
+ _controller: '\Drupal\ml_engine\Controller\JobController::overview'
requirements:
_permission: 'administer ml_engine'
-ml_engine.test.job.cancel:
+ml_engine.test.job.list.refresh:
+ path: '/admin/config/media/ml_engine/test/job/list/refresh'
+ defaults:
+ _title: 'ML Engine - Job List Refresh'
+ _description: 'Try Job'
+ _controller: '\Drupal\ml_engine\Controller\JobController::refresh_overview_page'
+ requirements:
+ _permission: 'administer ml_engine'
+
+ml_engine.test.job.cancel.form:
path: '/admin/config/media/ml_engine/test/job/cancel'
defaults:
_title: 'ML Engine - Job Cancel'
_description: 'Try Job'
_form: '\Drupal\ml_engine\Form\TestJobCancel'
requirements:
- _permission: 'administer ml_engine' \ No newline at end of file
+ _permission: 'administer ml_engine'
+
+ml_engine.test.job.cancel:
+ path: '/admin/config/media/ml_engine/test/job/cancel/{job}'
+ defaults:
+ _title: 'ML Engine - Job Cancel'
+ _description: 'Try Job'
+ _controller: '\Drupal\ml_engine\Controller\JobController::cancelList'
+ requirements:
+ _permission: 'administer ml_engine'
diff --git a/ml_engine.services.yml b/ml_engine.services.yml
new file mode 100644
index 0000000..06b307c
--- /dev/null
+++ b/ml_engine.services.yml
@@ -0,0 +1,16 @@
+services:
+ ml_engine.job:
+ class: Drupal\ml_engine\Job
+ arguments: []
+
+ ml_engine.project:
+ class: Drupal\ml_engine\project
+ arguments: []
+
+ ml_engine.predict:
+ class: Drupal\ml_engine\predict
+ arguments: []
+
+ ml_engine.cloud_service:
+ class: Drupal\ml_engine\MLEngineService
+ arguments: []
diff --git a/src/Controller/JobController.php b/src/Controller/JobController.php
new file mode 100644
index 0000000..5666936
--- /dev/null
+++ b/src/Controller/JobController.php
@@ -0,0 +1,99 @@
+<?php
+
+namespace Drupal\ml_engine\Controller;
+
+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;
+
+/**
+ * Returns responses for aggregator module routes.
+ */
+class JobController extends ControllerBase {
+
+ public function __construct() {}
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function create(ContainerInterface $container) {
+ return new static();
+ }
+
+ public function get_config(){
+ return \Drupal::configFactory()->getEditable('ml_engine.test.job');
+ }
+
+ public function refresh_overview_page() {
+ \Drupal::service('ml_engine.job')->UpdateJobList();
+ drupal_set_message("Job list refreshed", "status");
+ $url = new RedirectResponse('../list');
+ $url->send();
+ }
+
+
+ public function overview() {
+ $jobs_array = $this->get_config()->get('list');
+ $header = [$this->t('Job Name'), $this->t('Status'), $this->t('Create Time'), $this->t('End Time'), $this->t('Operations')];
+ $rows = [];
+
+ if(!$jobs_array){
+ $jobs_array = [];
+ }
+
+ foreach ($jobs_array as $job) {
+ $row = [];
+ $row[] = $job['jobId'];
+ $row[] = $job['state'];
+ $row[] = $job['createTime'];
+ $row[] = $job['endTime'];
+ $links = [];
+
+ // There are eight training states, refer to
+ // https://cloud.google.com/ml-engine/reference/rest/v1/projects.jobs#State
+
+ $training_states = array('QUEUED','PREPARING','RUNNING');
+
+ if(in_array($job['state'],$training_states)){
+ $links['edit'] = [
+ 'title' => $this->t('Cancel'),
+ 'url' => Url::fromRoute('ml_engine.test.job.cancel', ['job' => $job['jobId']])
+ ];
+ }
+
+ $row[] = [
+ 'data' => [
+ '#type' => 'operations',
+ '#links' => $links,
+ ],
+ ];
+ $rows[] = $row;
+ }
+ $build['feeds'] = [
+ '#prefix' => '<h3>' . $this->t('Jobs overview') . '</h3>',
+ '#type' => 'table',
+ '#header' => $header,
+ '#rows' => $rows,
+ '#empty' => $this->t('No jobs available.'),
+ ];
+
+ return $build;
+ }
+
+ public function cancelList($job){
+ $response = \Drupal::service('ml_engine.job')->cancel($job);
+ if($response['success']){
+ drupal_set_message('Job Cancelled Successfully', 'status');
+ }else{
+ drupal_set_message($response['response']['message'], 'error');
+ }
+ \Drupal::service('ml_engine.job')->UpdateJobList();
+ $url = new RedirectResponse('../list');
+ $url->send();
+ }
+
+
+}
diff --git a/src/Form/TestCredential copy.php b/src/Form/TestCredential copy.php
new file mode 100644
index 0000000..7fe9e2d
--- /dev/null
+++ b/src/Form/TestCredential copy.php
@@ -0,0 +1,126 @@
+<?php
+
+/**
+ * @file
+ * Contains Drupal\slack\Form\SendTestMessageForm.
+ */
+
+namespace Drupal\ml_engine\Form;
+
+use Drupal\Core\Form\FormBase;
+use Drupal\Core\Form\FormStateInterface;
+
+
+/**
+ * Class SendTestMessageForm.
+ *
+ * @package Drupal\slack\Form
+ */
+class TestCredential extends FormBase {
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getFormId() {
+ return 'ml_engine_get_test_prediction';
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function buildForm(array $form, FormStateInterface $form_state) {
+
+ $config = \Drupal::configFactory()->getEditable('ml_engine.test');
+
+ $form['project'] = array(
+ '#type' => 'textfield',
+ '#title' => $this->t('Project name'),
+ '#required' => TRUE,
+ '#default_value' => $config->get('project')
+ );
+
+ $form['credential'] = array(
+ '#type' => 'textarea',
+ '#title' => $this->t('Credential'),
+ '#required' => TRUE,
+ '#default_value' => $config->get('credential'),
+ '#rows' => 25,
+ );
+
+ $form['actions']['submit'] = array(
+ '#type' => 'submit',
+ '#value' => $this->t('Save'),
+ '#button_type' => 'primary',
+ );
+
+ // Print credential verification statue.
+ if ($status = $config->get('status')){
+ $form['response'] = array(
+ '#type' => 'textfield',
+ '#title' => $this->t('Response'),
+ '#attributes' => array('readonly' => 'readonly'),
+ '#default_value' => $status,
+ '#weight' => 100,
+ );
+ }
+
+ // Print credential verification error.
+ if ($error = $config->get('error')){
+ $form['error'] = array(
+ '#type' => 'textarea',
+ '#title' => $this->t('Error'),
+ '#attributes' => array('readonly' => 'readonly'),
+ '#default_value' => $error,
+ '#rows' => 15,
+ '#weight' => 101,
+ );
+ }
+
+ return $form;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function submitForm(array &$form, FormStateInterface $form_state) {
+
+ $config = \Drupal::configFactory()->getEditable('ml_engine.test');
+ $config->delete();
+ $credential = $form_state->getValue('credential');
+ $project = $form_state->getValue('project');
+ $config ->set('credential',$credential) ->set('project',$project) ->save();
+
+ $credential_json = json_decode($credential,true);
+ $credential_json_required_keys = ['type', 'project_id', 'private_key_id', 'private_key',
+ 'client_email', 'client_id', 'auth_uri', 'token_uri',
+ 'auth_provider_x509_cert_url', 'client_x509_cert_url'];
+
+ $array_difference = array_diff($credential_json_required_keys, array_keys($credential_json));
+
+ if ($array_difference){
+ $message = t("Credential Keys [ '@keys' ] are missing",array('@keys'=>join("', '",$array_difference)));
+ drupal_set_message($message,'error');
+ $config ->set('status',"Verification Failed") ->set('error', $message)->save();
+ return;
+ }
+ $client = new \Google_Client();
+ $client->setAuthConfig($credential_json);
+ $client->addScope(\Google_Service_CloudMachineLearningEngine::CLOUD_PLATFORM);
+ $service = new \Google_Service_CloudMachineLearningEngine($client);
+
+ try{
+ $response = $service->projects->getConfig($project);
+ $config ->set('status', "Verification Successfull")->save();
+ drupal_set_message("Successfully set project and credential");
+ } catch (\DomainException $ex){
+ $config ->set('status',"Verification Failed") ->set('error', "Please check the private key")->save();
+ drupal_set_message($ex->getMessage(),'error');
+ return;
+ } catch (\Google_Service_Exception $ex){
+ $config ->set('status',"Verification Failed") ->set('error', $ex->getMessage())->save();
+ drupal_set_message($ex->getMessage(),'error');
+ return;
+ }
+ }
+
+}
diff --git a/src/Form/TestCredential.php b/src/Form/TestCredential.php
index 19cf86b..d0301b8 100644
--- a/src/Form/TestCredential.php
+++ b/src/Form/TestCredential.php
@@ -1,72 +1,58 @@
<?php
-/**
- * @file
- * Contains Drupal\slack\Form\SendTestMessageForm.
- */
-
namespace Drupal\ml_engine\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
-use \Firebase\JWT\JWT;
-
-/**
- * Class SendTestMessageForm.
- *
- * @package Drupal\slack\Form
- */
class TestCredential extends FormBase {
- /**
- * {@inheritdoc}
- */
+ public $config;
+
+ public function __construct(){
+ $this->config = \Drupal::configFactory()->getEditable('ml_engine.test.project');
+ }
+
public function getFormId() {
- return 'ml_engine_get_test_prediction';
+ return 'ml_engine_credential';
}
- /**
- * {@inheritdoc}
- */
public function buildForm(array $form, FormStateInterface $form_state) {
- $config = \Drupal::configFactory()->getEditable('ml_engine.test');
-
- $form['project'] = array(
+ $form['name'] = array(
'#type' => 'textfield',
'#title' => $this->t('Project name'),
'#required' => TRUE,
- '#default_value' => $config->get('project')
+ '#default_value' => $this->config->get('name')
);
$form['credential'] = array(
'#type' => 'textarea',
'#title' => $this->t('Credential'),
+ '#description' => t('Please paste the service account credential json of your Google Cloud Project.'),
'#required' => TRUE,
- '#default_value' => $config->get('credential'),
+ '#default_value' => $this->config->get('credential'),
'#rows' => 25,
);
$form['actions']['submit'] = array(
'#type' => 'submit',
- '#value' => $this->t('Save'),
+ '#value' => $this->t('Verify and Save'),
'#button_type' => 'primary',
);
- // Print credential verification statue.
- if ($status = $config->get('status')){
+ if ($response = $this->config->get('response')){
$form['response'] = array(
'#type' => 'textfield',
'#title' => $this->t('Response'),
'#attributes' => array('readonly' => 'readonly'),
- '#default_value' => $status,
+ '#default_value' => $response,
'#weight' => 100,
);
}
// Print credential verification error.
- if ($error = $config->get('error')){
+ if ($error = $this->config->get('error')){
$form['error'] = array(
'#type' => 'textarea',
'#title' => $this->t('Error'),
@@ -84,44 +70,27 @@ class TestCredential extends FormBase {
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
-
- $config = \Drupal::configFactory()->getEditable('ml_engine.test');
- $config->delete();
- $credential = $form_state->getValue('credential');
- $project = $form_state->getValue('project');
- $config ->set('credential',$credential) ->set('project',$project) ->save();
-
- $credential_json = json_decode($credential,true);
- $credential_json_required_keys = ['type', 'project_id', 'private_key_id', 'private_key',
- 'client_email', 'client_id', 'auth_uri', 'token_uri',
- 'auth_provider_x509_cert_url', 'client_x509_cert_url'];
-
- $array_difference = array_diff($credential_json_required_keys, array_keys($credential_json));
+ $this->config->delete();
+
+ $form_keys = ['credential', 'name'];
+ foreach ($form_keys as $key){
+ ${$key} = $form_state->getValue($key);
+ $this->config->set($key,${$key})->save();
+ }
- if ($array_difference){
- $message = t("Credential Keys [ '@keys' ] are missing",array('@keys'=>join("', '",$array_difference)));
- drupal_set_message($message,'error');
- $config ->set('status',"Verification Failed") ->set('error', $message)->save();
+ $credential = json_decode($credential,true);
+ $response = \Drupal::service('ml_engine.project')->verify_credential($name, $credential);
+
+ if($response['success']){
+ $this->config->set('response',(array) $response['response'])->save();
+ drupal_set_message('Succesfully Verified Credential', 'status');
return;
- }
- $client = new \Google_Client();
- $client->setAuthConfig($credential_json);
- $client->addScope(\Google_Service_CloudMachineLearningEngine::CLOUD_PLATFORM);
- $service = new \Google_Service_CloudMachineLearningEngine($client);
-
- try{
- $response = $service->projects->getConfig($project);
- $config ->set('status', "Verification Successfull")->save();
- drupal_set_message("Successfully set project and credential");
- } catch (\DomainException $ex){
- $config ->set('status',"Verification Failed") ->set('error', "Please check the private key")->save();
- drupal_set_message($ex->getMessage(),'error');
+ }else{
+ $this->config->set('error', (array) $response['response'])->save();
+ drupal_set_message($response['response']['message'], 'error');
return;
- } catch (\Google_Service_Exception $ex){
- $config ->set('status',"Verification Failed") ->set('error', $ex->getMessage())->save();
- drupal_set_message($ex->getMessage(),'error');
- return;
}
+
}
}
diff --git a/src/Form/TestJobCancel.php b/src/Form/TestJobCancel.php
index 60695ce..e3054e3 100644
--- a/src/Form/TestJobCancel.php
+++ b/src/Form/TestJobCancel.php
@@ -1,41 +1,31 @@
<?php
-/**
- * @file
- * Contains Drupal\slack\Form\SendTestMessageForm.
- */
-
namespace Drupal\ml_engine\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
+use Drupal\ml_engine\Controller\JobController;
-/**
- * Class SendTestMessageForm.
- *
- * @package Drupal\slack\Form
- */
class TestJobCancel extends FormBase {
- /**
- * {@inheritdoc}
- */
+ public $config;
+
+ public function __construct(){
+ $this->config = \Drupal::configFactory()->getEditable('ml_engine.test.job.cancel');
+ }
+
public function getFormId() {
- return 'ml_engine_get_test_prediction';
+ return 'ml_engine_job_cancel';
}
- /**
- * {@inheritdoc}
- */
public function buildForm(array $form, FormStateInterface $form_state) {
- $config = \Drupal::configFactory()->getEditable('ml_engine.test.job.cancel');
$form['job_name'] = array(
'#type' => 'textfield',
'#title' => $this->t('Job Name'),
'#required' => TRUE,
- '#default_value' => $config->get('job_name'),
+ '#default_value' => $this->config->get('job_name'),
);
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
@@ -45,7 +35,7 @@ class TestJobCancel extends FormBase {
);
// Print prediction response.
- if ($response = $config->get('response')){
+ if ($response = $this->config->get('response')){
$form['response'] = array(
'#type' => 'textarea',
'#title' => $this->t('Response'),
@@ -58,12 +48,12 @@ class TestJobCancel extends FormBase {
}
// Print prediction error.
- if ($error = $config->get('error')){
+ if ($error = $this->config->get('error')){
$form['error'] = array(
'#type' => 'textarea',
'#title' => $this->t('Error'),
'#attributes' => array('readonly' => 'readonly'),
- '#default_value' => $error,
+ '#default_value' => json_encode($error, JSON_PRETTY_PRINT),
'#rows' => 15,
'#weight' => 100
);
@@ -76,45 +66,23 @@ class TestJobCancel extends FormBase {
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
-
- $config = \Drupal::configFactory()->getEditable('ml_engine.test.job.cancel');
- $config->delete();
- $credential = \Drupal::configFactory()->getEditable('ml_engine.test')->get('credential');
-
- // Set config variables.
- $project_name = \Drupal::configFactory()->getEditable('ml_engine.test')->get('project');
- $job_name = $form_state->getValue('job_name');
- $config ->set('job_name', $job_name) ->save();
- $name = $project_name."/jobs/".$job_name;
- // Set parameters for prediction request.
- $credential_json = json_decode($credential, true);
-
- // Creting client and services.
- $client = new \Google_Client();
- $client->setAuthConfig($credential_json);
- $client->addScope(\Google_Service_CloudMachineLearningEngine::CLOUD_PLATFORM);
- $service = new \Google_Service_CloudMachineLearningEngine($client);
-
- // Get Job details.
- try{
- $response = $service->projects_jobs->cancel($name, new \Google_Service_CloudMachineLearningEngine_GoogleCloudMlV1CancelJobRequest());
- }catch (\Google_Service_Exception $ex){
- $error = json_decode($ex->getMessage(), true)['error'];
- $message = $error['message'];
- $code = $error['code'];
- $config->set('error',$message)->save();
- drupal_set_message($message,'error');
+ $this->config->delete();
+ $job = $form_state->getValue('job_name');
+ $this->config ->set('job_name', $job) ->save();
+
+ $response = \Drupal::service('ml_engine.job')->cancel($job);
+
+
+ if($response['success']){
+ $this->config->set('response','Job Cancelled')->save();
+ drupal_set_message('Job Cancelled Successfully', 'status');
return;
- }
- if ($response){
- $config->set('response', "Job Cancelled")->save();
}else{
- $config->set('error', (array) $response )->save();
+ $this->config->set('error', $response['response'])->save();
+ drupal_set_message($response['response']['message'], 'error');
+ return;
}
-
-
-
}
-
+
}
diff --git a/src/Form/TestJobCreate.php b/src/Form/TestJobCreate.php
index 2213ff4..51dd2ef 100644
--- a/src/Form/TestJobCreate.php
+++ b/src/Form/TestJobCreate.php
@@ -1,101 +1,99 @@
<?php
-/**
- * @file
- * Contains Drupal\slack\Form\SendTestMessageForm.
- */
-
namespace Drupal\ml_engine\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
-/**
- * Class SendTestMessageForm.
- *
- * @package Drupal\slack\Form
- */
+
class TestJobCreate extends FormBase {
- /**
- * {@inheritdoc}
- */
+
public function getFormId() {
- return 'ml_engine_get_test_prediction';
+ return 'ml_engine_job_create';
+ }
+
+ public function __construct(){
+ $this->config = \Drupal::configFactory()->getEditable('ml_engine.test.job.create');
}
- /**
- * {@inheritdoc}
- */
public function buildForm(array $form, FormStateInterface $form_state) {
- $config = \Drupal::configFactory()->getEditable('ml_engine.test.job.create');
- $form['job_name'] = array(
+ $form['job'] = array(
'#type' => 'textfield',
'#title' => $this->t('Job Name'),
'#required' => TRUE,
- '#default_value' => $config->get('job_name'),
+ '#default_value' => $this->config->get('job'),
);
$form['package_uri'] = array(
'#type' => 'textfield',
'#title' => $this->t('Package URI'),
'#required' => TRUE,
- '#default_value' => $config->get('package_uri'),
+ '#default_value' => $this->config->get('package_uri'),
);
$form['module'] = array(
'#type' => 'textfield',
'#title' => $this->t('Module'),
'#required' => TRUE,
- '#default_value' => $config->get('module'),
+ '#default_value' => $this->config->get('module'),
);
$form['train_data_uri'] = array(
'#type' => 'textfield',
'#title' => $this->t('Train Data URI'),
- '#required' => TRUE,
- '#default_value' => $config->get('train_data_uri'),
+ '#default_value' => $this->config->get('train_data_uri'),
);
$form['test_data_uri'] = array(
'#type' => 'textfield',
'#title' => $this->t('Test Data URI'),
- '#required' => TRUE,
- '#default_value' => $config->get('test_data_uri'),
+ '#default_value' => $this->config->get('test_data_uri'),
);
$form['train_steps'] = array(
- '#type' => 'textfield',
+ '#type' => 'number',
'#title' => $this->t('Train Steps'),
'#required' => TRUE,
- '#default_value' => $config->get('train_steps'),
+ '#default_value' => $this->config->get('train_steps'),
);
$form['verbosity'] = array(
'#type' => 'textfield',
'#title' => $this->t('Verbosity'),
- '#required' => TRUE,
- '#default_value' => $config->get('verbosity'),
+ '#default_value' => $this->config->get('verbosity'),
);
$form['job_dir'] = array(
'#type' => 'textfield',
'#title' => $this->t('Output Directory'),
'#required' => TRUE,
- '#default_value' => $config->get('job_dir'),
+ '#default_value' => $this->config->get('job_dir'),
);
$form['region'] = array(
- '#type' => 'textfield',
+ '#type' => 'select',
+ '#options' => array(
+ 'us-central1' => t('us-central1'),
+ 'us-east1' => t('us-east1'),
+ 'europe-west1' => t('europe-west1'),
+ 'asia-east1' => t('asia-east1')
+ ),
'#title' => $this->t('Region'),
'#required' => TRUE,
- '#default_value' => $config->get('region'),
+ '#default_value' => $this->config->get('region'),
);
+
$form['scale_tier'] = array(
- '#type' => 'textfield',
+ '#type' => 'select',
+ '#options' => array(
+ 'BASIC' => t('BASIC'),
+ 'STANDARD_1' => t('STANDARD_1'),
+ 'PREMIUM_1' => t('PREMIUM_1'),
+ 'BASIC_GPU' => t('BASIC_GPU')
+ ),
'#title' => $this->t('Scale Tier'),
'#required' => TRUE,
- '#default_value' => $config->get('scale_tier'),
+ '#default_value' => $this->config->get('scale_tier'),
);
$form['arguments'] = array(
'#type' => 'textarea',
'#title' => $this->t('Arguments'),
- '#required' => TRUE,
- '#default_value' => $config->get('arguments'),
+ '#default_value' => $this->config->get('arguments'),
'#rows' => 15,
);
$form['actions']['#type'] = 'actions';
@@ -106,24 +104,24 @@ class TestJobCreate extends FormBase {
);
// Print prediction response.
- if ($state = $config->get('state')){
+ if ($response = $this->config->get('response')){
$form['response'] = array(
'#type' => 'textarea',
'#title' => $this->t('Response'),
'#attributes' => array('readonly' => 'readonly'),
- '#default_value' => $state,
+ '#default_value' => json_encode($response,JSON_PRETTY_PRINT),
'#rows' => 15,
'#weight' => 100
);
}
// Print prediction error.
- if ($error = $config->get('error')){
+ if ($error = $this->config->get('error')){
$form['error'] = array(
'#type' => 'textarea',
'#title' => $this->t('Error'),
'#attributes' => array('readonly' => 'readonly'),
- '#default_value' => $error,
+ '#default_value' => json_encode($error,JSON_PRETTY_PRINT),
'#rows' => 15,
'#weight' => 100
);
@@ -132,103 +130,31 @@ class TestJobCreate extends FormBase {
return $form;
}
- /**
- * {@inheritdoc}
- */
public function submitForm(array &$form, FormStateInterface $form_state) {
-
- $config = \Drupal::configFactory()->getEditable('ml_engine.test.job.create');
- $config->delete();
- // Set config variables.
- $project_name = \Drupal::configFactory()->getEditable('ml_engine.test')->get('project');
- $job_name = $form_state->getValue('job_name');
-
- $package_uri = $form_state->getValue('package_uri');
- $module = $form_state->getValue('module');
-
- $job_dir = $form_state->getValue('job_dir');
- $region = $form_state->getValue('region');
- $scale_tier = $form_state->getValue('scale_tier');
- $credential = \Drupal::configFactory()->getEditable('ml_engine.test')->get('credential');
-
- $arguments = $form_state->getValue('arguments');
- $train_data_uri = $form_state->getValue('train_data_uri');
- $test_data_uri = $form_state->getValue('test_data_uri');
- $train_steps = $form_state->getValue('train_steps');
- $verbosity = $form_state->getValue('verbosity');
-
- $new_arguments = [
- '--train--files',
- $train_data_uri,
- '--eval-files',
- $test_data_uri,
- '--train-steps',
- $train_steps,
- 'verbosity',
- $verbosity
- ];
-
- $package_array = array($package_uri);
-
- $config
- ->set('job_name', $job_name)
- ->set('package_uri', $package_uri)
- ->set('module', $module)
- ->set('train_data_uri',$train_data_uri)
- ->set('test_data_uri',$test_data_uri)
- ->set('train_steps',$train_steps)
- ->set('verbosity',$verbosity)
- ->set('job_dir', $job_dir)
- ->set('region', $region)
- ->set('scale_tier', $scale_tier)
- ->set('arguments', $arguments)
- ->set('credential',$credential)
- ->save();
-
-
- // Set parameters for prediction request.
- $credential_json = json_decode($credential, true);
-
- // Creting client and services.
- $client = new \Google_Client();
- $client->setAuthConfig($credential_json);
- $client->addScope(\Google_Service_CloudMachineLearningEngine::CLOUD_PLATFORM);
- $service = new \Google_Service_CloudMachineLearningEngine($client);
+ $this->config->delete();
+
+ $keys = array('job','package_uri','module','train_data_uri',
+ 'test_data_uri', 'train_steps', 'verbosity','job_dir','region','scale_tier','arguments');
+
+ $jobPara = [];
- $input = new \Google_Service_CloudMachineLearningEngine_GoogleCloudMlV1TrainingInput();
- $input->setScaleTier($scale_tier);
- $input->setPackageUris($package_array);
- $input->setPythonModule($module);
- $input->setRegion($region);
- $input->setJobDir($job_dir);
- $input->setArgs($new_arguments);
-
- $job = new \Google_Service_CloudMachineLearningEngine_GoogleCloudMlV1Job();
- $job->setJobId($job_name);
- $job->setTrainingInput($input);
-
- // Send prediction request.
- try{
- $response = $service->projects_jobs->create($project_name,$job);
- } catch (\Google_Service_Exception $ex){
- $error = json_decode($ex->getMessage(), true)['error'];
- $message = $error['message'];
- $code = $error['code'];
- $config->set('error',$message)->save();
- drupal_set_message($message,'error');
- return;
+ foreach ($keys as $key) {
+ ${$key} = $form_state->getValue($key);
+ $jobPara[$key] = ${$key};
+ $this->config->set($key,${$key})->save();
}
- $error = $response->__get('error');
- $state = $response->getState();
- print "<pre>";
- print $state;
- print "</pre>";
+ $status = \Drupal::service('ml_engine.job')->JobCreate($jobPara);
-
- $config->set('state', $state)->save();
- $config->set('error', $error)->save();
+ if($status['success']){
+ drupal_set_message('Successfully created job '.$job, "status");
+ $response_job = (array) $status['response'];
+ $this->config->set('response', $response_job)->save();
+ }else{
+ drupal_set_message($status['response']['message'], "error");
+ $this->config->set('error', $status['response'])->save();
+ }
}
diff --git a/src/Form/TestJobGet.php b/src/Form/TestJobGet.php
index 04b9241..3e1a619 100644
--- a/src/Form/TestJobGet.php
+++ b/src/Form/TestJobGet.php
@@ -1,41 +1,29 @@
<?php
-
-/**
- * @file
- * Contains Drupal\slack\Form\SendTestMessageForm.
- */
-
namespace Drupal\ml_engine\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
-/**
- * Class SendTestMessageForm.
- *
- * @package Drupal\slack\Form
- */
class TestJobGet extends FormBase {
- /**
- * {@inheritdoc}
- */
+ public $config;
+
public function getFormId() {
- return 'ml_engine_get_test_prediction';
+ return 'ml_engine_job_get';
+ }
+
+ public function __construct(){
+ $this->config = \Drupal::configFactory()->getEditable('ml_engine.test.job.get');
}
- /**
- * {@inheritdoc}
- */
public function buildForm(array $form, FormStateInterface $form_state) {
- $config = \Drupal::configFactory()->getEditable('ml_engine.test.job.get');
$form['job_name'] = array(
'#type' => 'textfield',
'#title' => $this->t('Job Name'),
'#required' => TRUE,
- '#default_value' => $config->get('job_name'),
+ '#default_value' => $this->config->get('job_name'),
);
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
@@ -45,7 +33,7 @@ class TestJobGet extends FormBase {
);
// Print prediction response.
- if ($response = $config->get('response')){
+ if ($response = $this->config->get('response')){
$form['response'] = array(
'#type' => 'textarea',
'#title' => $this->t('Response'),
@@ -57,7 +45,7 @@ class TestJobGet extends FormBase {
}
// Print prediction error.
- if ($error = $config->get('error')){
+ if ($error = $this->config->get('error')){
$form['error'] = array(
'#type' => 'textarea',
'#title' => $this->t('Error'),
@@ -76,41 +64,19 @@ class TestJobGet extends FormBase {
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
- $config = \Drupal::configFactory()->getEditable('ml_engine.test.job.get');
- $config->delete();
- $credential = \Drupal::configFactory()->getEditable('ml_engine.test')->get('credential');
-
- // Set config variables.
- $project_name = \Drupal::configFactory()->getEditable('ml_engine.test')->get('project');
- $job_name = $form_state->getValue('job_name');
- $config ->set('job_name', $job_name) ->save();
- $name = $project_name."/jobs/".$job_name;
+ $this->config->delete();
+ $job = $form_state->getValue('job_name');
+ $this->config ->set('job_name', $job) ->save();
- // Set parameters for prediction request.
- $credential_json = json_decode($credential, true);
-
- // Creting client and services.
- $client = new \Google_Client();
- $client->setAuthConfig($credential_json);
- $client->addScope(\Google_Service_CloudMachineLearningEngine::CLOUD_PLATFORM);
- $service = new \Google_Service_CloudMachineLearningEngine($client);
-
- // Get Job details.
- try{
- $response = $service->projects_jobs->get($name);
- }catch (\Google_Service_Exception $ex){
- $error = json_decode($ex->getMessage(), true)['error'];
- $message = $error['message'];
- $code = $error['code'];
- $config->set('error',$message)->save();
- drupal_set_message($message,'error');
- return;
+ $status = \Drupal::service('ml_engine.job')->get($job);
+ if($status['success']){
+ drupal_set_message('Successfully got job '.$job, "status");
+ $response_job = (array) $status['response'];
+ $this->config->set('response', $response_job)->save();
+ }else{
+ drupal_set_message($status['response']['message'], "error");
+ $this->config->set('error', $status['response'])->save();
}
- $job = (array) $response;
-
- $config->set('response', $job)->save();
-
-
}
}
diff --git a/src/Form/TestJobList.php b/src/Form/TestJobList.php
deleted file mode 100644
index bbf6d63..0000000
--- a/src/Form/TestJobList.php
+++ /dev/null
@@ -1,114 +0,0 @@
-<?php
-
-/**
- * @file
- * Contains Drupal\slack\Form\SendTestMessageForm.
- */
-
-namespace Drupal\ml_engine\Form;
-
-use Drupal\Core\Form\FormBase;
-use Drupal\Core\Form\FormStateInterface;
-
-
-/**
- * Class SendTestMessageForm.
- *
- * @package Drupal\slack\Form
- */
-class TestJobList extends FormBase {
-
- /**
- * {@inheritdoc}
- */
- public function getFormId() {
- return 'ml_engine_get_test_prediction';
- }
-
- /**
- * {@inheritdoc}
- */
- public function buildForm(array $form, FormStateInterface $form_state) {
-
- $config = \Drupal::configFactory()->getEditable('ml_engine.test.job.list');
- $form['name'] = array(
- '#type' => 'textfield',
- '#title' => $this->t('Project Name'),
- '#required' => TRUE,
- '#default_value' => $config->get('name'),
- );
- $form['actions']['#type'] = 'actions';
- $form['actions']['submit'] = array(
- '#type' => 'submit',
- '#value' => $this->t('Get Jobs'),
- '#button_type' => 'primary',
- );
-
- // Print Job List response.
- if ($response = $config->get('jobs')){
- $form['response'] = array(
- '#type' => 'textarea',
- '#title' => $this->t('Response'),
- '#attributes' => array('readonly' => 'readonly'),
- '#default_value' => json_encode($response, JSON_PRETTY_PRINT),
- '#rows' => 15,
- '#weight' => 100
- );
- }
-
- // Print prediction error.
- if ($error = $config->get('error')){
- $form['error'] = array(
- '#type' => 'textarea',
- '#title' => $this->t('Error'),
- '#attributes' => array('readonly' => 'readonly'),
- '#default_value' => $error,
- '#rows' => 15,
- '#weight' => 100
- );
- }
-
- return $form;
- }
-
- /**
- * {@inheritdoc}
- */
- public function submitForm(array &$form, FormStateInterface $form_state) {
-
- $config = \Drupal::configFactory()->getEditable('ml_engine.test.job.list');
- $config->delete();
- $credential = \Drupal::configFactory()->getEditable('ml_engine.test')->get('credential');
-
- // Set config variables.
- $name = $form_state->getValue('name');
- $config ->set('name', $name) ->save();
-
-
- // Set parameters for prediction request.
- $credential_json = json_decode($credential, true);
-
- // Creting client and services.
- $client = new \Google_Client();
- $client->setAuthConfig($credential_json);
- $client->addScope(\Google_Service_CloudMachineLearningEngine::CLOUD_PLATFORM);
- $service = new \Google_Service_CloudMachineLearningEngine($client);
-
- // Get Job details.
- $response = $service->projects_jobs->listProjectsJobs($name);
- $jobs = $response->__get('jobs');
- $jobs_array = [];
-
- for ($i=0; $i<count($jobs); $i++){
- $jobs_array[$i] = (array) $jobs[$i];
- }
-
- $config->set('jobs',$jobs_array)->save();
-
- print "<pre>";
- print_r($jobs_array);
- print "</pre>";
-
- }
-
-}
diff --git a/src/Form/TestPredict.php b/src/Form/TestPredict.php
index d361980..d32da0f 100644
--- a/src/Form/TestPredict.php
+++ b/src/Form/TestPredict.php
@@ -1,47 +1,34 @@
<?php
-/**
- * @file
- * Contains Drupal\slack\Form\SendTestMessageForm.
- */
-
namespace Drupal\ml_engine\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
-/**
- * Class SendTestMessageForm.
- *
- * @package Drupal\slack\Form
- */
class TestPredict extends FormBase {
- /**
- * {@inheritdoc}
- */
+ public function __construct(){
+ $this->config = \Drupal::configFactory()->getEditable('ml_engine.test.prediction');
+ }
+
public function getFormId() {
return 'ml_engine_get_test_prediction';
}
- /**
- * {@inheritdoc}
- */
public function buildForm(array $form, FormStateInterface $form_state) {
- $config = \Drupal::configFactory()->getEditable('ml_engine.test.prediction');
$form['model_name'] = array(
'#type' => 'textfield',
'#title' => $this->t('Model Name'),
'#required' => TRUE,
- '#default_value' => $config->get('model_name'),
+ '#default_value' => $this->config->get('model_name'),
);
- $form['json'] = array(
+ $form['data'] = array(
'#type' => 'textarea',
'#title' => $this->t('JSON'),
'#required' => TRUE,
- '#default_value' => $config->get('json'),
+ '#default_value' => $this->config->get('data'),
'#rows' => 15
);
$form['actions']['#type'] = 'actions';
@@ -52,7 +39,7 @@ class TestPredict extends FormBase {
);
// Print prediction response.
- if ($response = $config->get('response')){
+ if ($response = $this->config->get('response')){
$form['response'] = array(
'#type' => 'textarea',
'#title' => $this->t('Response'),
@@ -64,12 +51,12 @@ class TestPredict extends FormBase {
}
// Print prediction error.
- if ($error = $config->get('error')){
+ if ($error = $this->config->get('error')){
$form['error'] = array(
'#type' => 'textarea',
'#title' => $this->t('Error'),
'#attributes' => array('readonly' => 'readonly'),
- '#default_value' => $error,
+ '#default_value' => json_encode($error, JSON_PRETTY_PRINT),
'#rows' => 15,
'#weight' => 100,
);
@@ -81,51 +68,25 @@ class TestPredict extends FormBase {
/**
* {@inheritdoc}
*/
- public function submitForm(array &$form, FormStateInterface $form_state) {
-
- $config = \Drupal::configFactory()->getEditable('ml_engine.test.prediction');
- $config->delete();
-
- // Set config variables.
- $credential = \Drupal::configFactory()->getEditable('ml_engine.test')->get('credential');
- $project = \Drupal::configFactory()->getEditable('ml_engine.test')->get('project');
- $data = $form_state->getValue('json');
- $model_name = $form_state->getValue('model_name');
- $url = $project."/models/".$model_name;
-
- $config
- ->set('model_name',$model_name)->set('json', $data)
- ->set('credential',$credential)
- ->save();
-
- // Set parameters for prediction request.
- $credential_json = json_decode($credential, true);
+ public function submitForm(array &$form, FormStateInterface $form_state) {
+ $this->config->delete();
+ $keys = ["data", "model_name"];
+ foreach ($keys as $key){
+ ${$key} = $form_state->getValue($key);
+ $this->config->set($key, ${$key})->save();
+ }
$data_array = ["instances" => json_decode($data,true)];
+ $response = \Drupal::service('ml_engine.predict')->predict($model_name,$data_array);
- // Creting client and services.
- $client = new \Google_Client();
- $client->setAuthConfig($credential_json);
- $client->addScope(\Google_Service_CloudMachineLearningEngine::CLOUD_PLATFORM);
- $service = new \Google_Service_CloudMachineLearningEngine($client);
-
- // Send prediction request.
- try{
- $response = $service->projects->predict($url, new \Google_Service_CloudMachineLearningEngine_GoogleCloudMlV1PredictRequest($data_array));
- } catch (\Google_Service_Exception $ex){
- $error = json_decode($ex->getMessage(), true)['error'];
- $message = $error['message'];
- $code = $error['code'];
- $config->set('error',$message)->save();
- drupal_set_message($message,'error');
+ if($response['success']){
+ $this->config->set('response',(array) $response['response'])->save();
+ drupal_set_message('Succesfully Got Prediction', 'status');
return;
- }
- if($error = $response->__get('error')){
- $config->set('error', $error)->save();
- drupal_set_message($error,'error');
+ }else{
+ $this->config->set('error', (array) $response['response'])->save();
+ drupal_set_message($response['response']['message'], 'error');
return;
}
- $predictions = $response->__get('predictions');
- $config->set('response', $predictions)->save();
}
diff --git a/src/Job.php b/src/Job.php
new file mode 100644
index 0000000..88f1998
--- /dev/null
+++ b/src/Job.php
@@ -0,0 +1,110 @@
+<?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 Job extends MLEngineBase{
+
+ public $config;
+
+ public function __construct() {
+ parent::__construct();
+ $this->config = \Drupal::configFactory()->getEditable('ml_engine.test.job');
+ }
+
+ public static function create(ContainerInterface $container) {
+ return new static();
+ }
+
+ public function UpdateJobList() {
+ $service = $this->create_service();
+ $response = $service->projects_jobs->listProjectsJobs($this->project_name);
+ $jobs = $response->__get('jobs');
+
+ $jobs_array = [];
+
+ for ($i=0; $i<count($jobs); $i++){
+ $jobs_array[$i] = (array) $jobs[$i];
+ }
+
+ $this->config->clear('list')->save();
+ $this->config->set('list',$jobs_array)->save();
+ return $jobs_array;
+ }
+
+ public function cancel($job){
+ $job_full_name = $this->project_name."/jobs/".$job;
+ $service = $this->create_service();
+
+ try{
+ $response = $service->projects_jobs->cancel($job_full_name, new \Google_Service_CloudMachineLearningEngine_GoogleCloudMlV1CancelJobRequest());
+ 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($job){
+ $job_full_name = $this->project_name."/jobs/".$job;
+ $service = $this->create_service();
+
+ try{
+ $response = $service->projects_jobs->get($job_full_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 createInputObject(array $para){
+ foreach (array_keys($para) as $key) {
+ ${$key} = $para[$key];
+ }
+ $arguments_array = ['--train--files', $train_data_uri, '--eval-files', $test_data_uri,
+ '--train-steps', $train_steps, 'verbosity', $verbosity];
+ $package_array = array($package_uri);
+ $input = new \Google_Service_CloudMachineLearningEngine_GoogleCloudMlV1TrainingInput();
+
+ $input->setScaleTier($scale_tier);
+ $input->setPackageUris($package_array);
+ $input->setPythonModule($module);
+ $input->setRegion($region);
+ $input->setJobDir($job_dir);
+ $input->setArgs($arguments_array);
+
+ return $input;
+ }
+
+ private function createJobObject(array $para){
+ $input = $this->createInputObject($para);
+
+ $job = new \Google_Service_CloudMachineLearningEngine_GoogleCloudMlV1Job();
+ $job->setJobId($para['job']);
+ $job->setTrainingInput($input);
+
+ return $job;
+ }
+
+ public function JobCreate(array $para){
+ $job = $this->createJobObject($para);
+ $service = $this->create_service();
+
+ try{
+ $response = $service->projects_jobs->create($this->project_name,$job);
+ return array( "success" => 1, "response" => $response );
+ }catch (\Google_Service_Exception $ex){
+ $error = json_decode($ex->getMessage(), true)['error'];
+ return array( "success" => 0, "response" => $error);
+ }
+ }
+
+}
diff --git a/src/MLEngineBase.php b/src/MLEngineBase.php
new file mode 100644
index 0000000..b241281
--- /dev/null
+++ b/src/MLEngineBase.php
@@ -0,0 +1,33 @@
+<?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;
+
+/**
+ * Returns responses for aggregator module routes.
+ */
+class MLEngineBase {
+
+ public $project_name;
+
+ public function __construct() {
+ $project = \Drupal::service('ml_engine.project');
+ $this->project_name = $project->get_name();
+ }
+
+ public static function create(ContainerInterface $container) {
+ return new static();
+ }
+
+ public function create_service(){
+ return \Drupal::service('ml_engine.cloud_service')->create_service();
+ }
+
+
+}
diff --git a/src/MLEngineService.php b/src/MLEngineService.php
new file mode 100644
index 0000000..e497cb9
--- /dev/null
+++ b/src/MLEngineService.php
@@ -0,0 +1,37 @@
+<?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;
+
+class MLEngineService {
+
+ public $credential;
+
+ public function __construct() {
+ }
+
+ public static function create(ContainerInterface $container) {
+ return new static();
+ }
+
+ public function create_service(array $credential=[]){
+ // Creting client and services.
+
+ if(!$credential) {
+ $credential = \Drupal::service('ml_engine.project')->get_credential();
+ }
+ $client = new \Google_Client();
+ $client->setAuthConfig($credential);
+ $client->addScope(\Google_Service_CloudMachineLearningEngine::CLOUD_PLATFORM);
+ $service = new \Google_Service_CloudMachineLearningEngine($client);
+ return $service;
+ }
+
+
+}
diff --git a/src/Predict.php b/src/Predict.php
new file mode 100644
index 0000000..52d97cd
--- /dev/null
+++ b/src/Predict.php
@@ -0,0 +1,32 @@
+<?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 Predict extends MLEngineBase{
+
+ public static function create(ContainerInterface $container) {
+ return new static();
+ }
+
+ public function predict($model,array $data){
+ $model_full_name = $this->project_name."/models/".$model;
+
+ $service = $this->create_service();
+ try{
+ $response = $service->projects->predict($model_full_name, new \Google_Service_CloudMachineLearningEngine_GoogleCloudMlV1PredictRequest($data));
+ return array( "success" => 1, "response" => $response );
+ }catch (\Google_Service_Exception $ex){
+ $error = json_decode($ex->getMessage(), true)['error'];
+ return array( "success" => 0, "response" => $error);
+ }
+ }
+
+}
diff --git a/src/Project.php b/src/Project.php
new file mode 100644
index 0000000..9f2a3b0
--- /dev/null
+++ b/src/Project.php
@@ -0,0 +1,63 @@
+<?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;
+
+class Project {
+
+ private $credential_required_keys;
+
+ public function __construct() {
+ $this->credential_required_keys = ['type', 'project_id', 'private_key_id', 'private_key', 'client_email', 'client_id', 'auth_uri', 'token_uri', 'auth_provider_x509_cert_url', 'client_x509_cert_url'];
+ }
+
+ public static function create(ContainerInterface $container) {
+ return new static();
+ }
+
+ public function get_project(){
+ return \Drupal::configFactory()->getEditable('ml_engine.test.project');
+ }
+
+ public function get_credential(){
+ return json_decode($this->get_project()->get('credential'),true);
+ }
+
+ public function get_name(){
+ return $this->get_project()->get('name');
+ }
+
+ public function verify_credential($project_name, array $credential){
+
+ $key_difference = array_diff($this->credential_required_keys, array_keys($credential));
+
+ if ($key_difference){
+ $message = t("Credential Keys [ '@keys' ] are missing",array('@keys'=>join("', '",$key_difference)));
+ return array(
+ 'success' => 0,
+ 'response'=>array('message'=>$message),
+ );
+ }
+
+ $service = \Drupal::service('ml_engine.cloud_service')->create_service();
+
+ try{
+ $response = $service->projects->getConfig($project_name);
+ return array('success' => 1,'response' =>$response);
+ } catch (\DomainException $ex){
+ $error = array('message' => $ex->getMessage());
+ return array('success' => 0,'response'=> array('message'=>"Please check the private key"));
+ } catch (\Google_Service_Exception $ex){
+ $error = json_decode($ex->getMessage(), true)['error'];
+ return array( "success" => 0, "response" => $error);
+ }
+
+ }
+
+}