Skip to content

Intelligent Analytics API for South African Tender Data - Serverless AWS Lambda delivering real-time business intelligence from comprehensive procurement databases with multi-persona analytics and geographic insights.

License

Notifications You must be signed in to change notification settings

Bread-Corp/AnalyticsQueryLambda

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

12 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“Š Tender Analytics Lambda Function β€” Business Intelligence Powerhouse

AWS Lambda Python 3.9 Amazon RDS SQL Server API Gateway

Transforming tender data into actionable business intelligence! πŸ“ˆ This AWS Lambda function serves as the analytical brain behind the Tender Tool's dynamic dashboard, delivering real-time insights and personalized analytics that power data-driven procurement decisions across South Africa's business landscape.

πŸ“š Table of Contents

πŸ“œ Overview

Welcome to the data science command center! 🎯 This serverless analytics engine transforms raw tender information from our comprehensive database into intelligent, actionable insights. Whether you're a public user exploring market trends or a power user managing complex watchlists, this function delivers personalized analytics that drive smart procurement decisions! πŸš€

What makes it analytically awesome? πŸ’‘

  • 🎭 Multi-Persona Intelligence: Adapts analytics based on user roles and permissions
  • ⚑ Real-Time Processing: Live calculations from massive tender databases
  • πŸ”— API Integration: Seamlessly combines database analytics with external user services
  • 🎯 Smart Fallbacks: Graceful degradation ensures users always get valuable insights

✨ Features

Our analytics engine delivers three distinct intelligence levels, each tailored to specific user needs:

🌍 Public Analytics (Default View)

Perfect for market researchers and newcomers exploring the procurement landscape:

  • πŸ“Š Total Tender Count: Real-time calculation of all opportunities in the system
  • 🟒 Live Opportunity Tracker: Current count of open tenders ready for bidding
  • πŸ”΄ Historical Analysis: Count of closed tenders for trend analysis
  • βš–οΈ Market Ratio Intelligence: Open-to-closed ratio for market health assessment
  • πŸ“ˆ Status Breakdown: Visual representation of opportunity distribution
  • πŸ—ΊοΈ Geographic Intelligence: Provincial breakdown across South Africa's procurement landscape

πŸ‘€ Standard User Analytics

Enhanced insights for registered users managing their procurement portfolios:

  • βœ… All Public Analytics (comprehensive market view)
  • πŸ“‹ Personal Watchlist Intelligence:
    • πŸ‘€ Total tenders under surveillance
    • 🎯 Open opportunities in your portfolio
    • πŸ“Š Personal opportunity ratio analysis
    • ⏰ Deadline proximity alerts (closing soon vs. future opportunities)

🦸 Super User Analytics

Executive-level intelligence for platform administrators and power users:

  • βœ… All Public Analytics (complete market overview)
  • 🏭 Source Distribution Analysis: Tender counts by provider (Eskom, Transnet, SANRAL, SARS, eTenders)
  • πŸ‘₯ Platform Administration Intelligence:
    • πŸ“Š Total platform user count
    • πŸ‘€ Standard user demographics
    • 🦸 Super user population
    • πŸ“ˆ Monthly registration growth metrics

βš™οΈ Architecture & Workflow

Our analytics engine follows an intelligent, adaptive processing flow:

πŸ”„ The Intelligence Pipeline:

  1. 🌐 API Gateway Trigger: HTTP requests hit our /analytics endpoint with lightning-fast response times

  2. πŸ” User Intelligence Detection: Smart header analysis to determine user context and permissions level

  3. 🧠 Adaptive Processing Logic:

    πŸ“₯ Request Analysis
    β”œβ”€ 🚫 No User ID β†’ Public Analytics Pipeline
    β”œβ”€ πŸ‘€ User ID Present
    β”‚   β”œβ”€ πŸ” Super User Check β†’ External API Call
    β”‚   β”œβ”€ 🦸 Super User Confirmed β†’ Full Intelligence Suite
    β”‚   β”œβ”€ πŸ‘€ Standard User β†’ Watchlist Intelligence
    β”‚   └─ ❌ Fallback β†’ Public Analytics
    └─ πŸ“Š Response Generation
    
  4. πŸ—„οΈ Database Intelligence: Lightning-fast SQL queries against our comprehensive RDS database using optimized pymssql connections

  5. πŸ”— External API Orchestration: Seamless integration with user management and watchlist services for personalized insights

  6. πŸ“Š Response Optimization: JSON payload optimization for maximum dashboard performance

