UnamSanctam / UnamWebPanel

MIT License
175 stars 58 forks source link

Rules for automatic configuration change #278

Closed anbu-squad closed 9 months ago

anbu-squad commented 1 year ago

I try to add functionality to create the rules of automatic change of configurations depending on the status to start

  1. Added status drop-down list function templates.php

    function unamtStatusSelect($label, $id, $statusOptions, $selectAttributes = [], $optionAttributes = [])
    {
    
    $select = (!empty($label) ? "<label>{$label}</label>" : '') . "<select name='{$id}' id='{$id}'";
    
    $select .= " class='form-control select2 hook-select2 {$options['classes']}'";
    
    if (!empty($selectAttributes)) {
        foreach ($selectAttributes as $attr => $value) {
            $select .= " {$attr}='{$value}'";
        }
    }
    
    $select .= ">\n";
    
    foreach ($statusOptions as $value => $text) {
        $select .= "<option value='{$value}'";
    
        if (!empty($optionAttributes)) {
            foreach ($optionAttributes as $attr => $attrValue) {
                $select .= " {$attr}='{$attrValue}'";
            }
        }
    
        $select .= ">{$text}</option>\n";
    }
    
    $select .= "</select>\n";
    
    return $select;
    }
  2. Added a form for selecting status and configuration, as well as a table for displaying added rules configurationsAjax.php
    
    $statusOptions = [
    'Stopped' => unamtMinerStatus(1),
    'Active' => unamtMinerStatus(2),
    'Idle' => unamtMinerStatus(3),
    'Paused' => unamtMinerStatus(4),
    'Insufficient VRAN' => unamtMinerStatus(5),
    'Starting' => unamtMinerStatus(6),
    'Error' => unamtMinerStatus(7),
    'Offline' => unamtMinerStatus(-1),
    ];

