Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
aa64705
docs: add sync api samples with json request (#13)
changsongd Apr 6, 2022
4601bcf
docs: add get_operation code snippets (#12)
changsongd Apr 8, 2022
8fe6675
docs: add code snippets for async api (#18)
changsongd Apr 8, 2022
f00be8e
docs: add long timeout code snippet (#20)
changsongd Apr 11, 2022
bd3825c
docs: update operation id (#23)
changsongd Apr 12, 2022
ea2013d
chore: run blacken on all directories with a noxfile (#16)
parthea Apr 12, 2022
5ea889d
chore(deps): update dependency google-cloud-storage to v2.3.0 (#24)
renovate-bot Apr 13, 2022
64a8934
chore(python): add nox session to sort python imports (#26)
gcf-owl-bot[bot] Apr 21, 2022
576dc17
chore(deps): update dependency pytest to v7.1.2 (#29)
renovate-bot Apr 25, 2022
6fd5238
chore(deps): update dependency google-cloud-optimization to v0.1.1 (#32)
renovate-bot Apr 26, 2022
6dfe340
chore(deps): update dependency google-cloud-optimization to v1 (#35)
renovate-bot Apr 27, 2022
a484038
fix: require python 3.7+ (#50)
gcf-owl-bot[bot] Jul 10, 2022
226f37e
chore(deps): update all dependencies (#45)
renovate-bot Jul 24, 2022
a74b9ea
chore(deps): update all dependencies (#55)
renovate-bot Aug 2, 2022
3ec7ffd
chore(deps): update all dependencies (#57)
renovate-bot Aug 5, 2022
e902bec
chore(deps): update all dependencies (#58)
renovate-bot Aug 9, 2022
f0e0b03
chore(deps): update dependency google-cloud-optimization to v1.1.1 (#70)
renovate-bot Sep 2, 2022
6c81459
chore(deps): update dependency pytest to v7.1.3 (#75)
renovate-bot Sep 6, 2022
e9ed704
chore: detect samples tests in nested directories (#79)
gcf-owl-bot[bot] Sep 13, 2022
a42b9f1
chore(deps): update dependency google-cloud-optimization to v1.1.2 (#83)
renovate-bot Oct 3, 2022
06db1ee
chore(deps): update dependency google-cloud-optimization to v1.1.3 (#86)
renovate-bot Oct 10, 2022
60b0b26
chore(deps): update dependency pytest to v7.2.0 (#87)
renovate-bot Oct 26, 2022
64cde18
Merge remote-tracking branch 'migration/main' into python-optimizatio…
donmccasland Nov 8, 2022
696f381
removing noxfile.py
donmccasland Nov 8, 2022
be61736
Merge branch 'main' into python-optimization-migration
donmccasland Nov 8, 2022
fccca2d
Updating CODEOWNERS and blunderbuss.yml
donmccasland Nov 9, 2022
f62e38f
Merge branch 'main' into python-optimization-migration
donmccasland Nov 9, 2022
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
/monitoring/opencensus @yuriatgoogle @GoogleCloudPlatform/python-samples-reviewers
/monitoring/prometheus @yuriatgoogle @GoogleCloudPlatform/python-samples-reviewers
/notebooks/**/* @alixhami @GoogleCloudPlatform/python-samples-reviewers
/optimization/**/* @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/python-samples-reviewers
/opencensus/**/* @GoogleCloudPlatform/python-samples-reviewers
/people-and-planet-ai/**/* @davidcavazos @GoogleCloudPlatform/python-samples-reviewers
/profiler/**/* @GoogleCloudPlatform/python-samples-reviewers
Expand Down
4 changes: 4 additions & 0 deletions .github/blunderbuss.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ assign_issues_by:
- 'api: notebooks'
to:
- alixhami
- labels:
- 'api: optimization'
to:
- GoogleCloudPlatform/dee-data-ai
- labels:
- 'api: people-and-planet-ai'
to:
Expand Down
59 changes: 59 additions & 0 deletions optimization/snippets/async_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START cloudoptimization_async_api]

from google.api_core.exceptions import GoogleAPICallError
from google.cloud import optimization_v1

# TODO(developer): Uncomment these variables before running the sample.
# project_id= 'YOUR_PROJECT_ID'
# request_file_name = 'YOUR_REQUEST_FILE_NAME'
# request_model_gcs_path = 'gs://YOUR_PROJECT/YOUR_BUCKET/YOUR_REQUEST_MODEL_PATH'
# model_solution_gcs_path = 'gs://YOUR_PROJECT/YOUR_BUCKET/YOUR_SOLUCTION_PATH'


def call_async_api(
project_id: str, request_model_gcs_path: str, model_solution_gcs_path_prefix: str
) -> None:
"""Call the async api for fleet routing."""
# Use the default credentials for the environment to authenticate the client.
fleet_routing_client = optimization_v1.FleetRoutingClient()
request_file_name = "resources/async_request.json"

with open(request_file_name, "r") as f:
fleet_routing_request = optimization_v1.BatchOptimizeToursRequest.from_json(
f.read()
)
fleet_routing_request.parent = f"projects/{project_id}"
for idx, mc in enumerate(fleet_routing_request.model_configs):
mc.input_config.gcs_source.uri = request_model_gcs_path
model_solution_gcs_path = f"{model_solution_gcs_path_prefix}_{idx}"
mc.output_config.gcs_destination.uri = model_solution_gcs_path

# The timeout argument for the gRPC call is independent from the `timeout`
# field in the request's OptimizeToursRequest message(s).
operation = fleet_routing_client.batch_optimize_tours(fleet_routing_request)
print(operation.operation.name)

try:
# Block to wait for the job to finish.
result = operation.result()
print(result)
# Do you stuff.
except GoogleAPICallError:
print(operation.operation.error)


# [END cloudoptimization_async_api]
49 changes: 49 additions & 0 deletions optimization/snippets/async_api_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import uuid

import google.auth
from google.cloud import storage
import pytest

import async_api


# TODO(developer): Replace the variables in the file before use.
# A sample request model can be found at resources/async_request_model.json.
TEST_UUID = uuid.uuid4()
BUCKET = f"optimization-ai-{TEST_UUID}"
OUTPUT_PREFIX = f"code_snippets_test_output_{TEST_UUID}"
INPUT_URI = "gs://cloud-samples-data/optimization-ai/async_request_model.json"
BATCH_OUTPUT_URI_PREFIX = "gs://{}/{}/".format(BUCKET, OUTPUT_PREFIX)


@pytest.fixture(autouse=True)
def setup_teardown() -> None:
"""Create a temporary bucket to store optimization output."""
storage_client = storage.Client()
bucket = storage_client.create_bucket(BUCKET)

yield

bucket.delete(force=True)


def test_call_async_api(capsys: pytest.LogCaptureFixture) -> None:
_, project_id = google.auth.default()
async_api.call_async_api(project_id, INPUT_URI, BATCH_OUTPUT_URI_PREFIX)
out, _ = capsys.readouterr()

assert "operations" in out
34 changes: 34 additions & 0 deletions optimization/snippets/get_operation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START cloudoptimization_get_operation]
from google.cloud import optimization_v1


def get_operation(operation_full_id: str) -> None:
"""Get operation details and status."""
# TODO(developer): Uncomment and set the following variables
# operation_full_id = \
# "projects/[projectId]/operations/[operationId]"

client = optimization_v1.FleetRoutingClient()
# Get the latest state of a long-running operation.
response = client.transport.operations_client.get_operation(operation_full_id)

print("Name: {}".format(response.name))
print("Operation details:")
print(response)


# [END cloudoptimization_get_operation]
41 changes: 41 additions & 0 deletions optimization/snippets/get_operation_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import google.auth
from google.cloud import optimization_v1
import pytest

import get_operation


@pytest.fixture(scope="function")
def operation_id() -> str:
fleet_routing_client = optimization_v1.FleetRoutingClient()

_, project_id = google.auth.default()
fleet_routing_request = {"parent": f"projects/{project_id}"}

# Make the request
operation = fleet_routing_client.batch_optimize_tours(fleet_routing_request)

yield operation.operation.name


def test_get_operation_status(
capsys: pytest.LogCaptureFixture, operation_id: str
) -> None:
get_operation.get_operation(operation_id)
out, _ = capsys.readouterr()
assert "Operation details" in out
42 changes: 42 additions & 0 deletions optimization/snippets/noxfile_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Default TEST_CONFIG_OVERRIDE for python repos.

# You can copy this file into your directory, then it will be imported from
# the noxfile.py.

# The source of truth:
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py

TEST_CONFIG_OVERRIDE = {
# You can opt out from the test for specific Python versions.
"ignored_versions": ["2.7", "3.6"],
# Old samples are opted out of enforcing Python type hints
# All new samples should feature them
"enforce_type_hints": True,
# An envvar key for determining the project id to use. Change it
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
# build specific Cloud project. You can also use your own string
# to use your own Cloud project.
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
# If you need to use a specific version of pip,
# change pip_version_override to the string representation
# of the version number, for example, "20.2.4"
"pip_version_override": None,
# A dictionary you want to inject into your test. Don't put any
# secrets here. These values will override predefined values.
"envs": {},
}
2 changes: 2 additions & 0 deletions optimization/snippets/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pytest==7.2.0

2 changes: 2 additions & 0 deletions optimization/snippets/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
google-cloud-optimization==1.1.3
google-cloud-storage==2.5.0
33 changes: 33 additions & 0 deletions optimization/snippets/resources/async_request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"parent": "projects/${YOUR_GCP_PROJECT_ID}",
"model_configs":[
{
"input_config":{
"gcs_source":{
"uri":"${REQUEST_MODEL_GCS_PATH}"
},
"data_format":"JSON"
},
"output_config":{
"gcs_destination":{
"uri":"${MODEL_SOLUTION_GCS_PATH}"
},
"data_format":"JSON"
}
},
{
"input_config":{
"gcs_source":{
"uri":"${REQUEST_MODEL_GCS_PATH}"
},
"data_format":"JSON"
},
"output_config":{
"gcs_destination":{
"uri":"${MODEL_SOLUTION_GCS_PATH}"
},
"data_format":"JSON"
}
}
]
}
Loading