Skip to content

Commit

Permalink
Fix mozilla-it#461: Add endpoint to expose Acoustic /configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Nov 25, 2022
1 parent 3d0c007 commit 652ac69
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
27 changes: 27 additions & 0 deletions ctms/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys
import time
from base64 import b64decode
from collections import defaultdict
from datetime import datetime, timedelta
from functools import lru_cache
from typing import Dict, List, Literal, Optional, Tuple, Union
Expand Down Expand Up @@ -39,6 +40,8 @@
create_contact,
create_or_update_contact,
get_api_client_by_id,
get_all_acoustic_fields,
get_all_acoustic_newsletters_mapping,
get_bulk_contacts,
get_contact_by_email_id,
get_email,
Expand Down Expand Up @@ -916,6 +919,30 @@ def metrics(request: Request):
return Response(generate_latest(registry), status_code=200, headers=headers)


@app.get("/configuration", tags=["Platform"])
def configuration(
request: Request,
db_session: Session = Depends(get_db),
):
"""Return configuration information, publicly readable"""
all_fields = get_all_acoustic_fields(db_session)
fields_grouped_by_tablename = defaultdict(list)
for entry in all_fields:
fields_grouped_by_tablename[entry.tablename].append(entry.field)

newsletter_mappings = {
entry.source: entry.destination
for entry in get_all_acoustic_newsletters_mapping(db_session)
}

return {
"acoustic": {
"sync_fields": fields_grouped_by_tablename,
"newsletter_mappings": newsletter_mappings,
}
}


def _process_stripe_object(
db_session: Session, data: Dict
) -> Tuple[Optional[UUID], Optional[str], Optional[str], StripeIngestActions]:
Expand Down
4 changes: 3 additions & 1 deletion ctms/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,9 @@ def get_product_id(prod: ProductBaseSchema) -> str:


def get_all_acoustic_fields(dbsession, tablename=None):
query = dbsession.query(AcousticField)
query = dbsession.query(AcousticField).order_by(
asc(AcousticField.tablename), asc(AcousticField.field)
)
if tablename:
query = query.filter(AcousticField.tablename == tablename)
return query.all()
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,11 @@ def test_crash_unauthorized(anon_client):
resp = anon_client.get("/__crash__")
assert resp.status_code == 401
assert resp.json() == {"detail": "Not authenticated"}


def test_exposed_configuration(anon_client, dbsession):
resp = anon_client.get("/configuration")
exposed = resp.json()

assert "fxa_lang" in exposed["acoustic"]["sync_fields"]["main"]
assert exposed["acoustic"]["newsletter_mappings"]["mozilla-rally"] == "sub_rally"
4 changes: 2 additions & 2 deletions tests/unit/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
# Higher numbers = more ways to slice data, more storage, more processing time for summaries

# Cardinality of ctms_requests_total counter
METHOD_PATH_CODE_COMBINATIONS = 50
METHOD_PATH_CODE_COMBINATIONS = 51

# Cardinality of ctms_requests_duration_seconds histogram
METHOD_PATH_CODEFAM_COMBOS = 36
METHOD_PATH_CODEFAM_COMBOS = 37
DURATION_BUCKETS = 8
DURATION_COMBINATIONS = METHOD_PATH_CODEFAM_COMBOS * (DURATION_BUCKETS + 2)

Expand Down

0 comments on commit 652ac69

Please sign in to comment.