mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 23:55:58 -05:00
Add, init and config performance platform client to prepare/upload daily notification stats
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import os
|
||||
import uuid
|
||||
import json
|
||||
|
||||
from flask import Flask, _request_ctx_stack
|
||||
from flask import request, url_for, g, jsonify
|
||||
@@ -18,6 +17,7 @@ from app.clients.email.aws_ses import AwsSesClient
|
||||
from app.clients.sms.firetext import FiretextClient
|
||||
from app.clients.sms.loadtesting import LoadtestingClient
|
||||
from app.clients.sms.mmg import MMGClient
|
||||
from app.clients.performance_platform.performance_platform_client import PerformancePlatformClient
|
||||
from app.encryption import Encryption
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ aws_ses_client = AwsSesClient()
|
||||
encryption = Encryption()
|
||||
statsd_client = StatsdClient()
|
||||
redis_store = RedisClient()
|
||||
performance_platform_client = PerformancePlatformClient()
|
||||
|
||||
clients = Clients()
|
||||
|
||||
|
||||
0
app/clients/performance_platform/__init__.py
Normal file
0
app/clients/performance_platform/__init__.py
Normal file
@@ -0,0 +1,57 @@
|
||||
import base64
|
||||
import json
|
||||
from requests import request
|
||||
|
||||
from flask import current_app
|
||||
|
||||
|
||||
class PerformancePlatformClient:
|
||||
|
||||
def init_app(self, app):
|
||||
self.active = app.config.get('PERFORMANCE_PLATFORM_ENABLED')
|
||||
if self.active:
|
||||
self.bearer_token = app.config.get('PERFORMANCE_PLATFORM_TOKEN')
|
||||
self.performance_platform_url = current_app.config.get('PERFORMANCE_PLATFORM_URL')
|
||||
|
||||
def send_performance_stats(self, date, channel, count, period):
|
||||
if self.active:
|
||||
payload = {
|
||||
'_timestamp': date,
|
||||
'service': 'govuk-notify',
|
||||
'channel': channel,
|
||||
'count': count,
|
||||
'dataType': 'notifications',
|
||||
'period': period
|
||||
}
|
||||
self._add_id_for_payload(payload)
|
||||
self._send_stats_to_performance_platform(payload)
|
||||
|
||||
def _send_stats_to_performance_platform(self, payload):
|
||||
headers = {
|
||||
'Content-Type': "application/json",
|
||||
'Authorization': 'Bearer {}'.format(self.bearer_token)
|
||||
}
|
||||
resp = request(
|
||||
"POST",
|
||||
self.performance_platform_url,
|
||||
data=json.dumps(payload),
|
||||
headers=headers
|
||||
)
|
||||
|
||||
if resp.status_code != 200:
|
||||
current_app.logger.error(
|
||||
"Performance platform update request failed with {} '{}'".format(
|
||||
resp.status_code,
|
||||
resp.json())
|
||||
)
|
||||
|
||||
def _add_id_for_payload(self, payload):
|
||||
payload_string = '{}{}{}{}{}'.format(
|
||||
payload['_timestamp'],
|
||||
payload['service'],
|
||||
payload['channel'],
|
||||
payload['dataType'],
|
||||
payload['period']
|
||||
)
|
||||
_id = base64.b64encode(payload_string.encode('utf-8'))
|
||||
payload.update({'_id': _id.decode('utf-8')})
|
||||
@@ -50,6 +50,11 @@ class Config(object):
|
||||
REDIS_URL = os.getenv('REDIS_URL')
|
||||
REDIS_ENABLED = os.getenv('REDIS_ENABLED') == '1'
|
||||
|
||||
# Performance platform
|
||||
PERFORMANCE_PLATFORM_ENABLED = os.getenv('PERFORMANCE_PLATFORM_ENABLED') == '1'
|
||||
PERFORMANCE_PLATFORM_URL = os.getenv('PERFORMANCE_PLATFORM_URL')
|
||||
PERFORMANCE_PLATFORM_TOKEN = os.getenv('PERFORMANCE_PLATFORM_TOKEN')
|
||||
|
||||
# Logging
|
||||
DEBUG = False
|
||||
LOGGING_STDOUT_JSON = os.getenv('LOGGING_STDOUT_JSON') == '1'
|
||||
|
||||
Reference in New Issue
Block a user