function get_document_stats
Retrieves aggregated statistics about controlled documents from a Neo4j database, including status and type distributions for visualization in charts.
/tf/active/vicechatdev/CDocs/controllers/admin_controller.py
130 - 165
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
loggingtypingCDocs.dbCDocs.models.documentCDocs.models.user_extensionsCDocs.db.schema_managerCDocs.configCDocs.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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_system_stats 74.7% similar
-
function get_all_documents 71.6% similar
-
function get_review_statistics 71.1% similar
-
function get_database_statistics 70.8% similar
-
function get_approval_statistics_v1 65.8% similar