function get_review_statistics
Retrieves comprehensive review statistics from a Neo4j graph database, including counts of review cycles, approval rates, and reviewer decisions.
/tf/active/vicechatdev/CDocs/controllers/review_controller.py
1774 - 1882
moderate
Purpose
This function queries a Neo4j database to aggregate and calculate statistics about document review cycles and reviewer assignments. It provides metrics such as total reviews, pending/completed reviews, approval/rejection counts, and calculated approval rates for both review cycles and individual reviewer decisions. The function is designed to provide dashboard-level insights into the review system's performance and status.
Source Code
def get_review_statistics() -> Dict[str, Any]:
"""
Get review statistics for the system.
Returns:
Dictionary with review statistics
"""
try:
query = """
// Count all review cycles
MATCH (r:ReviewCycle)
WITH count(r) as total_reviews
// Count pending and in-progress reviews
OPTIONAL MATCH (r:ReviewCycle)
WHERE r.status = 'PENDING' OR r.status = 'IN_PROGRESS'
WITH count(r) as pending_reviews, total_reviews
// Count completed reviews with approved decision
OPTIONAL MATCH (r:ReviewCycle)
WHERE r.status = 'COMPLETED' AND r.decision = 'APPROVED'
WITH count(r) as approved_reviews, pending_reviews, total_reviews
// Count completed reviews with rejected decision
OPTIONAL MATCH (r:ReviewCycle)
WHERE r.status = 'COMPLETED' AND r.decision = 'REJECTED'
WITH count(r) as rejected_reviews, approved_reviews, pending_reviews, total_reviews
// Count reviewer assignments with approved decisions
OPTIONAL MATCH (ra:ReviewerAssignment)
WHERE ra.status = 'COMPLETED' AND
(ra.decision = 'APPROVED' OR ra.decision = 'APPROVED_WITH_COMMENTS')
WITH count(ra) as approved_decisions,
rejected_reviews, approved_reviews, pending_reviews, total_reviews
// Count reviewer assignments with rejected decisions
OPTIONAL MATCH (ra:ReviewerAssignment)
WHERE ra.status = 'COMPLETED' AND ra.decision = 'REJECTED'
WITH count(ra) as rejected_decisions,
approved_decisions, rejected_reviews, approved_reviews,
pending_reviews, total_reviews
RETURN {
total_reviews: total_reviews,
pending_reviews: pending_reviews,
completed_reviews: (approved_reviews + rejected_reviews),
approved_reviews: approved_reviews,
rejected_reviews: rejected_reviews,
review_approval_rate: CASE
WHEN (approved_reviews + rejected_reviews) > 0
THEN 100.0 * approved_reviews / (approved_reviews + rejected_reviews)
ELSE 0
END,
approved_decisions: approved_decisions,
rejected_decisions: rejected_decisions,
decision_approval_rate: CASE
WHEN (approved_decisions + rejected_decisions) > 0
THEN 100.0 * approved_decisions / (approved_decisions + rejected_decisions)
ELSE 0
END
} as statistics
"""
# Execute query
result = db.run_query(query)
if not result or len(result) == 0 or 'statistics' not in result[0]:
# Return default statistics if query returned no results
return {
"success": True,
"statistics": {
"total_reviews": 0,
"pending_reviews": 0,
"completed_reviews": 0,
"approved_reviews": 0,
"rejected_reviews": 0,
"review_approval_rate": 0,
"approved_decisions": 0,
"rejected_decisions": 0,
"decision_approval_rate": 0
}
}
# Return statistics from query
return {
"success": True,
"statistics": result[0].get('statistics', {})
}
except Exception as e:
logger.error(f"Error getting review statistics: {e}")
logger.error(traceback.format_exc())
# Return error with default statistics
return {
"success": False,
"statistics": {
"total_reviews": 0,
"pending_reviews": 0,
"completed_reviews": 0,
"approved_reviews": 0,
"rejected_reviews": 0,
"review_approval_rate": 0,
"approved_decisions": 0,
"rejected_decisions": 0,
"decision_approval_rate": 0
},
"error": str(e)
}
Return Value
Type: Dict[str, Any]
Returns a dictionary with structure: {'success': bool, 'statistics': dict, 'error': str (optional)}. The 'statistics' dictionary contains: 'total_reviews' (int), 'pending_reviews' (int), 'completed_reviews' (int), 'approved_reviews' (int), 'rejected_reviews' (int), 'review_approval_rate' (float, 0-100), 'approved_decisions' (int), 'rejected_decisions' (int), 'decision_approval_rate' (float, 0-100). On error, 'success' is False and 'error' contains the error message, with statistics defaulting to zeros.
Dependencies
logginguuidostypingdatetimetracebackCDocsCDocs.dbCDocs.configCDocs.models.documentCDocs.models.reviewCDocs.models.user_extensionsCDocs.utilsCDocs.controllersCDocs.controllers.document_controllerCDocs.controllers.share_controllerneo4j
Required Imports
import logging
import traceback
from typing import Dict, Any
from CDocs import db
from CDocs.controllers import log_controller_action
Usage Example
from CDocs.controllers.review_controller import get_review_statistics
# Get review statistics
result = get_review_statistics()
if result['success']:
stats = result['statistics']
print(f"Total Reviews: {stats['total_reviews']}")
print(f"Pending Reviews: {stats['pending_reviews']}")
print(f"Approval Rate: {stats['review_approval_rate']:.2f}%")
print(f"Decision Approval Rate: {stats['decision_approval_rate']:.2f}%")
else:
print(f"Error: {result.get('error', 'Unknown error')}")
Best Practices
- Always check the 'success' field in the returned dictionary before accessing statistics
- The function returns default zero values for all statistics on error or when no data exists, ensuring consistent response structure
- The function is decorated with @log_controller_action which logs all invocations for audit purposes
- Approval rates are calculated as percentages (0-100) and return 0 when no completed reviews exist to avoid division by zero
- The function distinguishes between ReviewCycle-level approvals (overall review outcomes) and ReviewerAssignment-level decisions (individual reviewer decisions)
- Error handling is comprehensive - exceptions are logged with full stack traces while returning a safe default response
- The Cypher query uses OPTIONAL MATCH to handle cases where certain node types may not exist in the database
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_approval_statistics_v1 83.1% similar
-
function get_system_stats 77.3% similar
-
function get_document_stats 71.1% similar
-
function get_review_cycle 69.7% similar
-
function get_approval_statistics 69.5% similar