From d466943baf524bba4f26476c4ff1ae9f76105f8d Mon Sep 17 00:00:00 2001 From: Aidan Cooper Date: Tue, 17 Oct 2023 16:48:42 +0100 Subject: [PATCH 1/3] Create customTableUsage.js --- .../Custom Table Usage/customTableUsage.js | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Background Scripts/Custom Table Usage/customTableUsage.js diff --git a/Background Scripts/Custom Table Usage/customTableUsage.js b/Background Scripts/Custom Table Usage/customTableUsage.js new file mode 100644 index 0000000000..9715d69c8b --- /dev/null +++ b/Background Scripts/Custom Table Usage/customTableUsage.js @@ -0,0 +1,55 @@ +const USAGE_COUNT_CONFIG = [ + { "table": "sys_dictionary", "field": "reference", "title": "Dictionary" }, + { "table": "item_option_new", "field": "reference", "title": "Variables" }, + { "table": "sys_script", "field": "collection", "title": "Business Rules" }, + { "table": "sys_script_client", "field": "table", "title": "Client Scripts" }, + { "table": "sys_dictionary", "field": "name", "title": "Dictionary Entries" }, + { "table": "sys_dictionary_override", "field": "name", "title": "Dictionary Overrides" }, + // { "table": "sysevent_email_action", "field": "collection ", "title": "Notifications", "query": "" }, + { "table": "sys_ui_action", "field": "table", "title": "UI Actions" }, + { "table": "sys_security_acl", "field": "name", "title": "ACL", "query": "STARTSWITH" }, + { "table": "sys_ui_policy", "field": "table", "title": "UI Policies", }, + { "table": "sys_data_policy2", "field": "model_table", "title": "Data Policy" }, + { "table": "sys_ui_style", "field": "name", "title": "Styles" }, + { "table": "sysrule_view", "field": "table", "title": "View Rules" }, + { "table": "wf_workflow", "field": "table", "title": "Workflows" }, + { "table": "sys_hub_flow", "field": "sys_id", "title": "Flows", "query": "", "query_field": "sys_id" }, +]; + +// get list of fields to query from the table +// grab any fields which are listed as query_fields in the usage config, and add name and label. +var selectFields = USAGE_COUNT_CONFIG.map(function (_obj) { + return _obj.query_field; +}).filter(Boolean); +selectFields.push('name'); +selectFields.push('label'); + +var gqTables = new global.GlideQuery('sys_db_object') + .where('name', 'STARTSWITH', 'u_') + // don't want m2m tables + .where('name', 'NOT LIKE', 'm2m') + // don't want tables extended from Import Set Row or Query Builder Results + // apologies for the hard-coded sys_ids, they'll never change, right? + .where('super_class', 'NOT IN', ['88d993c3b4232110320f8dc279c8042b', '897b97c7b4632110320f8dc279c80489']) + .select(selectFields) + .map(function (_table) { + var references = {}; + _table.references = references; + + USAGE_COUNT_CONFIG.forEach(function (_usageConfig) { + var query_type = _usageConfig['query'] ? _usageConfig['query'] : "="; + var query_field = _usageConfig['query_field'] ? _usageConfig['query_field'] : 'name'; + + var gqUsageCount = new global.GlideQuery(_usageConfig.table) + .where(_usageConfig.field, query_type, _table[query_field]) + .count(); + + references[_usageConfig.title] = gqUsageCount; + }) + delete _table["sys_id"]; // get rid of the sys_id field + + return _table; + }) + .reduce(function (arr, e) { arr.push(e); return arr; }, []); + +gs.info(JSON.stringify(gqTables, '', 3)) From 1330b9f074c3ad5a81af87c80939809a56c4a275 Mon Sep 17 00:00:00 2001 From: Aidan Cooper Date: Tue, 17 Oct 2023 16:54:36 +0100 Subject: [PATCH 2/3] Update customTableUsage.js Add script includes --- Background Scripts/Custom Table Usage/customTableUsage.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Background Scripts/Custom Table Usage/customTableUsage.js b/Background Scripts/Custom Table Usage/customTableUsage.js index 9715d69c8b..a98d90e23b 100644 --- a/Background Scripts/Custom Table Usage/customTableUsage.js +++ b/Background Scripts/Custom Table Usage/customTableUsage.js @@ -14,6 +14,7 @@ const USAGE_COUNT_CONFIG = [ { "table": "sysrule_view", "field": "table", "title": "View Rules" }, { "table": "wf_workflow", "field": "table", "title": "Workflows" }, { "table": "sys_hub_flow", "field": "sys_id", "title": "Flows", "query": "", "query_field": "sys_id" }, + { "table": "sys_script_include", "field": "script", "title": "Script Include", 'query': 'CONTAINS'} ]; // get list of fields to query from the table @@ -21,6 +22,7 @@ const USAGE_COUNT_CONFIG = [ var selectFields = USAGE_COUNT_CONFIG.map(function (_obj) { return _obj.query_field; }).filter(Boolean); + selectFields.push('name'); selectFields.push('label'); @@ -53,3 +55,6 @@ var gqTables = new global.GlideQuery('sys_db_object') .reduce(function (arr, e) { arr.push(e); return arr; }, []); gs.info(JSON.stringify(gqTables, '', 3)) + + + From 355117f07a6ca382baa59dff49a68edeefd58155 Mon Sep 17 00:00:00 2001 From: Aidan Cooper Date: Tue, 17 Oct 2023 16:56:06 +0100 Subject: [PATCH 3/3] Create readme.md --- .../Custom Table Usage/readme.md | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Background Scripts/Custom Table Usage/readme.md diff --git a/Background Scripts/Custom Table Usage/readme.md b/Background Scripts/Custom Table Usage/readme.md new file mode 100644 index 0000000000..41055563f7 --- /dev/null +++ b/Background Scripts/Custom Table Usage/readme.md @@ -0,0 +1,52 @@ +# Custom Table Usage Count + +This script provides a way of counting where any custom tables (u_) are used in the instance. + +Returns JSON object similar to the following: + +```json +[ + { + "name": "u_cmdb_ci_key_value_staging", + "label": "Key Value Staging", + "references": { + "Dictionary": 0, + "Variables": 0, + "Business Rules": 0, + "Client Scripts": 0, + "Dictionary Entries": 86, + "Dictionary Overrides": 0, + "UI Actions": 0, + "ACL": 0, + "UI Policies": 0, + "Data Policy": 0, + "Styles": 0, + "View Rules": 0, + "Workflows": 0, + "Flows": 0 + } + } +] +``` + +Easily extended by adding more entries in the USAGE_COUNT_CONFIG object. + +```javascript +const USAGE_COUNT_CONFIG = [ + { "table": "sys_dictionary", "field": "reference", "title": "Dictionary" }, + { "table": "item_option_new", "field": "reference", "title": "Variables" }, + { "table": "sys_script", "field": "collection", "title": "Business Rules" }, + { "table": "sys_script_client", "field": "table", "title": "Client Scripts" }, + { "table": "sys_dictionary", "field": "name", "title": "Dictionary Entries" }, + { "table": "sys_dictionary_override", "field": "name", "title": "Dictionary Overrides" }, + { "table": "sys_ui_action", "field": "table", "title": "UI Actions" }, + { "table": "sys_security_acl", "field": "name", "title": "ACL", "query": "STARTSWITH" }, + { "table": "sys_ui_policy", "field": "table", "title": "UI Policies", }, + { "table": "sys_data_policy2", "field": "model_table", "title": "Data Policy" }, + { "table": "sys_ui_style", "field": "name", "title": "Styles" }, + { "table": "sysrule_view", "field": "table", "title": "View Rules" }, + { "table": "wf_workflow", "field": "table", "title": "Workflows" }, + { "table": "sys_hub_flow", "field": "sys_id", "title": "Flows", "query": "", "query_field": "sys_id" }, + { "table": "sys_script_include", "field": "script", "title": "Script Include", 'query': 'CONTAINS'} +]; +```