Add checkbox to disable caching

This commit is contained in:
Antonin Delpeuch 2017-03-09 00:21:34 +00:00
parent 32c232c2d6
commit 22124ac57e
8 changed files with 80 additions and 56 deletions

View File

@ -54,6 +54,7 @@ public class AddColumnByFetchingURLsCommand extends EngineDependentCommand {
int columnInsertIndex = Integer.parseInt(request.getParameter("columnInsertIndex"));
int delay = Integer.parseInt(request.getParameter("delay"));
String onError = request.getParameter("onError");
boolean cacheResponses = Boolean.parseBoolean(request.getParameter("cacheResponses"));
return new ColumnAdditionByFetchingURLsOperation(
engineConfig,
@ -62,7 +63,8 @@ public class AddColumnByFetchingURLsCommand extends EngineDependentCommand {
TextTransformOperation.stringToOnError(onError),
newColumnName,
columnInsertIndex,
delay
delay,
cacheResponses
);
}

View File

@ -85,6 +85,7 @@ public class ColumnAdditionByFetchingURLsOperation extends EngineDependentOperat
final protected String _newColumnName;
final protected int _columnInsertIndex;
final protected int _delay;
final protected boolean _cacheResponses;
static public AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception {
JSONObject engineConfig = obj.getJSONObject("engineConfig");
@ -96,7 +97,8 @@ public class ColumnAdditionByFetchingURLsOperation extends EngineDependentOperat
TextTransformOperation.stringToOnError(obj.getString("onError")),
obj.getString("newColumnName"),
obj.getInt("columnInsertIndex"),
obj.getInt("delay")
obj.getInt("delay"),
obj.optBoolean("cacheResponses", false) // false for retro-compatibility
);
}
@ -107,7 +109,8 @@ public class ColumnAdditionByFetchingURLsOperation extends EngineDependentOperat
OnError onError,
String newColumnName,
int columnInsertIndex,
int delay
int delay,
boolean cacheResponses
) {
super(engineConfig);
@ -119,6 +122,7 @@ public class ColumnAdditionByFetchingURLsOperation extends EngineDependentOperat
_columnInsertIndex = columnInsertIndex;
_delay = delay;
_cacheResponses = cacheResponses;
}
@Override
@ -135,6 +139,7 @@ public class ColumnAdditionByFetchingURLsOperation extends EngineDependentOperat
writer.key("urlExpression"); writer.value(_urlExpression);
writer.key("onError"); writer.value(TextTransformOperation.onErrorToString(_onError));
writer.key("delay"); writer.value(_delay);
writer.key("cacheResponses"); writer.value(_cacheResponses);
writer.endObject();
}
@ -165,7 +170,8 @@ public class ColumnAdditionByFetchingURLsOperation extends EngineDependentOperat
project,
engine,
eval,
getBriefDescription(null)
getBriefDescription(null),
_cacheResponses
);
}
@ -181,35 +187,39 @@ public class ColumnAdditionByFetchingURLsOperation extends EngineDependentOperat
Project project,
Engine engine,
Evaluable eval,
String description
String description,
boolean cacheResponses
) throws JSONException {
super(description);
_project = project;
_engine = engine;
_eval = eval;
_historyEntryID = HistoryEntry.allocateID();
_urlCache = CacheBuilder.newBuilder()
.maximumSize(2048)
.expireAfterWrite(10, TimeUnit.MINUTES)
.build(
new CacheLoader<String, Serializable>() {
public Serializable load(String urlString) {
Serializable result = fetch(urlString);
try {
// Always sleep for the delay, no matter how long the
// request took. This is more responsible than substracting
// the time spend requesting the URL, because it naturally
// slows us down if the server is busy and takes a long time
// to reply.
if (_delay > 0) {
Thread.sleep(_delay);
}
} catch (InterruptedException e) {
return null;
}
return result;
}
});
_urlCache = null;
if (cacheResponses) {
_urlCache = CacheBuilder.newBuilder()
.maximumSize(2048)
.expireAfterWrite(10, TimeUnit.MINUTES)
.build(
new CacheLoader<String, Serializable>() {
public Serializable load(String urlString) {
Serializable result = fetch(urlString);
try {
// Always sleep for the delay, no matter how long the
// request took. This is more responsible than substracting
// the time spend requesting the URL, because it naturally
// slows us down if the server is busy and takes a long time
// to reply.
if (_delay > 0) {
Thread.sleep(_delay);
}
} catch (InterruptedException e) {
return null;
}
return result;
}
});
}
}
@Override
@ -250,8 +260,20 @@ public class ColumnAdditionByFetchingURLsOperation extends EngineDependentOperat
List<CellAtRow> responseBodies = new ArrayList<CellAtRow>(urls.size());
for (int i = 0; i < urls.size(); i++) {
CellAtRow urlData = urls.get(i);
CellAtRow cellAtRow = cachedFetch(urlData);
if (cellAtRow != null) {
String urlString = urlData.cell.value.toString();
Serializable response = null;
if (_urlCache != null) {
response = cachedFetch(urlString);
} else {
response = fetch(urlString);
}
if (response != null) {
CellAtRow cellAtRow = new CellAtRow(
urlData.row,
new Cell(response, null));
responseBodies.add(cellAtRow);
}
@ -259,7 +281,7 @@ public class ColumnAdditionByFetchingURLsOperation extends EngineDependentOperat
if (_canceled) {
break;
}
}
}
if (!_canceled) {
@ -279,30 +301,21 @@ public class ColumnAdditionByFetchingURLsOperation extends EngineDependentOperat
}
}
CellAtRow cachedFetch(CellAtRow urlData) {
String urlString = urlData.cell.value.toString();
Serializable cellResult = null;
try {
cellResult = _urlCache.get(urlString);
} catch(ExecutionException e) {
}
if (cellResult != null) {
return new CellAtRow(
urlData.row,
new Cell(cellResult, null));
}
return null;
}
Serializable cachedFetch(String urlString) {
try {
return _urlCache.get(urlString);
} catch(ExecutionException e) {
return null;
}
}
Serializable fetch(String urlString) {
URL url = null;
try {
url = new URL(urlString);
} catch (MalformedURLException e) {
return null;
}
URL url = null;
try {
url = new URL(urlString);
} catch (MalformedURLException e) {
return null;
}
try {
URLConnection urlConnection = url.openConnection();

View File

@ -134,7 +134,8 @@ public class UrlFetchingTests extends RefineTest {
OnError.SetToBlank,
"rand",
1,
500);
500,
true);
ProcessManager pm = project.getProcessManager();
Process process = op.createProcess(project, options);
process.startPerforming(pm);

View File

@ -510,6 +510,7 @@
"on-error": "On error",
"set-blank": "set to blank",
"store-err": "store error",
"cache-responses": "Cache responses",
"copy-val": "copy value from original column",
"warning-col-name": "You must enter a column name.",
"add-col-fetch": "Add column by fetching URLs based on column",

View File

@ -510,6 +510,7 @@
"on-error": "On error",
"set-blank": "set to blank",
"store-err": "store error",
"cache-responses": "Cache responses",
"copy-val": "copy value from original column",
"warning-col-name": "You must enter a column name.",
"add-col-fetch": "Add column by fetching URLs based on column",

View File

@ -510,6 +510,7 @@
"on-error": "En cas derreur",
"set-blank": "vider la cellule",
"store-err": "conserver lerreur",
"cache-responses": "Mettre les réponses en cache",
"copy-val": "copier la valeur depuis la colonne originale",
"warning-col-name": "Vous devez indiquer un nom de colonne.",
"add-col-fetch": "Ajouter une colonne en moissonnant les données depuis les URL dune colonne",

View File

@ -12,11 +12,14 @@
</tr>
<tr>
<td width="1%" style="white-space: pre;" bind="or_views_onErr"></td>
<td colspan="3">
<td>
<input type="radio" name="dialog-onerror-choice" value="set-to-blank" checked id="$add-column-error-set-to-blank"/>
<label for="$add-column-error-set-to-blank" bind="or_views_setBlank"></label>
<input type="radio" name="dialog-onerror-choice" value="store-error" id="$add-column-error-store-error" />
<label for="$add-column-error-store-error" bind="or_views_storeErr"></label></td>
<td colspan="2">
<input type="checkbox" name="dialog-cache-responses" id="$add-column-cache-responses" checked="checked" />
<label for="$add-column-cache-responses" bind="or_views_cacheResponses"></label></td>
</tr>
<tr><td colspan="4"><h3><span bind="or_views_urlFetch"></span></h3></td></tr>
<tr><td colspan="4">$EXPRESSION_PREVIEW_WIDGET$</td></tr>
@ -27,4 +30,4 @@
<button class="button" bind="cancelButton"></button>
</div>
</div>
</div>
</div>

View File

@ -103,6 +103,7 @@ DataTableColumnHeaderUI.extendMenu(function(column, columnHeaderUI, menu) {
elmts.or_views_onErr.text($.i18n._('core-views')["on-error"]);
elmts.or_views_setBlank.text($.i18n._('core-views')["set-blank"]);
elmts.or_views_storeErr.text($.i18n._('core-views')["store-err"]);
elmts.or_views_cacheResponses.text($.i18n._('core-views')["cache-responses"]);
elmts.or_views_urlFetch.text($.i18n._('core-views')["url-fetch"]);
elmts.okButton.html($.i18n._('core-buttons')["ok"]);
elmts.cancelButton.text($.i18n._('core-buttons')["cancel"]);
@ -135,7 +136,8 @@ DataTableColumnHeaderUI.extendMenu(function(column, columnHeaderUI, menu) {
newColumnName: columnName,
columnInsertIndex: columnIndex + 1,
delay: elmts.throttleDelayInput[0].value,
onError: $('input[name="dialog-onerror-choice"]:checked')[0].value
onError: $('input[name="dialog-onerror-choice"]:checked')[0].value,
cacheResponses: $('input[name="dialog-cache-responses"]')[0].checked,
},
null,
{ modelsChanged: true }