Added Btrfs class and fixed configurator select html element issues

This commit is contained in:
Ziver Koc 2023-01-06 00:22:07 +01:00
parent 990813aa59
commit bbd03b14be
4 changed files with 146 additions and 17 deletions

View file

@ -58,7 +58,7 @@ public class OpenAPIHttpPage implements HttpPage {
out.setHeader(HttpHeader.HEADER_CONTENT_TYPE, "application/json");
wsdl.write(out);
} else {
// Output human readable interface
// Output human-readable interface
out.println("<!DOCTYPE html>");
out.println("<html>");

View file

@ -0,0 +1,78 @@
package zutil.osal.linux.app;
import zutil.osal.MultiCommandExecutor;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Wrapper for the linux Btrfs command to query and modify btrfs based filesystems.
*/
public class Btrfs {
private static String BTRFS_CMD = "btrfs";
private static SimpleDateFormat DATE_PARSER = new SimpleDateFormat("dow mon dd hh:mm:ss zzz yyyy");
// The disk used as an id to btrfs
private String path;
// Terminal where all commands will be executed.
private MultiCommandExecutor terminal;
public Btrfs(String path) {
this(path, new MultiCommandExecutor());
}
public Btrfs(String path, MultiCommandExecutor terminal) {
this.path = path;
this.terminal = terminal;
}
// ****************************************************
// Balance functions
// ****************************************************
public void startBalance() throws IOException {
execBtrfsCommand("balance start", path);
}
public void cancelBalance() throws IOException {
execBtrfsCommand("balance cancel", path);
}
public void getBalanceProgress() throws IOException {
execBtrfsCommand("balance status", path);
}
// ****************************************************
// Scrub functions
// ****************************************************
public void startScrub() throws IOException {
execBtrfsCommand("scrub start", path);
}
public void cancelScrub() throws IOException {
execBtrfsCommand("scrub cancel", path);
}
public void getScrubProgress() throws IOException {
execBtrfsCommand("scrub status", path);
}
public Date getLastScrubDate() throws IOException {
String log = execBtrfsCommand("scrub status", path);
return null;
}
// ****************************************************
// Utility functions
// ****************************************************
public String execBtrfsCommand(String params, String path) throws IOException {
terminal.exec(BTRFS_CMD + " " + params + " " + path);
return terminal.readAll();
}
}

View file

@ -28,26 +28,36 @@
if (formTemplateId != null)
modal.find("#" + formTemplateId).empty(); // clear form div
// select dynamic form
var selector = modal.find("select[name=type]");
selector.val(button.data("type"));
selector.change(); // Update dynamic inputs
// set dynamic form data
$.each(button.attr(), function(fieldName, value) {
if (fieldName.startsWith("data-")) {
fieldName = fieldName.substring(5);
if(fieldName.startsWith("data-")) {
fieldName = fieldName.substring(5); // remove prefix data-
// case insensitive search
input = modal.find("input").filter(function() {
return this.name.toLowerCase() == fieldName;
var input = modal.find("input, select").filter(function() {
if (this.name.toLowerCase() == fieldName) {
if (this.type == "hidden" && modal.find("input[type=checkbox][name=" + fieldName + "]") != null)
return false; // Workaround for the default(false) boolean input
return true;
}
return false;
});
if (input.attr("type") == "checkbox") { // special handling for checkboxes
input.attr("value", "true");
if (value=="true") input.attr("checked", "true");
else input.removeAttr("checked");
// Add default false value as a unchecked checkbox is not included in the post
input.parent().prepend("<input type='hidden' name='" + input.attr("name") + "' value='false' />");
} else {
input.val(value);
if (input != null) {
if (input.prop("type") == "checkbox") { // special handling for checkboxes
input.prop("value", "true");
input.prop("checked", value == "true");
if (modal.find("input[type=hidden][name=" + fieldName + "]") == null) {
// Add default false value as a unchecked checkbox is not included in the post
input.parent().prepend("<input type='hidden' name='" + input.prop("name") + "' value='false' />");
}
} else {
input.val(value);
if (input.prop("tagName") == "SELECT")
input.change(); // required for select elements to update properly
}
}
}
});

View file

@ -0,0 +1,41 @@
package zutil.osal.linux.app;
import junit.framework.TestCase;
import org.junit.Test;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class BtrfsTest {
@Test
public void testGetScrubProgress() {
}
//@Test
public void testGetLastScrubDate() throws IOException, ParseException {
final String[] cmd = {null};
Btrfs btrfs = new Btrfs("/dev/sda1") {
public String execBtrfsCommand(String params, String path) throws IOException {
cmd[0] = params + " " + path;
return "scrub status for 3a6b2a74-05a9-464d-bbbd-15710c548f76\n" +
" scrub started at Fri Feb 4 00:29:54 2022 and finished after 58380 seconds\n" +
" total bytes scrubbed: 15.74TiB with 0 errors\n";
}
};
Date actualDate = btrfs.getLastScrubDate();
Date expectedDate = new SimpleDateFormat("dow mon dd hh:mm:ss zzz yyyy").parse("Fri Feb 4 00:29:54 2022");
assertEquals("scrub status /dev/sda1", cmd[0]);
assertTrue("Dates are not close enough to each other!",
Math.abs(expectedDate.getTime() - actualDate.getTime()) < 1000);
}
}