identityscope 0.28.3 → 0.28.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +914 -917
  3. package/SECURITY.md +56 -56
  4. package/dist/node/cli.js +26 -26
  5. package/dist/node/monthly.js +17 -17
  6. package/package.json +2 -2
  7. package/reports/access-profile-members.yaml +29 -29
  8. package/reports/access-profile-membership-counts.yaml +17 -17
  9. package/reports/access-profiles-without-owner.yaml +16 -16
  10. package/reports/access-request-audit-trail.yaml +36 -36
  11. package/reports/accounts-by-source.yaml +19 -19
  12. package/reports/active-identities-without-manager.yaml +22 -22
  13. package/reports/aggregation-failures.yaml +29 -29
  14. package/reports/aggregation-history.yaml +27 -27
  15. package/reports/all-sources.yaml +16 -16
  16. package/reports/application-accounts.yaml +32 -32
  17. package/reports/application-entitlements.yaml +31 -31
  18. package/reports/campaign-progress.yaml +23 -23
  19. package/reports/certification-decisions.yaml +31 -31
  20. package/reports/direct-reports-per-manager.yaml +20 -20
  21. package/reports/disabled-accounts-of-active-people.yaml +33 -33
  22. package/reports/duplicate-emails.yaml +16 -16
  23. package/reports/entitlement-holders.yaml +35 -35
  24. package/reports/entitlements-held-by-inactive-identities.yaml +31 -31
  25. package/reports/entitlements-without-owner.yaml +28 -28
  26. package/reports/identities-by-lifecycle-state.yaml +15 -15
  27. package/reports/identities-missing-key-attributes.yaml +27 -27
  28. package/reports/identities-with-processing-errors.yaml +19 -19
  29. package/reports/identity-attribute-mappings.yaml +27 -27
  30. package/reports/machine-accounts.yaml +28 -28
  31. package/reports/managers-and-direct-reports.yaml +23 -23
  32. package/reports/most-access.yaml +18 -18
  33. package/reports/new-identities.yaml +24 -24
  34. package/reports/old-passwords.yaml +35 -35
  35. package/reports/pending-reviews-by-reviewer.yaml +20 -20
  36. package/reports/person-access.yaml +31 -31
  37. package/reports/privileged-access.yaml +29 -29
  38. package/reports/provisioning-error-causes.yaml +27 -27
  39. package/reports/provisioning-errors.yaml +32 -32
  40. package/reports/provisioning-failures.yaml +28 -28
  41. package/reports/recent-access-requests.yaml +22 -22
  42. package/reports/recent-activity.yaml +32 -32
  43. package/reports/recent-leavers.yaml +34 -34
  44. package/reports/role-members.yaml +29 -29
  45. package/reports/role-membership-counts.yaml +18 -18
  46. package/reports/roles-without-owner.yaml +17 -17
  47. package/reports/sources-needing-attention.yaml +23 -23
  48. package/reports/stuck-access-requests.yaml +29 -29
  49. package/reports/team-access.yaml +30 -30
  50. package/reports/terminated-users-with-active-accounts.yaml +46 -46
  51. package/reports/uncorrelated-accounts.yaml +38 -38
  52. package/ui/js/00-setup.js +6 -6
  53. package/ui/js/01-small-helpers.js +143 -143
  54. package/ui/js/02-connection.js +150 -150
  55. package/ui/js/03-health-check.js +17 -17
  56. package/ui/js/04-library.js +135 -135
  57. package/ui/js/05-pickers.js +163 -163
  58. package/ui/js/06-report-view.js +157 -157
  59. package/ui/js/07-fields.js +85 -85
  60. package/ui/js/08-designer.js +360 -360
  61. package/ui/js/09-yaml.js +22 -22
  62. package/ui/js/10-instant-check.js +171 -171
  63. package/ui/js/11-lookup.js +161 -161
  64. package/ui/js/12-tenant.js +115 -115
  65. package/ui/js/13-source-configuration-and-diagnostics.js +64 -64
  66. package/ui/js/14-certifications.js +68 -68
  67. package/ui/js/15-monthly-report.js +24 -24
  68. package/ui/js/16-ownership.js +39 -39
  69. package/ui/js/17-compare-tenants.js +102 -102
  70. package/ui/js/18-audit.js +181 -181
  71. package/ui/js/19-insights.js +108 -108
  72. package/ui/js/20-role-mining.js +35 -35
  73. package/ui/js/21-share-reports.js +45 -45
  74. package/ui/js/22-partner-overview-all-customer-tenants.js +228 -228
  75. package/ui/js/23-in-app-guidance.js +217 -217
  76. package/ui/js/24-role-tools.js +220 -220
  77. package/ui/js/25-schedules.js +264 -264
  78. package/ui/js/26-start.js +7 -7
  79. package/ui/js/27-warnings.js +224 -224
  80. package/ui/js/28-audit-readiness.js +99 -99
  81. package/ui/js/29-lifecycle.js +182 -182
  82. package/ui/js/30-labs.js +265 -265
  83. package/ui/js/31-build.js +203 -203
  84. package/ui/js/32-lab-examples.js +148 -148
  85. package/ui/js/33-finder.js +89 -89
  86. package/ui/js/34-governance.js +129 -129
  87. package/ui/js/35-watch.js +266 -266
  88. package/ui/js/36-model.js +247 -247
  89. package/ui/js/37-logs-and-watch.js +224 -224
  90. package/ui/shell.html +112 -112
  91. package/ui/style.css +350 -350
