Skip to content

Commit 4357dd5

Browse files
authored
Merge pull request #890 from ajcooper72/CustomTableUsage
Custom table usage
2 parents d0e5029 + 355117f commit 4357dd5

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const USAGE_COUNT_CONFIG = [
2+
{ "table": "sys_dictionary", "field": "reference", "title": "Dictionary" },
3+
{ "table": "item_option_new", "field": "reference", "title": "Variables" },
4+
{ "table": "sys_script", "field": "collection", "title": "Business Rules" },
5+
{ "table": "sys_script_client", "field": "table", "title": "Client Scripts" },
6+
{ "table": "sys_dictionary", "field": "name", "title": "Dictionary Entries" },
7+
{ "table": "sys_dictionary_override", "field": "name", "title": "Dictionary Overrides" },
8+
// { "table": "sysevent_email_action", "field": "collection ", "title": "Notifications", "query": "" },
9+
{ "table": "sys_ui_action", "field": "table", "title": "UI Actions" },
10+
{ "table": "sys_security_acl", "field": "name", "title": "ACL", "query": "STARTSWITH" },
11+
{ "table": "sys_ui_policy", "field": "table", "title": "UI Policies", },
12+
{ "table": "sys_data_policy2", "field": "model_table", "title": "Data Policy" },
13+
{ "table": "sys_ui_style", "field": "name", "title": "Styles" },
14+
{ "table": "sysrule_view", "field": "table", "title": "View Rules" },
15+
{ "table": "wf_workflow", "field": "table", "title": "Workflows" },
16+
{ "table": "sys_hub_flow", "field": "sys_id", "title": "Flows", "query": "", "query_field": "sys_id" },
17+
{ "table": "sys_script_include", "field": "script", "title": "Script Include", 'query': 'CONTAINS'}
18+
];
19+
20+
// get list of fields to query from the table
21+
// grab any fields which are listed as query_fields in the usage config, and add name and label.
22+
var selectFields = USAGE_COUNT_CONFIG.map(function (_obj) {
23+
return _obj.query_field;
24+
}).filter(Boolean);
25+
26+
selectFields.push('name');
27+
selectFields.push('label');
28+
29+
var gqTables = new global.GlideQuery('sys_db_object')
30+
.where('name', 'STARTSWITH', 'u_')
31+
// don't want m2m tables
32+
.where('name', 'NOT LIKE', 'm2m')
33+
// don't want tables extended from Import Set Row or Query Builder Results
34+
// apologies for the hard-coded sys_ids, they'll never change, right?
35+
.where('super_class', 'NOT IN', ['88d993c3b4232110320f8dc279c8042b', '897b97c7b4632110320f8dc279c80489'])
36+
.select(selectFields)
37+
.map(function (_table) {
38+
var references = {};
39+
_table.references = references;
40+
41+
USAGE_COUNT_CONFIG.forEach(function (_usageConfig) {
42+
var query_type = _usageConfig['query'] ? _usageConfig['query'] : "=";
43+
var query_field = _usageConfig['query_field'] ? _usageConfig['query_field'] : 'name';
44+
45+
var gqUsageCount = new global.GlideQuery(_usageConfig.table)
46+
.where(_usageConfig.field, query_type, _table[query_field])
47+
.count();
48+
49+
references[_usageConfig.title] = gqUsageCount;
50+
})
51+
delete _table["sys_id"]; // get rid of the sys_id field
52+
53+
return _table;
54+
})
55+
.reduce(function (arr, e) { arr.push(e); return arr; }, []);
56+
57+
gs.info(JSON.stringify(gqTables, '', 3))
58+
59+
60+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Custom Table Usage Count
2+
3+
This script provides a way of counting where any custom tables (u_) are used in the instance.
4+
5+
Returns JSON object similar to the following:
6+
7+
```json
8+
[
9+
{
10+
"name": "u_cmdb_ci_key_value_staging",
11+
"label": "Key Value Staging",
12+
"references": {
13+
"Dictionary": 0,
14+
"Variables": 0,
15+
"Business Rules": 0,
16+
"Client Scripts": 0,
17+
"Dictionary Entries": 86,
18+
"Dictionary Overrides": 0,
19+
"UI Actions": 0,
20+
"ACL": 0,
21+
"UI Policies": 0,
22+
"Data Policy": 0,
23+
"Styles": 0,
24+
"View Rules": 0,
25+
"Workflows": 0,
26+
"Flows": 0
27+
}
28+
}
29+
]
30+
```
31+
32+
Easily extended by adding more entries in the USAGE_COUNT_CONFIG object.
33+
34+
```javascript
35+
const USAGE_COUNT_CONFIG = [
36+
{ "table": "sys_dictionary", "field": "reference", "title": "Dictionary" },
37+
{ "table": "item_option_new", "field": "reference", "title": "Variables" },
38+
{ "table": "sys_script", "field": "collection", "title": "Business Rules" },
39+
{ "table": "sys_script_client", "field": "table", "title": "Client Scripts" },
40+
{ "table": "sys_dictionary", "field": "name", "title": "Dictionary Entries" },
41+
{ "table": "sys_dictionary_override", "field": "name", "title": "Dictionary Overrides" },
42+
{ "table": "sys_ui_action", "field": "table", "title": "UI Actions" },
43+
{ "table": "sys_security_acl", "field": "name", "title": "ACL", "query": "STARTSWITH" },
44+
{ "table": "sys_ui_policy", "field": "table", "title": "UI Policies", },
45+
{ "table": "sys_data_policy2", "field": "model_table", "title": "Data Policy" },
46+
{ "table": "sys_ui_style", "field": "name", "title": "Styles" },
47+
{ "table": "sysrule_view", "field": "table", "title": "View Rules" },
48+
{ "table": "wf_workflow", "field": "table", "title": "Workflows" },
49+
{ "table": "sys_hub_flow", "field": "sys_id", "title": "Flows", "query": "", "query_field": "sys_id" },
50+
{ "table": "sys_script_include", "field": "script", "title": "Script Include", 'query': 'CONTAINS'}
51+
];
52+
```

0 commit comments

Comments
 (0)