Statistics calculation & analytics

Introduction#

Placement math: company offer counts, package distributions, selection trends, branch splits, time series. What gets cached, what hits MongoDB live, and privacy limits on student-level data.

Project structure#

The statistics service is implemented as a modular Python service integrated into the broader notification bot ecosystem. Key components include:

  • Placement statistics calculator service for computing branch-wise, company-wise, and overall metrics
  • Database service for retrieving and persisting placement offers and statistics
  • Email processing pipeline that extracts and structures placement offers for analytics
  • Configuration and client modules for environment management and database connectivity

Core components#

This section outlines the core components that compute statistics.

  • PlacementStatsCalculatorService

  • Computes overall statistics, branch-wise metrics, and company-wise aggregations

  • Filters students by company, role, location, package range, and search query

  • Provides CSV export functionality for administrative reporting

  • Calculates average, median, and highest packages per unique student

  • Computes placement percentages by branch and overall

  • DatabaseService

  • Retrieves placement offers from MongoDB for analytics

  • Provides raw statistics computation for official placement data

  • Manages collections for notices, jobs, placement offers, users, and official placement data

  • PlacementService

  • Extracts and structures placement offers from emails using LLM orchestration

  • Sanitizes privacy-sensitive information

  • Emits events for new offers and updates for real-time updates

  • DBClient and Configuration

  • Manages MongoDB connection and collection access

  • Centralized configuration via Pydantic Settings with environment variables

Architecture overview#

The statistics architecture integrates data ingestion, processing, persistence, and analytics computation across services and databases.

Detailed component analysis#

PlacementStatsCalculatorService#

This service encapsulates all statistics computation logic, including:

  • Branch resolution from enrollment numbers using predefined ranges
  • Package calculation prioritizing student-specific packages, matching role packages, single-role packages, and maximum among multiple roles
  • Unique student counting per branch and per placement offer
  • Average, median, and highest package computations using the highest package per unique student
  • Branch-wise placement percentages based on eligible student counts
  • Company-wise statistics including student counts, role profiles, and average packages
  • Filtering pipeline supporting company, role, location, package range, and search query
  • CSV export generation for administrative reporting

Database integration and data retrieval#

The DatabaseService provides:

  • Retrieval of placement offers for analytics via get_all_offers
  • Raw statistics computation for official placement data
  • Upsert logic for merging offers and emitting events for new and updated offers
  • Collection access for notices, jobs, placement offers, users, and official placement data

Email processing and privacy sanitization#

PlacementService classifies, extracts, validates, and strips privacy fields. Only final offers make it through.

Data models and storage#

The system stores placement offers and related data in MongoDB collections. The PlacementOffers collection holds extracted offers with company, roles, students, and metadata. The OfficialPlacementData collection stores aggregated statistics snapshots.

Dependency analysis#

The statistics service depends on:

  • DatabaseService for data retrieval and persistence
  • PlacementService for extracting and structuring placement offers
  • Configuration and DBClient for environment and database connectivity

Performance considerations#

  • Caching: Settings are cached via lru_cache to avoid repeated environment loading
  • Batch operations: DatabaseService uses bulk upserts and aggregation for statistics
  • Indexing: Recommended indexes on frequently queried fields (e.g., company, processing_status, timestamps)
  • Connection pooling: PyMongo manages connection pooling automatically
  • Real-time updates: Events emitted on new/updated offers trigger immediate recalculations

[No sources needed since this section provides general guidance]

Troubleshooting guide#

Common issues and resolutions:

  • Database connection failures: Verify MONGO_CONNECTION_STR and IP whitelist for MongoDB Atlas
  • Empty statistics: Ensure placement offers exist in PlacementOffers collection and are not filtered out by exclusions
  • Privacy concerns: PlacementService sanitizes email headers and forwarded information before saving
  • Export issues: Confirm CSV export filters and ensure placements are available

Conclusion#

Stats service rolls offers into company, branch, and package views, with filters and export hooks. It reads the same MongoDB the email pipeline writes, so numbers move when new offers land. Keep student-level fields out of exports.