From 04ae06623eafbfeea21460f5eac97dac7a1be9ff Mon Sep 17 00:00:00 2001 From: Roan Song Date: Tue, 5 Jul 2022 08:30:28 +0200 Subject: [PATCH 1/2] Added logging for calls to mautic_proxy In the process of moving from Mautic/Acquia to Hubspot. Starting by adding a log to develop a deeper understanding of requests, in order to prepare for writing the Hubspot part, which would see requests temporarily be sent to both Acquia and Hubspot before turning the Acquia requests off. * Added MauticLog table * Added entry for every call to mautic_proxy --- app/app/settings.py | 3 +- app/dashboard/views.py | 12 +++++-- app/mautic_logging/__init__.py | 0 app/mautic_logging/admin.py | 11 +++++++ app/mautic_logging/apps.py | 5 +++ app/mautic_logging/migrations/0001_initial.py | 32 +++++++++++++++++++ app/mautic_logging/migrations/__init__.py | 0 app/mautic_logging/models.py | 16 ++++++++++ app/mautic_logging/tests.py | 3 ++ app/mautic_logging/views.py | 3 ++ 10 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 app/mautic_logging/__init__.py create mode 100644 app/mautic_logging/admin.py create mode 100644 app/mautic_logging/apps.py create mode 100644 app/mautic_logging/migrations/0001_initial.py create mode 100644 app/mautic_logging/migrations/__init__.py create mode 100644 app/mautic_logging/models.py create mode 100644 app/mautic_logging/tests.py create mode 100644 app/mautic_logging/views.py diff --git a/app/app/settings.py b/app/app/settings.py index f2fd7a8bb0a..aa5ffe280dd 100644 --- a/app/app/settings.py +++ b/app/app/settings.py @@ -156,7 +156,8 @@ 'adminsortable2', 'debug_toolbar', 'passport', - 'quadraticlands' + 'quadraticlands', + 'mautic_logging', ] MIDDLEWARE = [ diff --git a/app/dashboard/views.py b/app/dashboard/views.py index b67ec0f9481..8d2719a15c3 100644 --- a/app/dashboard/views.py +++ b/app/dashboard/views.py @@ -103,6 +103,7 @@ wall_post_email, ) from marketing.models import Keyword +from mautic_logging.models import MauticLog from oauth2_provider.decorators import protected_resource from oauthlib.oauth2.rfc6749.errors import InvalidGrantError from perftools.models import JSONStore, StaticJsonEnv @@ -7538,9 +7539,16 @@ def mautic_proxy_backend(method="GET", endpoint='', payload=None, params=None): if payload: body_unicode = payload.decode('utf-8') payload = json.loads(body_unicode) - response = getattr(requests, method.lower())(url=url, headers=headers, params=params, data=json.dumps(payload)).json() + http_response = getattr(requests, method.lower())(url=url, headers=headers, params=params, data=json.dumps(payload)) else: - response = getattr(requests, method.lower())(url=url, headers=headers, params=params).json() + http_response = getattr(requests, method.lower())(url=url, headers=headers, params=params) + + response = http_response.json() + + + # Temporary logging of Mautic interaction in order to prepare for a move over from Mautic to Hubspot. + log = MauticLog(method=method, endpoint=endpoint, payload=payload, params=params, status_code=http_response.status_code) + log.save() return response diff --git a/app/mautic_logging/__init__.py b/app/mautic_logging/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/app/mautic_logging/admin.py b/app/mautic_logging/admin.py new file mode 100644 index 00000000000..78871bb72a2 --- /dev/null +++ b/app/mautic_logging/admin.py @@ -0,0 +1,11 @@ +from django.contrib import admin + +from .models import MauticLog + + +class MauticLogAdmin(admin.ModelAdmin): + ordering = ['-id'] + list_display = ['created_on', 'endpoint', 'status_code', 'method', 'params', 'payload'] + search_fields = ['created_on', 'endpoint', 'status_code', 'method', 'params', 'payload'] + +admin.site.register(MauticLog, MauticLogAdmin) diff --git a/app/mautic_logging/apps.py b/app/mautic_logging/apps.py new file mode 100644 index 00000000000..bb30bb4d749 --- /dev/null +++ b/app/mautic_logging/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class MauticLoggingConfig(AppConfig): + name = 'mautic_logging' diff --git a/app/mautic_logging/migrations/0001_initial.py b/app/mautic_logging/migrations/0001_initial.py new file mode 100644 index 00000000000..79db498b267 --- /dev/null +++ b/app/mautic_logging/migrations/0001_initial.py @@ -0,0 +1,32 @@ +# Generated by Django 2.2.24 on 2022-07-05 06:10 + +import django.contrib.postgres.fields.jsonb +from django.db import migrations, models +import economy.models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='MauticLog', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('created_on', models.DateTimeField(db_index=True, default=economy.models.get_time)), + ('modified_on', models.DateTimeField(default=economy.models.get_time)), + ('status_code', models.IntegerField()), + ('method', models.CharField(max_length=5)), + ('endpoint', models.TextField()), + ('payload', django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=dict, null=True)), + ('params', django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=dict, null=True)), + ], + options={ + 'abstract': False, + }, + ), + ] diff --git a/app/mautic_logging/migrations/__init__.py b/app/mautic_logging/migrations/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/app/mautic_logging/models.py b/app/mautic_logging/models.py new file mode 100644 index 00000000000..3d3c199f76f --- /dev/null +++ b/app/mautic_logging/models.py @@ -0,0 +1,16 @@ +from django.contrib.postgres.fields import JSONField +from django.db import models + +from economy.models import SuperModel + + +class MauticLog(SuperModel): + """Define the MauticLog model. Used to store requests to the mautic_proxy for further analysis""" + status_code = models.IntegerField() + method = models.CharField(max_length=5) + endpoint = models.TextField() + payload = JSONField(null=True, default=dict, blank=True) + params = JSONField(null=True, default=dict, blank=True) + + def __str__(self): + return f"[{self.status_code}] {self.endpoint}" diff --git a/app/mautic_logging/tests.py b/app/mautic_logging/tests.py new file mode 100644 index 00000000000..7ce503c2dd9 --- /dev/null +++ b/app/mautic_logging/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/app/mautic_logging/views.py b/app/mautic_logging/views.py new file mode 100644 index 00000000000..91ea44a218f --- /dev/null +++ b/app/mautic_logging/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. From be8f483759460a665ef3c5b789748a28e973a8e4 Mon Sep 17 00:00:00 2001 From: Roan Song Date: Tue, 5 Jul 2022 08:33:54 +0200 Subject: [PATCH 2/2] Wrapped DB call in try-except --- app/dashboard/views.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/dashboard/views.py b/app/dashboard/views.py index 8d2719a15c3..7d20146424c 100644 --- a/app/dashboard/views.py +++ b/app/dashboard/views.py @@ -7547,8 +7547,11 @@ def mautic_proxy_backend(method="GET", endpoint='', payload=None, params=None): # Temporary logging of Mautic interaction in order to prepare for a move over from Mautic to Hubspot. - log = MauticLog(method=method, endpoint=endpoint, payload=payload, params=params, status_code=http_response.status_code) - log.save() + try: + log = MauticLog(method=method, endpoint=endpoint, payload=payload, params=params, status_code=http_response.status_code) + log.save() + except Exception: + pass return response