ronantakizawa/codereview-bench huggingface dataset:
Repository: louislam/uptime-kuma
File: src/i18n.js (JavaScript)

Code before:

for (let lang in languageList) {
    messages[lang] = {
        languageName: languageList[lang]
    };
}

const rtlLangs = [ "fa", "ar-SY", "ur" ];

/**
 * Find the best matching locale to display
 * If no locale can be matched, the default is "en"
 * @returns {string} the locale that should be displayed
 */
export function currentLocale() {
    for (const locale of [ localStorage.locale, navigator.language, ...navigator.languages ]) {
        // localstorage might not have a value or there might not be a language in `navigator.language`
        if (!locale) {
            continue
        }
        if (locale in messages) {
            return locale;
        }
        // some locales are further specified such as "en-US".
        // If we only have a generic locale for this, we can use it too
        const genericLocale = l.split("-")[0];
        if (genericLocale in messages) {
            return genericLocale;
        }
    }
    return "en";
}

export const localeDirection = () => {
    return rtlLangs.includes(currentLocale()) ? "rtl" : "ltr";
};

export const i18n = createI18n({
    locale: currentLocale(),
    fallbackLocale: "en",
    silentFallbackWarn: true,
    silentTranslationWarn: true,
    messages: messages,
});


Review comment (suggestion):
minor typo

```suggestion
        const genericLocale = locale.split("-")[0];
```

Code after:

for (let lang in languageList) {
    messages[lang] = {
        languageName: languageList[lang]
    };
}

const rtlLangs = [ "fa", "ar-SY", "ur" ];

/**
 * Find the best matching locale to display
 * If no locale can be matched, the default is "en"
 * @returns {string} the locale that should be displayed
 */
export function currentLocale() {
    for (const locale of [ localStorage.locale, navigator.language, ...navigator.languages ]) {
        // localstorage might not have a value or there might not be a language in `navigator.language`
        if (!locale) {
            continue;
        }
        if (locale in messages) {
            return locale;
        }
        // some locales are further specified such as "en-US".
        // If we only have a generic locale for this, we can use it too
        const genericLocale = locale.split("-")[0];
        if (genericLocale in messages) {
            return genericLocale;
        }
    }
    return "en";
}

export const localeDirection = () => {
    return rtlLangs.includes(currentLocale()) ? "rtl" : "ltr";
};

export const i18n = createI18n({
    locale: currentLocale(),
    fallbackLocale: "en",
    silentFallbackWarn: true,
    silentTranslationWarn: true,
    messages: messages,
});
Repository: louislam/uptime-kuma
File: src/i18n.js (JavaScript)

Code before:
    "ur": "Urdu",
    "ge": "ქართული"
};

let messages = {
    en,
};

for (let lang in languageList) {
    messages[lang] = {
        languageName: languageList[lang]
    };
}

const rtlLangs = [ "fa", "ar-SY", "ur" ];

/**
 * Find the best matching locale to display
 * If no locale can be matched, the default is "en"
 * @returns {string} the locale that should be displayed
 */
export function currentLocale() {
    for (const locale of [ localStorage.locale, navigator.language, ...navigator.languages ]) {
        // localstorage might not have a value or there might not be a language in `navigator.language`
        if (!locale) {
            continue
        }
        if (locale in messages) {
            return locale;
        }
        // some locales are further specified such as "en-US".
        // If we only have a generic locale for this, we can use it too
        const genericLocale = locale.split("-")[0];
        if (genericLocale in messages) {
            return genericLocale;
        }
    }
    return "en";
}

export const localeDirection = () => {
    return rtlLangs.includes(currentLocale()) ? "rtl" : "ltr";
};

export const i18n = createI18n({
    locale: currentLocale(),
    fallbackLocale: "en",
    silentFallbackWarn: true,
    silentTranslationWarn: true,
    messages: messages,
});

Review comment (suggestion):
Formatting fix
```suggestion
            continue;
```

Code after:
    "ur": "Urdu",
    "ge": "ქართული"
};

let messages = {
    en,
};

for (let lang in languageList) {
    messages[lang] = {
        languageName: languageList[lang]
    };
}

const rtlLangs = [ "fa", "ar-SY", "ur" ];

/**
 * Find the best matching locale to display
 * If no locale can be matched, the default is "en"
 * @returns {string} the locale that should be displayed
 */