@@ -1,360 +1,360 @@
1
- // ---------- designer ----------
2
- var TEMPLATES = [
3
- { key: 'people', label: 'People', desc: 'One row per identity', icon: 'people', dataset: 'identities', cols: ['identity.displayName', 'identity.email', 'identity.attributes.department', 'identity.attributes.cloudLifecycleState', 'identity.manager.displayName'] },
4
- { key: 'people-accounts', label: 'People and their accounts', desc: 'One row per account of each person', icon: 'apps', dataset: 'identities', expand: 'identity.accounts', cols: ['identity.displayName', 'identity.attributes.cloudLifecycleState', 'account.source.name', 'account.name', 'account.disabled'] },
5
- { key: 'people-access', label: 'People and their access', desc: 'One row per role, access profile or entitlement held', icon: 'layers', dataset: 'identities', expand: 'identity.access', cols: ['identity.displayName', 'identity.email', 'access.type', 'access.displayName', 'access.source.name', 'access.privileged'] },
6
- { key: 'accounts', label: 'Accounts', desc: 'All accounts, linked or not', icon: 'apps', dataset: 'accounts', cols: ['account.sourceName', 'account.name', 'account.nativeIdentity', 'account.uncorrelated', 'account.disabled'] },
7
- { key: 'entitlements', label: 'Entitlements', desc: 'Groups, profiles, permissions', icon: 'layers', dataset: 'entitlements', cols: ['entitlement.source.name', 'entitlement.name', 'entitlement.owner.name', 'entitlement.privileged', 'entitlement.requestable'] },
8
- { key: 'roles', label: 'Roles', desc: 'Role catalog', icon: 'layers', dataset: 'roles', cols: ['role.name', 'role.owner.name', 'role.enabled', 'role.requestable', 'role.accessProfileCount'] },
9
- { key: 'accessProfiles', label: 'Access profiles', desc: 'Access profile catalog', icon: 'layers', dataset: 'accessProfiles', cols: ['accessProfile.source.name', 'accessProfile.name', 'accessProfile.owner.name', 'accessProfile.enabled', 'accessProfile.requestable'] },
10
- { key: 'sources', label: 'Sources', desc: 'Connected applications', icon: 'db', dataset: 'sources', cols: ['source.name', 'source.type', 'source.status', 'source.owner.name', 'source.authoritative'] },
11
- { key: 'events', label: 'Audit events', desc: 'What happened, when, by whom', icon: 'pulse', dataset: 'events', cols: ['event.created', 'event.name', 'event.type', 'event.status', 'event.target.name', 'event.actor.name'] },
12
- { key: 'accessRequests', label: 'Access requests', desc: 'Requests and where they stand', icon: 'inbox', dataset: 'accessRequests', cols: ['request.created', 'request.requestedFor.name', 'request.name', 'request.type', 'request.state'] },
13
- { key: 'certificationItems', label: 'Review decisions', desc: 'Certification items and decisions', icon: 'cert', dataset: 'certificationItems', cols: ['item.campaign.name', 'item.certification.reviewer.name', 'item.identitySummary.name', 'item.accessSummary.access.name', 'item.decision'] },
14
- { key: 'activities', label: 'Account activities', desc: 'Provisioning: creates, changes, errors', icon: 'pulse', dataset: 'accountActivities', cols: ['activity.created', 'activity.action', 'activity.recipient.name', 'activity.sources', 'activity.status', 'activity.errors'] },
15
- { key: 'mappings', label: 'Identity mappings', desc: 'Where each identity attribute comes from', icon: 'db', dataset: 'identityProfiles', expand: 'profile.mappings', cols: ['profile.name', 'mapping.attribute', 'mapping.kind', 'mapping.source', 'mapping.sourceAttribute', 'mapping.transform'] },
16
- { key: 'campaigns', label: 'Campaigns', desc: 'Certification campaigns', icon: 'cert', dataset: 'campaigns', cols: ['campaign.name', 'campaign.status', 'campaign.deadline', 'campaign.totalCertifications', 'campaign.completedCertifications'] }
17
- ];
18
- function templateFor(d) {
19
- return TEMPLATES.filter(function (t) { return t.dataset === d.from.dataset && (t.expand || '') === (d.expand ? d.expand.path : ''); })[0];
20
- }
21
- function applyTemplate(d, t) {
22
- d.from = { dataset: t.dataset };
23
- if (t.expand) d.expand = { path: t.expand, as: t.expand === 'identity.access' ? 'access' : t.expand === 'profile.mappings' ? 'mapping' : 'account' }; else delete d.expand;
24
- delete d.where; delete d.joins; delete d.filter; delete d.sort; delete d.group; delete d.limit;
25
- Object.keys(d.params || {}).forEach(function (k) { delete d.params[k]; });
26
- delete d.params;
27
- var ds = datasetOf(t.dataset);
28
- d.columns = t.cols.map(function (p) {
29
- var root = p.split('.')[0], rest = p.split('.').slice(1).join('.');
30
- var list = root === (ds.alias) ? ds.fields : (ds.itemFields && ds.itemFields[(t.expand || '').split('.').slice(1).join('.')]) || [];
31
- var f = list.filter(function (x) { return x.path === rest; })[0];
32
- return { field: p, label: f ? f.label : undefined, format: f ? formatFor(f.type) : undefined };
33
- });
34
- }
35
- function newReport() {
36
- var def = { id: 'my-report-' + Date.now().toString(36), name: 'New report', category: 'My reports', from: { dataset: 'identities' } };
37
- applyTemplate(def, TEMPLATES[0]);
38
- state.current = { source: null, def: def, custom: true, isNew: true };
39
- state.tab = 'design';
40
- state.result = null;
41
- state.groupBy = '';
42
- state.runError = null;
43
- state.paramValues = {};
44
- state.dirty = true;
45
- renderLibrary();
46
- renderMain();
47
- }
48
- $('btn-new').addEventListener('click', newReport);
49
-
50
- function changed() { state.dirty = true; state.current.yaml = null; }
51
- function card(stepNo, iconName, title, hint) {
52
- return el('section', { className: 'card' }, el('div', { className: 'card-h' },
53
- el('h3', {}, stepNo ? el('span', { className: 'step', text: String(stepNo) }) : icon(iconName), title),
54
- hint ? el('span', { className: 'muted small', text: hint }) : null));
55
- }
56
-
57
- function renderDesign(w) {
58
- var c = state.current, d = c.def, ds = datasetOf(d.from.dataset);
59
- ensureDisc(d.from.dataset);
60
- var disc = state.disc[d.from.dataset] || {};
61
-
62
- // about
63
- var nameIn = el('input', { className: 'input', value: d.name });
64
- nameIn.addEventListener('input', function () { d.name = nameIn.value; changed(); });
65
- var descIn = el('input', { className: 'input', value: d.description || '', placeholder: 'What this report shows and who it is for' });
66
- descIn.addEventListener('input', function () { d.description = descIn.value || undefined; changed(); });
67
- var about = card(0, 'file', 'About this report');
68
- about.appendChild(el('div', { className: 'grid', style: 'grid-template-columns:1fr 2fr' },
69
- el('label', { className: 'field' }, el('span', { className: 'lbl', text: 'Name' }), nameIn),
70
- el('label', { className: 'field' }, el('span', { className: 'lbl', text: 'Description' }), descIn)));
71
- if (!c.custom) about.appendChild(el('div', { className: 'note', text: 'This is a ready-made report. Your changes are saved as your own copy under "My reports".' }));
72
- w.appendChild(about);
73
-
74
- // 1. data
75
- var tpl = templateFor(d);
76
- var data = card(1, null, 'What do you want a row to be?', disc.loading ? 'reading a sample of your tenant...' : disc.sampled !== undefined ? 'fields checked against ' + disc.sampled + ' records of your tenant' : '');
77
- data.appendChild(el('div', { className: 'tiles' }, TEMPLATES.map(function (t) {
78
- return el('button', { className: 'tile' + (tpl === t ? ' active' : ''), onclick: function () {
79
- if (tpl === t) return;
80
- if ((d.filter || d.where || d.group) && !confirm('Start over with "' + t.label + '"? Filters and columns are reset.')) return;
81
- applyTemplate(d, t); changed(); renderMain();
82
- } }, catIcon({ people: 'People', apps: 'Application owners', layers: 'Access model', db: 'Hygiene', pulse: 'Operations', inbox: 'Requests', cert: 'Certifications' }[t.icon]), el('div', {}, el('b', { text: t.label }), el('span', { text: t.desc })));
83
- })));
84
- if (!tpl) data.appendChild(el('div', { className: 'note', text: 'This report reads ' + ds.label.toLowerCase() + (d.expand ? ', one row per item of ' + d.expand.path : '') + '. Pick a tile to start over with another kind of row.' }));
85
- if (disc.error) data.appendChild(el('div', { className: 'note', text: 'Could not read a sample from your tenant, so only the standard fields are offered. ' + disc.error }));
86
- if (d.where || (d.expand && d.expand.where) || (d.joins && d.joins.length)) {
87
- data.appendChild(el('div', { className: 'note', text: 'This report also has built-in rules and lookups. They are kept as they are; see the YAML tab.' }));
88
- }
89
- w.appendChild(data);
90
-
91
- // 2. filters
92
- w.appendChild(filtersCard(d));
93
-
94
- // 3. columns or counts
95
- w.appendChild(columnsCard(d));
96
-
97
- // 4. sort and limit
98
- var sortCard = card(4, null, 'Order and size');
99
- var s0 = (d.sort && d.sort[0]) || '';
100
- var dir = el('select', { className: 'input' }, el('option', { value: '', text: 'A → Z, smallest, oldest first' }), el('option', { value: '-', text: 'Z → A, largest, newest first' }));
101
- dir.value = s0.charAt(0) === '-' ? '-' : '';
102
- var sortField = s0.replace(/^[-+]/, '');
103
- var writeSort = function () { if (sortField) d.sort = [dir.value + sortField]; else delete d.sort; changed(); };
104
- dir.addEventListener('change', writeSort);
105
- var lim = el('input', { className: 'input', type: 'number', min: '1', placeholder: 'all rows', value: d.limit || '' });
106
- lim.addEventListener('input', function () { var n = Number(lim.value); if (lim.value && n > 0) d.limit = Math.floor(n); else delete d.limit; changed(); });
107
- sortCard.appendChild(el('div', { className: 'grid' },
108
- el('label', { className: 'field' }, el('span', { className: 'lbl', text: 'Sort by' }), fieldSelect(d, sortField, function (v) { sortField = v; writeSort(); }, { empty: 'Report order' })),
109
- el('label', { className: 'field' }, el('span', { className: 'lbl', text: 'Direction' }), dir),
110
- el('label', { className: 'field' }, el('span', { className: 'lbl', text: 'Only the first' }), lim)));
111
- w.appendChild(sortCard);
112
-
113
- // advanced
114
- var search = ds.kind === 'search';
115
- var qv = search ? d.from.query : d.from.filter;
116
- var qIn = el('input', { className: 'input', value: Array.isArray(qv) ? qv.join(search ? ' AND ' : ' and ') : (qv || ''), placeholder: ds.hint || '' });
117
- qIn.addEventListener('input', function () {
118
- var key = search ? 'query' : 'filter';
119
- if (qIn.value.trim()) d.from[key] = qIn.value; else delete d.from[key];
120
- changed();
121
- });
122
- w.appendChild(el('section', { className: 'card' }, el('details', { className: 'adv' }, el('summary', { text: 'Advanced: make big reports faster' }),
123
- el('p', { className: 'muted small', text: search ? 'An ISC search query narrows what is read from the tenant before your filters run. Your filters above still decide the result.' : 'An ISC API filter narrows what is read from the tenant before your filters run.' }),
124
- el('label', { className: 'field' }, el('span', { className: 'lbl', text: search ? 'Search query' : 'API filter' }), qIn))));
125
-
126
- // values to try the asked filters with
127
- if (d.params && Object.keys(d.params).length) {
128
- state.paramValues = state.paramValues || {};
129
- var tc = card(0, 'sliders', 'Try it with', 'these are asked each time the report runs');
130
- tc.appendChild(paramsGrid(d.params, state.paramValues, null, d));
131
- w.appendChild(tc);
132
- }
133
-
134
- // actions
135
- var msg = el('span', { className: 'muted small' });
136
- w.appendChild(el('div', { className: 'actionbar' },
137
- el('button', { className: 'btn', onclick: function () { run(500); setTimeout(function () { var o = $('run-out'); if (o) o.scrollIntoView({ behavior: 'smooth', block: 'start' }); }, 50); } }, icon('eye'), 'Preview'),
138
- el('button', { className: 'btn primary', onclick: function () { save(msg); } }, icon('save'), c.custom ? 'Save' : 'Save as my report'),
139
- c.custom && !c.isNew ? el('button', { className: 'btn ghost danger', onclick: function () {
140
- if (!confirm('Delete "' + c.def.name + '"?')) return;
141
- api('/api/reports/' + c.def.id, 'DELETE').then(function () { state.current = null; toast('Report deleted'); loadLibrary(); }).catch(function (e) { msg.textContent = errorText(e); });
142
- } }, icon('trash'), 'Delete') : null,
143
- msg));
144
- w.appendChild(el('div', { id: 'run-out' }));
145
- renderResult();
146
- }
147
-
148
- function filtersCard(d) {
149
- var fc = card(2, null, 'Which rows?', 'leave empty for all');
150
- var f = d.filter;
151
- var list = !f ? [] : f.all ? f.all : f.any ? f.any : f.not ? null : [f];
152
- var flat = list && list.every(function (x) { return typeof x.field === 'string'; });
153
- if (!flat) { fc.appendChild(el('div', { className: 'muted', text: 'These filters are nested; edit them in the YAML tab.' })); return fc; }
154
- var mode = f && f.any ? 'any' : 'all';
155
- var write = function () {
156
- list.forEach(function (x) { if (!needsValue(x.op)) delete x.value; });
157
- var valid = list.filter(function (x) { return x.field; });
158
- if (!valid.length) delete d.filter; else if (valid.length === 1) d.filter = valid[0]; else d.filter = mode === 'any' ? { any: valid } : { all: valid };
159
- changed();
160
- };
161
- if (list.length > 1) {
162
- var modeSel = el('select', { className: 'input', style: 'width:auto' }, el('option', { value: 'all', text: 'all' }), el('option', { value: 'any', text: 'any' }));
163
- modeSel.value = mode;
164
- modeSel.addEventListener('change', function () { mode = modeSel.value; write(); });
165
- fc.appendChild(el('div', { className: 'joiner' }, 'Keep rows that match', modeSel, 'of these:'));
166
- }
167
- list.forEach(function (x, i) { fc.appendChild(conditionRow(d, x, function () { write(); }, function () { removeAsk(d, x); list.splice(i, 1); write(); renderMain(); })); });
168
- fc.appendChild(el('button', { className: 'btn', onclick: function () {
169
- var first = flatFields(d)[0];
170
- list.push({ field: first ? first.value : '', op: opsFor(first ? first.type : 'string')[0][0], value: '' });
171
- if (!needsValue(list[list.length - 1].op)) delete list[list.length - 1].value;
172
- write(); renderMain();
173
- } }, icon('plus'), 'Add a filter'));
174
- return fc;
175
- }
176
-
177
- function paramNameFor(d, label) {
178
- var base = label.replace(/[^A-Za-z0-9 ]/g, ' ').trim().split(/\s+/).map(function (w, i) { w = w.toLowerCase(); return i ? w.charAt(0).toUpperCase() + w.slice(1) : w; }).join('') || 'value';
179
- if (/^[0-9]/.test(base)) base = 'p' + base;
180
- var n = base, k = 2;
181
- while (d.params && d.params[n]) n = base + k++;
182
- return n;
183
- }
184
- function removeAsk(d, x) {
185
- var m = typeof x.value === 'string' && ASK.exec(x.value);
186
- if (m && d.params) { delete d.params[m[1]]; if (!Object.keys(d.params).length) delete d.params; }
187
- }
188
-
189
- function conditionRow(d, x, write, remove) {
190
- var info = fieldInfo(d, x.field);
191
- var ops = opsFor(info.type);
192
- if (!ops.some(function (o) { return o[0] === x.op; })) ops = ops.concat([[x.op, (state.catalog.operators.filter(function (o) { return o.op === x.op; })[0] || { label: x.op }).label]]);
193
- var askM = typeof x.value === 'string' && ASK.exec(x.value);
194
- var fieldSel = fieldSelect(d, x.field, function (v) {
195
- removeAsk(d, x);
196
- x.field = v;
197
- var t = fieldInfo(d, v).type;
198
- x.op = opsFor(t)[0][0];
199
- if (needsValue(x.op)) x.value = ''; else delete x.value;
200
- write(); renderMain();
201
- });
202
- var opSel = el('select', { className: 'input' }, ops.map(function (o) { return el('option', { value: o[0], text: o[1] }); }));
203
- opSel.value = x.op;
204
- opSel.addEventListener('change', function () {
205
- var wasList = x.op === 'in' || x.op === 'notIn';
206
- x.op = opSel.value;
207
- var isList = x.op === 'in' || x.op === 'notIn';
208
- if (!needsValue(x.op)) { removeAsk(d, x); delete x.value; }
209
- else if (!askM && wasList !== isList) x.value = isList ? (x.value ? [String(x.value)] : []) : (Array.isArray(x.value) ? (x.value[0] || '') : x.value || '');
210
- else if (askM) { var p = d.params[askM[1]]; p.type = isList ? 'list' : (x.op === 'newerThanDays' || x.op === 'olderThanDays' || info.type === 'number') ? 'number' : 'string'; }
211
- write(); renderMain();
212
- });
213
- var valueBox = el('div');
214
- if (askM) {
215
- var p = (d.params || {})[askM[1]] || {};
216
- var q = el('input', { className: 'input', value: p.description || '', placeholder: 'Question shown when running' });
217
- q.addEventListener('input', function () { p.description = q.value || undefined; changed(); });
218
- valueBox.appendChild(q);
219
- } else if (needsValue(x.op)) {
220
- valueBox.appendChild(valueEditor(d, x, info, write));
221
- }
222
- var askBox = el('input', { type: 'checkbox', checked: !!askM });
223
- askBox.addEventListener('change', function () {
224
- if (askBox.checked) {
225
- // Source names become a source drop-down: ask for the source, filter on its ID.
226
- var idField = isSourceName(x.field) ? x.field.replace(/source\.name$|source\.displayableName$/, 'source.id').replace(/sourceName$/, 'sourceId') : null;
227
- if (idField && flatFields(d).some(function (f) { return f.value === idField; })) { x.field = idField; x.value = Array.isArray(x.value) ? [] : ''; }
228
- var listOp = x.op === 'in' || x.op === 'notIn';
229
- var numeric = x.op === 'newerThanDays' || x.op === 'olderThanDays' || info.type === 'number';
230
- var name = paramNameFor(d, fieldInfo(d, x.field).label);
231
- d.params = d.params || {};
232
- var spec = { type: listOp ? 'list' : numeric ? 'number' : 'string', description: fieldInfo(d, x.field).label + (numeric && x.op !== 'eq' ? ' (days)' : '') };
233
- if (isSourceId(x.field)) spec.pick = 'sources';
234
- var cur = x.value;
235
- if (cur !== '' && cur !== undefined && !(Array.isArray(cur) && !cur.length) && !isSourceId(x.field)) spec['default'] = cur;
236
- d.params[name] = spec;
237
- x.value = '${' + name + '}';
238
- state.paramValues = state.paramValues || {};
239
- state.paramValues[name] = spec['default'] !== undefined ? spec['default'] : spec.type === 'list' ? [] : '';
240
- } else {
241
- var m = ASK.exec(x.value);
242
- var prev = m && d.params && d.params[m[1]];
243
- removeAsk(d, x);
244
- x.value = prev && prev['default'] !== undefined ? prev['default'] : (x.op === 'in' || x.op === 'notIn') ? [] : '';
245
- }
246
- write(); renderMain();
247
- });
248
- var ask = needsValue(x.op) ? el('label', { className: 'ask' }, askBox, el('span', { text: askM ? 'Asked when the report runs (and in schedules).' : 'Ask for this value each time the report runs' })) : null;
249
- return el('div', { className: 'cond' }, fieldSel, opSel, valueBox,
250
- el('button', { className: 'btn icon ghost', title: 'Remove this filter', onclick: remove }, icon('x')), ask);
251
- }
252
-
253
- function valueEditor(d, x, info, write) {
254
- var listOp = x.op === 'in' || x.op === 'notIn';
255
- var path = info.path || x.field;
256
- if (isSourceId(path) || isSourceName(path)) {
257
- return picker({ options: loadPick('sources'), multiple: listOp, value: x.value, valueKey: isSourceId(path) ? 'value' : 'label', onChange: function (v) { x.value = v; write(); }, placeholder: listOp ? 'Pick sources' : 'Pick a source' });
258
- }
259
- if (x.op === 'newerThanDays' || x.op === 'olderThanDays' || info.type === 'number') {
260
- var n = el('input', { className: 'input', type: 'number', value: x.value === undefined ? '' : String(x.value), placeholder: x.op === 'newerThanDays' || x.op === 'olderThanDays' ? 'days, e.g. 30' : 'number' });
261
- n.addEventListener('input', function () { x.value = n.value === '' ? '' : Number(n.value); write(); });
262
- return n;
263
- }
264
- if (listOp) {
265
- if (info.values && info.values.length && info.values.length <= 50) {
266
- return picker({ options: info.values.map(function (v) { return { value: v, label: v }; }), multiple: true, value: x.value, onChange: function (v) { x.value = v; write(); }, placeholder: 'Pick or type values' });
267
- }
268
- return tokenInput({ value: x.value, suggestions: info.values, onChange: function (v) { x.value = v; write(); } });
269
- }
270
- var id = 'sv' + Math.random().toString(36).slice(2);
271
- var t = el('input', { className: 'input', value: x.value === undefined ? '' : String(x.value), list: info.values && info.values.length ? id : null, placeholder: info.values && info.values.length ? 'e.g. ' + info.values.slice(0, 2).join(', ') : 'value' });
272
- t.addEventListener('input', function () { x.value = t.value; write(); });
273
- return el('div', {}, t, info.values && info.values.length ? el('datalist', { id: id }, info.values.map(function (v) { return el('option', { value: v }); })) : null);
274
- }
275
-
276
- function columnsCard(d) {
277
- var counting = !!(d.group && d.group.by && d.group.by.length);
278
- var cc = card(3, null, 'What to show', null);
279
- var modeSel = el('div', { className: 'chips', style: 'margin-bottom:14px' },
280
- el('button', { className: 'chip' + (!counting ? ' active' : ''), onclick: function () {
281
- if (!counting) return;
282
- d.columns = state.current.stash || d.columns; delete d.group; changed(); renderMain();
283
- } }, icon('cols'), 'A list of rows'),
284
- el('button', { className: 'chip' + (counting ? ' active' : ''), onclick: function () {
285
- if (counting) return;
286
- state.current.stash = clone(d.columns || []);
287
- var first = (d.columns && d.columns[0] && d.columns[0].field) || (flatFields(d)[0] || {}).value;
288
- d.group = { by: [first], countLabel: 'Count' };
289
- syncGroupColumns(d); changed(); renderMain();
290
- } }, icon('sigma'), 'Counts (summary)'));
291
- cc.appendChild(modeSel);
292
- if (counting) {
293
- cc.appendChild(el('p', { className: 'muted small', style: 'margin-top:0', text: 'One row per combination of these fields, with how many rows have it. Largest first.' }));
294
- d.group.by.forEach(function (b, i) {
295
- cc.appendChild(el('div', { className: 'colrow', style: 'grid-template-columns:28px 1fr auto' }, el('span', { className: 'n', text: String(i + 1) }),
296
- fieldSelect(d, b, function (v) { d.group.by[i] = v; syncGroupColumns(d); changed(); }, { counting: true }),
297
- el('button', { className: 'btn icon ghost', title: 'Remove', disabled: d.group.by.length === 1, onclick: function () { d.group.by.splice(i, 1); syncGroupColumns(d); changed(); renderMain(); } }, icon('x'))));
298
- });
299
- if (d.group.by.length < 4) cc.appendChild(fieldSelect(d, '', function (v) { if (!v) return; d.group.by.push(v); syncGroupColumns(d); changed(); renderMain(); }, { empty: '+ Also count by...', counting: true }));
300
- var cl = el('input', { className: 'input', value: d.group.countLabel || 'Count', style: 'max-width:260px' });
301
- cl.addEventListener('input', function () { d.group.countLabel = cl.value || undefined; changed(); });
302
- cc.appendChild(el('label', { className: 'field', style: 'margin:14px 0 0' }, el('span', { className: 'lbl', text: 'Heading of the count column' }), cl));
303
- return cc;
304
- }
305
- var formats = [['', 'Automatic'], ['text', 'Text'], ['date', 'Date'], ['datetime', 'Date and time'], ['number', 'Number'], ['boolean', 'Yes / No'], ['list', 'List'], ['count', 'How many'], ['json', 'Raw']];
306
- d.columns = d.columns || [];
307
- d.columns.forEach(function (col, i) {
308
- var li = el('input', { className: 'input', value: col.label || '', placeholder: fieldInfo(d, col.field).label });
309
- li.addEventListener('input', function () { col.label = li.value || undefined; changed(); });
310
- var fs = el('select', { className: 'input' }, formats.map(function (x) { return el('option', { value: x[0], text: x[1] }); }));
311
- fs.value = col.format || '';
312
- fs.addEventListener('change', function () { col.format = fs.value || undefined; changed(); });
313
- var move = function (delta) {
314
- var j = i + delta;
315
- if (j < 0 || j >= d.columns.length) return;
316
- var t = d.columns[i]; d.columns[i] = d.columns[j]; d.columns[j] = t;
317
- changed(); renderMain();
318
- };
319
- cc.appendChild(el('div', { className: 'colrow' }, el('span', { className: 'n', text: String(i + 1) }),
320
- fieldSelect(d, col.field, function (v) { col.field = v; var inf = fieldInfo(d, v); col.label = inf.label; col.format = formatFor(inf.type); changed(); renderMain(); }),
321
- li, fs,
322
- el('div', { className: 'bar', style: 'gap:2px' },
323
- el('button', { className: 'btn icon ghost', title: 'Move up', disabled: i === 0, onclick: function () { move(-1); } }, icon('up')),
324
- el('button', { className: 'btn icon ghost', title: 'Move down', disabled: i === d.columns.length - 1, onclick: function () { move(1); } }, icon('down')),
325
- el('button', { className: 'btn icon ghost', title: 'Remove', onclick: function () { d.columns.splice(i, 1); changed(); renderMain(); } }, icon('x')))));
326
- });
327
- if (!d.columns.length) cc.appendChild(el('div', { className: 'note', text: 'Add at least one column.' }));
328
- var addSel = fieldSelect(d, '', function (v) {
329
- if (!v) return;
330
- var inf = fieldInfo(d, v);
331
- d.columns.push({ field: v, label: inf.label, format: formatFor(inf.type) });
332
- changed(); renderMain();
333
- }, { empty: '+ Add a column...' });
334
- addSel.style.maxWidth = '420px';
335
- cc.appendChild(el('div', { style: 'margin-top:10px' }, addSel));
336
- return cc;
337
- }
338
- function syncGroupColumns(d) {
339
- d.columns = d.group.by.filter(Boolean).map(function (b) { var inf = fieldInfo(d, b); return { field: b, label: inf.label, format: inf.type === 'date' ? 'date' : undefined }; });
340
- }
341
-
342
- function save(msg) {
343
- var c = state.current;
344
- var def = clone(c.def);
345
- if (!c.custom) {
346
- def.id = def.id + '-copy';
347
- def.name = def.name + ' (my copy)';
348
- var n = 2;
349
- while (state.reports.some(function (r) { return r.definition.id === def.id; })) def.id = c.def.id + '-copy-' + n++;
350
- }
351
- def.category = c.custom ? (def.category || 'My reports') : 'My reports';
352
- msg.textContent = '';
353
- api('/api/reports', 'POST', { definition: def }).then(function (r) {
354
- toast('Saved "' + def.name + '" to My reports');
355
- state.dirty = false;
356
- state.current = null;
357
- return loadLibrary(r.saved);
358
- }).catch(function (e) { msg.textContent = ''; msg.appendChild(el('span', { className: 'error', text: errorText(e) })); });
359
- }
360
-
1
+ // ---------- designer ----------
2
+ var TEMPLATES = [
3
+ { key: 'people', label: 'People', desc: 'One row per identity', icon: 'people', dataset: 'identities', cols: ['identity.displayName', 'identity.email', 'identity.attributes.department', 'identity.attributes.cloudLifecycleState', 'identity.manager.displayName'] },
4
+ { key: 'people-accounts', label: 'People and their accounts', desc: 'One row per account of each person', icon: 'apps', dataset: 'identities', expand: 'identity.accounts', cols: ['identity.displayName', 'identity.attributes.cloudLifecycleState', 'account.source.name', 'account.name', 'account.disabled'] },
5
+ { key: 'people-access', label: 'People and their access', desc: 'One row per role, access profile or entitlement held', icon: 'layers', dataset: 'identities', expand: 'identity.access', cols: ['identity.displayName', 'identity.email', 'access.type', 'access.displayName', 'access.source.name', 'access.privileged'] },
6
+ { key: 'accounts', label: 'Accounts', desc: 'All accounts, linked or not', icon: 'apps', dataset: 'accounts', cols: ['account.sourceName', 'account.name', 'account.nativeIdentity', 'account.uncorrelated', 'account.disabled'] },
7
+ { key: 'entitlements', label: 'Entitlements', desc: 'Groups, profiles, permissions', icon: 'layers', dataset: 'entitlements', cols: ['entitlement.source.name', 'entitlement.name', 'entitlement.owner.name', 'entitlement.privileged', 'entitlement.requestable'] },
8
+ { key: 'roles', label: 'Roles', desc: 'Role catalog', icon: 'layers', dataset: 'roles', cols: ['role.name', 'role.owner.name', 'role.enabled', 'role.requestable', 'role.accessProfileCount'] },
9
+ { key: 'accessProfiles', label: 'Access profiles', desc: 'Access profile catalog', icon: 'layers', dataset: 'accessProfiles', cols: ['accessProfile.source.name', 'accessProfile.name', 'accessProfile.owner.name', 'accessProfile.enabled', 'accessProfile.requestable'] },
10
+ { key: 'sources', label: 'Sources', desc: 'Connected applications', icon: 'db', dataset: 'sources', cols: ['source.name', 'source.type', 'source.status', 'source.owner.name', 'source.authoritative'] },
11
+ { key: 'events', label: 'Audit events', desc: 'What happened, when, by whom', icon: 'pulse', dataset: 'events', cols: ['event.created', 'event.name', 'event.type', 'event.status', 'event.target.name', 'event.actor.name'] },
12
+ { key: 'accessRequests', label: 'Access requests', desc: 'Requests and where they stand', icon: 'inbox', dataset: 'accessRequests', cols: ['request.created', 'request.requestedFor.name', 'request.name', 'request.type', 'request.state'] },
13
+ { key: 'certificationItems', label: 'Review decisions', desc: 'Certification items and decisions', icon: 'cert', dataset: 'certificationItems', cols: ['item.campaign.name', 'item.certification.reviewer.name', 'item.identitySummary.name', 'item.accessSummary.access.name', 'item.decision'] },
14
+ { key: 'activities', label: 'Account activities', desc: 'Provisioning: creates, changes, errors', icon: 'pulse', dataset: 'accountActivities', cols: ['activity.created', 'activity.action', 'activity.recipient.name', 'activity.sources', 'activity.status', 'activity.errors'] },
15
+ { key: 'mappings', label: 'Identity mappings', desc: 'Where each identity attribute comes from', icon: 'db', dataset: 'identityProfiles', expand: 'profile.mappings', cols: ['profile.name', 'mapping.attribute', 'mapping.kind', 'mapping.source', 'mapping.sourceAttribute', 'mapping.transform'] },
16
+ { key: 'campaigns', label: 'Campaigns', desc: 'Certification campaigns', icon: 'cert', dataset: 'campaigns', cols: ['campaign.name', 'campaign.status', 'campaign.deadline', 'campaign.totalCertifications', 'campaign.completedCertifications'] }
17
+ ];
18
+ function templateFor(d) {
19
+ return TEMPLATES.filter(function (t) { return t.dataset === d.from.dataset && (t.expand || '') === (d.expand ? d.expand.path : ''); })[0];
20
+ }
21
+ function applyTemplate(d, t) {
22
+ d.from = { dataset: t.dataset };
23
+ if (t.expand) d.expand = { path: t.expand, as: t.expand === 'identity.access' ? 'access' : t.expand === 'profile.mappings' ? 'mapping' : 'account' }; else delete d.expand;
24
+ delete d.where; delete d.joins; delete d.filter; delete d.sort; delete d.group; delete d.limit;
25
+ Object.keys(d.params || {}).forEach(function (k) { delete d.params[k]; });
26
+ delete d.params;
27
+ var ds = datasetOf(t.dataset);
28
+ d.columns = t.cols.map(function (p) {
29
+ var root = p.split('.')[0], rest = p.split('.').slice(1).join('.');
30
+ var list = root === (ds.alias) ? ds.fields : (ds.itemFields && ds.itemFields[(t.expand || '').split('.').slice(1).join('.')]) || [];
31
+ var f = list.filter(function (x) { return x.path === rest; })[0];
32
+ return { field: p, label: f ? f.label : undefined, format: f ? formatFor(f.type) : undefined };
33
+ });
34
+ }
35
+ function newReport() {
36
+ var def = { id: 'my-report-' + Date.now().toString(36), name: 'New report', category: 'My reports', from: { dataset: 'identities' } };
37
+ applyTemplate(def, TEMPLATES[0]);
38
+ state.current = { source: null, def: def, custom: true, isNew: true };
39
+ state.tab = 'design';
40
+ state.result = null;
41
+ state.groupBy = '';
42
+ state.runError = null;
43
+ state.paramValues = {};
44
+ state.dirty = true;
45
+ renderLibrary();
46
+ renderMain();
47
+ }
48
+ $('btn-new').addEventListener('click', newReport);
49
+
50
+ function changed() { state.dirty = true; state.current.yaml = null; }
51
+ function card(stepNo, iconName, title, hint) {
52
+ return el('section', { className: 'card' }, el('div', { className: 'card-h' },
53
+ el('h3', {}, stepNo ? el('span', { className: 'step', text: String(stepNo) }) : icon(iconName), title),
54
+ hint ? el('span', { className: 'muted small', text: hint }) : null));
55
+ }
56
+
57
+ function renderDesign(w) {
58
+ var c = state.current, d = c.def, ds = datasetOf(d.from.dataset);
59
+ ensureDisc(d.from.dataset);
60
+ var disc = state.disc[d.from.dataset] || {};
61
+
62
+ // about
63
+ var nameIn = el('input', { className: 'input', value: d.name });
64
+ nameIn.addEventListener('input', function () { d.name = nameIn.value; changed(); });
65
+ var descIn = el('input', { className: 'input', value: d.description || '', placeholder: 'What this report shows and who it is for' });
66
+ descIn.addEventListener('input', function () { d.description = descIn.value || undefined; changed(); });
67
+ var about = card(0, 'file', 'About this report');
68
+ about.appendChild(el('div', { className: 'grid', style: 'grid-template-columns:1fr 2fr' },
69
+ el('label', { className: 'field' }, el('span', { className: 'lbl', text: 'Name' }), nameIn),
70
+ el('label', { className: 'field' }, el('span', { className: 'lbl', text: 'Description' }), descIn)));
71
+ if (!c.custom) about.appendChild(el('div', { className: 'note', text: 'This is a ready-made report. Your changes are saved as your own copy under "My reports".' }));
72
+ w.appendChild(about);
73
+
74
+ // 1. data
75
+ var tpl = templateFor(d);
76
+ var data = card(1, null, 'What do you want a row to be?', disc.loading ? 'reading a sample of your tenant...' : disc.sampled !== undefined ? 'fields checked against ' + disc.sampled + ' records of your tenant' : '');
77
+ data.appendChild(el('div', { className: 'tiles' }, TEMPLATES.map(function (t) {
78
+ return el('button', { className: 'tile' + (tpl === t ? ' active' : ''), onclick: function () {
79
+ if (tpl === t) return;
80
+ if ((d.filter || d.where || d.group) && !confirm('Start over with "' + t.label + '"? Filters and columns are reset.')) return;
81
+ applyTemplate(d, t); changed(); renderMain();
82
+ } }, catIcon({ people: 'People', apps: 'Application owners', layers: 'Access model', db: 'Hygiene', pulse: 'Operations', inbox: 'Requests', cert: 'Certifications' }[t.icon]), el('div', {}, el('b', { text: t.label }), el('span', { text: t.desc })));
83
+ })));
84
+ if (!tpl) data.appendChild(el('div', { className: 'note', text: 'This report reads ' + ds.label.toLowerCase() + (d.expand ? ', one row per item of ' + d.expand.path : '') + '. Pick a tile to start over with another kind of row.' }));
85
+ if (disc.error) data.appendChild(el('div', { className: 'note', text: 'Could not read a sample from your tenant, so only the standard fields are offered. ' + disc.error }));
86
+ if (d.where || (d.expand && d.expand.where) || (d.joins && d.joins.length)) {
87
+ data.appendChild(el('div', { className: 'note', text: 'This report also has built-in rules and lookups. They are kept as they are; see the YAML tab.' }));
88
+ }
89
+ w.appendChild(data);
90
+
91
+ // 2. filters
92
+ w.appendChild(filtersCard(d));
93
+
94
+ // 3. columns or counts
95
+ w.appendChild(columnsCard(d));
96
+
97
+ // 4. sort and limit
98
+ var sortCard = card(4, null, 'Order and size');
99
+ var s0 = (d.sort && d.sort[0]) || '';
100
+ var dir = el('select', { className: 'input' }, el('option', { value: '', text: 'A → Z, smallest, oldest first' }), el('option', { value: '-', text: 'Z → A, largest, newest first' }));
101
+ dir.value = s0.charAt(0) === '-' ? '-' : '';
102
+ var sortField = s0.replace(/^[-+]/, '');
103
+ var writeSort = function () { if (sortField) d.sort = [dir.value + sortField]; else delete d.sort; changed(); };
104
+ dir.addEventListener('change', writeSort);
105
+ var lim = el('input', { className: 'input', type: 'number', min: '1', placeholder: 'all rows', value: d.limit || '' });
106
+ lim.addEventListener('input', function () { var n = Number(lim.value); if (lim.value && n > 0) d.limit = Math.floor(n); else delete d.limit; changed(); });
107
+ sortCard.appendChild(el('div', { className: 'grid' },
108
+ el('label', { className: 'field' }, el('span', { className: 'lbl', text: 'Sort by' }), fieldSelect(d, sortField, function (v) { sortField = v; writeSort(); }, { empty: 'Report order' })),
109
+ el('label', { className: 'field' }, el('span', { className: 'lbl', text: 'Direction' }), dir),
110
+ el('label', { className: 'field' }, el('span', { className: 'lbl', text: 'Only the first' }), lim)));
111
+ w.appendChild(sortCard);
112
+
113
+ // advanced
114
+ var search = ds.kind === 'search';
115
+ var qv = search ? d.from.query : d.from.filter;
116
+ var qIn = el('input', { className: 'input', value: Array.isArray(qv) ? qv.join(search ? ' AND ' : ' and ') : (qv || ''), placeholder: ds.hint || '' });
117
+ qIn.addEventListener('input', function () {
118
+ var key = search ? 'query' : 'filter';
119
+ if (qIn.value.trim()) d.from[key] = qIn.value; else delete d.from[key];
120
+ changed();
121
+ });
122
+ w.appendChild(el('section', { className: 'card' }, el('details', { className: 'adv' }, el('summary', { text: 'Advanced: make big reports faster' }),
123
+ el('p', { className: 'muted small', text: search ? 'An ISC search query narrows what is read from the tenant before your filters run. Your filters above still decide the result.' : 'An ISC API filter narrows what is read from the tenant before your filters run.' }),
124
+ el('label', { className: 'field' }, el('span', { className: 'lbl', text: search ? 'Search query' : 'API filter' }), qIn))));
125
+
126
+ // values to try the asked filters with
127
+ if (d.params && Object.keys(d.params).length) {
128
+ state.paramValues = state.paramValues || {};
129
+ var tc = card(0, 'sliders', 'Try it with', 'these are asked each time the report runs');
130
+ tc.appendChild(paramsGrid(d.params, state.paramValues, null, d));
131
+ w.appendChild(tc);
132
+ }
133
+
134
+ // actions
135
+ var msg = el('span', { className: 'muted small' });
136
+ w.appendChild(el('div', { className: 'actionbar' },
137
+ el('button', { className: 'btn', onclick: function () { run(500); setTimeout(function () { var o = $('run-out'); if (o) o.scrollIntoView({ behavior: 'smooth', block: 'start' }); }, 50); } }, icon('eye'), 'Preview'),
138
+ el('button', { className: 'btn primary', onclick: function () { save(msg); } }, icon('save'), c.custom ? 'Save' : 'Save as my report'),
139
+ c.custom && !c.isNew ? el('button', { className: 'btn ghost danger', onclick: function () {
140
+ if (!confirm('Delete "' + c.def.name + '"?')) return;
141
+ api('/api/reports/' + c.def.id, 'DELETE').then(function () { state.current = null; toast('Report deleted'); loadLibrary(); }).catch(function (e) { msg.textContent = errorText(e); });
142
+ } }, icon('trash'), 'Delete') : null,
143
+ msg));
144
+ w.appendChild(el('div', { id: 'run-out' }));
145
+ renderResult();
146
+ }
147
+
148
+ function filtersCard(d) {
149
+ var fc = card(2, null, 'Which rows?', 'leave empty for all');
150
+ var f = d.filter;
151
+ var list = !f ? [] : f.all ? f.all : f.any ? f.any : f.not ? null : [f];
152
+ var flat = list && list.every(function (x) { return typeof x.field === 'string'; });
153
+ if (!flat) { fc.appendChild(el('div', { className: 'muted', text: 'These filters are nested; edit them in the YAML tab.' })); return fc; }
154
+ var mode = f && f.any ? 'any' : 'all';
155
+ var write = function () {
156
+ list.forEach(function (x) { if (!needsValue(x.op)) delete x.value; });
157
+ var valid = list.filter(function (x) { return x.field; });
158
+ if (!valid.length) delete d.filter; else if (valid.length === 1) d.filter = valid[0]; else d.filter = mode === 'any' ? { any: valid } : { all: valid };
159
+ changed();
160
+ };
161
+ if (list.length > 1) {
162
+ var modeSel = el('select', { className: 'input', style: 'width:auto' }, el('option', { value: 'all', text: 'all' }), el('option', { value: 'any', text: 'any' }));
163
+ modeSel.value = mode;
164
+ modeSel.addEventListener('change', function () { mode = modeSel.value; write(); });
165
+ fc.appendChild(el('div', { className: 'joiner' }, 'Keep rows that match', modeSel, 'of these:'));
166
+ }
167
+ list.forEach(function (x, i) { fc.appendChild(conditionRow(d, x, function () { write(); }, function () { removeAsk(d, x); list.splice(i, 1); write(); renderMain(); })); });
168
+ fc.appendChild(el('button', { className: 'btn', onclick: function () {
169
+ var first = flatFields(d)[0];
170
+ list.push({ field: first ? first.value : '', op: opsFor(first ? first.type : 'string')[0][0], value: '' });
171
+ if (!needsValue(list[list.length - 1].op)) delete list[list.length - 1].value;
172
+ write(); renderMain();
173
+ } }, icon('plus'), 'Add a filter'));
174
+ return fc;
175
+ }
176
+
177
+ function paramNameFor(d, label) {
178
+ var base = label.replace(/[^A-Za-z0-9 ]/g, ' ').trim().split(/\s+/).map(function (w, i) { w = w.toLowerCase(); return i ? w.charAt(0).toUpperCase() + w.slice(1) : w; }).join('') || 'value';
179
+ if (/^[0-9]/.test(base)) base = 'p' + base;
180
+ var n = base, k = 2;
181
+ while (d.params && d.params[n]) n = base + k++;
182
+ return n;
183
+ }
184
+ function removeAsk(d, x) {
185
+ var m = typeof x.value === 'string' && ASK.exec(x.value);
186
+ if (m && d.params) { delete d.params[m[1]]; if (!Object.keys(d.params).length) delete d.params; }
187
+ }
188
+
189
+ function conditionRow(d, x, write, remove) {
190
+ var info = fieldInfo(d, x.field);
191
+ var ops = opsFor(info.type);
192
+ if (!ops.some(function (o) { return o[0] === x.op; })) ops = ops.concat([[x.op, (state.catalog.operators.filter(function (o) { return o.op === x.op; })[0] || { label: x.op }).label]]);
193
+ var askM = typeof x.value === 'string' && ASK.exec(x.value);
194
+ var fieldSel = fieldSelect(d, x.field, function (v) {
195
+ removeAsk(d, x);
196
+ x.field = v;
197
+ var t = fieldInfo(d, v).type;
198
+ x.op = opsFor(t)[0][0];
199
+ if (needsValue(x.op)) x.value = ''; else delete x.value;
200
+ write(); renderMain();
201
+ });
202
+ var opSel = el('select', { className: 'input' }, ops.map(function (o) { return el('option', { value: o[0], text: o[1] }); }));
203
+ opSel.value = x.op;
204
+ opSel.addEventListener('change', function () {
205
+ var wasList = x.op === 'in' || x.op === 'notIn';
206
+ x.op = opSel.value;
207
+ var isList = x.op === 'in' || x.op === 'notIn';
208
+ if (!needsValue(x.op)) { removeAsk(d, x); delete x.value; }
209
+ else if (!askM && wasList !== isList) x.value = isList ? (x.value ? [String(x.value)] : []) : (Array.isArray(x.value) ? (x.value[0] || '') : x.value || '');
210
+ else if (askM) { var p = d.params[askM[1]]; p.type = isList ? 'list' : (x.op === 'newerThanDays' || x.op === 'olderThanDays' || info.type === 'number') ? 'number' : 'string'; }
211
+ write(); renderMain();
212
+ });
213
+ var valueBox = el('div');
214
+ if (askM) {
215
+ var p = (d.params || {})[askM[1]] || {};
216
+ var q = el('input', { className: 'input', value: p.description || '', placeholder: 'Question shown when running' });
217
+ q.addEventListener('input', function () { p.description = q.value || undefined; changed(); });
218
+ valueBox.appendChild(q);
219
+ } else if (needsValue(x.op)) {
220
+ valueBox.appendChild(valueEditor(d, x, info, write));
221
+ }
222
+ var askBox = el('input', { type: 'checkbox', checked: !!askM });
223
+ askBox.addEventListener('change', function () {
224
+ if (askBox.checked) {
225
+ // Source names become a source drop-down: ask for the source, filter on its ID.
226
+ var idField = isSourceName(x.field) ? x.field.replace(/source\.name$|source\.displayableName$/, 'source.id').replace(/sourceName$/, 'sourceId') : null;
227
+ if (idField && flatFields(d).some(function (f) { return f.value === idField; })) { x.field = idField; x.value = Array.isArray(x.value) ? [] : ''; }
228
+ var listOp = x.op === 'in' || x.op === 'notIn';
229
+ var numeric = x.op === 'newerThanDays' || x.op === 'olderThanDays' || info.type === 'number';
230
+ var name = paramNameFor(d, fieldInfo(d, x.field).label);
231
+ d.params = d.params || {};
232
+ var spec = { type: listOp ? 'list' : numeric ? 'number' : 'string', description: fieldInfo(d, x.field).label + (numeric && x.op !== 'eq' ? ' (days)' : '') };
233
+ if (isSourceId(x.field)) spec.pick = 'sources';
234
+ var cur = x.value;
235
+ if (cur !== '' && cur !== undefined && !(Array.isArray(cur) && !cur.length) && !isSourceId(x.field)) spec['default'] = cur;
236
+ d.params[name] = spec;
237
+ x.value = '${' + name + '}';
238
+ state.paramValues = state.paramValues || {};
239
+ state.paramValues[name] = spec['default'] !== undefined ? spec['default'] : spec.type === 'list' ? [] : '';
240
+ } else {
241
+ var m = ASK.exec(x.value);
242
+ var prev = m && d.params && d.params[m[1]];
243
+ removeAsk(d, x);
244
+ x.value = prev && prev['default'] !== undefined ? prev['default'] : (x.op === 'in' || x.op === 'notIn') ? [] : '';
245
+ }
246
+ write(); renderMain();
247
+ });
248
+ var ask = needsValue(x.op) ? el('label', { className: 'ask' }, askBox, el('span', { text: askM ? 'Asked when the report runs (and in schedules).' : 'Ask for this value each time the report runs' })) : null;
249
+ return el('div', { className: 'cond' }, fieldSel, opSel, valueBox,
250
+ el('button', { className: 'btn icon ghost', title: 'Remove this filter', onclick: remove }, icon('x')), ask);
251
+ }
252
+
253
+ function valueEditor(d, x, info, write) {
254
+ var listOp = x.op === 'in' || x.op === 'notIn';
255
+ var path = info.path || x.field;
256
+ if (isSourceId(path) || isSourceName(path)) {
257
+ return picker({ options: loadPick('sources'), multiple: listOp, value: x.value, valueKey: isSourceId(path) ? 'value' : 'label', onChange: function (v) { x.value = v; write(); }, placeholder: listOp ? 'Pick sources' : 'Pick a source' });
258
+ }
259
+ if (x.op === 'newerThanDays' || x.op === 'olderThanDays' || info.type === 'number') {
260
+ var n = el('input', { className: 'input', type: 'number', value: x.value === undefined ? '' : String(x.value), placeholder: x.op === 'newerThanDays' || x.op === 'olderThanDays' ? 'days, e.g. 30' : 'number' });
261
+ n.addEventListener('input', function () { x.value = n.value === '' ? '' : Number(n.value); write(); });
262
+ return n;
263
+ }
264
+ if (listOp) {
265
+ if (info.values && info.values.length && info.values.length <= 50) {
266
+ return picker({ options: info.values.map(function (v) { return { value: v, label: v }; }), multiple: true, value: x.value, onChange: function (v) { x.value = v; write(); }, placeholder: 'Pick or type values' });
267
+ }
268
+ return tokenInput({ value: x.value, suggestions: info.values, onChange: function (v) { x.value = v; write(); } });
269
+ }
270
+ var id = 'sv' + Math.random().toString(36).slice(2);
271
+ var t = el('input', { className: 'input', value: x.value === undefined ? '' : String(x.value), list: info.values && info.values.length ? id : null, placeholder: info.values && info.values.length ? 'e.g. ' + info.values.slice(0, 2).join(', ') : 'value' });
272
+ t.addEventListener('input', function () { x.value = t.value; write(); });
273
+ return el('div', {}, t, info.values && info.values.length ? el('datalist', { id: id }, info.values.map(function (v) { return el('option', { value: v }); })) : null);
274
+ }
275
+
276
+ function columnsCard(d) {
277
+ var counting = !!(d.group && d.group.by && d.group.by.length);
278
+ var cc = card(3, null, 'What to show', null);
279
+ var modeSel = el('div', { className: 'chips', style: 'margin-bottom:14px' },
280
+ el('button', { className: 'chip' + (!counting ? ' active' : ''), onclick: function () {
281
+ if (!counting) return;
282
+ d.columns = state.current.stash || d.columns; delete d.group; changed(); renderMain();
283
+ } }, icon('cols'), 'A list of rows'),
284
+ el('button', { className: 'chip' + (counting ? ' active' : ''), onclick: function () {
285
+ if (counting) return;
286
+ state.current.stash = clone(d.columns || []);
287
+ var first = (d.columns && d.columns[0] && d.columns[0].field) || (flatFields(d)[0] || {}).value;
288
+ d.group = { by: [first], countLabel: 'Count' };
289
+ syncGroupColumns(d); changed(); renderMain();
290
+ } }, icon('sigma'), 'Counts (summary)'));
291
+ cc.appendChild(modeSel);
292
+ if (counting) {
293
+ cc.appendChild(el('p', { className: 'muted small', style: 'margin-top:0', text: 'One row per combination of these fields, with how many rows have it. Largest first.' }));
294
+ d.group.by.forEach(function (b, i) {
295
+ cc.appendChild(el('div', { className: 'colrow', style: 'grid-template-columns:28px 1fr auto' }, el('span', { className: 'n', text: String(i + 1) }),
296
+ fieldSelect(d, b, function (v) { d.group.by[i] = v; syncGroupColumns(d); changed(); }, { counting: true }),
297
+ el('button', { className: 'btn icon ghost', title: 'Remove', disabled: d.group.by.length === 1, onclick: function () { d.group.by.splice(i, 1); syncGroupColumns(d); changed(); renderMain(); } }, icon('x'))));
298
+ });
299
+ if (d.group.by.length < 4) cc.appendChild(fieldSelect(d, '', function (v) { if (!v) return; d.group.by.push(v); syncGroupColumns(d); changed(); renderMain(); }, { empty: '+ Also count by...', counting: true }));
300
+ var cl = el('input', { className: 'input', value: d.group.countLabel || 'Count', style: 'max-width:260px' });
301
+ cl.addEventListener('input', function () { d.group.countLabel = cl.value || undefined; changed(); });
302
+ cc.appendChild(el('label', { className: 'field', style: 'margin:14px 0 0' }, el('span', { className: 'lbl', text: 'Heading of the count column' }), cl));
303
+ return cc;
304
+ }
305
+ var formats = [['', 'Automatic'], ['text', 'Text'], ['date', 'Date'], ['datetime', 'Date and time'], ['number', 'Number'], ['boolean', 'Yes / No'], ['list', 'List'], ['count', 'How many'], ['json', 'Raw']];
306
+ d.columns = d.columns || [];
307
+ d.columns.forEach(function (col, i) {
308
+ var li = el('input', { className: 'input', value: col.label || '', placeholder: fieldInfo(d, col.field).label });
309
+ li.addEventListener('input', function () { col.label = li.value || undefined; changed(); });
310
+ var fs = el('select', { className: 'input' }, formats.map(function (x) { return el('option', { value: x[0], text: x[1] }); }));
311
+ fs.value = col.format || '';
312
+ fs.addEventListener('change', function () { col.format = fs.value || undefined; changed(); });
313
+ var move = function (delta) {
314
+ var j = i + delta;
315
+ if (j < 0 || j >= d.columns.length) return;
316
+ var t = d.columns[i]; d.columns[i] = d.columns[j]; d.columns[j] = t;
317
+ changed(); renderMain();
318
+ };
319
+ cc.appendChild(el('div', { className: 'colrow' }, el('span', { className: 'n', text: String(i + 1) }),
320
+ fieldSelect(d, col.field, function (v) { col.field = v; var inf = fieldInfo(d, v); col.label = inf.label; col.format = formatFor(inf.type); changed(); renderMain(); }),
321
+ li, fs,
322
+ el('div', { className: 'bar', style: 'gap:2px' },
323
+ el('button', { className: 'btn icon ghost', title: 'Move up', disabled: i === 0, onclick: function () { move(-1); } }, icon('up')),
324
+ el('button', { className: 'btn icon ghost', title: 'Move down', disabled: i === d.columns.length - 1, onclick: function () { move(1); } }, icon('down')),
325
+ el('button', { className: 'btn icon ghost', title: 'Remove', onclick: function () { d.columns.splice(i, 1); changed(); renderMain(); } }, icon('x')))));
326
+ });
327
+ if (!d.columns.length) cc.appendChild(el('div', { className: 'note', text: 'Add at least one column.' }));
328
+ var addSel = fieldSelect(d, '', function (v) {
329
+ if (!v) return;
330
+ var inf = fieldInfo(d, v);
331
+ d.columns.push({ field: v, label: inf.label, format: formatFor(inf.type) });
332
+ changed(); renderMain();
333
+ }, { empty: '+ Add a column...' });
334
+ addSel.style.maxWidth = '420px';
335
+ cc.appendChild(el('div', { style: 'margin-top:10px' }, addSel));
336
+ return cc;
337
+ }
338
+ function syncGroupColumns(d) {
339
+ d.columns = d.group.by.filter(Boolean).map(function (b) { var inf = fieldInfo(d, b); return { field: b, label: inf.label, format: inf.type === 'date' ? 'date' : undefined }; });
340
+ }
341
+
342
+ function save(msg) {
343
+ var c = state.current;
344
+ var def = clone(c.def);
345
+ if (!c.custom) {
346
+ def.id = def.id + '-copy';
347
+ def.name = def.name + ' (my copy)';
348
+ var n = 2;
349
+ while (state.reports.some(function (r) { return r.definition.id === def.id; })) def.id = c.def.id + '-copy-' + n++;
350
+ }
351
+ def.category = c.custom ? (def.category || 'My reports') : 'My reports';
352
+ msg.textContent = '';
353
+ api('/api/reports', 'POST', { definition: def }).then(function (r) {
354
+ toast('Saved "' + def.name + '" to My reports');
355
+ state.dirty = false;
356
+ state.current = null;
357
+ return loadLibrary(r.saved);
358
+ }).catch(function (e) { msg.textContent = ''; msg.appendChild(el('span', { className: 'error', text: errorText(e) })); });
359
+ }
360
+