πŸ“¦ Deployment

This section covers three deployment methods for the Analytics Query Handler Lambda Function. Choose the method that best fits your workflow and infrastructure preferences.

πŸ› οΈ Prerequisites

Before deploying, ensure you have:

  • AWS CLI configured with appropriate credentials πŸ”‘
  • AWS SAM CLI installed (pip install aws-sam-cli)
  • Python 3.9 runtime support in your target region
  • Access to AWS Lambda, RDS, API Gateway, and CloudWatch Logs services ☁️
  • Analytics layer dependencies for database connectivity
  • VPC configuration for RDS access (if applicable)

🎯 Method 1: AWS Toolkit Deployment

Deploy directly through your IDE using the AWS Toolkit extension.

Setup Steps:

  1. Install AWS Toolkit in your IDE (VS Code, IntelliJ, etc.)
  2. Configure AWS Profile with your credentials
  3. Open Project containing lambda_function.py and db_handler.py

Deploy Process:

  1. Right-click on lambda_function.py in your IDE
  2. Select "Deploy Lambda Function" from AWS Toolkit menu
  3. Configure Deployment:
    • Function Name: AnalyticsQueryHandler
    • Runtime: python3.9
    • Handler: lambda_function.lambda_handler
    • Memory: 128 MB
    • Timeout: 60 seconds
  4. Add Layers manually after deployment:
    • analytics-layer (for database connectivity)
  5. Set Environment Variables:
    DB_ENDPOINT=tender-tool-db.c2hq4seoidxc.us-east-1.rds.amazonaws.com
    DB_NAME=tendertool_db
    DB_USER=AnalyticsAppUser
    DB_PASSWORD=T3nder$Tool_DB_2025!
    USER_FETCH_API_URL=https://api.example.com/dev/tenderuser/fetch/{}
    WATCHLIST_API_URL=https://api.example.com/dev/watchlist/{}
    
  6. Configure IAM Permissions for RDS, VPC, and CloudWatch Logs
  7. Set up API Gateway manually and connect to the Lambda function

Post-Deployment:

  • Test the function using the AWS Toolkit test feature
  • Monitor logs through CloudWatch integration
  • Verify database connectivity and API Gateway integration
  • Test analytics endpoints with different user roles

πŸš€ Method 2: SAM Deployment

Use AWS SAM for infrastructure-as-code deployment with the provided template.

Initial Setup:

# Install AWS SAM CLI
pip install aws-sam-cli

# Verify installation
sam --version

Create Required Layer Directory:

Since the template references an analytics layer not included in the repository, create it:

# Create analytics layer directory
mkdir -p analytics-layer/python

# Install required database and HTTP connectivity packages
pip install pymssql -t analytics-layer/python/
pip install sqlalchemy -t analytics-layer/python/
pip install requests -t analytics-layer/python/
pip install urllib3 -t analytics-layer/python/

Build and Deploy:

# Build the SAM application
sam build

# Deploy with guided configuration (first time)
sam deploy --guided

# Follow the prompts:
# Stack Name: analytics-query-handler-stack
# AWS Region: us-east-1 (or your preferred region)
# Confirm changes before deploy: Y
# Allow SAM to create IAM roles: Y
# Save parameters to samconfig.toml: Y

Environment Variables Setup:

The template already includes the required database environment variables:

# Already configured in template.yml
Environment:
  Variables:
    DB_ENDPOINT: tender-tool-db.c2hq4seoidxc.us-east-1.rds.amazonaws.com
    DB_NAME: tendertool_db
    DB_PASSWORD: T3nder$Tool_DB_2025!
    DB_USER: AnalyticsAppUser

Add Optional API Environment Variables:

