Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ba32fe1

Browse filesBrowse files
authored
samples: export data to BigQuery (#45)
1 parent c578db4 commit ba32fe1
Copy full SHA for ba32fe1

File tree

Expand file treeCollapse file tree

5 files changed

+134
-20
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+134
-20
lines changed
Open diff view settings
Collapse file
+42Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2021 Google LLC
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+
# [START contactcenterinsights_export_to_bigquery]
16+
from google.cloud import contact_center_insights_v1
17+
18+
19+
def export_to_bigquery(
20+
project_id: str,
21+
bigquery_project_id: str,
22+
bigquery_dataset_id: str,
23+
bigquery_table_id: str,
24+
) -> None:
25+
# Construct an export request.
26+
request = contact_center_insights_v1.ExportInsightsDataRequest()
27+
request.parent = contact_center_insights_v1.ContactCenterInsightsClient.common_location_path(
28+
project_id, "us-central1"
29+
)
30+
request.big_query_destination.project_id = bigquery_project_id
31+
request.big_query_destination.dataset = bigquery_dataset_id
32+
request.big_query_destination.table = bigquery_table_id
33+
request.filter = 'agent_id="007"'
34+
35+
# Call the Insights client to export data to BigQuery.
36+
insights_client = contact_center_insights_v1.ContactCenterInsightsClient()
37+
export_operation = insights_client.export_insights_data(request=request)
38+
export_operation.result(timeout=600000)
39+
print("Exported data to BigQuery")
40+
41+
42+
# [END contactcenterinsights_export_to_bigquery]
Collapse file

‎packages/google-cloud-contact-center-insights/samples/snippets/noxfile.py‎

Copy file name to clipboardExpand all lines: packages/google-cloud-contact-center-insights/samples/snippets/noxfile.py
+25-19Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,29 @@
3939

4040
TEST_CONFIG = {
4141
# You can opt out from the test for specific Python versions.
42-
'ignored_versions': [],
43-
42+
"ignored_versions": [],
4443
# Old samples are opted out of enforcing Python type hints
4544
# All new samples should feature them
46-
'enforce_type_hints': False,
47-
45+
"enforce_type_hints": False,
4846
# An envvar key for determining the project id to use. Change it
4947
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
5048
# build specific Cloud project. You can also use your own string
5149
# to use your own Cloud project.
52-
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
50+
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
5351
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
5452
# If you need to use a specific version of pip,
5553
# change pip_version_override to the string representation
5654
# of the version number, for example, "20.2.4"
5755
"pip_version_override": None,
5856
# A dictionary you want to inject into your test. Don't put any
5957
# secrets here. These values will override predefined values.
60-
'envs': {},
58+
"envs": {},
6159
}
6260

6361

6462
try:
6563
# Ensure we can import noxfile_config in the project's directory.
66-
sys.path.append('.')
64+
sys.path.append(".")
6765
from noxfile_config import TEST_CONFIG_OVERRIDE
6866
except ImportError as e:
6967
print("No user noxfile_config found: detail: {}".format(e))
@@ -78,12 +76,12 @@ def get_pytest_env_vars() -> Dict[str, str]:
7876
ret = {}
7977

8078
# Override the GCLOUD_PROJECT and the alias.
81-
env_key = TEST_CONFIG['gcloud_project_env']
79+
env_key = TEST_CONFIG["gcloud_project_env"]
8280
# This should error out if not set.
83-
ret['GOOGLE_CLOUD_PROJECT'] = os.environ[env_key]
81+
ret["GOOGLE_CLOUD_PROJECT"] = os.environ[env_key]
8482

8583
# Apply user supplied envs.
86-
ret.update(TEST_CONFIG['envs'])
84+
ret.update(TEST_CONFIG["envs"])
8785
return ret
8886

8987

@@ -92,11 +90,14 @@ def get_pytest_env_vars() -> Dict[str, str]:
9290
ALL_VERSIONS = ["3.6", "3.7", "3.8", "3.9"]
9391

9492
# Any default versions that should be ignored.
95-
IGNORED_VERSIONS = TEST_CONFIG['ignored_versions']
93+
IGNORED_VERSIONS = TEST_CONFIG["ignored_versions"]
9694

9795
TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS])
9896

99-
INSTALL_LIBRARY_FROM_SOURCE = os.environ.get("INSTALL_LIBRARY_FROM_SOURCE", False) in ("True", "true")
97+
INSTALL_LIBRARY_FROM_SOURCE = os.environ.get("INSTALL_LIBRARY_FROM_SOURCE", False) in (
98+
"True",
99+
"true",
100+
)
100101
#
101102
# Style Checks
102103
#
@@ -141,7 +142,7 @@ def _determine_local_import_names(start_dir: str) -> List[str]:
141142