export function currentLocale() {
    for (const locale of [ localStorage.locale, navigator.language, ...navigator.languages ]) {
        // localstorage might not have a value or there might not be a language in `navigator.language`
        if (!locale) {
            continue;
        }
        if (locale in messages) {
            return locale;
        }
        // some locales are further specified such as "en-US".
        // If we only have a generic locale for this, we can use it too
        const genericLocale = locale.split("-")[0];
        if (genericLocale in messages) {
            return genericLocale;
        }
    }
    return "en";
}

export const localeDirection = () => {
    return rtlLangs.includes(currentLocale()) ? "rtl" : "ltr";
};

export const i18n = createI18n({
    locale: currentLocale(),
    fallbackLocale: "en",
    silentFallbackWarn: true,
    silentTranslationWarn: true,
    messages: messages,
});
Repository: louislam/uptime-kuma
File: server/monitor-types/snmp.js (JavaScript)

Code before:
const { MonitorType } = require("./monitor-type");
const { UP, DOWN } = require("../../src/util");
const snmp = require("snmp-native");

class SNMPMonitorType extends MonitorType {
    name = "snmp";

    /**
     * Checks the SNMP value against the condition and control value.
     * @param {object} monitor The monitor object associated with the check.
     * @param {object} heartbeat The heartbeat object to update.
     * @param {object} _server Unused server object.
     */
    async check(monitor, heartbeat, _server) {
        try {
            const session = new snmp.Session({ host: monitor.ipAddress, community: monitor.snmpCommunityString, version: monitor.snmpVersion });

            session.get({ oid: monitor.snmpOid }, (err, varbinds) => {
                if (err) {
                    heartbeat.status = DOWN;
                    heartbeat.msg = `Error: ${err.message}`;
                    return;
                }

                // Assuming only one varbind is returned
                const value = varbinds[0].value;

                // Convert value to appropriate type based on SNMP type (assuming it's integer or string for simplicity)
                const numericValue = parseInt(value);
                const stringValue = value.toString();

                // Check against condition and control value
                switch (monitor.snmpCondition) {
                    case '>':
                        heartbeat.status = numericValue > monitor.snmpControlValue ? UP : DOWN;
                        break;
                    case '>=':

Review comment (refactor):
You can simplify your live here:

```suggestion
     * @inheritdoc
```

Code after:
const { MonitorType } = require("./monitor-type");
const { UP, log, evaluateJsonQuery } = require("../../src/util");
const snmp = require("net-snmp");

class SNMPMonitorType extends MonitorType {
    name = "snmp";

    /**
     * @inheritdoc
     */
    async check(monitor, heartbeat, _server) {
        let session;
        try {
            const sessionOptions = {
                port: monitor.port || "161",
                retries: monitor.maxretries,
                timeout: monitor.timeout * 1000,
                version: snmp.Version[monitor.snmpVersion],
            };
            session = snmp.createSession(monitor.hostname, monitor.radiusPassword, sessionOptions);

            // Handle errors during session creation
            session.on("error", (error) => {
                throw new Error(`Error creating SNMP session: ${error.message}`);
            });

            const varbinds = await new Promise((resolve, reject) => {
                session.get([ monitor.snmpOid ], (error, varbinds) => {
                    error ? reject(error) : resolve(varbinds);
                });
            });
            log.debug("monitor", `SNMP: Received varbinds (Type: ${snmp.ObjectType[varbinds[0].type]} Value: ${varbinds[0].value})`);

            if (varbinds.length === 0) {
                throw new Error(`No varbinds returned from SNMP session (OID: ${monitor.snmpOid})`);
            }
Repository: louislam/uptime-kuma
File: db/knex_migrations/2024-04-26-0000-snmp-monitor.js (JavaScript)

Code before:
exports.up = function (knex) {
    return knex.schema
        .alterTable("monitor", function (table) {
            table.string("snmp_community_string", 255).defaultTo("public"); // Add snmp_community_string column
            table.string("snmp_oid").defaultTo(null); // Add oid column
            table.enum("snmp_version", [ "1", "2c", "3" ]).defaultTo("2c"); // Add snmp_version column with enum values
            table.float("snmp_control_value").defaultTo(null); // Add control_value column as float
            table.string("snmp_condition").defaultTo(null); // Add oid column
        });
};

exports.down = function (knex) {
    return knex.schema.alterTable("monitor", function (table) {
        table.dropColumn("snmp_community_string");
        table.dropColumn("snmp_oid");
        table.dropColumn("snmp_version");
        table.dropColumn("snmp_control_value");
        table.dropColumn("snmp_condition");
    });
};


Review comment (performance):
```suggestion
            table.string("snmp_community_string", 255).defaultTo("public"); // Add snmp_community_string column
            table.string("snmp_oid").defaultTo(null);
            table.enum("snmp_version", [ "1", "2c", "3" ]).defaultTo("2c");
            table.float("snmp_control_value").defaultTo(null);
            table.string("snmp_condition").defaultTo(null);
```

Code after:
exports.up = function (knex) {
    return knex.schema
        .alterTable("monitor", function (table) {
            table.string("snmp_oid").defaultTo(null);
            table.enum("snmp_version", [ "1", "2c", "3" ]).defaultTo("2c");
            table.string("json_path_operator").defaultTo(null);
        });
};

exports.down = function (knex) {
    return knex.schema.alterTable("monitor", function (table) {
        table.dropColumn("snmp_oid");
        table.dropColumn("snmp_version");
        table.dropColumn("json_path_operator");
    });
};
Repository: louislam/uptime-kuma
File: db/knex_migrations/2024-04-26-0000-snmp-monitor.js (JavaScript)

Code before:
exports.up = function (knex) {
    return knex.schema
        .alterTable("monitor", function (table) {
            table.string("snmp_community_string", 255).defaultTo("public"); // Add snmp_community_string column
            table.string("snmp_oid").defaultTo(null); // Add oid column
            table.enum("snmp_version", [ "0", "1", "3" ]).defaultTo("1"); // Add snmp_version column with enum values (0: SNMPv1, 1: SNMPv2c, 3: SNMPv3)
            table.float("snmp_control_value").defaultTo(null); // Add control_value column as float
            table.string("snmp_condition").defaultTo(null); // Add oid column
        });
};

exports.down = function (knex) {
    return knex.schema.alterTable("monitor", function (table) {
        table.dropColumn("snmp_community_string");
        table.dropColumn("snmp_oid");
        table.dropColumn("snmp_version");
        table.dropColumn("snmp_control_value");
        table.dropColumn("snmp_condition");
    });
};


Review comment (performance):
```suggestion
            table.float("snmp_control_value").defaultTo(null);
            table.string("snmp_condition").defaultTo(null);
```

Code after:
exports.up = function (knex) {
    return knex.schema
        .alterTable("monitor", function (table) {
            table.string("snmp_oid").defaultTo(null);
            table.enum("snmp_version", [ "1", "2c", "3" ]).defaultTo("2c");
            table.string("json_path_operator").defaultTo(null);
        });
};

exports.down = function (knex) {
    return knex.schema.alterTable("monitor", function (table) {
        table.dropColumn("snmp_oid");
        table.dropColumn("snmp_version");
        table.dropColumn("json_path_operator");
    });
};
Repository: louislam/uptime-kuma
File: db/knex_migrations/2024-04-26-0000-snmp-monitor.js (JavaScript)

Code before:
exports.up = function (knex) {
    return knex.schema
        .alterTable("monitor", function (table) {
            table.string("snmp_community_string", 255).defaultTo("public"); // Add snmp_community_string column
            table.string("snmp_oid").defaultTo(null); // Add oid column
            table.enum("snmp_version", [ "0", "1", "3" ]).defaultTo("1"); // Add snmp_version column with enum values (0: SNMPv1, 1: SNMPv2c, 3: SNMPv3)
            table.float("snmp_control_value").defaultTo(null); // Add control_value column as float
            table.string("snmp_condition").defaultTo(null); // Add oid column
        });
};

exports.down = function (knex) {
    return knex.schema.alterTable("monitor", function (table) {
        table.dropColumn("snmp_community_string");
        table.dropColumn("snmp_oid");
        table.dropColumn("snmp_version");
        table.dropColumn("snmp_control_value");
        table.dropColumn("snmp_condition");
    });
};


Review comment (performance):
```suggestion
            table.string("snmp_community_string", 255).defaultTo("public");
            table.string("snmp_oid").defaultTo(null);
```

Code after:
exports.up = function (knex) {
    return knex.schema
        .alterTable("monitor", function (table) {
            table.string("snmp_oid").defaultTo(null);
            table.enum("snmp_version", [ "1", "2c", "3" ]).defaultTo("2c");
            table.string("json_path_operator").defaultTo(null);
        });
};

exports.down = function (knex) {
    return knex.schema.alterTable("monitor", function (table) {
        table.dropColumn("snmp_oid");
        table.dropColumn("snmp_version");
        table.dropColumn("json_path_operator");
    });
};
Repository: louislam/uptime-kuma
File: server/monitor-types/snmp.js (JavaScript)

Code before:
                session.get([ monitor.snmpOid ], (error, varbinds) => {
                    error ? reject(error) : resolve(varbinds);
                });
            });
            log.debug("monitor", `SNMP: Received varbinds (Type: ${snmp.ObjectType[varbinds[0].type]} Value: ${varbinds[0].value})`);

            if (varbinds.length === 0) {
                throw new Error(`No varbinds returned from SNMP session (OID: ${monitor.snmpOid})`);
            }

            if (varbinds[0].type === snmp.ObjectType.NoSuchInstance) {
                throw new Error(`The SNMP query returned that no instance exists for OID ${monitor.snmpOid}`);
            }

            // We restrict querying to one OID per monitor, therefore `varbinds[0]` will always contain the value we're interested in.
            const value = varbinds[0].value;

            const { status, response } = await evaluateJsonQuery(value, monitor.jsonPath, monitor.jsonPathOperator, monitor.expectedValue);

            heartbeat.status = status ? UP : DOWN;
            heartbeat.msg = `SNMP value ${status ? "passes" : "does not pass"} `;
            heartbeat.msg += `comparison: ${response} ${monitor.jsonPathOperator} ${monitor.expectedValue}.`;

        } catch (err) {
            heartbeat.status = DOWN;
            heartbeat.msg = `Error: ${err.message}`;
        } finally {
            if (session) {
                session.close();
            }
        }
    }
}

module.exports = {
    SNMPMonitorType,
};


Review comment (suggestion):
Otherwise the retry-logic in the try-catch block is not triggered.
Also unsure if the prefix is nessesary

```suggestion
            throw err;
```

Code after:
                });
            });
            log.debug("monitor", `SNMP: Received varbinds (Type: ${snmp.ObjectType[varbinds[0].type]} Value: ${varbinds[0].value})`);

            if (varbinds.length === 0) {
                throw new Error(`No varbinds returned from SNMP session (OID: ${monitor.snmpOid})`);
            }

            if (varbinds[0].type === snmp.ObjectType.NoSuchInstance) {
                throw new Error(`The SNMP query returned that no instance exists for OID ${monitor.snmpOid}`);
            }

            // We restrict querying to one OID per monitor, therefore `varbinds[0]` will always contain the value we're interested in.
            const value = varbinds[0].value;

            const { status, response } = await evaluateJsonQuery(value, monitor.jsonPath, monitor.jsonPathOperator, monitor.expectedValue);

            if (status) {
                heartbeat.status = UP;
                heartbeat.msg = `JSON query passes (comparing ${response} ${monitor.jsonPathOperator} ${monitor.expectedValue})`;
            } else {
                throw new Error(`JSON query does not pass (comparing ${response} ${monitor.jsonPathOperator} ${monitor.expectedValue})`);
            }
        } finally {
            if (session) {
                session.close();
            }
        }
    }
}

