This commit is contained in:
Fabio Sinibaldi 2019-01-24 14:25:23 +00:00
parent e06ab292bd
commit 4e27039e1d
7 changed files with 52 additions and 20 deletions

View File

@ -2,6 +2,7 @@ package org.gcube.application.perform.service.engine.dm;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.Map;
@ -78,7 +79,7 @@ public class ImporterMonitor implements DMMonitorListener {
DBQueryDescriptor queryValues=new DBQueryDescriptor().
add(DBField.ImportRoutine.fields.get(ImportRoutine.ID), routine.getId()).
add(DBField.ImportRoutine.fields.get(ImportRoutine.STATUS), status.toString()).
add(DBField.ImportRoutine.fields.get(ImportRoutine.END), endTime);
add(DBField.ImportRoutine.fields.get(ImportRoutine.END), endTime != null ? new Timestamp(endTime.toEpochMilli()) : null);
PreparedStatement psUpdate=Queries.UPDATE_IMPORT_STATUS.get(conn, queryValues);
psUpdate.executeUpdate();
@ -87,6 +88,7 @@ public class ImporterMonitor implements DMMonitorListener {
}
}catch(Throwable t) {
log.warn("Unable to update status on database");
log.debug("Error : ",t);
}
}

View File

@ -40,7 +40,7 @@ public class ExportCSVQuery extends Query {
// AREA
if(theRequest.getAreas().size()>0 && schema.getAreaField()!=null) {
if(theRequest.getAreas().size()>0 && schema.getAreaField()!=null && actualStructure.containsKey(schema.getAreaField())) {
String areaField=actualStructure.get(schema.getAreaField()).getFieldName();
StringBuilder areas=new StringBuilder();
for(String area:theRequest.getAreas()) {
@ -50,7 +50,7 @@ public class ExportCSVQuery extends Query {
}
// QUARTER
if(theRequest.getQuarters().size()>0 && schema.getQuarterField()!=null) {
if(theRequest.getQuarters().size()>0 && schema.getQuarterField()!=null && actualStructure.containsKey(schema.getQuarterField())) {
String quarterField=actualStructure.get(schema.getQuarterField()).getFieldName();
StringBuilder quarterString=new StringBuilder();
for(String q:theRequest.getQuarters()) {
@ -60,7 +60,7 @@ public class ExportCSVQuery extends Query {
}
// SPECIES ID
if(theRequest.getSpeciesIds().size()>0 && schema.getSpeciesField()!=null) {
if(theRequest.getSpeciesIds().size()>0 && schema.getSpeciesField()!=null && actualStructure.containsKey(schema.getSpeciesField())) {
String speciesField=actualStructure.get(schema.getSpeciesField()).getFieldName();
StringBuilder speciesString=new StringBuilder();
for(String s:theRequest.getSpeciesIds()) {
@ -70,7 +70,7 @@ public class ExportCSVQuery extends Query {
}
// PERIOD
if(theRequest.getPeriods().size()>0 && schema.getPeriodField()!=null) {
if(theRequest.getPeriods().size()>0 && schema.getPeriodField()!=null && actualStructure.containsKey(schema.getPeriodField())) {
String periodField=actualStructure.get(schema.getPeriodField()).getFieldName();
StringBuilder periodString=new StringBuilder();
for(String p:theRequest.getPeriods()) {
@ -79,12 +79,14 @@ public class ExportCSVQuery extends Query {
orGroups.add(periodString.substring(0,periodString.lastIndexOf("OR")));
}
StringBuilder toReturn=new StringBuilder();
StringBuilder toReturn=new StringBuilder("");
for(String orGroup:orGroups) {
toReturn.append(orGroup+ " AND ");
}
if(toReturn.length()>0)
return toReturn.substring(0, toReturn.lastIndexOf("AND"));
else return toReturn.toString();
}
@ -96,7 +98,7 @@ public class ExportCSVQuery extends Query {
StringBuilder b=new StringBuilder();
for(DBField f:fields)
b.append(f.getFieldName()+",");
fieldList=b.toString().substring(0,b.lastIndexOf(",")-1);
fieldList=b.toString().substring(0,b.lastIndexOf(","));
}
@Override
@ -113,9 +115,12 @@ public class ExportCSVQuery extends Query {
selectedFields.replaceAll(mapping.getKey(), caseBuilder.toString());
}
String conditionString =getConditionString();
if(conditionString.length()>0) conditionString= "WHERE "+conditionString;
return String.format("SELECT %1$s FROM %2$s WHERE %3%s",
selectedFields, tablename, getConditionString());
return String.format("SELECT %1$s FROM %2$s %3$s",
selectedFields, tablename, conditionString);
}
}

View File

@ -42,8 +42,9 @@ public class Queries {
// "acquire"
// set lock = hostname where ID =? and LOCK is null
// Acquired = updated rows == 1
public static final Query ACQUIRE_IMPORT_ROUTINE=new Query("UPDATE "+ImportRoutine.TABLE+" SET "+ImportRoutine.LOCK+"=? WHERE "+ImportRoutine.FARM_ID+" = ? AND "+ImportRoutine.LOCK+" IS NULL",
new DBField[]{DBField.ImportRoutine.fields.get(ImportRoutine.LOCK),DBField.ImportRoutine.fields.get(ImportRoutine.FARM_ID)});
public static final Query ACQUIRE_IMPORT_ROUTINE=new Query("UPDATE "+ImportRoutine.TABLE+" SET "+ImportRoutine.LOCK+"=? WHERE "+ImportRoutine.ID+" = ? AND "+ImportRoutine.LOCK+" IS NULL",
new DBField[]{DBField.ImportRoutine.fields.get(ImportRoutine.LOCK),
DBField.ImportRoutine.fields.get(ImportRoutine.ID)});
public static final Query GET_IMPORT_ROUTINE_BY_ID=new Query("SELECT * from "+ImportRoutine.TABLE+" WHERE "+ImportRoutine.ID+" = ?",
new DBField[]{DBField.ImportRoutine.fields.get(ImportRoutine.ID)});

View File

@ -231,10 +231,6 @@ public class ImportedTable {
return toReturn;
}finally {
conn.close();
if(writer!=null) {
writer.flush();
writer.close();
}
if(printer!=null) {
printer.flush();
printer.close();

View File

@ -1,10 +1,15 @@
package org.gcube.application.perform.service;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Paths;
import java.sql.SQLException;
import javax.ws.rs.core.Application;
import org.gcube.application.perform.service.engine.impl.ImporterImpl;
import org.gcube.application.perform.service.engine.impl.PerformanceManagerImpl;
import org.gcube.application.perform.service.engine.model.InternalException;
import org.gcube.application.perform.service.engine.utils.ISUtils;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.glassfish.jersey.test.JerseyTest;
@ -17,11 +22,15 @@ public class CommonTest extends JerseyTest{
private static final Logger log= LoggerFactory.getLogger(AnagraphicTests.class);
@BeforeClass
public static void init() throws MalformedURLException {
public static void init() throws IOException, SQLException, InternalException {
LocalConfiguration.init(Paths.get("src/main/webapp/WEB-INF/config.properties").toUri().toURL());
TokenSetter.set("/gcube/preprod/preVRE");
ISUtils.setFixedToken(SecurityTokenProvider.instance.get());
PerformServiceLifecycleManager.initSchema("src/main/webapp/WEB-INF");
new ImporterImpl().init();
PerformanceManagerImpl.initDatabase();
}

View File

@ -4,9 +4,14 @@ import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Paths;
import java.sql.SQLException;
import java.util.Arrays;
import org.gcube.application.perform.service.engine.PerformanceManager;
import org.gcube.application.perform.service.engine.impl.ExportCSVQuery;
import org.gcube.application.perform.service.engine.impl.PerformanceManagerImpl;
import org.gcube.application.perform.service.engine.model.CSVExportRequest;
import org.gcube.application.perform.service.engine.model.InternalException;
import org.gcube.application.perform.service.engine.model.importer.AnalysisType;
public class LoadSchemaTest {
@ -17,6 +22,16 @@ public class LoadSchemaTest {
TokenSetter.set("/gcube/preprod/preVRE");
PerformanceManagerImpl.initDatabase();
PerformanceManager mng =new PerformanceManagerImpl();
CSVExportRequest req=new CSVExportRequest(new AnalysisType("GROW_OUT_AGGREGATED", "GROW_OUT_AGGREGATED"));
req.addAreas(Arrays.asList("A1","A2"));
req.addFarmId(12682549l);
req.addPeriods(Arrays.asList("p1"));
req.addSpecies(Arrays.asList("Gadidae"));
System.out.println(mng.generateCSV(req));
}
}

View File

@ -3,20 +3,24 @@ package org.gcube.application.perform.service;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import org.junit.BeforeClass;
import org.junit.Test;
public class PerformanceTest extends CommonTest{
@Test
public void getBatch() {
public void getPerformance() {
WebTarget target=
target(ServiceConstants.Performance.PATH).
queryParam(ServiceConstants.Performance.AREA_PARAMETER, "A1","A2").
queryParam(ServiceConstants.Performance.QUARTER_PARAMETER, "Q1","Q2").
queryParam(ServiceConstants.Performance.FARM_ID_PARAMETER, "1233556","12346").
queryParam(ServiceConstants.Performance.SPECIES_ID_PARAMETER, "Gadilidae","Tonno").
queryParam(ServiceConstants.Performance.BATCH_TYPE_PARAMETER, "GROW_OUT_AGGREGATED").
queryParam(ServiceConstants.Performance.PERIOD_PARAMETER, "First","Spring");
queryParam(ServiceConstants.Performance.PERIOD_PARAMETER, "First","Spring").
queryParam(ServiceConstants.Performance.FARM_ID_PARAMETER, "1233556","12346").
queryParam(ServiceConstants.Performance.BATCH_TYPE_PARAMETER, "GROW_OUT_AGGREGATED");
System.out.println(target.getUri());
Response resp=target.request().get();