Last active
April 23, 2024 06:01
-
-
Save inebritov/8e9b27b8cb721a6ba93138e0c3681ca5 to your computer and use it in GitHub Desktop.
Проверяет валидность конфигурации устройств в агенте
This file contains hidden or 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
javascript:fetch("https://gist.githubusercontent.com/inebritov/8e9b27b8cb721a6ba93138e0c3681ca5/raw/agent-validator.js?cachebust="+Math.floor(1000*Math.random())).then((t=>t.text())).then((js=>{eval(js),window.__AgentValidator()})).catch((t=>console.error(t))); |
This file contains hidden or 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
/* | |
Валидация устройств агента. | |
Как использовать: | |
1. Открываем страницу агента | |
2. Нажимаем Ctrl+Shift+B - отобразить панель закладок браузера | |
3. В панели закладок нажимаем правой кнопкой, выбираем добавить | |
4. Название "agent validate", а в URL вставляем содержимое файла agent-validator.bookmarklet.js | |
5. Сохраняем закладку | |
Теперь если на странице агента нажать на эту закладку, скрипт подсветит красным устройства, у которых невалидная конфигурация. | |
В последствии, валидация будет добавлена в функции агента по-умолчанию. | |
*/ | |
window.__AgentValidator = () => { | |
fetch(location.protocol + '//' + location.host + "/api/v1/devices") | |
.then(response => console.log(response.status) || response) | |
.then(response => response.text()) | |
.then(body => validateDevices(body)); | |
function addDeviceAlert(device, error) { | |
error = error || ''; | |
let row = document.querySelector('[data-row-key="' + device.id + '"]'); | |
row.style.background = 'pink'; | |
return device.name + ' - ' + error + '\n'; | |
} | |
function validateDevices(body) { | |
let devices = JSON.parse(body).devices; | |
if (!devices) alert('Нет устройств'); | |
console.log(devices); | |
let errors = 0, message = ''; | |
for (let device of devices) { | |
let map = device.feature_mappings; | |
if (!map || Object.keys(map).length == 0) { | |
errors++; | |
message += addDeviceAlert(device, 'стёрлись функции'); | |
continue; | |
} | |
for (let key in map) { | |
if (!map[key].mappings || map[key].mappings.length == 0) { | |
errors++; | |
message += addDeviceAlert(device, 'нет функций'); | |
continue; | |
} | |
for (let item of map[key].mappings) { | |
if (!item.status_object) { | |
errors++; | |
message += addDeviceAlert(device, 'пустой топик статуса'); | |
continue; | |
} | |
} | |
} | |
} | |
let text = 'Неверно сконфигурированных устройств: ' + errors + '\n' + message; | |
console.log(text); | |
alert(text); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment