SMART widget (needs corrections?)

Started by franciz, August 20, 2024, 05:33:13 PM

Previous topic - Next topic
August 20, 2024, 05:33:13 PM Last Edit: August 30, 2024, 06:08:42 PM by franciz
EDIT: Corrected by AI

export default class Smart extends BaseTableWidget {
    constructor() {
        super();
        this.tickTimeout = 300;
        this.disks = null;
    }

    getMarkup() {
        const $container = $('<div></div>');
        const $smarttable = this.createTable('smart-table', {
            headerPosition: 'left',
        });
        $container.append($smarttable);
        return $container;
    }

    async onWidgetTick() {
        if (this.disks && this.disks.devices) {
            for (const device of this.disks.devices) {
                try {
                    const data = await ajaxCall("/api/smart/service/info", { type: "H", device });
                    const health = data.output.includes("PASSED");
                    $(`#${device}`).css({ color: health ? "green" : "red", fontSize: '150%' });
                    $(`#${device}`).text(health ? "OK" : "FAILED");
                } catch (error) {
                    super.updateTable('smart-table', [[["Error"], $(`<span>${this.translations.nosmart} ${device}: ${error}</span>`).prop('outerHTML')]]);
                }
            }
        }
    }

async onMarkupRendered() {
        try {
            this.disks = await ajaxCall("/api/smart/service/list", {});
            const rows = [];
            for (const device of this.disks.devices) {
                const field = $(`<span id="${device}">`).prop('outerHTML');
                rows.push([[device], field]);
            }
            super.updateTable('smart-table', rows);
        } catch (error) {
            super.updateTable('smart-table', [[["Error"], $(`<span>${this.translations.nodisk}: ${error}</span>`).prop('outerHTML')]]);
        }
    }
}


<metadata>
    <smart>
        <filename>Smart.js</filename>
        <endpoints>
            <endpoint>/api/smart/service/list</endpoint>
            <endpoint>/api/smart/service/info</endpoint>
        </endpoints>
        <translations>
            <title>SMART Status</title>
            <nodisk>Error fetching disk list</nodisk>
            <nosmart>Error fetching SMART info for device</nosmart>
        </translations>
    </smart>
</metadata>


PS: Some stuff that should be fixed in the documentation.
1) To import the base js I had to use its relative path.
2) tickTimeout is in seconds and not ms

Plugin seems to be working correctly, can someone post this on github and make a pull request?
Thanks!

It got broken with 24.7.3 update, had to delete the import.

Things like this will keep getting broken unless you submit a PR on GitHub. Anyone can do that. Normally you don't grab some other people's code a submit it somewhere.

I have no idea what I am doing, I created 2 pull requests, 1 per file.
I think I cannot do it all in one when creating the request from github...