# Add external API URLs after initial deployment
aws lambda update-function-configuration \
    --function-name AnalyticsQueryHandler \
    --environment Variables='{
        "DB_ENDPOINT":"tender-tool-db.c2hq4seoidxc.us-east-1.rds.amazonaws.com",
        "DB_NAME":"tendertool_db",
        "DB_USER":"AnalyticsAppUser",
        "DB_PASSWORD":"T3nder$Tool_DB_2025!",
        "USER_FETCH_API_URL":"https://api.example.com/dev/tenderuser/fetch/{}",
        "WATCHLIST_API_URL":"https://api.example.com/dev/watchlist/{}"
    }'

Subsequent Deployments:

# Quick deployment after initial setup
sam build && sam deploy

Local Testing with SAM:

# Test function locally with API Gateway simulation
sam local start-api

# Test specific analytics endpoint
curl http://localhost:3000/analytics

# Test with user headers
curl -H "X-User-ID: user-12345" http://localhost:3000/analytics

SAM Deployment Advantages:

  • βœ… Complete infrastructure management
  • βœ… Automatic layer creation and management
  • βœ… API Gateway integration included
  • βœ… Environment variables defined in template
  • βœ… IAM permissions and VPC configuration
  • βœ… Easy rollback capabilities
  • βœ… CloudFormation integration

πŸ”„ Method 3: Workflow Deployment (CI/CD)

Automated deployment using GitHub Actions workflow for production environments.

Setup Requirements:

  1. GitHub Repository Secrets:

    AWS_ACCESS_KEY_ID: Your AWS access key
    AWS_SECRET_ACCESS_KEY: Your AWS secret key
    AWS_REGION: us-east-1 (or your target region)
    
  2. Pre-existing Lambda Function: The workflow updates an existing function, so deploy initially using Method 1 or 2.

Deployment Process:

  1. Create Release Branch:

    # Create and switch to release branch
    git checkout -b release
    
    # Make your changes to lambda_function.py or db_handler.py
    # Commit changes
    git add .
    git commit -m "feat: update analytics query processing logic"
    
    # Push to trigger deployment
    git push origin release
  2. Automatic Deployment: The workflow will:

    • Checkout the code
    • Configure AWS credentials
    • Create deployment zip with lambda_function.py and db_handler.py
    • Update the existing Lambda function code
    • Maintain existing configuration (layers, environment variables, API Gateway, etc.)

Manual Trigger:

You can also trigger deployment manually:

  1. Go to Actions tab in your GitHub repository
  2. Select "Deploy Python Lambda to AWS" workflow
  3. Click "Run workflow"
  4. Choose the release branch
  5. Click "Run workflow" button

Workflow Deployment Advantages:

  • βœ… Automated CI/CD pipeline
  • βœ… Consistent deployment process
  • βœ… Audit trail of deployments
  • βœ… Easy rollback to previous commits
  • βœ… No local environment dependencies

πŸ”§ Post-Deployment Configuration

Regardless of deployment method, verify the following:

Environment Variables Verification:

Ensure these environment variables are properly set:

# Verify environment variables via AWS CLI
aws lambda get-function-configuration \
    --function-name AnalyticsQueryHandler \
    --query 'Environment.Variables'

Expected output:

{
    "DB_ENDPOINT": "tender-tool-db.c2hq4seoidxc.us-east-1.rds.amazonaws.com",
    "DB_NAME": "tendertool_db",
    "DB_USER": "AnalyticsAppUser",
    "DB_PASSWORD": "T3nder$Tool_DB_2025!",
    "USER_FETCH_API_URL": "https://api.example.com/dev/tenderuser/fetch/{}",
    "WATCHLIST_API_URL": "https://api.example.com/dev/watchlist/{}"
}

Database User Setup:

Ensure the analytics database user exists and has proper permissions:

-- Connect to your SQL Server RDS instance
-- Create the analytics user if not exists
CREATE LOGIN AnalyticsAppUser WITH PASSWORD = 'T3nder$Tool_DB_2025!';
USE tendertool_db;
CREATE USER AnalyticsAppUser FOR LOGIN AnalyticsAppUser;

-- Grant required permissions for analytics queries
GRANT SELECT ON dbo.BaseTender TO AnalyticsAppUser;
GRANT SELECT ON dbo.TenderSource TO AnalyticsAppUser;
GRANT SELECT ON dbo.Province TO AnalyticsAppUser;
GRANT SELECT ON dbo.TenderStatus TO AnalyticsAppUser;
-- Add other necessary table permissions as needed

