-
-
Save totten/3d9700c860b51a5dd5e3ac39f125fab1 to your computer and use it in GitHub Desktop.
APIv4 field metadata report
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"keys(fields)": { | |
"COUNT": 1945, | |
"COMMON": { | |
"column_name": "1630 (83.80%)", | |
"custom_field_id": "1630 (83.80%)", | |
"custom_group_id": "1630 (83.80%)", | |
"data_type": "1945 (100.00%)", | |
"default_value": "1722 (88.53%)", | |
"deprecated": "1722 (88.53%)", | |
"description": "1945 (100.00%)", | |
"entity": "1722 (88.53%)", | |
"fk_entity": "1722 (88.53%)", | |
"help_post": "1630 (83.80%)", | |
"help_pre": "1630 (83.80%)", | |
"input_attrs": "1722 (88.53%)", | |
"input_type": "1722 (88.53%)", | |
"label": "1722 (88.53%)", | |
"name": "1945 (100.00%)", | |
"nullable": "1722 (88.53%)", | |
"operators": "1722 (88.53%)", | |
"options": "1945 (100.00%)", | |
"permission": "1630 (83.80%)", | |
"readonly": "1945 (100.00%)", | |
"required": "1722 (88.53%)", | |
"required_if": "1722 (88.53%)", | |
"serialize": "1945 (100.00%)", | |
"suffixes": "1722 (88.53%)", | |
"table_name": "1630 (83.80%)", | |
"title": "1945 (100.00%)", | |
"type": "1722 (88.53%)" | |
}, | |
"UNCOMMON": { | |
"add": "223 (11.47%)", | |
"custom_group": "12 (0.62%)", | |
"default": "224 (11.52%)", | |
"help_text": "223 (11.47%)", | |
"html_type": "223 (11.47%)" | |
}, | |
"OUTLIER": [] | |
}, | |
"keys(fields.input_attrs)": { | |
"COUNT": 1722, | |
"COMMON": { | |
"label": "473 (27.47%)" | |
}, | |
"UNCOMMON": { | |
"*NO-KEYS*": "92 (5.34%)", | |
"cols": "35 (2.03%)", | |
"control_field": "4 (0.23%)", | |
"date": "65 (3.77%)", | |
"end_date_years": "65 (3.77%)", | |
"maxlength": "148 (8.59%)", | |
"multiple": "8 (0.46%)", | |
"rows": "36 (2.09%)", | |
"start_date_years": "65 (3.77%)", | |
"time": "65 (3.77%)" | |
}, | |
"OUTLIER": { | |
"filter": "1 (0.06%)" | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function jp($ar) { | |
echo json_encode($ar, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES); | |
echo "\n"; | |
} | |
function keyCount(array $md) { | |
$counts = []; | |
foreach ($md as $item) { | |
if (!is_array($item)) { | |
$counts['*NO-KEYS*'] = 1 + ($counts['*NO-KEYS*'] ?? 0); | |
} | |
else { | |
foreach (array_keys($item) as $key) { | |
$counts[$key] = 1 + ($counts[$key] ?? 0); | |
} | |
} | |
} | |
ksort($counts); | |
return $counts; | |
} | |
function keySummary(array $md) { | |
$items = count($md); | |
$threshold = round($items / 4); | |
$counts = keyCount($md); | |
$buckets = ['COUNT' => $items, 'COMMON' => [], 'UNCOMMON' => [], 'OUTLIER' => []]; | |
foreach ($counts as $key => $count) { | |
if ($count < 3) { | |
$bucket = 'OUTLIER'; | |
} | |
elseif ($count < $threshold) { | |
$bucket = 'UNCOMMON'; | |
} | |
else { | |
$bucket = 'COMMON'; | |
} | |
$buckets[$bucket][$key] = sprintf('%d (%.2f%%)', $count, 100 * $count / $items); | |
} | |
return $buckets; | |
} | |
function collectKeys(array $md) { | |
$keys = array_reduce($md, function($carry, $item){ | |
return array_unique(array_merge( | |
$carry, | |
array_keys($item) | |
)); | |
}, []); | |
kort($keys); | |
return $keys; | |
} | |
function api4Metadata() { | |
$es = \Civi\Api4\Entity::get()->execute()->column('name'); | |
$allFields = []; | |
foreach ($es as $e) { | |
$fields = civicrm_api4($e, 'getFields'); | |
foreach ($fields as $field) { | |
$name = $field['name']; | |
$allFields["$e.$name"] = $field; | |
} | |
} | |
return $allFields; | |
} | |
$md = api4Metadata(); | |
// jp($md); | |
jp([ | |
'keys(fields)' => keySummary($md), | |
'keys(fields.input_attrs)' => keySummary(array_column($md, 'input_attrs')), | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment