Skip to content

Commit

Permalink
allow resubmission of cached requests in Langviews
Browse files Browse the repository at this point in the history
  • Loading branch information
MusikAnimal committed Apr 19, 2016
1 parent cb5e26c commit 536993a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 39 deletions.
61 changes: 39 additions & 22 deletions javascripts/langviews/langviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class LangViews extends Pv {
* Valid values are those defined in config.specialRanges, constructed like `{range: 'last-month'}`,
* or a relative range like `{range: 'latest-N'}` where N is the number of days.
*/
if (this.specialRange) {
if (this.specialRange && !forCacheKey) {
params.range = this.specialRange.range;
} else {
params.start = this.daterangepicker.startDate.format('YYYY-MM-DD');
Expand Down Expand Up @@ -335,7 +335,7 @@ class LangViews extends Pv {
*/
// XXX: throttling
if (!hadFailure) {
simpleStorage.set(cacheKey, true, {TTL: 600000});
simpleStorage.set(this.getCacheKey(), true, {TTL: 600000});
}
}
});
Expand All @@ -347,11 +347,7 @@ class LangViews extends Pv {
* so we use simpleStorage to keep track of what the user has recently queried.
*/
// XXX: throttling
const cacheKey = `lv-cache-${this.hashCode(
JSON.stringify(this.getParams(true))
)}`,
isCached = simpleStorage.hasKey(cacheKey),
requestFn = isCached ? makeRequest : this.rateLimit(makeRequest, 100, this);
const requestFn = this.isRequestCached() ? makeRequest : this.rateLimit(makeRequest, 100, this);

interWikiKeys.forEach((dbName, index) => {
requestFn(dbName);
Expand All @@ -360,6 +356,24 @@ class LangViews extends Pv {
return dfd;
}

/**
* Return cache key for current params
* @return {String} key
*/
getCacheKey() {
return `lv-cache-${this.hashCode(
JSON.stringify(this.getParams(true))
)}`;
}

/**
* Check simple storage to see if a request with the current params would be cached
* @return {Boolean} cached or not
*/
isRequestCached() {
return simpleStorage.hasKey(this.getCacheKey());
}

/**
* Query Wikidata to find data about a given page across all sister projects
* @param {String} dbName - database name of source project
Expand Down Expand Up @@ -611,22 +625,25 @@ class LangViews extends Pv {
*/
processArticle() {
// XXX: throttling
/** Check if user has exceeded request limit and throw error */
if (simpleStorage.hasKey('langviews-throttle')) {
const timeRemaining = Math.round(simpleStorage.getTTL('langviews-throttle') / 1000);

/** > 0 check to combat race conditions */
if (timeRemaining > 0) {
return this.writeMessage(`
Please wait <b>${timeRemaining}</b> seconds before submitting another request.<br/>
Apologies for the inconvenience. This is a temporary throttling tactic.<br/>
See <a href="https://phabricator.wikimedia.org/T124314" target="_blank">phab:T124314</a>
for more information.
`, true);
/** allow resubmission of queries that are cached */
if (!this.isRequestCached()) {
/** Check if user has exceeded request limit and throw error */
if (simpleStorage.hasKey('langviews-throttle')) {
const timeRemaining = Math.round(simpleStorage.getTTL('langviews-throttle') / 1000);

/** > 0 check to combat race conditions */
if (timeRemaining > 0) {
return this.writeMessage(`
Please wait <b>${timeRemaining}</b> seconds before submitting another request.<br/>
Apologies for the inconvenience. This is a temporary throttling tactic.<br/>
See <a href="https://phabricator.wikimedia.org/T124314" target="_blank">phab:T124314</a>
for more information.
`, true);
}
} else {
/** limit to one request every 3 minutes */
simpleStorage.set('langviews-throttle', true, {TTL: 120000});
}
} else {
/** limit to one request every 3 minutes */
simpleStorage.set('langviews-throttle', true, {TTL: 120000});
}

const page = $(config.articleInput).val();
Expand Down
Loading

0 comments on commit 536993a

Please sign in to comment.