Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions Background Scripts/Custom Table Usage/customTableUsage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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" },
{ "table": "sys_script_include", "field": "script", "title": "Script Include", 'query': 'CONTAINS'}
];

// 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))



52 changes: 52 additions & 0 deletions Background Scripts/Custom Table Usage/readme.md
Original file line number Diff line number Diff line change
@@ -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'}
];
```