Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fairness cache update #31

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions src/main/java/fr/lirmm/fairness/assessment/FairServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import fr.lirmm.fairness.assessment.controllers.RequestController;
import fr.lirmm.fairness.assessment.controllers.ResponseController;
import fr.lirmm.fairness.assessment.models.Configuration;
import fr.lirmm.fairness.assessment.models.Ontology;
import fr.lirmm.fairness.assessment.models.PortalInstance;
import fr.lirmm.fairness.assessment.utils.ResultCache;
Expand All @@ -26,23 +27,28 @@
*
*/
public class FairServlet extends HttpServlet {
public static long lastUpdate = -1;
public static long UPDATE_RATE = (1000 * 60 * 60) * 12;// 12 hours


private static final long serialVersionUID = -2749023988723161904L;
private final ResultCache resultCache = new ResultCache();

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException {


//
long startTime = System.currentTimeMillis();

ResponseController responseController = new ResponseController(resp); ;
ResponseController responseController = new ResponseController(resp);
;
RequestController requestController = new RequestController(req);
PortalInstance portalInstance = null;
try {
JsonObject ontologies = null;
portalInstance = requestController.getPortalInstance();


Logger.getAnonymousLogger().info("USE THE PORTAL : " + portalInstance.getName() + "; url= " + portalInstance.getUrl() + "; apikey= " + portalInstance.getApikey());
List<String> ontologyAcronymsToEvaluate = requestController.getOntologies();

Expand All @@ -60,6 +66,18 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
}
}
} else {

/**
* checking last time we updated fair cache file
*/
long now = System.currentTimeMillis();
System.out.println("API_KEY" + portalInstance.getApikey());

if ((now - lastUpdate) >= UPDATE_RATE) {
lastUpdate = now;
resultCache.deleteCacheFile(portalInstance);
}

JsonObject allResponses = resultCache.read(portalInstance);
if (ontologyAcronymsToEvaluate.size() == requestController.getPortalInstance().getAllOntologiesAcronyms().size()) {
ontologies = allResponses.getAsJsonObject("ontologies");
Expand All @@ -80,14 +98,13 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
}



responseController.respond(true, requestController.getRequestURI(req) , startTime ,"" , requestController);
Logger.getAnonymousLogger().info("EVALUATION ENDED WITH STATUS : " + responseController.getResponse().get("status") );
responseController.respond(true, requestController.getRequestURI(req), startTime, "", requestController);
Logger.getAnonymousLogger().info("EVALUATION ENDED WITH STATUS : " + responseController.getResponse().get("status"));

} catch (Exception e) {
e.printStackTrace();
try {
responseController.respond(false, requestController.getRequestURI(req) , startTime ,e.getMessage() ,requestController);
responseController.respond(false, requestController.getRequestURI(req), startTime, e.getMessage(), requestController);
} catch (Exception ex) {
ex.printStackTrace();
}
Expand All @@ -97,8 +114,6 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
}




private JsonElement getCombinedScore(JsonObject ontologies) throws JSONException {
CombinedFair combinedFair = new CombinedFair(ontologies.size());
Gson gson = new GsonBuilder().create();
Expand All @@ -117,5 +132,4 @@ private JsonElement evaluateOntology(String acronym, PortalInstance portalInstan
}



}
38 changes: 27 additions & 11 deletions src/main/java/fr/lirmm/fairness/assessment/utils/ResultCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ResultCache {
public static String FILE_SAVE_NAME = "save.json";


public void save(PortalInstance portalInstance){
public void save(PortalInstance portalInstance) {
try {
ResultCache resultCache = new ResultCache();
String portal = portalInstance.getName();
Expand All @@ -43,29 +43,30 @@ public void save(PortalInstance portalInstance){
output.add("ontologies", gson.toJsonTree(jsonObjects));

getFileSaveName(portal);
resultCache.store(output.toString() , FILE_SAVE_NAME);
resultCache.store(output.toString(), FILE_SAVE_NAME);
} catch (Exception e) {
e.printStackTrace();
}
}

public JsonObject read(PortalInstance portalInstance) throws IOException {
String portal = portalInstance.getName();
if (!this.isSaved(portal)){
System.out.println(portal+" save files not exist ");
if (!this.isSaved(portal)) {
System.out.println(portal + " save files not exist ");
this.save(portalInstance);
}
Gson gson = new GsonBuilder().create();

return gson.fromJson(this.get(FILE_SAVE_NAME) , JsonObject.class);
return gson.fromJson(this.get(FILE_SAVE_NAME), JsonObject.class);

}

public boolean isSaved(String portal){
public boolean isSaved(String portal) {
getFileSaveName(portal);
File f = new File(FILE_SAVE_NAME);
return (f.exists() && !f.isDirectory());
}

public void flush(String portal) {
getFileSaveName(portal);
File f = new File(FILE_SAVE_NAME);
Expand All @@ -82,7 +83,7 @@ private void store(String json, String fileName) throws IOException {

} finally {
try {
if(file != null){
if (file != null) {
file.flush();
file.close();
}
Expand All @@ -92,8 +93,10 @@ private void store(String json, String fileName) throws IOException {
}
}

private boolean createDirIfNotExist(String filePath){
String dirPath = filePath.substring(0, filePath.lastIndexOf(File.separator));
private boolean createDirIfNotExist(String filePath) {
System.out.println("rasmi: " + filePath);
String fileSeparator = filePath.contains("/") ? "/" : File.separator;
String dirPath = filePath.substring(0, filePath.lastIndexOf(fileSeparator));
File file = new File(dirPath);

return file.mkdir();
Expand All @@ -110,18 +113,31 @@ private String get(String filePath) throws IOException {
sb.append(System.lineSeparator());
line = br.readLine();
}
return sb.toString();
return sb.toString();
} finally {
br.close();
}
}

private String getFileSaveName(String portal) {
private String getFileSaveName(String portal) {
try {
FILE_SAVE_NAME = Configuration.getInstance().getPortalProperties(portal.toLowerCase(Locale.ROOT)).getProperty("cacheFilePath");
} catch (IOException e) {
e.printStackTrace();
}
return FILE_SAVE_NAME;
}

public void deleteCacheFile(PortalInstance portalInstance) {

if (isSaved(portalInstance.getName())) {
try {
new File(getFileSaveName(portalInstance.getName())).delete();
} catch (Exception e) {
// in case we don't have the right to delete
e.printStackTrace();
System.out.println("Couldn't delete file (maybe you should check your permissions)");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name=IndustryPortal
url=https://data.industryportal.enit.fr
apikey=
cacheFilePath=cache/fair-cache.json
cacheEnabled=true