API Gateway Configuration Verification:

Check that API Gateway is properly configured:

# List API Gateway APIs
aws apigatewayv2 get-apis

# Get specific API configuration
aws apigatewayv2 get-api --api-id [your-api-id]

# Test the analytics endpoint
curl https://[api-id].execute-api.[region].amazonaws.com/analytics

πŸ§ͺ Testing Your Deployment

After deployment, test the function thoroughly:

Test Analytics Endpoints:

# Test public analytics (no headers)
curl https://[api-id].execute-api.[region].amazonaws.com/analytics

# Test standard user analytics
curl -H "X-User-ID: user-12345" \
     https://[api-id].execute-api.[region].amazonaws.com/analytics

# Test super user analytics
curl -H "X-User-ID: superuser-67890" \
     https://[api-id].execute-api.[region].amazonaws.com/analytics

# Test direct Lambda invocation
aws lambda invoke \
    --function-name AnalyticsQueryHandler \
    --payload '{"httpMethod":"GET","path":"/analytics","headers":{}}' \
    response.json

Expected Success Response (Public Analytics):

{
    "statusCode": 200,
    "headers": {
        "Content-Type": "application/json",
        "Access-Control-Allow-Origin": "*"
    },
    "body": "{\"totalTenders\":15847,\"openTenders\":342,\"closedTenders\":15505,\"openToClosedRatio\":0.022}"
}

Expected Success Indicators:

  • βœ… Function executes without errors
  • βœ… CloudWatch logs show successful database connections
  • βœ… API Gateway returns 200 status codes
  • βœ… Analytics data is properly formatted JSON
  • βœ… Different user types receive appropriate data levels
  • βœ… External API integrations work (when configured)

πŸ” Monitoring and Maintenance

CloudWatch Metrics to Monitor:

  • Duration: Function execution time for analytics queries
  • Error Rate: Failed analytics requests
  • Memory Utilization: RAM usage during complex queries
  • API Gateway Metrics: Request counts and latency
  • Database Connection Health: RDS connection metrics

Log Analysis:

# View recent logs
aws logs tail /aws/lambda/AnalyticsQueryHandler --follow

# Search for successful analytics queries
aws logs filter-log-events \
    --log-group-name /aws/lambda/AnalyticsQueryHandler \
    --filter-pattern "Analytics query completed"

# Search for database connection issues
aws logs filter-log-events \
    --log-group-name /aws/lambda/AnalyticsQueryHandler \
    --filter-pattern "Database connection"

# Monitor API Gateway access logs
aws logs filter-log-events \
    --log-group-name /aws/apigateway/[api-id] \
    --filter-pattern "/analytics"

🚨 Troubleshooting Deployments

Analytics Layer Dependencies Missing

Issue: Database connectivity or HTTP request packages not available

Solution: Ensure analytics layer is properly created and attached:

# For SAM: Verify layer directory exists and contains packages
ls -la analytics-layer/python/
ls -la analytics-layer/python/pymssql/
ls -la analytics-layer/python/requests/

# For manual deployment: Create and upload layer separately
Database Connection Failures

Issue: Cannot connect to RDS SQL Server for analytics

Solution: Verify database configuration and network access:

  • Check DB_ENDPOINT points to correct RDS instance
  • Verify AnalyticsAppUser exists and has correct password
  • Ensure Lambda is in same VPC as RDS or configure VPC peering
  • Check RDS security groups allow Lambda subnet access
  • Verify database is accessible and not in maintenance mode
API Gateway Integration Issues

Issue: API Gateway not properly connected to Lambda

Solution: Verify API Gateway configuration:

  • Check API Gateway has correct Lambda integration
  • Verify Lambda permissions allow API Gateway invocation
  • Test API Gateway deployment and stage configuration
  • Check CORS settings if accessing from web applications
VPC and Security Group Configuration

Issue: Lambda cannot access RDS due to VPC restrictions

Solution: Configure VPC properly:

  • Ensure Lambda and RDS are in same VPC
  • Configure security group rules for database port (1433 for SQL Server)
  • Verify subnet routing and NAT gateway configuration
  • Check network ACLs allow database traffic