module.exports = {
    SNMPMonitorType,
};
Repository: louislam/uptime-kuma
File: server/notification-providers/keep.js (JavaScript)

Code before:
const NotificationProvider = require("./notification-provider");
const axios = require("axios");
const FormData = require("form-data");

class Keep extends NotificationProvider {
    name = "Keep";

    /**
     * @inheritdoc
     */
    async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
        const okMsg = "Sent Successfully.";

        try {
            let data = {
                heartbeat: heartbeatJSON,
                monitor: monitorJSON,
                msg,
            };
            let config = {
                headers: {
                    "x-api-key": notification.webhookAPIKey,
                },
            };

            const formData = new FormData();
            formData.append("data", JSON.stringify(data));
            config.headers = formData.getHeaders();

Review comment (refactor):
(in general, not only this PR)

I think we can remove this dependency in v2.0, as we have upgraded to the minimum supported node version to 18:
![image](https://github.com/louislam/uptime-kuma/assets/26258709/625f45bc-6200-447d-838a-c86819f6cbe6)


Code after:
const NotificationProvider = require("./notification-provider");
const axios = require("axios");

class Keep extends NotificationProvider {
    name = "Keep";

    /**
     * @inheritdoc
     */
    async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
        const okMsg = "Sent Successfully.";

        try {
            let data = {
                heartbeat: heartbeatJSON,
                monitor: monitorJSON,
                msg,
            };
            let config = {
                headers: {
                    "x-api-key": notification.webhookAPIKey,
                    "content-type": "application/json",
                },
            };

            let url = notification.webhookURL;

            if (url.endsWith("/")) {
Repository: louislam/uptime-kuma
File: src/components/notifications/Keep.vue (Vue)

Code before:
<template>
    <div class="mb-3">
        <label for="webhook-url" class="form-label">{{ $t("Host URL") }}</label>
        <input
            id="webhook-url"
            v-model="$parent.notification.webhookURL"
            type="url"
            pattern="https?://.+"
            class="form-control"
            required
        />
    </div>

    <div class="mb-3">
        <label for="webhook-apikey" class="form-label">{{ $t("API Key") }}</label>
        <input
            id="webhook-apikey"
            v-model="$parent.notification.webhookAPIKey"
            type="text"
            class="form-control"
            required
        />
    </div>
</template>

<script>
export default {
    computed: {

Review comment (suggestion):
Please make sure that all translations are included in [`src/lang/en.json`](https://github.com/louislam/uptime-kuma/blob/master/src/lang/en.json).
Only the ones which are can be translated by our awsome team of translators ^^

Code after:
<template>
    <div class="mb-3">
        <label for="webhook-url" class="form-label">{{ $t("Host URL") }}</label>
        <input
            id="webhook-url"
            v-model="$parent.notification.webhookURL"
            type="url"
            pattern="https?://.+"
            class="form-control"
            required
        />
        <div class="form-text">
            <i18n-t tag="p" keypath="Read more:">
                <a href="https://docs.keephq.dev/providers/documentation/uptimekuma-provider" target="_blank">https://docs.keephq.dev/providers/documentation/uptimekuma-provider</a>
            </i18n-t>
        </div>
    </div>

    <div class="mb-3">
        <label for="webhook-apikey" class="form-label">{{
            $t("API Key")
        }}</label>
        <HiddenInput
            id="webhook-apikey"
            v-model="$parent.notification.webhookAPIKey"
            :required="true"
        ></HiddenInput>
    </div>
Repository: louislam/uptime-kuma
File: src/components/settings/APIKeys.vue (Vue)

Code before:
<template>
    <div>
        <div
            v-if="settings.disableAuth"
            class="mt-5 d-flex align-items-center justify-content-center my-3" 
        >
            {{ $t("apiKeysDisabledMsg") }}
        </div>
        <div v-else>
            <div class="add-btn">
                <button class="btn btn-primary me-2" type="button" @click="$refs.apiKeyDialog.show()">
                    <font-awesome-icon icon="plus" /> {{ $t("Add API Key") }}
                </button>
            </div>

            <div>
                <span
                    v-if="Object.keys(keyList).length === 0"
                    class="d-flex align-items-center justify-content-center my-3"
                >
                    {{ $t("No API Keys") }}
                </span>

                <div
                    v-for="(item, index) in keyList"
                    :key="index"
                    class="item"
                    :class="item.status"
                >
                    <div class="left-part">

Review comment (suggestion):
Revert of https://github.com/louislam/uptime-kuma/pull/4723#discussion_r1585464005

```suggestion
            class="mt-5 d-flex align-items-center justify-content-center my-3"
```

Code after:
<template>
    <div>
        <div
            v-if="settings.disableAuth"
            class="mt-5 d-flex align-items-center justify-content-center my-3"
        >
            {{ $t("apiKeysDisabledMsg") }}
        </div>
        <div v-else>
            <div class="add-btn">
                <button class="btn btn-primary me-2" type="button" @click="$refs.apiKeyDialog.show()">
                    <font-awesome-icon icon="plus" /> {{ $t("Add API Key") }}
                </button>
            </div>

            <div>
                <span
                    v-if="Object.keys(keyList).length === 0"
                    class="d-flex align-items-center justify-content-center my-3"
                >
                    {{ $t("No API Keys") }}
                </span>

                <div
                    v-for="(item, index) in keyList"
                    :key="index"
                    class="item"
                    :class="item.status"
                >
                    <div class="left-part">