Skip to content

#146: Implement application_analytics_request Lambda with real DB queries#167

Open
dokababa wants to merge 3 commits into
saayam-for-all:mainfrom
dokababa:dokababa_146_application_analytics_api
Open

#146: Implement application_analytics_request Lambda with real DB queries#167
dokababa wants to merge 3 commits into
saayam-for-all:mainfrom
dokababa:dokababa_146_application_analytics_api

Conversation

@dokababa

@dokababa dokababa commented May 7, 2026

Copy link
Copy Markdown

Implements application_analytics_request.py as a real AWS Lambda function querying the Virginia PostgreSQL DB for the Super Admin dashboard analytics.

Closes #146 (pending Phase 3 deployment).

Phase 1 — Code (complete):

  • Request volume trend: last 7 days (daily), 30 days (daily), 1 year (monthly)
  • Category × country aggregation with filter_category, filter_country, sort_by params
  • Top 5 countries with rank
  • Try/except around DB connection (returns 500) and each query (returns [])
  • Cursor and connection closed in finally block
  • All 5 response keys always present
  • if __name__ == "__main__" block for local testing

Phase 2 — Tested locally:

  • Verified live against the Virginia dev RDS — Status 200, all 5 keys populated, filters work, connection closes cleanly
  • Added test_application_analytics_request.py (13 unit tests using mocks) covering: keys present, filters, empty DB, connection failures, finally-block closure
  • Added mock_local_db.py — in-memory SQLite harness mirroring the real schema for local dev without DB credentials

Phase 4 — Security:

  • Credentials read from os.environ only — nothing hardcoded

Pending

  • Phase 3 (AWS Lambda deployment) — needs team lead access

Tables/fields used

request (req_user_id, req_cat_id, submission_date), users (user_id, country_id), country (country_id, country_name), help_categories (cat_id, cat_name)

…th real DB queries

- Adds request volume trend queries (7-day/30-day daily, 1-year monthly)
- Adds category x country aggregation with filter_category, filter_country, sort_by
- Adds top 5 countries with rank
- Wraps DB connection in try/except (500 on failure)
- Wraps query functions in try/except (returns [] on error)
- Closes cursor and connection in finally block
- Reads DB credentials from os.environ (no hardcoded secrets)
- Adds mock_local_db.py SQLite harness for local testing without DB access
- Adds test_application_analytics_request.py with 13 unit tests covering all Phase 2 verifications
@dokababa dokababa force-pushed the dokababa_146_application_analytics_api branch from 22b4ea7 to 683df2a Compare May 7, 2026 20:35

@sana-desai sana-desai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed

@dokababa

Copy link
Copy Markdown
Author

Code reviewed and deployed to AWS Lambda as requestApplicationAnalytics. Test event with {} returned statusCode 200 with all 5 keys populated from the Virginia DB.

dokababa added 2 commits June 2, 2026 17:18
… feedback

- Replace plain env-var DB credentials with AWS Systems Manager Parameter
  Store via boto3 (reads /dev/saayam/db/Virginia/Analytics/user) to match
  the convention used by the other analytics Lambdas (beneficiariesTrendAnalysis,
  volunteerApplicationAnalytics)
- Add CORS headers (Access-Control-Allow-Origin/Headers/Methods) so the
  response can be consumed by the webapp
- Switch country lookups from country_name to country_code in
  top_countries and requests_by_category_region (returns 3-letter ISO
  codes: AFG, USA, ALA, etc.)
- Use UPPER(c.country_code) = %s for case-insensitive country matching
- Rename event filter keys from filter_category/filter_country to
  category/country per UI team request
- Update mock tests to stub boto3 and patch get_db_config so they run
  without AWS access

Verified end-to-end on deployed Lambda (requestApplicationAnalytics)
against the live Virginia RDS:
- Status 200 baseline with all 5 keys populated
- category-only filter returns matching category rows
- country-only filter returns matching country rows
- both-filter combination returns single matching row
…gation

Per reviewer feedback, move the request-volume aggregation out of pandas
and into SQL so all grouping happens in PostgreSQL.

Changes:
- get_request_volume_trend now uses DATE_TRUNC(unit, submission_date)
  with GROUP BY and ORDER BY in a single query, instead of pulling raw
  submission_date rows and using pandas to_period / groupby
- Drop 'import pandas as pd' from the Lambda (no longer used anywhere)
- Update test mocks: SAMPLE_DATES now returns 2-column (date, count)
  tuples to match the new SELECT shape
- Update tests to json.loads(response["body"]) since the body is a
  JSON-encoded string; also rename filter event keys in tests to
  category/country

Verified on deployed Lambda (requestApplicationAnalytics) against the
live Virginia RDS — output format is identical to the pandas version,
cold-start ~35% faster, memory drops from 125MB to 97MB.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement real API for application_analytics_request.py — complete, test & deploy

2 participants