echo unamtSection($larr['Configurations'], unamtRow( unamtCard('col-lg-6 col-xl-4', "

Automated Configuration Rules

", '', unamtFormContainer('add-rule', 'api/ajax-actions.php', unamtFormGroup( unamtStatusSelect("Select Status", 'ms_status', $statusOptions, ['classes'=>'nav-select']) ). unamtFormGroup( unamtSelect("Select Configuration", 'cf_name', $configoptions, ['classes'=>'nav-select', 'extras'=>"data-page='configurations'"]) ). unamtFormGroup( unamtHidden('cf_configID', getConfigValue('cf_configID')) ). unamtFormGroup( unamtSubmit("Add Rule", ['classes' => 'col btn-primary']) ) ). unamtFormContainer('config-update', 'api/ajax-actions.php', unamtFormGroup(unamtHidden('index', getConfigValue('cf_configID'))). unamtFormGroup(unamtTextarea("{$larr['Configuration']} JSON", 'data', getConfigValue('cf_data'), ['extras'=>''])) ) ) ) );

"; echo ""; echo ""; echo ""; echo ""; } ?>
Rule ID Miner Status Configuration
{$rule['rule_id']}" . unamtMinerStatus($rule['miner_status']) . "{$rule['configuration']}

3. Created a table

CREATE TABLE rules ( rule_id INTEGER PRIMARY KEY AUTOINCREMENT, rule_status INTEGER, rule_config INTEGER DEFAULT 0 )



Now, as far as I understand, we need to add functionality for clicking a button in **ajax-actions.php** to add to table
but I had difficulties at the stage of adding data to the table and I got lost in the answers of artificial intelligence

 as a result, the added rules need to be processed in endpoint.php, but the rules need to be added to the table first, as far as I understand
anbu-squad commented 1 year ago

This is just a start to formulate conditions depending on the status of the miner

in fact, I’m thinking of adding a configuration change depending on the maximum hashrate for a certain period of time

as well as drop-down lists of miner versions depending on supported algorithms

UnamSanctam commented 1 year ago

Yes you would add a metod into ajax-actions.php (add-rule in your case I think) that adds or updates the rule to the database. Then in the endpoint.php you would check the rules and get the correct configuration to return to the miner.

If you want to do it like you have done it then I would suggest pre-populating that table with all the existing statuses, so that you can just run "update" in the database to change it. Then you would do something like this in ajax-actions.php (haven't tested it myself):

case 'add-rule':
            $base->tableModify(getConn(), 'update', 'rules', 'rule', ['config'], ['status']);
            $base->unam_echoSuccess("{$larr['Rule']} {$larr['has_been_updated']}.");
            break;

(and in your form change ms_status to status and cf_name to name)

anbu-squad commented 1 year ago

UnamWebPanel/assets/php/templates.php

function unamtStatusSelect($label, $id, $statusOptions, $selectAttributes = [], $optionAttributes = [])
{
    // Создаем выпадающий список аналогично функции unamtSelect
    $select = (!empty($label) ? "<label>{$label}</label>" : '') . "<select name='{$id}' id='{$id}'";

    // Изменяем атрибут classes на нужное значение
    $select .= " class='form-control select2 hook-select2 {$options['classes']}'";

    if (!empty($selectAttributes)) {
        foreach ($selectAttributes as $attr => $value) {
            $select .= " {$attr}='{$value}'";
        }
    }

    $select .= ">\n";

    foreach ($statusOptions as $value => $text) {
        $select .= "<option value='{$value}'";

        if (!empty($optionAttributes)) {
            foreach ($optionAttributes as $attr => $attrValue) {
                $select .= " {$attr}='{$attrValue}'";
            }
        }

        $select .= ">{$text}</option>\n";
    }

    $select .= "</select>\n";

    return $select;
}
function generateConfigSelect($label, $id, $optionsArray, $selectedValue = null, $classes = '', $extras = ''){
    $select = "<select name='{$id}' class='form-control select2 hook-select2 {$classes}' {$extras}>";

    foreach ($optionsArray as $option) {
        $value = $option['value'];
        $text = $option['text'];
        $selected = ($selectedValue !== null && $selectedValue == $value) ? 'selected' : '';

        $select .= "<option value='{$value}' {$selected}>{$text}</option>";
    }

    $select .= "</select>";

    return (!empty($label) ? "<label>{$label}</label>" : '') . $select;
}

UnamWebPanel/internalPages/configurationsAjax.php

$configOptionsForRules = [];
$conditionalConfigOptions = [];

foreach ($configs as $configdata) {
    $configOptions = [
        'value' => $configdata['cf_configID'],
        'text' => $configdata['cf_name']
    ];

    $configOptionsForRules[] = $configOptions;
    $conditionalConfigOptions[] = $configOptions;
}
$statusOptions = [
    'Stopped' => unamtMinerStatus(1),
    'Active' => unamtMinerStatus(2),
    'Idle' => unamtMinerStatus(3),
    'Insufficient VRAN' => unamtMinerStatus(5),
    'Error' => unamtMinerStatus(7),
];
echo unamtSection($larr['Configurations'],
    unamtRow(
        unamtCard('col-lg-6 col-xl-4', "<h4>Automated Configuration Rules</h4>", '',
            unamtFormContainer('add-rule', 'api/ajax-actions.php',
                unamtFormGroup(
                    unamtStatusSelect("Select Status", 'status', $statusOptions, ['classes'=>'nav-select'])
                ).
                unamtFormGroup(
                    generateConfigSelect("Select Current Configuration", 'current_config_id', $configOptionsForRules, ['classes' => 'nav-select'])
                ).
                unamtFormGroup(
                    generateConfigSelect("Select Conditional Configuration", 'conditional_config_id', $conditionalConfigOptions, ['classes' => 'nav-select'])
                ).
                unamtFormGroup(
                    unamtSubmit("Add Rule", ['classes' => 'col btn-primary']) // Кнопку Add Rule переместили сюда
                )
            )
        )
    )
);
anbu-squad commented 1 year ago

I updated the form to create rules based on status

however, I am having difficulty displaying the created rules on the configuration page, is it most likely better to do this as a table?

UnamSanctam commented 1 year ago

Sorry I thought I had posted my reply, my mistake. Displaying the created rules as a table would probably be easiest yes since I assume you would also want something like a delete button for the rules, so a table would be the easiest for that.