diff --git a/src/components/Widgets/UptimeKuma.vue b/src/components/Widgets/UptimeKuma.vue index 0adf7bc2..5807160d 100644 --- a/src/components/Widgets/UptimeKuma.vue +++ b/src/components/Widgets/UptimeKuma.vue @@ -1,12 +1,24 @@ @@ -17,8 +29,7 @@ * Takes two optional parameters (`text` and `count`), and fetches a list of images * from dummyapis.com, then renders the results to the UI. */ -import axios from "axios"; -import WidgetMixin from "@/mixins/WidgetMixin"; +import WidgetMixin from '@/mixins/WidgetMixin'; export default { mixins: [WidgetMixin], @@ -36,26 +47,26 @@ export default { * If not present, or not a number, then return the default (5) */ apiKey() { - const apiKey = this.options.apiKey; + const { apiKey } = this.options; - if (!apiKey) throw "No API key set"; + if (!apiKey) throw new Error('No API key set'); return apiKey; }, /* Get users desired image text, or return `Dashy` */ url() { - const apiUrl = this.options.url; + const { url } = this.options; - if (!apiUrl) throw "No URL set"; + if (!url) throw new Error('No URL set'); - return apiUrl; + return url; }, authHeaders() { if (!this.options.apiKey) { return {}; } const encoded = window.btoa(`:${this.options.apiKey}`); - return { Authorization: `Basic ${encoded}` }; + return { Authorization: `Basic ${encoded}` }; }, }, methods: { @@ -73,73 +84,82 @@ export default { }, /* Convert API response data into a format to be consumed by the UI */ processData(response) { - const rows = response.split('\n').filter(row => row.startsWith("monitor_")); + const rows = response.split('\n').filter(row => row.startsWith('monitor_')); const monitors = new Map(); - const regex = /monitor_name="([^"]+)"/; + const regex = /monitor_name='([^']+)'/; - const getValue = (row) => row.match(/\b\d+\b$/); + const getValue = (row) => row.match(/\b\d+\b$/)[0]; - for (const row of rows) { - const key = row.match(/^(.*?)\{/); - const monitorName = row.match(regex); + for (let index = 0; index < rows.length; index++) { + const row = rows[x]; + + const key = row.match(/^(.*?)\{/)[1]; + const monitorName = row.match(regex)[1]; if (!monitors.has(monitorName)) { - monitors.set(monitorName, {name: monitorName}); + monitors.set(monitorName, { name: monitorName }); } - const existingMonitor = monitors.get(monitorName); - + const monitor = monitors.get(monitorName); const value = getValue(row); - switch (key) { - case "monitor_cert_days_remaining": { - existingMonitor.certDaysRemaining = value; - break; - } - case "monitor_cert_is_valid": { - existingMonitor.certValid = value; - break; - } - case "monitor_response_time": { - existingMonitor.responseTime = value; - break; - } - case "monitor_status": { - existingMonitor.status= value; - break; - } - default: - console.log("not matched", key); - break; - } + this.setMonitorValue(key, monitor, value); + this.monitors = Array.from(monitors.values()); + } + }, + setMonitorValue(key, monitor, value) { + switch (key) { + case 'monitor_cert_days_remaining': { + monitor.certDaysRemaining = value; + break; + } + case 'monitor_cert_is_valid': { + monitor.certValid = value; + break; + } + case 'monitor_response_time': { + monitor.responseTime = value; + break; + } + case 'monitor_status': { + monitor.status = value; + break; + } + default: + break; } - - console.log(monitors); - this.monitors = Array.from(monitors.values()); }, }, }; +