Files
VisualSapfor/src/TestingSystem/Common/TestsDatabase.java

275 lines
12 KiB
Java
Raw Normal View History

package TestingSystem.Common;
import Common.Constants;
2023-09-17 22:13:42 +03:00
import Common.Database.SQLITE.SQLiteDatabase;
import Common.Global;
import Common.Utils.Utils;
import GlobalData.Account.Account;
import Repository.Component.Sapfor.Sapfor;
import Repository.RepositoryRefuseException;
import TestingSystem.Common.Group.Group;
import TestingSystem.Common.Group.GroupsDBTable;
import TestingSystem.Common.Test.Test;
import TestingSystem.Common.Test.TestDBTable;
2023-12-15 02:34:30 +03:00
import TestingSystem.Common.TestingPackageToKill.TestingPackagesToKillDBTable;
import TestingSystem.DVM.Configuration.ConfigurationDBTable;
import TestingSystem.DVM.DVMPackage.DVMPackage;
import TestingSystem.DVM.DVMPackage.DVMPackageDBTable;
import TestingSystem.DVM.DVMTasks.DVMRunTasksSet;
import TestingSystem.SAPFOR.SapforConfiguration.SapforConfigurationDBTable;
import TestingSystem.SAPFOR.SapforConfigurationCommand.SapforConfigurationCommandsDBTable;
import TestingSystem.SAPFOR.SapforPackage.SapforPackage;
2023-12-15 18:38:05 +03:00
import TestingSystem.SAPFOR.SapforPackage.SapforPackageDBTable;
import TestingSystem.SAPFOR.ServerSapfor.ServerSapfor;
import TestingSystem.SAPFOR.ServerSapfor.ServerSapforsDBTable;
import Visual_DVM_2021.Passes.All.ZipFolderPass;
import Visual_DVM_2021.Passes.PassCode_2021;
import javafx.util.Pair;
import org.apache.commons.io.FileUtils;
2023-09-17 22:13:42 +03:00
import java.io.File;
2023-09-17 22:13:42 +03:00
import java.nio.file.Paths;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.Vector;
2023-09-17 22:13:42 +03:00
public class TestsDatabase extends SQLiteDatabase {
public ConfigurationDBTable configurations;
public TestDBTable tests;
public GroupsDBTable groups;
public DVMPackageDBTable dvmPackages;
2023-12-15 18:38:05 +03:00
public SapforPackageDBTable sapforPackages;
2023-09-17 22:13:42 +03:00
//--
2023-12-15 02:34:30 +03:00
public TestingPackagesToKillDBTable testingPackagesToKill;
//--
2023-09-17 22:13:42 +03:00
public SapforConfigurationDBTable sapforConfigurations;
public SapforConfigurationCommandsDBTable sapforConfigurationCommands;
//----
public ServerSapforsDBTable serverSapfors;
//---
public DVMRunTasksSet dvmRunTasks = new DVMRunTasksSet(); //задачи текущего пакета тестирования DVM
//--
2023-09-17 22:13:42 +03:00
public TestsDatabase() {
super(Paths.get(System.getProperty("user.dir"), "Data", Constants.tests_db_name + ".sqlite").toFile());
2023-09-17 22:13:42 +03:00
}
@Override
protected void initAllTables() throws Exception {
addTable(configurations = new ConfigurationDBTable());
addTable(groups = new GroupsDBTable());
addTable(tests = new TestDBTable());
addTable(dvmPackages = new DVMPackageDBTable());
2023-12-15 18:38:05 +03:00
addTable(sapforPackages = new SapforPackageDBTable());
2023-12-15 02:34:30 +03:00
addTable(testingPackagesToKill = new TestingPackagesToKillDBTable());
2023-09-17 22:13:42 +03:00
//-
addTable(sapforConfigurations = new SapforConfigurationDBTable());
addTable(sapforConfigurationCommands = new SapforConfigurationCommandsDBTable());
addTable(serverSapfors = new ServerSapforsDBTable());
2023-09-17 22:13:42 +03:00
}
@Override
public PassCode_2021 getSynchronizePassCode() {
return PassCode_2021.SynchronizeTests;
}
public Vector<SapforPackage> getFirstActiveSapforPackagesCopies() {
Vector<SapforPackage> res = new Vector<>();
Vector<SapforPackage> packages = new Vector<>();
SapforPackage activePackage = null;
//1. получить активные пакеты.
for (SapforPackage p : sapforPackages.Data.values()) {
switch (p.state) {
case Done:
case Aborted:
case Draft:
case ConnectionError:
break;
default:
packages.add(p);
break;
}
}
//2. отсортировать по приоритету.
packages.sort(new Comparator<SapforPackage>() {
@Override
public int compare(SapforPackage o1, SapforPackage o2) {
return Integer.compare(o1.state.ordinal(), o2.state.ordinal());
}
});
if (!packages.isEmpty()) {
activePackage = packages.lastElement();
if (activePackage.state.equals(TasksPackageState.Queued)) {
activePackage.state = TasksPackageState.TestsSynchronize;
try {
Update(activePackage);
} catch (Exception ex) {
ex.printStackTrace();
}
}
res.add(new SapforPackage(activePackage));
; //копия чтобы не было конфликта доступа с нитью планировщика.
}
return res;
}
public Vector<DVMPackage> getFirstActiveDVMPackagesCopies() {
Vector<DVMPackage> res = new Vector<>();
//--
LinkedHashMap<String, Vector<DVMPackage>> packagesByMachines = new LinkedHashMap<>();
//----
//1. Получить список активных пакетов по машинам.
for (DVMPackage dvmPackage : dvmPackages.Data.values()) {
switch (dvmPackage.state) {
case Done:
case Aborted:
case Draft:
case ConnectionError:
break;
default:
//активен.
Vector<DVMPackage> packages = null;
//--
if (packagesByMachines.containsKey(dvmPackage.machine_address)) {
packages = packagesByMachines.get(dvmPackage.machine_address);
} else {
packages = new Vector<>();
packagesByMachines.put(dvmPackage.machine_address, packages);
}
packages.add(dvmPackage);
break;
}
}
//2. Выбрать для каждой машины наиболее приоритетный пакет.
for (String machine : packagesByMachines.keySet()) {
Vector<DVMPackage> packages = packagesByMachines.get(machine);
if (!packages.isEmpty()) {
packages.sort(new Comparator<DVMPackage>() {
@Override
public int compare(DVMPackage o1, DVMPackage o2) {
return Integer.compare(o1.state.ordinal(), o2.state.ordinal());
}
});
//-
DVMPackage activePackage = packages.lastElement();
if (activePackage.state.equals(TasksPackageState.Queued)) {
activePackage.state = TasksPackageState.TestsSynchronize;
try {
Update(activePackage);
} catch (Exception ex) {
ex.printStackTrace();
}
}
res.add(new DVMPackage(activePackage)); //копия чтобы не было конфликта доступа с нитью планировщика.
}
}
return res;
}
//--
public void SaveTestFromSingleFile(ServerSapfor sapfor, Group group, Test test, File file) throws Exception {
File testDirectory = new File(Global.TestsDirectory, String.valueOf(test.id));
Utils.CheckAndCleanDirectory(testDirectory);
File testFile = Paths.get(testDirectory.getAbsolutePath(), file.getName()).toFile();
FileUtils.copyFile(file, testFile);
//----
//архивация.
File archive = test.getArchive();
if (archive.exists())
FileUtils.forceDelete(archive);
//----------->>
ZipFolderPass zip = new ZipFolderPass();
zip.Do(testDirectory.getAbsolutePath(), archive.getAbsolutePath());
//---
//Определение размерности
switch (group.language) {
case fortran:
// временная папка для анализа. чтобы не засорять нормальную.
File tempProject = Utils.getTempFileName("test");
FileUtils.forceMkdir(tempProject);
FileUtils.copyDirectory(testDirectory, tempProject);
//--
if (Sapfor.getMinMaxDim(Sapfor.getTempCopy(new File(sapfor.call_command)), tempProject, test)) {
Update(test);
} else
throw new RepositoryRefuseException("Не удалось определить размерность теста " + Utils.Brackets(test.description));
break;
case c:
test.max_dim = Utils.getCTestMaxDim(testFile);
Update(test);
break;
}
}
//---
public void CreateTestFromSingleFile(Account account, ServerSapfor sapfor, Group group, File file, String testDescription) throws Exception {
System.out.println("Создание теста " + file.getName());
Test test = new Test();
test.description = testDescription;
test.sender_name = account.name;
test.sender_address = account.email;
test.group_id = group.id;
test.files = file.getName();
Insert(test);
//->>
SaveTestFromSingleFile(sapfor, group, test, file);
}
public void RefreshGroup(Account account, ServerSapfor sapfor, Pair<Group, Vector<File>> groupData) throws Exception {
Group group = groupData.getKey();
Vector<File> files = groupData.getValue();
//--
Group oldGroup = groups.getGroupByDescription(group.language, group.description);
if (oldGroup == null) {
System.out.println("group " + Utils.Brackets(group.description) + " not found. create.");
Insert(group);
for (File file : files) {
String testDescription = Utils.getNameWithoutExtension(file.getName()) + "_" + group.language.getDVMCompile();
System.out.println("test " + Utils.Brackets(testDescription) + " create"); //добавить тест.
CreateTestFromSingleFile(account, sapfor, group, file, testDescription);
}
} else {
for (File file : files) {
String testDescription = Utils.getNameWithoutExtension(file.getName()) + "_" + group.language.getDVMCompile();
Test oldTest = tests.getTestByDescription(oldGroup.id, testDescription);
if (oldTest == null) {
System.out.println("test " + Utils.Brackets(testDescription) + " not found. create"); //добавить тест.
CreateTestFromSingleFile(account, sapfor, oldGroup, file, testDescription);
} else {
System.out.println(Utils.Brackets(testDescription) + " found. rewrite"); //добавить тест.
SaveTestFromSingleFile(sapfor, group, oldTest, file);
}
}
}
}
public Vector<DVMPackage> getFirstActiveDVMPackageCopyForMachineURL(String arg) {
Vector<DVMPackage> res = new Vector<>();
Vector<DVMPackage> machinePackages = new Vector<>();
//--
//1. Получить список активных пакетов для машины.
for (DVMPackage dvmPackage : dvmPackages.Data.values()) {
switch (dvmPackage.state) {
case Done:
case Aborted:
case Draft:
case ConnectionError:
break;
default:
if (dvmPackage.getMachine().getURL().equals(arg))
machinePackages.add(dvmPackage);
break;
}
}
if (!machinePackages.isEmpty()) {
machinePackages.sort(new Comparator<DVMPackage>() {
@Override
public int compare(DVMPackage o1, DVMPackage o2) {
return Integer.compare(o1.state.ordinal(), o2.state.ordinal());
}
});
//-
DVMPackage activePackage = machinePackages.lastElement();
if (activePackage.state.equals(TasksPackageState.Queued)) {
activePackage.state = TasksPackageState.TestsSynchronize;
try {
Update(activePackage);
} catch (Exception ex) {
ex.printStackTrace();
}
}
res.add(activePackage);
}
return res;
2024-04-13 20:09:26 +03:00
}
2023-09-17 22:13:42 +03:00
}