Skip to content

Commit

Permalink
fix: linting
Browse files Browse the repository at this point in the history
  • Loading branch information
kmaphoenix committed Nov 26, 2024
1 parent 3c9f71d commit 100fb39
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 74 deletions.
9 changes: 5 additions & 4 deletions examples/dfcx_agent_cicd/cicd_code/UAT/deploy.py
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -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"]
Expand Down
13 changes: 6 additions & 7 deletions examples/dfcx_agent_cicd/cicd_code/export/export.py
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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}"
Expand All @@ -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)

24 changes: 14 additions & 10 deletions examples/dfcx_agent_cicd/cicd_code/export/flow_impacted.py
Original file line number Diff line number Diff line change
@@ -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.
"""

Expand Down
15 changes: 9 additions & 6 deletions examples/dfcx_agent_cicd/cicd_code/prod/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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"]
Expand Down
54 changes: 29 additions & 25 deletions examples/dfcx_agent_cicd/cicd_code/shared/deployment.py
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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.
Expand All @@ -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):
Expand All @@ -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:
Expand All @@ -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}",
Expand Down Expand Up @@ -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()
Expand All @@ -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
"""
Expand Down Expand Up @@ -252,21 +254,23 @@ 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').
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(
Expand Down
23 changes: 14 additions & 9 deletions examples/dfcx_agent_cicd/cicd_code/shared/en_vs_other_lang.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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}',
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading

0 comments on commit 100fb39

Please sign in to comment.