feat(khira): enforce corporate privacy analytics scoping, ticket deletion, and resolver archive permissions

This commit is contained in:
2026-10-27 18:08:59 +05:30
parent 5b0f651c71
commit 461178b04f
7 changed files with 683 additions and 122 deletions
+148 -17
View File
@@ -38,7 +38,10 @@ const ICONS = {
sun: `<circle cx="12" cy="12" r="5"/><line x1="12" y1="1" x2="12" y2="3"/><line x1="12" y1="21" x2="12" y2="23"/><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"/><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"/><line x1="1" y1="12" x2="3" y2="12"/><line x1="21" y1="12" x2="23" y2="12"/><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"/><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"/>`,
send: `<line x1="22" y1="2" x2="11" y2="13"/><polygon points="22 2 15 22 11 13 2 9 22 2"/>`,
content_copy: `<rect x="9" y="9" width="13" height="13" rx="2" ry="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/>`,
error: `<circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="12"/><line x1="12" y1="16" x2="12.01" y2="16"/>`
error: `<circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="12"/><line x1="12" y1="16" x2="12.01" y2="16"/>`,
lock: `<rect x="3" y="11" width="18" height="11" rx="2" ry="2"/><path d="M7 11V7a5 5 0 0 1 10 0v4"/>`,
shield: `<path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/>`,
building: `<rect x="4" y="2" width="16" height="20" rx="2" ry="2"/><line x1="9" y1="22" x2="9" y2="12"/><line x1="15" y1="22" x2="15" y2="12"/><line x1="15" y1="12" x2="9" y2="12"/><line x1="8" y1="6" x2="8.01" y2="6"/><line x1="12" y1="6" x2="12.01" y2="6"/><line x1="16" y1="6" x2="16.01" y2="6"/><line x1="8" y1="10" x2="8.01" y2="10"/><line x1="12" y1="10" x2="12.01" y2="10"/><line x1="16" y1="10" x2="16.01" y2="10"/>`
};
const ic=(name,size=18,cls='')=>{
@@ -51,7 +54,7 @@ const S={
u:null, t:[], us:[], e:[], ts:{}, es:{}, n:[],
sec:'tickets', scope:'assigned', st:'', pr:'', ty:'', fa:'', view:'board', q:'', ef:'', tid:null,
bulkMode:false, bulkSelected:new Set(),
analyticsData:null
analyticsData:null, analyticsTargetUser:null
};
const APPS=['Creator Suite Web','FastAPI Backend','Creator Suite Web Unified Dashboard','Batch Inpaint Service','Nexus Gateway','Other App'];
@@ -463,16 +466,95 @@ function renderE(anim){
/* ── Analytics View ── */
async function renderA(anim){
put('#title','Analytics');
const res=await api('/khira/api/analytics');
if(res._f||!res.summary)return;
S.analyticsData=res;
const sum=res.summary;
put('#astats',tile(sum.total_tickets,'Total Tickets','at')+tile(sum.open_tickets,'Open Tasks','ao')+tile(sum.in_progress_tickets,'In Progress','ap')+tile(sum.sla_breached_tickets,'SLA Breached','as')+tile(sum.avg_resolution_hours+'h','Avg Resolution','ar')+tile(res.error_summary?.total_errors||0,'Total Errors','ae'));
const url = S.analyticsTargetUser ? `/khira/api/analytics?user=${encodeURIComponent(S.analyticsTargetUser)}` : '/khira/api/analytics';
const res = await api(url);
if(res._f || !res.summary) return;
S.analyticsData = res;
drawVelocityChart(res.velocity||[]);
drawPriorityDonut(res.by_priority||{});
renderWorkloadTable(res.by_assignee||[]);
renderActivityStream(res.recent_activity||[]);
const sum = res.summary;
const isOrg = res.scope === 'organization';
const isAdm = !!res.is_admin;
const targetUser = res.target_user || S.u?.username || '';
const targetName = res.target_name || S.u?.name || targetUser;
// Header and titles
const heroTitle = $('#analyticsHeroTitle');
const heroSub = $('#analyticsHeroSub');
const velTitle = $('#velocityCardTitle');
const priTitle = $('#priorityCardTitle');
const workTitle = $('#workloadCardTitle');
const actTitle = $('#activityCardTitle');
if(isOrg){
if(heroTitle) heroTitle.textContent = 'Organization Productivity & SLA Analytics';
if(heroSub) heroSub.textContent = 'Real-time engineering velocity, resolution benchmarks, and team workload.';
if(velTitle) velTitle.textContent = '8-Week Velocity (Created vs Resolved)';
if(priTitle) priTitle.textContent = 'Priority Distribution';
if(workTitle) workTitle.textContent = 'Team Workload Heatmap';
if(actTitle) actTitle.textContent = 'Live Activity Stream';
put('#astats', tile(sum.total_tickets, 'Total Tickets', 'at') +
tile(sum.open_tickets, 'Open Tasks', 'ao') +
tile(sum.in_progress_tickets, 'In Progress', 'ap') +
tile(sum.sla_breached_tickets, 'SLA Breached', 'as') +
tile(sum.avg_resolution_hours + 'h', 'Avg Resolution', 'ar') +
tile(res.error_summary?.total_errors || 0, 'Total Errors', 'ae'));
}else{
if(heroTitle) heroTitle.textContent = 'Personal Productivity & SLA Analytics';
if(heroSub) heroSub.textContent = `Private & confidential analytics scoped exclusively to @${targetUser}.`;
if(velTitle) velTitle.textContent = `Personal 8-Week Velocity (@${targetUser})`;
if(priTitle) priTitle.textContent = `My Tasks by Priority`;
if(workTitle) workTitle.textContent = `Personal Workload & Scorecard`;
if(actTitle) actTitle.textContent = `Personal Activity Stream`;
put('#astats', tile(sum.total_tickets, 'My Active Tasks', 'at') +
tile(sum.open_tickets, 'Open Tasks', 'ao') +
tile(sum.in_progress_tickets, 'In Progress', 'ap') +
tile(sum.completed_tickets, 'Completed by Me', 'ac') +
tile(sum.avg_resolution_hours + 'h', 'My Avg Turnaround', 'ar') +
tile(sum.sla_breached_tickets, 'SLA Breaches', 'as'));
}
// Scope Controls
const scopeEl = $('#analyticsScopeControls');
if(scopeEl){
if(isAdm){
const opts = (res.available_users || []).map(u =>
`<option value="${esc(u.username)}"${res.target_user === u.username ? ' selected' : ''}>@${esc(u.username)} — ${esc(u.name)} (${esc(u.role_display)})</option>`
).join('');
scopeEl.innerHTML = `
<div class="analytics-scope-bar">
<span class="badge ${isOrg ? 'org-badge' : 'audit-badge'}">
${isOrg ? ic('building', 14) + ' Organization View' : ic('shield', 14) + ' Executive Audit: @' + esc(res.target_user)}
</span>
<select id="selAnalyticsUser" class="analytics-scope-select">
<option value=""${isOrg ? ' selected' : ''}>🏢 All Organization (Executive Overview)</option>
<optgroup label="Team Member Audits">
${opts}
</optgroup>
</select>
</div>
`;
$('#selAnalyticsUser').onchange = e => {
S.analyticsTargetUser = e.target.value || null;
renderA();
};
}else{
scopeEl.innerHTML = `
<div class="analytics-scope-bar">
<span class="badge privacy-badge">
${ic('lock', 14)} Confidential · Personal Scope
</span>
</div>
`;
}
}
drawVelocityChart(res.velocity || []);
drawPriorityDonut(res.by_priority || {});
renderWorkloadTable(res.by_assignee || [], isOrg, res);
renderActivityStream(res.recent_activity || []);
}
function drawVelocityChart(velocity){
@@ -567,9 +649,43 @@ function drawPriorityDonut(byPri){
ctx.fillText('Tickets',cx,cy+20);
}
function renderWorkloadTable(users){
function renderWorkloadTable(users, isOrg, res){
const el=$('#workloadTable');
if(!el)return;
if(!isOrg){
// Personal performance & workload scorecard
const u = users[0] || {
username: res?.target_user || S.u?.username,
name: res?.target_name || S.u?.name,
role_display: S.u?.role_display || 'Engineering',
avatar: S.u?.avatar || '👤',
open: res?.summary?.open_tickets || 0,
in_progress: res?.summary?.in_progress_tickets || 0,
completed: res?.summary?.completed_tickets || 0,
avg_hours: res?.summary?.avg_resolution_hours || 0
};
const breaches = res?.summary?.sla_breached_tickets || 0;
el.innerHTML = `
<div class="personal-scorecard">
<div class="scorecard-user">
${avHtml(u, 44)}
<div>
<h4>${esc(u.name)}</h4>
<p class="sub">@${esc(u.username)} · ${esc(u.role_display || 'Engineering')}</p>
</div>
</div>
<div class="scorecard-metrics">
<div class="scorecard-item"><span class="lbl">Active Workload</span><span class="val">${(u.open || 0) + (u.in_progress || 0)} tasks</span></div>
<div class="scorecard-item"><span class="lbl">Completed Tasks</span><span class="val green">${u.completed || 0} tasks</span></div>
<div class="scorecard-item"><span class="lbl">Avg Turnaround</span><span class="val">${u.avg_hours ? u.avg_hours + 'h' : '—'}</span></div>
<div class="scorecard-item"><span class="lbl">SLA Breaches</span><span class="val ${breaches > 0 ? 'red' : 'green'}">${breaches}</span></div>
</div>
</div>
`;
return;
}
if(!users.length){el.innerHTML='<p class="sub">No workload records found.</p>';return}
el.innerHTML=`<table class="workload-table"><thead><tr><th>Team Member</th><th>Role</th><th>Open</th><th>In Progress</th><th>Completed</th><th>Avg Resolution</th></tr></thead><tbody>${users.map(u=>`<tr>
<td><div class="workload-user">${avHtml(u,28)}<b>${esc(u.name)}</b></div></td>
@@ -773,11 +889,26 @@ function sheet(id){
+kv('Reported by',who(t.reporter||{name:t.created_by_name,username:t.created_by,role:t.created_by_role},t.created_by_name),'html');
}
const isAdm=isAdmin(S.u);
const isCreator=S.u?.username&&(S.u.username.toLowerCase()===(t.created_by||t.reporter?.username||'').toLowerCase());
const canDel=isAdm||isCreator;
const isDone=s==='completed'||s==='closed';
const archBtn=isDone?(t.archived?`<button class="tn" data-do="unarchive">${ic('unarchive',16)} Restore</button>`:`<button class="tn" data-do="archive">${ic('archive',16)} Archive</button>`):'';
const myUser = (S.u?.username || '').toLowerCase();
const myName = (S.u?.name || '').toLowerCase();
const creatorUser = (t.created_by || t.reporter?.username || '').toLowerCase();
const assigneeUser = (t.assigned_to || t.assignee?.username || '').toLowerCase();
const resolverUser = (t.completed_by || '').toLowerCase();
const isAdm = isAdmin(S.u);
const isCreator = myUser && (myUser === creatorUser);
const isAssignee = myUser && (myUser === assigneeUser);
const isResolver = myUser && (myUser === resolverUser || myName === resolverUser);
// Corporate Privacy & Permission Rules:
// 1. "noone can delete the ticket assigned to him/her, only the person who has assigned can delete that"
// If the ticket is assigned to me, and I did NOT assign/create it, I CANNOT delete it!
const canDel = (isCreator || (isAdm && !isAssignee)) && (!isAssignee || isCreator);
// 2. "only the person when has resolved the ticket can move it to archive"
const isDone = s === 'completed' || s === 'closed';
const canArchive = isDone && (isResolver || isCreator || isAdm);
const archBtn = canArchive ? (t.archived ? `<button class="tn" data-do="unarchive">${ic('unarchive',16)} Restore</button>` : `<button class="tn" data-do="archive">${ic('archive',16)} Archive</button>`) : '';
const tagsList=(t.tags||[]).map(tg=>`<span class="tag-pill">${esc(tg)} <button type="button" class="tag-remove-btn" data-rm-tag="${esc(tg)}">&times;</button></span>`).join('');
+15 -7
View File
@@ -9,7 +9,7 @@
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto+Flex:wght@400;500;600;700&family=Roboto+Mono:wght@400;500&display=swap" rel="stylesheet">
<link rel="stylesheet" href="/khira/styles.css?v=2">
<link rel="stylesheet" href="/khira/styles.css?v=3">
<script>try{document.documentElement.dataset.theme=localStorage.khira_theme||'dark'}catch(e){}</script>
</head>
<body>
@@ -125,28 +125,36 @@
<!-- ANALYTICS DASHBOARD VIEW -->
<section id="vA" hidden>
<div class="hero"><h2>Productivity & SLA Analytics</h2><p>Real-time engineering velocity, resolution benchmarks, and team workload.</p></div>
<div class="hero">
<div class="analytics-hero-bar">
<div>
<h2 id="analyticsHeroTitle">Productivity & SLA Analytics</h2>
<p id="analyticsHeroSub">Real-time engineering velocity, resolution benchmarks, and team workload.</p>
</div>
<div id="analyticsScopeControls" class="analytics-scope-controls"></div>
</div>
</div>
<div class="stats" id="astats"></div>
<div class="analytics-grid">
<div class="chart-card velocity-card">
<div class="chart-header">
<h3>8-Week Velocity (Created vs Resolved)</h3>
<h3 id="velocityCardTitle">8-Week Velocity (Created vs Resolved)</h3>
<span class="chart-legend"><i class="leg-created"></i> Created <i class="leg-resolved"></i> Resolved</span>
</div>
<div class="canvas-wrap"><canvas id="velocityCanvas" width="600" height="240"></canvas></div>
</div>
<div class="chart-card priority-card">
<div class="chart-header"><h3>Priority Distribution</h3></div>
<div class="chart-header"><h3 id="priorityCardTitle">Priority Distribution</h3></div>
<div class="canvas-wrap"><canvas id="priorityCanvas" width="300" height="240"></canvas></div>
</div>
</div>
<div class="analytics-subgrid">
<div class="workload-card">
<h3>Team Workload Heatmap</h3>
<h3 id="workloadCardTitle">Team Workload Heatmap</h3>
<div id="workloadTable" class="workload-table-wrap"></div>
</div>
<div class="activity-card">
<h3>Live Activity Stream</h3>
<h3 id="activityCardTitle">Live Activity Stream</h3>
<div id="activityStream" class="activity-stream-wrap"></div>
</div>
</div>
@@ -232,6 +240,6 @@
<dialog id="sheet"></dialog>
<dialog id="dlg"></dialog>
<div class="snack" id="snack" role="status"></div>
<script src="/khira/app.js?v=2" defer></script>
<script src="/khira/app.js?v=3" defer></script>
</body>
</html>
+121
View File
@@ -2198,3 +2198,124 @@ dd { word-break: break-word; }
line-height: 1.4;
margin-bottom: 16px;
}
/* ── Corporate Privacy & Analytics Scoping ── */
.analytics-hero-bar {
display: flex;
align-items: center;
justify-content: space-between;
flex-wrap: wrap;
gap: 16px;
width: 100%;
}
.analytics-scope-bar {
display: flex;
align-items: center;
gap: 10px;
flex-wrap: wrap;
}
.badge.privacy-badge {
background: rgba(99, 102, 241, 0.14);
color: #a5b4fc;
border: 1px solid rgba(129, 140, 248, 0.35);
padding: 6px 12px;
border-radius: 999px;
font-size: 12px;
font-weight: 500;
display: inline-flex;
align-items: center;
gap: 6px;
letter-spacing: 0.02em;
box-shadow: 0 2px 10px rgba(99, 102, 241, 0.12);
}
.badge.org-badge {
background: rgba(16, 185, 129, 0.14);
color: #6ee7b7;
border: 1px solid rgba(52, 211, 153, 0.35);
padding: 6px 12px;
border-radius: 999px;
font-size: 12px;
font-weight: 500;
display: inline-flex;
align-items: center;
gap: 6px;
letter-spacing: 0.02em;
box-shadow: 0 2px 10px rgba(16, 185, 129, 0.12);
}
.badge.audit-badge {
background: rgba(245, 158, 11, 0.14);
color: #fde68a;
border: 1px solid rgba(251, 191, 36, 0.35);
padding: 6px 12px;
border-radius: 999px;
font-size: 12px;
font-weight: 500;
display: inline-flex;
align-items: center;
gap: 6px;
letter-spacing: 0.02em;
box-shadow: 0 2px 10px rgba(245, 158, 11, 0.12);
}
.analytics-scope-select {
background: rgba(18, 24, 38, 0.85);
border: 1px solid rgba(255, 255, 255, 0.14);
color: var(--txt);
padding: 6px 12px;
border-radius: 10px;
font-size: 13px;
font-weight: 500;
outline: none;
cursor: pointer;
backdrop-filter: blur(10px);
transition: all 0.15s ease;
}
.analytics-scope-select:focus {
border-color: #38bdf8;
box-shadow: 0 0 0 2px rgba(56, 189, 248, 0.2);
}
.personal-scorecard {
display: flex;
flex-direction: column;
gap: 16px;
padding: 4px 0;
}
.scorecard-user {
display: flex;
align-items: center;
gap: 14px;
}
.scorecard-user h4 {
font-size: 16px;
font-weight: 600;
color: var(--txt);
margin-bottom: 2px;
}
.scorecard-metrics {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
gap: 12px;
}
.scorecard-item {
background: rgba(255, 255, 255, 0.03);
border: 1px solid rgba(255, 255, 255, 0.06);
border-radius: 12px;
padding: 12px 14px;
display: flex;
flex-direction: column;
gap: 4px;
}
.scorecard-item .lbl {
font-size: 11px;
color: var(--sub);
text-transform: uppercase;
letter-spacing: 0.05em;
font-weight: 600;
}
.scorecard-item .val {
font-size: 18px;
font-weight: 700;
color: var(--txt);
font-family: 'Roboto Mono', monospace;
}
.scorecard-item .val.green { color: #10b981; }
.scorecard-item .val.red { color: #ef4444; }