External API Integration Failures

Issue: User fetch or watchlist APIs not responding

Solution: Implement robust error handling:

  • Verify external API endpoints are accessible
  • Check authentication tokens and API keys
  • Implement graceful fallback to public analytics
  • Monitor external API rate limits and quotas
Environment Variables Not Set

Issue: Missing database or API configuration

Solution: Set environment variables using AWS CLI:

aws lambda update-function-configuration \
    --function-name AnalyticsQueryHandler \
    --environment Variables='{
        "DB_ENDPOINT":"tender-tool-db.c2hq4seoidxc.us-east-1.rds.amazonaws.com",
        "DB_NAME":"tendertool_db",
        "DB_USER":"AnalyticsAppUser",
        "DB_PASSWORD":"T3nder$Tool_DB_2025!",
        "USER_FETCH_API_URL":"https://api.example.com/dev/tenderuser/fetch/{}",
        "WATCHLIST_API_URL":"https://api.example.com/dev/watchlist/{}"
    }'
Workflow Deployment Fails

Issue: GitHub Actions workflow errors

Solution:

  • Check repository secrets are correctly configured
  • Verify the target Lambda function exists in AWS
  • Ensure workflow has correct function ARN
  • Check that both lambda_function.py and db_handler.py exist in repository

Choose the deployment method that best fits your development workflow and infrastructure requirements. SAM deployment is recommended for development environments, while workflow deployment excels for production analytics services requiring high availability and consistent updates.

πŸš€ Usage

🌍 Public Analytics Request

curl https://your-api-id.execute-api.region.amazonaws.com/analytics

πŸ‘€ User-Specific Analytics Request

curl -H "X-User-ID: user-12345" \
     https://your-api-id.execute-api.region.amazonaws.com/analytics

🦸 Super User Analytics Request

curl -H "X-User-ID: superuser-67890" \
     https://your-api-id.execute-api.region.amazonaws.com/analytics

🧰 Troubleshooting

🚨 Common Analytics Challenges

Database Connection Timeouts

Issue: Lambda timing out on database connections.

Solution: Ensure your Lambda is in the same VPC as your RDS instance, or configure appropriate security groups. Database analytics requires reliable connectivity! πŸ—„οΈ

External API Integration Failures

Issue: User fetch or watchlist APIs returning errors.

Solution: Implement robust fallback logic - users should always receive at least public analytics. Check API endpoints and authentication tokens! πŸ”—

Performance Optimization

Issue: Slow response times for complex analytics queries.

Solution: Optimize your SQL queries, consider database indexing, and implement connection pooling. Analytics should be lightning-fast! ⚑

Layer Compatibility Issues

Issue: pymssql layer not working with Lambda runtime.

Solution: Ensure your layer was built on Linux x86_64 architecture matching your Lambda runtime. Use Docker for consistent builds! 🐳

πŸ“Š API Response Examples

🌍 Public Analytics Response

{
  "statusCode": 200,
  "body": {
    "totalTenders": 15847,
    "openTenders": 342,
    "closedTenders": 15505,
    "openToClosedRatio": 0.022,
    "statusBreakdown": {
      "Open": 342,
      "Closed": 15505
    },
    "provinceBreakdown": {
      "Gauteng": 4521,
      "Western Cape": 3102,
      "KwaZulu-Natal": 2876
    }
  }
}

πŸ‘€ Standard User Analytics Response

{
  "statusCode": 200,
  "body": {
    "totalTenders": 15847,
    "openTenders": 342,
    "standardUserAnalytics": {
      "totalWatchedTenders": 23,
      "openWatchedTenders": 8,
      "watchedOpenRatio": 0.348,
      "tendersClosingSoon": 3,
      "tendersClosingLater": 5
    }
  }
}

Built with love, bread, and code by Bread Corporation πŸ¦†β€οΈπŸ’»

About

Intelligent Analytics API for South African Tender Data - Serverless AWS Lambda delivering real-time business intelligence from comprehensive procurement databases with multi-persona analytics and geographic insights.

Topics

Resources

License

Stars

Watchers

Forks

Languages