Skip to content

Commit

Permalink
Merge pull request #1312 from uktrade/GREATUK-1229-move-market-guides…
Browse files Browse the repository at this point in the history
…-pipelines-from-staging

Disable market guides pre-release workflow
  • Loading branch information
rafa-garcia authored Oct 1, 2024
2 parents bbb0bc1 + d49f4fd commit f1d43fd
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 117 deletions.
1 change: 0 additions & 1 deletion conf/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ class BaseSettings(PydanticBaseSettings):
ownership_invite_subject: str = 'Confirm ownership of {company_name}’s Find a buyer profile'
collaborator_invite_subject: str = 'Confirm you’ve been added to {company_name}’s Find a buyer profile'
great_marketguides_teams_channel_email: str
great_marketguides_review_period_days: int = 10

# Automated email settings
verification_code_not_given_subject: str = 'Please verify your company’s Find a buyer profile'
Expand Down
2 changes: 0 additions & 2 deletions conf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,6 @@
OWNERSHIP_INVITE_SUBJECT = env.ownership_invite_subject
COLLABORATOR_INVITE_SUBJECT = env.collaborator_invite_subject
GREAT_MARKETGUIDES_TEAMS_CHANNEL_EMAIL = env.great_marketguides_teams_channel_email
GREAT_MARKETGUIDES_REVIEW_PERIOD_DAYS = env.great_marketguides_review_period_days


# Public storage for company profile logo
STORAGE_CLASSES = {
Expand Down
45 changes: 5 additions & 40 deletions dataservices/management/commands/helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import io
from datetime import datetime, timedelta
from sys import stdout
from datetime import datetime
from zipfile import ZipFile

import pandas as pd
Expand Down Expand Up @@ -46,35 +45,6 @@ def send_ingest_error_notify_email(view_name, error_details):
)


def send_review_request_message(view_name):
instance, _created = Metadata.objects.get_or_create(view_name=view_name)
last_release = datetime.strptime(instance.data['source']['last_release'], '%Y-%m-%dT%H:%M:%S')

try:
last_notification_sent = datetime.strptime(
instance.data['review_process']['notification_sent'], '%Y-%m-%dT%H:%M:%S'
)
except KeyError:
instance.data['review_process'] = {'notification_sent': None}
last_notification_sent = None

if last_notification_sent is None or (((last_notification_sent.timestamp() - last_release.timestamp())) < 0):
notifications_client().send_email_notification(
email_address=settings.GREAT_MARKETGUIDES_TEAMS_CHANNEL_EMAIL,
template_id=settings.GOVNOTIFY_GREAT_MARKETGUIDES_REVIEW_REQUEST_TEMPLATE_ID,
personalisation={
'view_name': view_name,
'review_url': 'https://great.staging.uktrade.digital/markets/',
'release_date': (
last_release + timedelta(days=settings.GREAT_MARKETGUIDES_REVIEW_PERIOD_DAYS)
).strftime('%d/%m/%Y'),
},
)
stdout.write(f"Sent review request notification for {view_name}")
instance.data['review_process']['notification_sent'] = datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
instance.save()


class BaseDataWorkspaceIngestionCommand(BaseCommand):
engine = sa.create_engine(settings.DATA_WORKSPACE_DATASETS_URL, execution_options={'stream_results': True})

Expand Down Expand Up @@ -114,15 +84,10 @@ def should_ingestion_run(self, view_name, table_name):
if great_metadata is not None:
great_metadata_date = datetime.strptime(great_metadata, '%Y-%m-%dT%H:%M:%S').date()
if swapped_date > great_metadata_date:
if settings.APP_ENVIRONMENT != 'production' or (
settings.APP_ENVIRONMENT == 'production'
and datetime.now().date()
> (swapped_date + timedelta(days=settings.GREAT_MARKETGUIDES_REVIEW_PERIOD_DAYS))
):
self.stdout.write(
self.style.SUCCESS(f'Importing {view_name} data into {settings.APP_ENVIRONMENT} env.')
)
return True
self.stdout.write(
self.style.SUCCESS(f'Importing {view_name} data into {settings.APP_ENVIRONMENT} env.')
)
return True

return False

Expand Down
13 changes: 3 additions & 10 deletions dataservices/management/commands/import_market_guides_data.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
from django.conf import settings
from django.core.management import BaseCommand, call_command

from dataservices.management.commands.helpers import (
MarketGuidesDataIngestionCommand,
send_ingest_error_notify_email,
send_review_request_message,
)
from dataservices.management.commands.helpers import MarketGuidesDataIngestionCommand, send_ingest_error_notify_email


class Command(BaseCommand):
Expand Down Expand Up @@ -45,10 +40,8 @@ def handle(self, *args, **options):
self.stdout.write(self.style.NOTICE(f'Running {command_name}'))
try:
call_command(command_name, **options)
call_command('import_metadata_source_data', table=table_view_names['table_name'])

