🔍 Code Extractor

function get_document_stats

Maturity: 49

Retrieves aggregated statistics about controlled documents from a Neo4j database, including status and type distributions for visualization in charts.

File:
/tf/active/vicechatdev/CDocs/controllers/admin_controller.py
Lines:
130 - 165
Complexity:
moderate

Purpose

This function queries a Neo4j graph database to collect document statistics for dashboard and reporting purposes. It returns two key metrics: the distribution of documents by status (DRAFT, IN_REVIEW, APPROVED) and by document type (SOP, POLICY, WORK_INSTRUCTION). The function includes error handling to return default zero-count distributions if queries fail or the database is unavailable.

Source Code

def get_document_stats() -> Dict[str, Any]:
    """Get document statistics for charts."""
    try:
        stats = {}
        
        # Document status distribution
        status_query = """
        MATCH (d:ControlledDocument)
        RETURN d.status as status, count(d) as count
        """
        status_result = db.run_query(status_query)
        if status_result:
            status_dist = {row['status']: row['count'] for row in status_result if 'status' in row}
            stats['status_distribution'] = status_dist
        else:
            stats['status_distribution'] = {'DRAFT': 0, 'IN_REVIEW': 0, 'APPROVED': 0}
        
        # Document type distribution
        type_query = """
        MATCH (d:ControlledDocument)
        RETURN d.docType as type, count(d) as count
        """
        type_result = db.run_query(type_query)
        if type_result:
            type_dist = {row['type']: row['count'] for row in type_result if 'type' in row}
            stats['type_distribution'] = type_dist
        else:
            stats['type_distribution'] = {'SOP': 0, 'POLICY': 0, 'WORK_INSTRUCTION': 0}
        
        return stats
    except Exception as e:
        logger.error(f"Error getting document stats: {e}")
        return {
            'status_distribution': {'DRAFT': 0, 'IN_REVIEW': 0, 'APPROVED': 0},
            'type_distribution': {'SOP': 0, 'POLICY': 0, 'WORK_INSTRUCTION': 0}
        }

Return Value

Type: Dict[str, Any]

Returns a dictionary with two keys: 'status_distribution' containing a mapping of document statuses to their counts, and 'type_distribution' containing a mapping of document types to their counts. On error, returns default dictionaries with zero counts for all standard statuses and types. Type: Dict[str, Any] where values are Dict[str, int].

Dependencies

  • logging
  • typing
  • CDocs.db
  • CDocs.models.document
  • CDocs.models.user_extensions
  • CDocs.db.schema_manager
  • CDocs.config
  • CDocs.utils.audit_trail

Required Imports

from typing import Dict, Any
import logging
from CDocs import db

Usage Example

from typing import Dict, Any
import logging
from CDocs import db

# Ensure logger is configured
logger = logging.getLogger(__name__)

# Call the function to get document statistics
stats = get_document_stats()

# Access status distribution
print(f"Draft documents: {stats['status_distribution'].get('DRAFT', 0)}")
print(f"In review: {stats['status_distribution'].get('IN_REVIEW', 0)}")
print(f"Approved: {stats['status_distribution'].get('APPROVED', 0)}")

# Access type distribution
print(f"SOPs: {stats['type_distribution'].get('SOP', 0)}")
print(f"Policies: {stats['type_distribution'].get('POLICY', 0)}")
print(f"Work Instructions: {stats['type_distribution'].get('WORK_INSTRUCTION', 0)}")

Best Practices

  • Ensure the Neo4j database connection is established before calling this function
  • The function returns default zero-count distributions on error, so always check if values are meaningful in production
  • Consider caching results if this function is called frequently, as it performs two database queries
  • The function assumes specific document statuses (DRAFT, IN_REVIEW, APPROVED) and types (SOP, POLICY, WORK_INSTRUCTION) - ensure your data model aligns with these
  • Monitor the logger for errors to detect database connectivity or query issues
  • The function uses db.run_query() which should handle connection pooling and transaction management
  • Consider adding query timeouts if the document collection grows large

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_system_stats 74.7% similar

    Retrieves comprehensive system statistics from a Neo4j graph database for display on an admin dashboard, including user counts, document counts, review cycles, and approval metrics.

    From: /tf/active/vicechatdev/CDocs/controllers/admin_controller.py
  • function get_all_documents 71.6% similar

    Retrieves all controlled documents from a Neo4j graph database with their associated owner information, formatted for administrative management interfaces.

    From: /tf/active/vicechatdev/CDocs/controllers/admin_controller.py
  • function get_review_statistics 71.1% similar

    Retrieves comprehensive review statistics from a Neo4j graph database, including counts of review cycles, approval rates, and reviewer decisions.

    From: /tf/active/vicechatdev/CDocs/controllers/review_controller.py
  • function get_database_statistics 70.8% similar

    Retrieves statistical information about a Neo4j graph database, including counts of nodes grouped by label and counts of relationships grouped by type.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function get_approval_statistics_v1 65.8% similar

    Retrieves comprehensive approval statistics from a Neo4j graph database, including counts of total, pending, completed, approved, and rejected approval cycles and decisions, along with calculated approval rates.

    From: /tf/active/vicechatdev/CDocs/controllers/approval_controller.py
← Back to Browse