142143
@nox.session
143144
def lint(session: nox.sessions.Session) -> None:
144-
if not TEST_CONFIG['enforce_type_hints']:
145+
if not TEST_CONFIG["enforce_type_hints"]:
145146
session.install("flake8", "flake8-import-order")
146147
else:
147148
session.install("flake8", "flake8-import-order", "flake8-annotations")
@@ -150,9 +151,11 @@ def lint(session: nox.sessions.Session) -> None:
150151
args = FLAKE8_COMMON_ARGS + [
151152
"--application-import-names",
152153
",".join(local_names),
153-
"."
154+
".",
154155
]
155156
session.run("flake8", *args)
157+
158+
156159
#
157160
# Black
158161
#
@@ -165,6 +168,7 @@ def blacken(session: nox.sessions.Session) -> None:
165168

166169
session.run("black", *python_files)
167170

171+
168172
#
169173
# Sample Tests
170174
#
@@ -173,7 +177,9 @@ def blacken(session: nox.sessions.Session) -> None:
173177
PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"]
174178

175179

176-
def _session_tests(session: nox.sessions.Session, post_install: Callable = None) -> None:
180+
def _session_tests(
181+
session: nox.sessions.Session, post_install: Callable = None
182+
) -> None:
177183
if TEST_CONFIG["pip_version_override"]:
178184
pip_version = TEST_CONFIG["pip_version_override"]
179185
session.install(f"pip=={pip_version}")
@@ -203,7 +209,7 @@ def _session_tests(session: nox.sessions.Session, post_install: Callable = None)
203209
# on travis where slow and flaky tests are excluded.
204210
# See http://doc.pytest.org/en/latest/_modules/_pytest/main.html
205211
success_codes=[0, 5],
206-
env=get_pytest_env_vars()
212+
env=get_pytest_env_vars(),
207213
)
208214

209215

@@ -213,9 +219,9 @@ def py(session: nox.sessions.Session) -> None:
213219
if session.python in TESTED_VERSIONS:
214220
_session_tests(session)
215221
else:
216-
session.skip("SKIPPED: {} tests are disabled for this sample.".format(
217-
session.python
218-
))
222+
session.skip(
223+
"SKIPPED: {} tests are disabled for this sample.".format(session.python)
224+
)
219225

220226

221227
#
Collapse file
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
google-api-core==2.0.1
2+
google-cloud-bigquery==2.26.0
23
google-cloud-contact-center-insights==0.2.0
Collapse file

‎packages/google-cloud-contact-center-insights/samples/snippets/test_enable_pubsub_notifications.py‎

Copy file name to clipboardExpand all lines: packages/google-cloud-contact-center-insights/samples/snippets/test_enable_pubsub_notifications.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def disable_pubsub_notifications(project_id):
7070

7171

7272
def test_enable_pubsub_notifications(
73-
capsys, project_id, pubsub_topics, disable_pubsub_notifications
73+
capsys, project_id, pubsub_topics, disable_pubsub_notifications
7474
):
7575
conversation_topic, analysis_topic = pubsub_topics
7676

Collapse file
+65Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright 2021 Google LLC
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+
import uuid
16+
17+
import google.auth
18+
19+
from google.cloud import bigquery
20+
21+
import pytest
22+
23+
import export_to_bigquery
24+
25+
GCLOUD_TESTS_PREFIX = "python_samples_tests"
26+
27+
28+
@pytest.fixture
29+
def project_id():
30+
_, project_id = google.auth.default()
31+
return project_id
32+
33+
34+
@pytest.fixture
35+
def unique_id():
36+
uuid_hex = uuid.uuid4().hex[:8]
37+
return f"{GCLOUD_TESTS_PREFIX}_{uuid_hex}"
38+
39+
40+
@pytest.fixture
41+
def bigquery_resources(project_id, unique_id):
42+
# Create a BigQuery dataset.
43+
bigquery_client = bigquery.Client()
44+
dataset_id = unique_id
45+
table_id = unique_id
46+
47+
dataset = bigquery.Dataset(f"{project_id}.{dataset_id}")
48+
dataset.location = "US"
49+
bigquery_client.create_dataset(dataset, timeout=30)
50+
51+
# Create a BigQuery table under the created dataset.
52+
table = bigquery.Table(f"{project_id}.{dataset_id}.{table_id}")
53+
bigquery_client.create_table(table)
54+
55+
yield dataset_id, table_id
56+
57+
# Delete the BigQuery dataset and table.
58+
bigquery_client.delete_dataset(dataset_id, delete_contents=True)
59+
60+
61+
def test_export_data_to_bigquery(capsys, project_id, bigquery_resources):
62+
dataset_id, table_id = bigquery_resources
63+
export_to_bigquery.export_to_bigquery(project_id, project_id, dataset_id, table_id)
64+
out, err = capsys.readouterr()
65+
assert "Exported data to BigQuery" in out

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.