if settings.APP_ENVIRONMENT == 'staging':
send_review_request_message(table_view_names['view_name'])
if options['write']:
call_command('import_metadata_source_data', table=table_view_names['table_name'])

self.stdout.write(self.style.SUCCESS(f'Finished import for {table_view_names["view_name"]}'))
except Exception as e:
Expand Down
43 changes: 17 additions & 26 deletions dataservices/management/commands/tests/test_import_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,35 +433,26 @@ def test_import_metadata_source_data_filter_tables():


@pytest.mark.django_db
@pytest.mark.parametrize(
'env, review_requested_x_times',
[('dev', 0), ('staging', 4), ('uat', 0), ('production', 0)],
)
@mock.patch('dataservices.management.commands.import_market_guides_data.call_command')
@mock.patch('dataservices.management.commands.helpers.MarketGuidesDataIngestionCommand.should_ingestion_run')
@mock.patch('dataservices.management.commands.import_market_guides_data.send_review_request_message')
def test_import_market_guides_data(
mock_send_review_request, mock_should_run, mock_call_command, env, review_requested_x_times
):
with override_settings(APP_ENVIRONMENT=env):
command_list = [
'import_uk_total_trade_data',
'import_uk_trade_in_goods_data',
'import_uk_trade_in_services_data',
'import_world_economic_outlook_data',
]
mock_should_run.return_value = False
management.call_command('import_market_guides_data', '--write')
assert mock_call_command.call_count == 0
def test_import_market_guides_data(mock_should_run, mock_call_command):
command_list = [
'import_uk_total_trade_data',
'import_uk_trade_in_goods_data',
'import_uk_trade_in_services_data',
'import_world_economic_outlook_data',
]
mock_should_run.return_value = False
management.call_command('import_market_guides_data', '--write')
assert mock_call_command.call_count == 0

mock_should_run.return_value = True
management.call_command('import_market_guides_data', '--write')
assert mock_call_command.call_count == 8
assert mock_send_review_request.call_count == review_requested_x_times
mock_should_run.return_value = True
management.call_command('import_market_guides_data', '--write')
assert mock_call_command.call_count == 8

for command in command_list:
assert command in str(mock_call_command.call_args_list)
assert 'write=True' in str(mock_call_command.call_args_list)
for command in command_list:
assert command in str(mock_call_command.call_args_list)
assert 'write=True' in str(mock_call_command.call_args_list)


@pytest.mark.django_db
Expand All @@ -478,7 +469,7 @@ def test_import_market_guides_data_dry_run(mock_call_command, mock_should_run):

management.call_command('import_market_guides_data')

assert mock_call_command.call_count == 8
assert mock_call_command.call_count == 4

for command in command_list:
assert command in str(mock_call_command.call_args_list)
Expand Down
38 changes: 0 additions & 38 deletions dataservices/tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import re
from datetime import datetime, timedelta
from unittest import mock

import pytest
from django.conf import settings
from freezegun import freeze_time

from dataservices import helpers, models
from dataservices.management.commands import helpers as dmch
Expand Down Expand Up @@ -202,42 +200,6 @@ def test_notify_error_message(mock_notify):
assert mock_notify.call_count == 1


@freeze_time('2023-09-13T15:21:10')
@pytest.mark.django_db
@mock.patch('dataservices.management.commands.helpers.notifications_client')
@pytest.mark.parametrize(
"last_release, notification_sent, result",
[
(datetime.now().strftime('%Y-%m-%dT%H:%M:%S'), None, 1),
(
datetime.now().strftime('%Y-%m-%dT%H:%M:%S'),
(datetime.now() - timedelta(days=10)).strftime('%Y-%m-%dT%H:%M:%S'),
1,
),
(
datetime.now().strftime('%Y-%m-%dT%H:%M:%S'),
(datetime.now() + timedelta(minutes=1)).strftime('%Y-%m-%dT%H:%M:%S'),
0,
),
],
)
def test_send_review_request_message(mock_notify, last_release, notification_sent, result):
data = {'source': {'last_release': last_release}}
data['review_process'] = {} if notification_sent is None else {'notification_sent': notification_sent}
factories.MetadataFactory(view_name='TestView', data=data)
dmch.send_review_request_message('TestView')
mock_notify.call_args = mock.call(
email_address='[email protected]',
template_id=settings.GOVNOTIFY_GREAT_MARKETGUIDES_REVIEW_REQUEST_TEMPLATE_ID,
personalisation={
'view_name': 'view_name',
'review_url': 'https://great.staging.uktrade.digital/markets/',
'release_date': 'dd/mm/YYYY',
},
)
assert mock_notify.call_count == result


@pytest.mark.django_db
@pytest.mark.parametrize(
'statista_vertical_name, expected_vertical_name',
Expand Down

0 comments on commit f1d43fd

Please sign in to comment.