Skip to content

Commit d34d524

Browse files
authored
chore(samples): interactive tutorials code samples for import user events (#152)
* chore: import user events interactive samples
1 parent c70edc6 commit d34d524

16 files changed

+709
-347
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright 2022 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main(datasetId) {
18+
// [START retail_import_user_events_big_query]
19+
20+
// Imports the Google Cloud client library.
21+
const {UserEventServiceClient} = require('@google-cloud/retail').v2;
22+
23+
// Instantiates a client.
24+
const retailClient = new UserEventServiceClient();
25+
26+
const projectId = await retailClient.getProjectId();
27+
const dataSchema = 'user_event';
28+
const tableId = 'events'; // TO CHECK ERROR HANDLING USE THE TABLE OF INVALID USER EVENTS
29+
30+
// Placement
31+
const parent = `projects/${projectId}/locations/global/catalogs/default_catalog`; // TO CHECK ERROR HANDLING PASTE THE INVALID CATALOG NAME HERE
32+
33+
// The desired input location of the data.
34+
const inputConfig = {
35+
bigQuerySource: {
36+
projectId,
37+
datasetId,
38+
tableId,
39+
dataSchema,
40+
},
41+
};
42+
43+
const IResponseParams = {
44+
IImportUserEventsResponse: 0,
45+
IImportMetadata: 1,
46+
IOperation: 2,
47+
};
48+
49+
const callImportUserEvents = async () => {
50+
// Construct request
51+
const request = {
52+
parent,
53+
inputConfig,
54+
};
55+
56+
console.log('Import request: ', request);
57+
58+
// Run request
59+
const [operation] = await retailClient.importUserEvents(request);
60+
const response = await operation.promise();
61+
const result = response[IResponseParams.IImportMetadata];
62+
console.log(
63+
`Number of successfully imported events: ${result.successCount | 0}`
64+
);
65+
console.log(
66+
`Number of failures during the importing: ${result.failureCount | 0}`
67+
);
68+
console.log(`Operation result: ${JSON.stringify(response)}`);
69+
};
70+
71+
console.log('Start events import');
72+
await callImportUserEvents();
73+
console.log('Events import finished');
74+
// [END retail_import_user_events_big_query]
75+
}
76+
77+
process.on('unhandledRejection', err => {
78+
console.error(err.message);
79+
process.exitCode = 1;
80+
});
81+
82+
main(
83+
...(() => {
84+
const argv = process.argv.slice(2);
85+
return argv.length ? argv : ['user_events'];
86+
})()
87+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright 2022 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main(bucketName) {
18+
// [START retail_import_user_events_gcs]
19+
20+
// Imports the Google Cloud client library.
21+
const {UserEventServiceClient} = require('@google-cloud/retail').v2;
22+
23+
// Instantiates a client.
24+
const retailClient = new UserEventServiceClient();
25+
26+
const projectId = await retailClient.getProjectId();
27+
28+
//TODO(developer) set the environment variable value which will be used as the bucket name
29+
const gcsBucket = `gs://${bucketName}`;
30+
const gcsErrorsBucket = `gs://${bucketName}/error`;
31+
const gcsEventsObject = 'user_events.json'; // TO CHECK ERROR HANDLING USE THE JSON WITH INVALID USER EVENTS
32+
33+
// Placement
34+
const parent = `projects/${projectId}/locations/global/catalogs/default_catalog`; // TO CHECK ERROR HANDLING PASTE THE INVALID CATALOG NAME HERE
35+
36+
// The desired input location of the data.
37+
const inputConfig = {
38+
gcsSource: {
39+
inputUris: [gcsBucket + '/' + gcsEventsObject],
40+
dataSchema: 'user_event',
41+
},
42+
};
43+
44+
// The desired location of errors incurred during the Import.
45+
const errorsConfig = {
46+
gcsPrefix: gcsErrorsBucket,
47+
};
48+
49+
const IResponseParams = {
50+
IImportUserEventsResponse: 0,
51+
IImportMetadata: 1,
52+
IOperation: 2,
53+
};
54+
55+
const callImportUserEvents = async () => {
56+
// Construct request
57+
const request = {
58+
parent,
59+
inputConfig,
60+
errorsConfig,
61+
};
62+
63+
console.log('Import request: ', request);
64+
65+
// Run request
66+
const [operation] = await retailClient.importUserEvents(request);
67+
const response = await operation.promise();
68+
const result = response[IResponseParams.IImportMetadata];
69+
console.log(
70+
`Number of successfully imported events: ${result.successCount | 0}`
71+
);
72+
console.log(
73+
`Number of failures during the importing: ${result.failureCount | 0}`
74+
);
75+
console.log(`Operation result: ${JSON.stringify(response)}`);
76+
};
77+
78+
console.log('Start events import');
79+
await callImportUserEvents();
80+
console.log('Events import finished');
81+
// [END retail_import_user_events_gcs]
82+
}
83+
84+
process.on('unhandledRejection', err => {
85+
console.error(err.message);
86+
process.exitCode = 1;
87+
});
88+
89+
main(
90+
...(() => {
91+
const argv = process.argv.slice(2);
92+
return argv.length ? argv : [process.env['EVENTS_BUCKET_NAME']];
93+
})()
94+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright 2022 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main() {
18+
// [START retail_import_user_events_inline]
19+
20+
// Imports the Google Cloud client library.
21+
const {UserEventServiceClient} = require('@google-cloud/retail').v2;
22+
23+
// Instantiates a client.
24+
const retailClient = new UserEventServiceClient();
25+
26+
const projectId = await retailClient.getProjectId();
27+
28+
// Placement
29+
const parent = `projects/${projectId}/locations/global/catalogs/default_catalog`;
30+
31+
// Create events
32+
const generateEvent = eventType => {
33+
return {
34+
eventType,
35+
visitorId: 'visitor_' + Math.random().toString(36).slice(2),
36+
eventTime: {
37+
seconds: Math.round(Date.now() / 1000),
38+
},
39+
};
40+
};
41+
42+
// The desired input location of the data.
43+
const inputConfig = {
44+
userEventInlineSource: {
45+
userEvents: [
46+
generateEvent('home-page-view'),
47+
generateEvent('home-page-view'),
48+
generateEvent('home-page-view'),
49+
],
50+
},
51+
};
52+
53+
const IResponseParams = {
54+
IImportUserEventsResponse: 0,
55+
IImportMetadata: 1,
56+
IOperation: 2,
57+
};
58+
59+
const callImportUserEvents = async () => {
60+
// Construct request
61+
const request = {
62+
parent,
63+
inputConfig,
64+
};
65+
66+
console.log('Import request: ', request);
67+
68+
// Run request
69+
const [operation] = await retailClient.importUserEvents(request);
70+
const response = await operation.promise();
71+
const result = response[IResponseParams.IImportMetadata];
72+
console.log(
73+
`Number of successfully imported events: ${result.successCount | 0}`
74+
);
75+
console.log(
76+
`Number of failures during the importing: ${result.failureCount | 0}`
77+
);
78+
console.log(`Operation result: ${JSON.stringify(response)}`);
79+
};
80+
81+
console.log('Start events import');
82+
await callImportUserEvents();
83+
console.log('Events import finished');
84+
// [END retail_import_user_events_inline]
85+
}
86+
87+
process.on('unhandledRejection', err => {
88+
console.error(err.message);
89+
process.exitCode = 1;
90+
});
91+
92+
main();

retail/interactive-tutorials/product/set-inventory.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ async function main(generatedProductId) {
8181
// Set inventory with current time
8282
console.log('Start set inventory');
8383
await callSetInventory();
84-
await utils.delay(180000);
84+
await utils.delay(200000);
8585

8686
// Get product
8787
let changedProduct = await utils.getProduct(createdProduct.name);
@@ -94,7 +94,7 @@ async function main(generatedProductId) {
9494
product.priceInfo.price = 20.0;
9595
setTime = {seconds: Math.round(Date.now() / 1000) - 86400};
9696
await callSetInventory();
97-
await utils.delay(180000);
97+
await utils.delay(200000);
9898

9999
// Get product
100100
changedProduct = await utils.getProduct(createdProduct.name);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2022 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main() {
18+
const utils = require('./setup-cleanup');
19+
20+
const dataset = 'user_events';
21+
22+
//Delete created bigquery dataset
23+
await utils.deleteBqDataset(dataset);
24+
}
25+
26+
process.on('unhandledRejection', err => {
27+
console.error(err.message);
28+
process.exitCode = 1;
29+
});
30+
31+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2022 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main() {
18+
const utils = require('./setup-cleanup');
19+
20+
// The ID of your GCS bucket
21+
const bucketName = process.env['EVENTS_BUCKET_NAME'];
22+
23+
//Delete created bucket
24+
await utils.deleteBucket(bucketName);
25+
}
26+
27+
process.on('unhandledRejection', err => {
28+
console.error(err.message);
29+
process.exitCode = 1;
30+
});
31+
32+
main();

0 commit comments

Comments
 (0)