Refactor dashboard data serialization and mock context for improved clarity
Some checks failed
CI / ci (push) Has been cancelled
Some checks failed
CI / ci (push) Has been cancelled
- Introduced `serialize_audit_for_response` and `serialize_current_cluster_for_template` functions to handle JSON serialization of audit and cluster data, enhancing data consistency for API responses and template rendering. - Updated `get_mock_context` in `mock_data.py` to utilize the new serialization functions, simplifying the mock data structure and improving readability. - Refactored `collect_context` and `collect_audits` in `views.py` to leverage the new serialization methods, ensuring a cleaner and more maintainable codebase. - Added unit tests for the new serialization functions to ensure correctness and reliability of data formatting.
This commit is contained in:
@@ -1,3 +1,15 @@
|
||||
// Escape for safe HTML text content (prevents XSS when inserting into HTML)
|
||||
function escapeHtml(text) {
|
||||
if (text == null) return '';
|
||||
const s = String(text);
|
||||
return s
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''');
|
||||
}
|
||||
|
||||
// Format bytes to GB (matches Django convert_bytes filter default)
|
||||
function formatBytes(bytes, targetUnit = 'GB') {
|
||||
if (bytes == null || isNaN(Number(bytes))) return '0';
|
||||
@@ -24,6 +36,16 @@ function getColorWithOpacity(className) {
|
||||
|
||||
return computedColor;
|
||||
}
|
||||
// Format audit date for display (ISO string -> short date, e.g. "Feb 1")
|
||||
function formatAuditDate(isoString) {
|
||||
if (!isoString) return '';
|
||||
try {
|
||||
return new Date(isoString).toLocaleDateString('en-US', { month: 'short', day: 'numeric' });
|
||||
} catch (e) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
// Utility function to calculate mean and standard deviation
|
||||
function calculateStats(data) {
|
||||
if (!data || data.length === 0) return { mean: 0, std: 0 };
|
||||
|
||||
Reference in New Issue
Block a user