1 | ;
|
2 | /*!
|
3 | * Copyright 2019 Google Inc. All Rights Reserved.
|
4 | *
|
5 | * Licensed under the Apache License, Version 2.0 (the "License");
|
6 | * you may not use this file except in compliance with the License.
|
7 | * You may obtain a copy of the License at
|
8 | *
|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
10 | *
|
11 | * Unless required by applicable law or agreed to in writing, software
|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 | * See the License for the specific language governing permissions and
|
15 | * limitations under the License.
|
16 | */
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.Model = void 0;
|
19 | const common_1 = require("@google-cloud/common");
|
20 | const promisify_1 = require("@google-cloud/promisify");
|
21 | const arrify = require("arrify");
|
22 | const extend = require("extend");
|
23 | /**
|
24 | * The model export formats accepted by BigQuery.
|
25 | *
|
26 | * @type {array}
|
27 | * @private
|
28 | */
|
29 | const FORMATS = ['ML_TF_SAVED_MODEL', 'ML_XGBOOST_BOOSTER'];
|
30 | /**
|
31 | * Model objects are returned by methods such as {@link Dataset#model} and
|
32 | * {@link Dataset#getModels}.
|
33 | *
|
34 | * @class
|
35 | * @param {Dataset} dataset {@link Dataset} instance.
|
36 | * @param {string} id The ID of the model.
|
37 | *
|
38 | * @example
|
39 | * ```
|
40 | * const {BigQuery} = require('@google-cloud/bigquery');
|
41 | * const bigquery = new BigQuery();
|
42 | * const dataset = bigquery.dataset('my-dataset');
|
43 | *
|
44 | * const model = dataset.model('my-model');
|
45 | * ```
|
46 | */
|
47 | class Model extends common_1.ServiceObject {
|
48 | constructor(dataset, id) {
|
49 | const methods = {
|
50 | /**
|
51 | * @callback DeleteModelCallback
|
52 | * @param {?Error} err Request error, if any.
|
53 | * @param {object} apiResponse The full API response.
|
54 | */
|
55 | /**
|
56 | * Delete the model.
|
57 | *
|
58 | * See {@link https://cloud.google.com/bigquery/docs/reference/rest/v2/models/delete| Models: delete API Documentation}
|
59 | *
|
60 | * @method Model#delete
|
61 | * @param {DeleteModelCallback} [callback] The callback function.
|
62 | * @param {?error} callback.err An error returned while making this
|
63 | * request.
|
64 | * @param {object} callback.apiResponse The full API response.
|
65 | * @returns {Promise}
|
66 | *
|
67 | * @example
|
68 | * ```
|
69 | * const {BigQuery} = require('@google-cloud/bigquery');
|
70 | * const bigquery = new BigQuery();
|
71 | * const dataset = bigquery.dataset('my-dataset');
|
72 | * const model = dataset.model('my-model');
|
73 | *
|
74 | * model.delete((err, apiResponse) => {});
|
75 | *
|
76 | * ```
|
77 | * @example If the callback is omitted we'll return a Promise.
|
78 | * ```
|
79 | * const [apiResponse] = await model.delete();
|
80 | * ```
|
81 | * @example If successful, the response body is empty.
|
82 | * ```
|
83 | * ```
|
84 | */
|
85 | delete: true,
|
86 | /**
|
87 | * @callback ModelExistsCallback
|
88 | * @param {?Error} err Request error, if any.
|
89 | * @param {boolean} exists Indicates if the model exists.
|
90 | */
|
91 | /**
|
92 | * @typedef {array} ModelExistsResponse
|
93 | * @property {boolean} 0 Indicates if the model exists.
|
94 | */
|
95 | /**
|
96 | * Check if the model exists.
|
97 | *
|
98 | * @method Model#exists
|
99 | * @param {ModelExistsCallback} [callback] The callback function.
|
100 | * @param {?error} callback.err An error returned while making this
|
101 | * request.
|
102 | * @param {boolean} callback.exists Whether the model exists or not.
|
103 | * @returns {Promise<ModelExistsResponse>}
|
104 | *
|
105 | * @example
|
106 | * ```
|
107 | * const {BigQuery} = require('@google-cloud/bigquery');
|
108 | * const bigquery = new BigQuery();
|
109 | * const dataset = bigquery.dataset('my-dataset');
|
110 | * const model = dataset.model('my-model');
|
111 | *
|
112 | * model.exists((err, exists) => {});
|
113 | *
|
114 | * ```
|
115 | * @example If the callback is omitted we'll return a Promise.
|
116 | * ```
|
117 | * const [exists] = await model.exists();
|
118 | * ```
|
119 | */
|
120 | exists: true,
|
121 | /**
|
122 | * @callback GetModelCallback
|
123 | * @param {?Error} err Request error, if any.
|
124 | * @param {Model} model The model.
|
125 | * @param {object} apiResponse The full API response body.
|
126 | */
|
127 | /**
|
128 | * @typedef {array} GetModelResponse
|
129 | * @property {Model} 0 The model.
|
130 | * @property {object} 1 The full API response body.
|
131 | */
|
132 | /**
|
133 | * Get a model if it exists.
|
134 | *
|
135 | * See {@link https://cloud.google.com/bigquery/docs/reference/rest/v2/models/get| Models: get API Documentation}
|
136 | *
|
137 | * @method Model#get:
|
138 | * @param {GetModelCallback} [callback] The callback function.
|
139 | * @param {?error} callback.err An error returned while making this
|
140 | * request.
|
141 | * @param {Model} callback.model The {@link Model}.
|
142 | * @param {object} callback.apiResponse The full API response.
|
143 | * @returns {Promise<GetModelResponse>}
|
144 | *
|
145 | * @example
|
146 | * ```
|
147 | * const {BigQuery} = require('@google-cloud/bigquery');
|
148 | * const bigquery = new BigQuery();
|
149 | * const dataset = bigquery.dataset('my-dataset');
|
150 | * const model = dataset.model('my-model');
|
151 | *
|
152 | * model.get(err => {
|
153 | * if (!err) {
|
154 | * // `model.metadata` has been populated.
|
155 | * }
|
156 | * });
|
157 | *
|
158 | * ```
|
159 | * @example If the callback is omitted we'll return a Promise.
|
160 | * ```
|
161 | * await model.get();
|
162 | * ```
|
163 | */
|
164 | get: true,
|
165 | /**
|
166 | * @callback GetModelMetadataCallback
|
167 | * @param {?Error} err Request error, if any.
|
168 | * @param {object} metadata The model metadata.
|
169 | * @param {object} apiResponse The full API response.
|
170 | */
|
171 | /**
|
172 | * @typedef {array} GetModelMetadataResponse
|
173 | * @property {object} 0 The model metadata.
|
174 | * @property {object} 1 The full API response.
|
175 | */
|
176 | /**
|
177 | * Return the metadata associated with the model.
|
178 | *
|
179 | * See {@link https://cloud.google.com/bigquery/docs/reference/rest/v2/models/get| Models: get API Documentation}
|
180 | *
|
181 | * @method Model#getMetadata
|
182 | * @param {GetModelMetadataCallback} [callback] The callback function.
|
183 | * @param {?error} callback.err An error returned while making this
|
184 | * request.
|
185 | * @param {object} callback.metadata The metadata of the model.
|
186 | * @param {object} callback.apiResponse The full API response.
|
187 | * @returns {Promise<GetModelMetadataResponse>}
|
188 | *
|
189 | * @example
|
190 | * ```
|
191 | * const {BigQuery} = require('@google-cloud/bigquery');
|
192 | * const bigquery = new BigQuery();
|
193 | * const dataset = bigquery.dataset('my-dataset');
|
194 | * const model = dataset.model('my-model');
|
195 | *
|
196 | * model.getMetadata((err, metadata, apiResponse) => {});
|
197 | *
|
198 | * ```
|
199 | * @example If the callback is omitted we'll return a Promise.
|
200 | * ```
|
201 | * const [metadata, apiResponse] = await model.getMetadata();
|
202 | * ```
|
203 | */
|
204 | getMetadata: true,
|
205 | /**
|
206 | * @callback SetModelMetadataCallback
|
207 | * @param {?Error} err Request error, if any.
|
208 | * @param {object} metadata The model metadata.
|
209 | * @param {object} apiResponse The full API response.
|
210 | */
|
211 | /**
|
212 | * @typedef {array} SetModelMetadataResponse
|
213 | * @property {object} 0 The model metadata.
|
214 | * @property {object} 1 The full API response.
|
215 | */
|
216 | /**
|
217 | * See {@link https://cloud.google.com/bigquery/docs/reference/rest/v2/models/patch| Models: patch API Documentation}
|
218 | *
|
219 | * @method Model#setMetadata
|
220 | * @param {object} metadata The metadata key/value object to set.
|
221 | * @param {SetModelMetadataCallback} [callback] The callback function.
|
222 | * @param {?error} callback.err An error returned while making this
|
223 | * request.
|
224 | * @param {object} callback.metadata The updated metadata of the model.
|
225 | * @param {object} callback.apiResponse The full API response.
|
226 | * @returns {Promise<SetModelMetadataResponse>}
|
227 | *
|
228 | * @example
|
229 | * ```
|
230 | * const {BigQuery} = require('@google-cloud/bigquery');
|
231 | * const bigquery = new BigQuery();
|
232 | * const dataset = bigquery.dataset('my-dataset');
|
233 | * const model = dataset.model('my-model');
|
234 | *
|
235 | * const metadata = {
|
236 | * friendlyName: 'TheBestModelEver'
|
237 | * };
|
238 | *
|
239 | * model.setMetadata(metadata, (err, metadata, apiResponse) => {});
|
240 | *
|
241 | * ```
|
242 | * @example If the callback is omitted we'll return a Promise.
|
243 | * ```
|
244 | * const [metadata, apiResponse] = await model.setMetadata(metadata);
|
245 | * ```
|
246 | */
|
247 | setMetadata: true,
|
248 | };
|
249 | super({
|
250 | parent: dataset,
|
251 | baseUrl: '/models',
|
252 | id,
|
253 | methods,
|
254 | });
|
255 | this.dataset = dataset;
|
256 | this.bigQuery = dataset.bigQuery;
|
257 | }
|
258 | createExtractJob(destination, optionsOrCallback, cb) {
|
259 | let options = typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
|
260 | const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
|
261 | options = extend(true, options, {
|
262 | destinationUris: arrify(destination).map(dest => {
|
263 | if (common_1.util.isCustomType(dest, 'storage/file')) {
|
264 | return ('gs://' + dest.bucket.name + '/' + dest.name);
|
265 | }
|
266 | if (typeof dest === 'string') {
|
267 | return dest;
|
268 | }
|
269 | throw new Error('Destination must be a string or a File object.');
|
270 | }),
|
271 | });
|
272 | if (options.format) {
|
273 | options.format = options.format.toUpperCase();
|
274 | if (FORMATS.includes(options.format)) {
|
275 | options.destinationFormat = options.format;
|
276 | delete options.format;
|
277 | }
|
278 | else {
|
279 | throw new Error('Destination format not recognized: ' + options.format);
|
280 | }
|
281 | }
|
282 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
283 | const body = {
|
284 | configuration: {
|
285 | extract: extend(true, options, {
|
286 | sourceModel: {
|
287 | datasetId: this.dataset.id,
|
288 | projectId: this.dataset.projectId,
|
289 | modelId: this.id,
|
290 | },
|
291 | }),
|
292 | },
|
293 | };
|
294 | if (options.jobPrefix) {
|
295 | body.jobPrefix = options.jobPrefix;
|
296 | delete options.jobPrefix;
|
297 | }
|
298 | if (options.jobId) {
|
299 | body.jobId = options.jobId;
|
300 | delete options.jobId;
|
301 | }
|
302 | this.bigQuery.createJob(body, callback);
|
303 | }
|
304 | extract(destination, optionsOrCallback, cb) {
|
305 | const options = typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
|
306 | const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
|
307 | this.createExtractJob(destination, options, (err, job, resp) => {
|
308 | if (err) {
|
309 | callback(err, resp);
|
310 | return;
|
311 | }
|
312 | job.on('error', callback).on('complete', metadata => {
|
313 | callback(null, metadata);
|
314 | });
|
315 | });
|
316 | }
|
317 | }
|
318 | exports.Model = Model;
|
319 | /*! Developer Documentation
|
320 | *
|
321 | * All async methods (except for streams) will return a Promise in the event
|
322 | * that a callback is omitted.
|
323 | */
|
324 | (0, promisify_1.promisifyAll)(Model);
|
325 | //# sourceMappingURL=model.js.map |
\ | No newline at end of file |