OPNsense Forum

English Forums => 24.7, 24.10 Legacy Series => Topic started by: franciz on August 20, 2024, 05:33:13 PM

Title: SMART widget (needs corrections?)
Post by: franciz on August 20, 2024, 05:33:13 PM
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
Title: Re: SMART widget (needs corrections?)
Post by: franciz on August 27, 2024, 08:06:50 PM
Plugin seems to be working correctly, can someone post this on github and make a pull request?
Thanks!
Title: Re: SMART widget (needs corrections?)
Post by: franciz on August 30, 2024, 06:08:24 PM
It got broken with 24.7.3 update, had to delete the import.
Title: Re: SMART widget (needs corrections?)
Post by: doktornotor on August 30, 2024, 06:10:14 PM
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.
Title: Re: SMART widget (needs corrections?)
Post by: franciz on September 02, 2024, 07:06:20 PM
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...