# HG changeset patch # User ymh # Date 1465485074 -7200 # Node ID 1bcc373adabb473a5ef1bd453f6956184db3a7d4 # Parent b8a783ca6c4a90ecdfb674ac3925042ad91352ef Optimize resolver service to minimize number of requests diff -r b8a783ca6c4a -r 1bcc373adabb common/corpus-common-addon/.projectile diff -r b8a783ca6c4a -r 1bcc373adabb common/corpus-common-addon/addon/services/resolver-service.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/corpus-common-addon/addon/services/resolver-service.js Thu Jun 09 17:11:14 2016 +0200 @@ -0,0 +1,104 @@ +import Ember from 'ember'; +import _ from 'lodash'; +import store from 'corpus-common-addon/utils/store'; + +export default Ember.Service.extend({ + + promises: null, + queryRegister: null, + + storeKeyBase: null, + apiPath: null, + resDocRoot: null, + + processId: function(id) { return id; }, + + getReturnDictKey: function(id) { return id; }, + + init() { + this._super(...arguments); + this.set('promises', []); + this.set('queryRegister',{}); + this.doQuery = _.debounce(this._doQuery,10); + }, + + env: function() { + return Ember.getOwner(this).resolveRegistration('config:environment'); + }, + + + _getStoreKey: function(id) { + return this.storeKeyBase+':'+id; + }, + + _doQuery: function() { + var queryRegister = this.get('queryRegister'); + this.set('queryRegister', {}); + var ids = Object.keys(queryRegister).join(','); + if(!ids) { + _.each(queryRegister, (resolve_reject) => { + resolve_reject[0]({}); + }); + } + Ember.$.ajax({ + //TODO Configuration ? + url: this.env().baseURL.replace(/\/$/,'') + this.apiPath +ids, + + success: (itemDoc) => { + _.each(queryRegister, (resolve_reject) => { + resolve_reject[0](itemDoc[this.resDocRoot]); + }); + }, + error: (req, status, error) => { + _.each(queryRegister, (resolve_reject) => { + resolve_reject[1](status + ':' + error); + }); + + } + }); + }, + + // make the query for the name. + // return a Promise + queryName: function(id) { + var queryRegister = this.get('queryRegister'); + return new Ember.RSVP.Promise(function(resolve, reject) { + queryRegister[id] = [resolve, reject]; + this.doQuery(this); + }.bind(this)); + }, + + getName: function(id) { + + if(!id) { + return new Ember.RSVP.Promise(function(resolve/*, reject*/) { + resolve(''); + }); + } + + var objectId = this.processId(id); + + var namePromise = null; + + var storeKey = this._getStoreKey(id); + var name = store.get(storeKey); + + if( name !== null) { + namePromise = new Ember.RSVP.Promise(function(resolve/*, reject*/) { + resolve(name); + }); + } else if ( storeKey in this.get('promises')) { + namePromise = this.get('promises')[storeKey]; + } else { + //TODO: handle error !!! + namePromise = this.queryName(objectId) + .then(function(names) { + delete this.get('promises')[storeKey]; + return store.set(storeKey, names[this.getReturnDictKey(objectId)]); + }.bind(this)); + this.get('promises')[storeKey] = namePromise; + } + return namePromise; + } + +}); diff -r b8a783ca6c4a -r 1bcc373adabb common/corpus-common-addon/addon/utils/store.js --- a/common/corpus-common-addon/addon/utils/store.js Wed Jun 08 21:40:22 2016 +0200 +++ b/common/corpus-common-addon/addon/utils/store.js Thu Jun 09 17:11:14 2016 +0200 @@ -5,15 +5,15 @@ set: function(key, val, exp) { var expiration = exp; if(typeof exp === 'undefined') { - expiration = constants.DEFAULT_STORE_EXP; + expiration = constants.DEFAULT_STORE_EXP; } store.set(key, { val:val, exp:expiration, time:new Date().getTime() }); return val; }, get: function(key) { - var info = store.get(key) - if (!info) { return null } - if (new Date().getTime() - info.time > info.exp) { return null } - return info.val + var info = store.get(key); + if (!info) { return null; } + if (new Date().getTime() - info.time > info.exp) { return null; } + return info.val; } -} +}; diff -r b8a783ca6c4a -r 1bcc373adabb common/corpus-common-addon/app/services/bnf-resolver.js --- a/common/corpus-common-addon/app/services/bnf-resolver.js Wed Jun 08 21:40:22 2016 +0200 +++ b/common/corpus-common-addon/app/services/bnf-resolver.js Thu Jun 09 17:11:14 2016 +0200 @@ -1,71 +1,34 @@ -import Ember from 'ember'; -import store from 'corpus-common-addon/utils/store'; -import * as constants from 'corpus-common-addon/utils/constants' +import * as constants from 'corpus-common-addon/utils/constants'; -export default Ember.Service.extend({ - env: function() { - return Ember.getOwner(this).resolveRegistration('config:environment') - }, - _getStoreKey: function(id) { - return 'bnf:'+id; - }, - getLabel: function(id) { +import ResolverService from 'corpus-common-addon/services/resolver-service'; + +export default ResolverService.extend({ - if(!id) { - return new Ember.RSVP.Promise(function(resolve/*, reject*/) { - resolve(""); - }); - } + storeKeyBase: 'bnf', + apiPath: '/api/v1/bnf/', + resDocRoot: 'bnfids', - var bnfId = id; - if(id.startsWith(constants.BNF_BASE_URL + constants.BNF_ARK_BASE_ID)) { - bnfId = id.slice((constants.BNF_BASE_URL + constants.BNF_ARK_BASE_ID).length); - } - else if (id.startsWith(constants.BNF_ARK_BASE_URL + constants.BNF_ARK_BASE_ID)) { - bnfId = id.slice((constants.BNF_ARK_BASE_URL + constants.BNF_ARK_BASE_ID).length); - } - else if (id.startsWith(constants.BNF_ARK_BASE_ID)) { - bnfId = id.slice(constants.BNF_ARK_BASE_ID.length); - } - var labelPromise = null; - - var storeKey = this._getStoreKey(id); - var label = store.get(storeKey); + init() { + this._super(...arguments); + console.log("BNF", this); + }, - if(!label) { - //TODO: handle error !!! - labelPromise = this.queryLabel(bnfId) - .then( function(response) { - return store.set(storeKey, response); - }); - } - else { - labelPromise = new Ember.RSVP.Promise(function(resolve/*, reject*/) { - resolve(label); - }); - } - return labelPromise; - - }, + processId: function(id) { + var bnfId = id; + if(id.startsWith(constants.BNF_BASE_URL + constants.BNF_ARK_BASE_ID)) { + bnfId = id.slice((constants.BNF_BASE_URL + constants.BNF_ARK_BASE_ID).length); + } + else if (id.startsWith(constants.BNF_ARK_BASE_URL + constants.BNF_ARK_BASE_ID)) { + bnfId = id.slice((constants.BNF_ARK_BASE_URL + constants.BNF_ARK_BASE_ID).length); + } + else if (id.startsWith(constants.BNF_ARK_BASE_ID)) { + bnfId = id.slice(constants.BNF_ARK_BASE_ID.length); + } + return bnfId; + }, - // make the query for the name. - // return a Promise - queryLabel: function(id) { - return new Ember.RSVP.Promise(function(resolve, reject) { - Ember.$.ajax({ - //TODO Configuration for the host ? - url: this.env().baseURL.replace(/\/$/,"") + "/api/v1/bnf/"+id, - success: function(bnfDoc) { - var labels = bnfDoc.bnfids; - var expectedId = constants.BNF_ARK_BASE_ID + id; - resolve((expectedId in labels)?labels[expectedId]:null); - }, - error: function(req, status, error) { - reject(status + ":" + error); - } - }); - }.bind(this)); - } + getReturnDictKey: function(id) { return constants.BNF_ARK_BASE_ID+id; }, + getLabel: function(id) { return this.getName(id); } }); diff -r b8a783ca6c4a -r 1bcc373adabb common/corpus-common-addon/app/services/lexvo-resolver.js --- a/common/corpus-common-addon/app/services/lexvo-resolver.js Wed Jun 08 21:40:22 2016 +0200 +++ b/common/corpus-common-addon/app/services/lexvo-resolver.js Thu Jun 09 17:11:14 2016 +0200 @@ -1,61 +1,26 @@ -import Ember from 'ember'; -import store from 'corpus-common-addon/utils/store'; -import * as constants from 'corpus-common-addon/utils/constants' +import * as constants from 'corpus-common-addon/utils/constants'; -export default Ember.Service.extend({ - env: function() { - return Ember.getOwner(this).resolveRegistration('config:environment') - }, - _getStoreKey: function(id) { - return 'lexvo:'+id; - }, - getName: function(id) { +import ResolverService from 'corpus-common-addon/services/resolver-service'; - if(!id) { - return new Ember.RSVP.Promise(function(resolve/*, reject*/) { - resolve(""); - }); - } +export default ResolverService.extend({ - var lexvoId = id; - if(id.startsWith(constants.LEXVO_BASE_URL)) { - lexvoId = id.slice(constants.LEXVO_BASE_URL.length); - } - var namePromise = null; - - var storeKey = this._getStoreKey(id); - var name = store.get(storeKey); + storeKeyBase: 'lexvo', + apiPath: '/api/v1/lexvo/', + resDocRoot: 'lexvoids', - if(!name) { - //TODO: handle error !!! - namePromise = this.queryName(lexvoId) - .then( function(response) { - return store.set(storeKey, response); - }); - } - else { - namePromise = new Ember.RSVP.Promise(function(resolve/*, reject*/) { - resolve(name); - }); - } - return namePromise; - }, + init() { + this._super(...arguments); + console.log("LEXVO", this); + }, - // make the query for the name. - // return a Promise - queryName: function(id) { - return new Ember.RSVP.Promise(function(resolve, reject) { - Ember.$.ajax({ - //TODO Configuration ? - url: this.env().baseURL.replace(/\/$/,"") + "/api/v1/lexvo/"+id, - success: function(lexvoDoc) { - var names = lexvoDoc.lexvoids; - resolve((id in names)?names[id]:null); - }, - error: function(req, status, error) { - reject(status + ":" + error); + processId: function(id) { + var lexvoId = id; + if(id.startsWith(constants.LEXVO_BASE_URL)) { + lexvoId = id.slice(constants.LEXVO_BASE_URL.length); } - }); - }.bind(this)); - } + return lexvoId; + }, + + getReturnDictKey: function(id) { return id; } + }); diff -r b8a783ca6c4a -r 1bcc373adabb common/corpus-common-addon/app/services/viaf-resolver.js --- a/common/corpus-common-addon/app/services/viaf-resolver.js Wed Jun 08 21:40:22 2016 +0200 +++ b/common/corpus-common-addon/app/services/viaf-resolver.js Thu Jun 09 17:11:14 2016 +0200 @@ -1,51 +1,26 @@ -import Ember from 'ember'; -import store from 'corpus-common-addon/utils/store'; -import * as constants from 'corpus-common-addon/utils/constants' +import * as constants from 'corpus-common-addon/utils/constants'; + +import ResolverService from 'corpus-common-addon/services/resolver-service'; -export default Ember.Service.extend({ - env: function() { - return Ember.getOwner(this).resolveRegistration('config:environment') - }, - _getStoreKey: function(id) { - return 'viaf:'+id; - }, - getName: function(id) { - var viafId = id; - if(id.startsWith(constants.VIAF_BASE_URL)) { - viafId = id.slice(constants.VIAF_BASE_URL.length); - } - var storeKey = this._getStoreKey(id); - var namePromise = null; +export default ResolverService.extend({ + + storeKeyBase: 'viaf', + apiPath: '/api/v1/viaf/', + resDocRoot: 'viafids', - var name = store.get(storeKey); - if(!name) { - //TODO: handle error !!! - namePromise = this.queryName(viafId) - .then( function(response) { - return store.set(storeKey, response); - }); - } - else { - namePromise = new Ember.RSVP.Promise(function(resolve/*, reject*/) { - resolve(name); - }); - } - return namePromise; - }, - // make the query for the name. - // return a Promise - queryName: function(id) { - return new Ember.RSVP.Promise(function(resolve, reject) { - Ember.$.ajax({ - url: this.env().baseURL.replace(/\/$/,"") + "/api/v1/viaf/"+id, - success: function(viafDoc) { - var names = viafDoc.viafids; - resolve((id in names)?names[id]:null); - }, - error: function(req, status, error) { - reject(status + ":" + error); + init() { + this._super(...arguments); + console.log("VIAF", this); + }, + + processId: function(id) { + var viafId = id; + if(id.startsWith(constants.VIAF_BASE_URL)) { + viafId = id.slice(constants.VIAF_BASE_URL.length); } - }); - }.bind(this)); - } + return viafId; + }, + + getReturnDictKey: function(id) { return id; } + });