From 100fb39761a700e56fc21b0c309f30cd3c71b1be Mon Sep 17 00:00:00 2001 From: Patrick Marlow Date: Tue, 26 Nov 2024 17:27:14 -0600 Subject: [PATCH] fix: linting --- .../dfcx_agent_cicd/cicd_code/UAT/deploy.py | 9 ++-- .../cicd_code/export/export.py | 13 +++-- .../cicd_code/export/flow_impacted.py | 24 +++++---- .../dfcx_agent_cicd/cicd_code/prod/deploy.py | 15 +++--- .../cicd_code/shared/deployment.py | 54 ++++++++++--------- .../cicd_code/shared/en_vs_other_lang.py | 23 ++++---- .../cicd_code/shared/fullfillment_helper.py | 5 +- .../cicd_code/shared/test_case_run.py | 21 ++++---- .../cicd_code/shared/webhook_update.py | 3 +- 9 files changed, 93 insertions(+), 74 deletions(-) diff --git a/examples/dfcx_agent_cicd/cicd_code/UAT/deploy.py b/examples/dfcx_agent_cicd/cicd_code/UAT/deploy.py index 56fffe96..ebb27e40 100644 --- a/examples/dfcx_agent_cicd/cicd_code/UAT/deploy.py +++ b/examples/dfcx_agent_cicd/cicd_code/UAT/deploy.py @@ -1,12 +1,11 @@ """ UAT Deployment functions""" -import sys import json import logging +import sys from shared.deployment import Deployment - #from .shared.deployments import Deployment # logging config logging.basicConfig( @@ -32,7 +31,8 @@ def main(data): 8. Updates the datastore with UAT information. Args: - data: A dictionary containing configuration data, including the 'uat_webhook_env' key. + data: A dictionary containing configuration data, including the + 'uat_webhook_env' key. """ dep=Deployment(data) @@ -61,7 +61,8 @@ def main(data): config["sha_agent_gcs_location"]=sha_gs_loc config["target_project_id"] = config["uat_project"] config["target_environment_name"]=config["uat_env_deploy"] - with open("agent_artifacts/metadata.json" , encoding='utf-8') as metadata_file: + with open("agent_artifacts/metadata.json", + encoding='utf-8') as metadata_file: metadata = json.load(metadata_file) config["source_flow_names"]=metadata["source_flow_names"] diff --git a/examples/dfcx_agent_cicd/cicd_code/export/export.py b/examples/dfcx_agent_cicd/cicd_code/export/export.py index fd2504d9..ae1d94e2 100644 --- a/examples/dfcx_agent_cicd/cicd_code/export/export.py +++ b/examples/dfcx_agent_cicd/cicd_code/export/export.py @@ -1,13 +1,14 @@ """ export functions""" import json -import sys import logging +import sys + +from google.cloud import storage from dfcx_scrapi.core.agents import Agents from .flow_impacted import Impacted -from google.cloud import storage # logging config logging.basicConfig( @@ -23,7 +24,7 @@ def agent_to_gcs( gsloc): """Exports a Dialogflow CX agent to Google Cloud Storage (GCS). - This function exports a specified Dialogflow CX agent and its environment + This function exports a specified Dialogflow CX agent and its environment to a designated location in Google Cloud Storage. Args: @@ -62,8 +63,8 @@ def meta_to_gcs( ): """Exports metadata to a JSON file in Google Cloud Storage (GCS). - This function takes configuration data, flow names, a commit message, - GCS location information, agent name, and version IDs, and creates a JSON + This function takes configuration data, flow names, a commit message, + GCS location information, agent name, and version IDs, and creates a JSON file containing this metadata in the specified GCS bucket. Args: @@ -103,7 +104,6 @@ def meta_to_gcs( bucket=config["bucket"] user_commit_message=sys.argv[1] userid=sys.argv[2] - #updated_commit_message=f"{user_commit_message} by {userid} for {source_agent_name}" updated_commit_message = ( f"{user_commit_message} by {userid} " f"for {source_agent_name}" @@ -128,4 +128,3 @@ def meta_to_gcs( meta_to_gcs(config,source_flow_names, updated_commit_message,gs_loc, source_agent_name,bucket,impacted_version_ids) - \ No newline at end of file diff --git a/examples/dfcx_agent_cicd/cicd_code/export/flow_impacted.py b/examples/dfcx_agent_cicd/cicd_code/export/flow_impacted.py index 150120f5..d49c13ae 100644 --- a/examples/dfcx_agent_cicd/cicd_code/export/flow_impacted.py +++ b/examples/dfcx_agent_cicd/cicd_code/export/flow_impacted.py @@ -1,29 +1,33 @@ """ Getting impacted flow functions""" +from typing import Dict + from dfcx_scrapi.core.agents import Agents from dfcx_scrapi.core.environments import Environments from dfcx_scrapi.core.flows import Flows -from typing import Dict - class Impacted: """ - Analyzes and identifies changes in Dialogflow CX agent flows across environment versions. + Analyzes and identifies changes in Dialogflow CX agent flows across + environment versions. - This class retrieves information about a specified Dialogflow CX agent and its environment, - including version history and flow details. It then compares the latest two versions to - identify any changes in the flows, providing a mapping of impacted flow IDs and names. + This class retrieves information about a specified Dialogflow CX agent and + its environment, including version history and flow details. It then + compares the latest two versions to identify any changes in the flows, + providing a mapping of impacted flow IDs and names. Attributes: - source_project_id: The ID of the Google Cloud project where the agent resides. + source_project_id: The ID of the Google Cloud project where the agent + resides. source_agent_name: The display name of the agent. - environment_name: The display name of the agent's environment (default: "ready to deploy"). + environment_name: The display name of the agent's environment + (default: "ready to deploy"). Methods: - filter_flows: (Static method) Filters a flow map based on + filter_flows: (Static method) Filters a flow map based on differences between two environments. - check_flow: Identifies and returns a dictionary of + check_flow: Identifies and returns a dictionary of changed flows between the latest two versions. """ diff --git a/examples/dfcx_agent_cicd/cicd_code/prod/deploy.py b/examples/dfcx_agent_cicd/cicd_code/prod/deploy.py index 20aca8af..38154757 100644 --- a/examples/dfcx_agent_cicd/cicd_code/prod/deploy.py +++ b/examples/dfcx_agent_cicd/cicd_code/prod/deploy.py @@ -15,11 +15,11 @@ def main(data): """ Deploys and validates a Dialogflow CX agent in a production environment. - This function orchestrates the deployment and validation of a Dialogflow CX agent - in a production environment. It performs the following key steps: + This function orchestrates the deployment and validation of a Dialogflow CX + agent in a production environment. It performs the following key steps: 1. Imports the agent to the specified production webhook environment. - 2. Performs a language check on fulfillment entries, parameters, and routes, + 2. Performs a language check on fulfillment entries, parameters, and routes, specifically for French Canadian ('fr-ca'). 3. Collects flow IDs. 4. Manages version count and deletion. @@ -29,10 +29,12 @@ def main(data): 8. Updates the datastore with production information. Args: - data: A dictionary containing configuration data, including the 'prod_webhook_env' key. + data: A dictionary containing configuration data, including the + 'prod_webhook_env' key. Raises: - SystemExit: If the language check fails, indicating missing agent responses. + SystemExit: If the language check fails, indicating missing agent + responses. """ dep=Deployment(data) # call the steps sequentially @@ -71,7 +73,8 @@ def main(data): config["sha_agent_gcs_location"]=sha_agent_gcs_location config["target_project_id"] = config["prod_project"] config['target_environment_name']=config["prod_env_deploy"] - with open("agent_artifacts/metadata.json" , encoding='utf-8') as metadata_file: + with open("agent_artifacts/metadata.json", + encoding='utf-8') as metadata_file: metadata = json.load(metadata_file) config["source_flow_names"]=metadata["source_flow_names"] diff --git a/examples/dfcx_agent_cicd/cicd_code/shared/deployment.py b/examples/dfcx_agent_cicd/cicd_code/shared/deployment.py index e6b11183..02ec4bb4 100644 --- a/examples/dfcx_agent_cicd/cicd_code/shared/deployment.py +++ b/examples/dfcx_agent_cicd/cicd_code/shared/deployment.py @@ -1,23 +1,21 @@ """ Shared module to do deployement acting as a wrapper for deployment""" import datetime -import time +import json import logging import sys -import json - -from dfcx_scrapi.core.agents import Agents -from dfcx_scrapi.core.versions import Versions -from dfcx_scrapi.core.environments import Environments -from dfcx_scrapi.core.flows import Flows +import time from google.cloud.dialogflowcx_v3beta1 import types +from dfcx_scrapi.core.agents import Agents +from dfcx_scrapi.core.environments import Environments +from dfcx_scrapi.core.flows import Flows +from dfcx_scrapi.core.versions import Versions +from .en_vs_other_lang import en_vs_lang from .test_case_run import RunTestCases from .webhook_update import update_webhook -from .en_vs_other_lang import en_vs_lang - # logging config logging.basicConfig( @@ -30,8 +28,9 @@ class Deployment: """ Manages the deployment and lifecycle of Dialogflow CX agents. - This class provides methods for importing, testing, versioning, and deploying - Dialogflow CX agents across different environments. It handles tasks such as: + This class provides methods for importing, testing, versioning, and + deploying Dialogflow CX agents across different environments. It handles + tasks such as: - Importing agents from GCS. - Updating webhook configurations. @@ -48,10 +47,12 @@ class Deployment: import_agent: Imports an agent from GCS to a target project. test_case_validation: Runs test cases and validates the results. collect_flow_id: Collects the IDs of flows to be deployed. - version_count_delete: Manages version count and deletes old versions if necessary. + version_count_delete: Manages version count and deletes old versions if + necessary. version_cut: Creates new versions of the specified flows. deploy_versions: Deploys the new versions to the target environment. - dev_prod_sync: Synchronizes flows between development and production environments. + dev_prod_sync: Synchronizes flows between development and production + environments. datastore_update: Updates datastore settings for the agent. """ def __init__(self,input_dict): @@ -61,7 +62,7 @@ def __init__(self,input_dict): def import_agent(self,webhookenv): """Imports a Dialogflow CX agent to the target project. - This method restores a Dialogflow CX agent from a GCS bucket to the + This method restores a Dialogflow CX agent from a GCS bucket to the specified target project and updates the webhook URI for the agent. Args: @@ -77,7 +78,7 @@ def import_agent(self,webhookenv): self.target_agent_id=target_agent_details.name - #restoring the agent from the SHA ID folder + #restoring the agent from the SHA ID folder agent.restore_agent( agent_id=self.target_agent_id, gcs_bucket_uri=f"{self.sha_agent_gcs_location}/{self.agent_name}", @@ -115,9 +116,10 @@ def test_case_validation(self): def collect_flow_id(self): """Collects the IDs of flows to be deployed. - This method retrieves the IDs of the flows specified in `self.source_flow_names` - from the target Dialogflow CX agent. It introduces a 50-second delay to allow - for agent stabilization before fetching the flow IDs. + This method retrieves the IDs of the flows specified in + `self.source_flow_names` from the target Dialogflow CX agent. It + introduces a 50-second delay to allow for agent stabilization before + fetching the flow IDs. """ time.sleep(50) flow=Flows() @@ -137,7 +139,7 @@ def collect_flow_id(self): def version_count_delete(self): """ - 1. Check if the count of versions of a flow is not exceeding 20(limit) + 1. Check if the count of versions of a flow is not exceeding 20(limit) else delete the older version 2. and make room for new version cut """ @@ -252,11 +254,11 @@ def datastore_update(self,projectlevel): def fullfillment_lang_check(self,lang): """Checks fulfillment language coverage compared to English. - This method compares the fulfillment coverage of the specified language + This method compares the fulfillment coverage of the specified language (`lang`) with the English language ('en') for the given agent and flows. - It returns dataframes containing statistics on fulfillment entries, parameters, - and routes, along with a boolean result indicating whether all elements have - agent responses in the specified language. + It returns dataframes containing statistics on fulfillment entries, + parameters, and routes, along with a boolean result indicating whether + all elements have agent responses in the specified language. Args: lang: The language code to compare against English (e.g., 'fr-ca'). @@ -264,9 +266,11 @@ def fullfillment_lang_check(self,lang): Returns: A tuple containing: - entry_df: DataFrame with statistics on entry fulfillment coverage. - - param_df: DataFrame with statistics on parameter fulfillment coverage. + - param_df: DataFrame with statistics on parameter fulfillment + coverage. - route_df: DataFrame with statistics on route fulfillment coverage. - - result: A boolean indicating if all elements have agent responses in the specified language. + - result: A boolean indicating if all elements have agent responses + in the specified language. """ entry_df,param_df,route_df,result= en_vs_lang( diff --git a/examples/dfcx_agent_cicd/cicd_code/shared/en_vs_other_lang.py b/examples/dfcx_agent_cicd/cicd_code/shared/en_vs_other_lang.py index 7577507f..ae564137 100644 --- a/examples/dfcx_agent_cicd/cicd_code/shared/en_vs_other_lang.py +++ b/examples/dfcx_agent_cicd/cicd_code/shared/en_vs_other_lang.py @@ -1,19 +1,24 @@ """ Compare the fullfillments of en vs french langauge or etc""" +import pandas as pd + from dfcx_scrapi.core.flows import Flows -import pandas as pd +from .fullfillment_helper import ( + PagesChild, + get_entry_ff, + get_param_ff, + get_route_ff, +) -from .fullfillment_helper import get_entry_ff,get_param_ff,get_route_ff -from .fullfillment_helper import PagesChild def en_vs_lang(agent_id,flows,lang): """Compares fulfillment coverage between English and a specified language. - This function analyzes the fulfillment configurations (entry fulfillments, - parameter fulfillments, and route fulfillments) for a given Dialogflow CX agent - and a set of flows. It compares the coverage of the specified language (`lang`) - with the English language ('en'), generating dataframes that highlight any - discrepancies in fulfillment setup. + This function analyzes the fulfillment configurations (entry fulfillments, + parameter fulfillments, and route fulfillments) for a given Dialogflow CX + agent and a set of flows. It compares the coverage of the specified language + (`lang`) with the English language ('en'), generating dataframes that + highlight any discrepancies in fulfillment setup. Args: agent_id: The ID of the Dialogflow CX agent. @@ -25,7 +30,7 @@ def en_vs_lang(agent_id,flows,lang): - entry_df: DataFrame with statistics on entry fulfillment coverage. - param_df: DataFrame with statistics on parameter fulfillment coverage. - route_df: DataFrame with statistics on route fulfillment coverage. - - result: A boolean indicating if all elements have agent responses + - result: A boolean indicating if all elements have agent responses in the specified language. """ entry_columns = ['flow','page', 'text_entry_en', f'text_entry_{lang}', diff --git a/examples/dfcx_agent_cicd/cicd_code/shared/fullfillment_helper.py b/examples/dfcx_agent_cicd/cicd_code/shared/fullfillment_helper.py index 6b70836f..90ef1efa 100644 --- a/examples/dfcx_agent_cicd/cicd_code/shared/fullfillment_helper.py +++ b/examples/dfcx_agent_cicd/cicd_code/shared/fullfillment_helper.py @@ -1,10 +1,11 @@ """ Helper functions for en vs lang""" -from dfcx_scrapi.core.pages import Pages - from google.cloud.dialogflowcx_v3beta1.services import pages from google.cloud.dialogflowcx_v3beta1.types import page as gcdc_page +from dfcx_scrapi.core.pages import Pages + + class PagesChild(Pages): """ Iterates over the pages object to get the fullfillment details diff --git a/examples/dfcx_agent_cicd/cicd_code/shared/test_case_run.py b/examples/dfcx_agent_cicd/cicd_code/shared/test_case_run.py index a1b4da64..dfbff4bd 100644 --- a/examples/dfcx_agent_cicd/cicd_code/shared/test_case_run.py +++ b/examples/dfcx_agent_cicd/cicd_code/shared/test_case_run.py @@ -1,11 +1,11 @@ """ Running test cases and produce results""" -from typing import Tuple, Dict import logging +from typing import Dict, Tuple -from dfcx_scrapi.core.test_cases import TestCases from dfcx_scrapi.core.agents import Agents from dfcx_scrapi.core.environments import Environments +from dfcx_scrapi.core.test_cases import TestCases # logging config logging.basicConfig( @@ -18,17 +18,20 @@ class RunTestCases: """ Manages and executes test cases for Dialogflow CX agents. - This class provides functionality to run test cases against a specified - Dialogflow CX agent and environment. It retrieves the necessary agent and - environment information and allows triggering test cases with optional tag filtering. + This class provides functionality to run test cases against a specified + Dialogflow CX agent and environment. It retrieves the necessary agent and + environment information and allows triggering test cases with optional tag + filtering. Attributes: project_id: The ID of the Google Cloud project where the agent resides. agent_name: The display name of the agent. - environment_name: The display name of the agent's environment (can be None). + environment_name: The display name of the agent's environment + (can be None). Methods: - triggerTestcase: Executes test cases for the agent, optionally filtered by tags. + triggerTestcase: Executes test cases for the agent, optionally filtered + by tags. """ def __init__( @@ -65,7 +68,7 @@ def trigger_test_case( agent_id=None, env_id=None) -> Tuple[Dict[str, int], bool] : """ - Function to trigger the test case module in dfcx + Function to trigger the test case module in dfcx Returns: Result: Dict of results boolean mentioning test case status @@ -94,7 +97,7 @@ def trigger_test_case( "UAT deployment will be done without test case validation" ) result={"Pass": 0, "Fail": 0} - return result, True + return result, True filtered_tc_id=[filtestcase.name for filtestcase in filtered_tc] print(filtered_tc_id) diff --git a/examples/dfcx_agent_cicd/cicd_code/shared/webhook_update.py b/examples/dfcx_agent_cicd/cicd_code/shared/webhook_update.py index 1f9feba2..37f7fd02 100644 --- a/examples/dfcx_agent_cicd/cicd_code/shared/webhook_update.py +++ b/examples/dfcx_agent_cicd/cicd_code/shared/webhook_update.py @@ -1,10 +1,9 @@ -""" Functions to update the webhook env""" +"""Functions to update the webhook env.""" import logging import re from dfcx_scrapi.core.webhooks import Webhooks - # logging config logging.basicConfig( level=logging.INFO,