Repository: ignite Updated Branches: refs/heads/ignite-843-rc2 3fc013ae4 -> 63bd2c0a3
IGNITE-843 WIP Rework server side to Promises. Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/63bd2c0a Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/63bd2c0a Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/63bd2c0a Branch: refs/heads/ignite-843-rc2 Commit: 63bd2c0a34581faa36c5e2b12e89ce9026a399b5 Parents: 3fc013a Author: Alexey Kuznetsov <[email protected]> Authored: Tue Feb 16 10:39:41 2016 +0700 Committer: Alexey Kuznetsov <[email protected]> Committed: Tue Feb 16 10:39:41 2016 +0700 ---------------------------------------------------------------------- .../main/js/controllers/caches-controller.js | 8 +- .../main/js/controllers/domains-controller.js | 14 +- .../src/main/js/serve/routes/clusters.js | 11 + .../src/main/js/serve/routes/domains.js | 203 +++++++++---------- 4 files changed, 127 insertions(+), 109 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/63bd2c0a/modules/control-center-web/src/main/js/controllers/caches-controller.js ---------------------------------------------------------------------- diff --git a/modules/control-center-web/src/main/js/controllers/caches-controller.js b/modules/control-center-web/src/main/js/controllers/caches-controller.js index c1837c8..4a8ee53 100644 --- a/modules/control-center-web/src/main/js/controllers/caches-controller.js +++ b/modules/control-center-web/src/main/js/controllers/caches-controller.js @@ -296,7 +296,13 @@ consoleModule.controller('cachesController', [ }); $scope.caches = data.caches; - $scope.clusters = data.clusters; + $scope.clusters = _.map(data.clusters, function (cluster) { + return { + value: cluster._id, + label: cluster.name, + caches: cluster.caches + }; + }); $scope.domains = _.sortBy(_.map(validFilter(data.domains, true, false), function (domain) { return { value: domain._id, http://git-wip-us.apache.org/repos/asf/ignite/blob/63bd2c0a/modules/control-center-web/src/main/js/controllers/domains-controller.js ---------------------------------------------------------------------- diff --git a/modules/control-center-web/src/main/js/controllers/domains-controller.js b/modules/control-center-web/src/main/js/controllers/domains-controller.js index 03ebd97..142e76c 100644 --- a/modules/control-center-web/src/main/js/controllers/domains-controller.js +++ b/modules/control-center-web/src/main/js/controllers/domains-controller.js @@ -83,7 +83,12 @@ consoleModule.controller('domainsController', function ($filter, $http, $timeout $http.post('/api/v1/configuration/domains/list') .success(function (data) { $scope.spaces = data.spaces; - $scope.clusters = data.clusters; + $scope.clusters = _.map(data.clusters, function (cluster) { + return { + value: cluster._id, + label: cluster.name + }; + }); $scope.caches = _mapCaches(data.caches); $scope.domains = data.domains; @@ -1137,7 +1142,12 @@ consoleModule.controller('domainsController', function ($filter, $http, $timeout $http.post('/api/v1/configuration/domains/list') .success(function (data) { $scope.spaces = data.spaces; - $scope.clusters = data.clusters; + $scope.clusters = _.map(data.clusters, function (cluster) { + return { + value: cluster._id, + label: cluster.name + }; + }); $scope.caches = _mapCaches(data.caches); $scope.domains = data.domains; http://git-wip-us.apache.org/repos/asf/ignite/blob/63bd2c0a/modules/control-center-web/src/main/js/serve/routes/clusters.js ---------------------------------------------------------------------- diff --git a/modules/control-center-web/src/main/js/serve/routes/clusters.js b/modules/control-center-web/src/main/js/serve/routes/clusters.js index c4260db..a04844a 100644 --- a/modules/control-center-web/src/main/js/serve/routes/clusters.js +++ b/modules/control-center-web/src/main/js/serve/routes/clusters.js @@ -43,9 +43,20 @@ module.exports.factory = function(_, express, mongo) { result.spaces = spaces; spacesIds = mongo.spacesIds(spaces); + return mongo.DomainModel.find({space: {$in: spacesIds}}).sort('valueType').exec(); + }) + .then((domains) => { + result.domains = domains; + return mongo.Cache.find({space: {$in: spacesIds}}).sort('name').exec(); }) .then((caches) => { + _.forEach(caches, (cache) => { + cache.domains = _.map(cache.domains, (domainId) => { + return _.find(result.domains, {_id: domainId}); + }); + }); + result.caches = caches; return mongo.Igfs.find({space: {$in: spacesIds}}).sort('name').exec(); http://git-wip-us.apache.org/repos/asf/ignite/blob/63bd2c0a/modules/control-center-web/src/main/js/serve/routes/domains.js ---------------------------------------------------------------------- diff --git a/modules/control-center-web/src/main/js/serve/routes/domains.js b/modules/control-center-web/src/main/js/serve/routes/domains.js index 3dc2116..f757910 100644 --- a/modules/control-center-web/src/main/js/serve/routes/domains.js +++ b/modules/control-center-web/src/main/js/serve/routes/domains.js @@ -24,7 +24,7 @@ module.exports = { inject: ['require(lodash)', 'require(express)', 'mongo'] }; -module.exports.factory = function(_, express, async, mongo) { +module.exports.factory = function(_, express, mongo) { return new Promise((factoryResolve) => { const router = new express.Router(); @@ -63,122 +63,113 @@ module.exports.factory = function(_, express, async, mongo) { .catch((err) => mongo.handleError(res, err)); }); - // function _saveDomainModel(domain, savedDomains, callback) { - // const caches = domain.caches; - // const cacheStoreChanges = domain.cacheStoreChanges; - // let domainId = domain._id; - // - // if (domainId) { - // Promise.all([ - // mongo.DomainModel.update({_id: domain._id}, domain, {upsert: true}).exec(), - // mongo.Cache.update({_id: {$in: caches}}, {$addToSet: {domains: domainId}}, {multi: true}).exec(), - // mongo.Cache.update({_id: {$nin: caches}}, {$pull: {domains: domainId}}, {multi: true}).exec() - // ]).then(() => { - // savedDomains.push(domain); - // - // _updateCacheStore(cacheStoreChanges, callback); - // }); - // } - // else { - // mongo.DomainModel.findOne({space: domain.space, valueType: domain.valueType}).exec() - // .then((found) => { - // if (found) - // reject(new Error('Domain model with value type: "' + found.valueType + '" already exist.')); - // - // return (new mongo.DomainModel(domain)).save(); - // }) - // .then((domainSaved) => { - // savedDomains.push(domainSaved); - // - // return mongo.Cache.update({_id: {$in: caches}}, {$addToSet: {domains: domainSaved._id}}, {multi: true}).exec(); - // }).then(() => _updateCacheStore(cacheStoreChanges)); - // } - // } - - // function _updateCacheStore(cacheStoreChanges, callback) { - // if (cacheStoreChanges && cacheStoreChanges.length > 0) { - // async.forEachOf(cacheStoreChanges, (change, idx, cb) => { - // mongo.Cache.update({_id: {$eq: change.cacheId}}, change.change, {}, (err) => { - // if (err) - // cb(err); - // else - // cb(); - // }); - // }, callback); - // } - // else - // callback(); - // } - - // function _save(domains, res) { - // if (domains && domains.length > 0) { - // const savedDomains = []; - // const generatedCaches = []; - // const promises = []; - // - // _.forEach(domains, (domain) => { - // promises.push(); - // }); - // - // Promise.all(promises) - // .then(() => res.send({savedDomains, generatedCaches})) - // .catch((err) => mongo.handleError(res, err)); - // - // //async.forEachOf(domains, (domain, idx, callback) => { - // // if (domain.newCache) { - // // mongo.Cache.findOne({space: domain.space, name: domain.newCache.name}, (errCacheFind, cacheFound) => { - // // if (mongo.processed(errCacheFind, res)) { - // // if (cacheFound) { - // // // Cache already exists, just save domain model. - // // domain.caches = [cacheFound._id]; - // // - // // _saveDomainModel(domain, savedDomains, callback); - // // } - // // else { - // // // If cache not found, then create it and associate with domain model. - // // const newCache = domain.newCache; - // // newCache.space = domain.space; - // // - // // (new mongo.Cache(newCache)).save((errCache, cache) => { - // // const cacheId = cache._id; - // // - // // if (mongo.processed(errCache, res)) { - // // mongo.Cluster.update({_id: {$in: cache.clusters}}, {$addToSet: {caches: cacheId}}, {multi: true}, (errCluster) => { - // // if (mongo.processed(errCluster, res)) { - // // domain.caches = [cacheId]; - // // generatedCaches.push(cache); - // // - // // _saveDomainModel(domain, savedDomains, callback); - // // } - // // }); - // // } - // // }); - // // } - // // } - // // }); - // // } - // // else - // // _saveDomainModel(domain, savedDomains, callback); - // //} - // } - // else - // res.status(500).send('Nothing to save!'); - // } + function _updateCacheStore(cacheStoreChanges) { + const promises = []; + + _.forEach(cacheStoreChanges, (change) => + promises.push(mongo.Cache.update({_id: {$eq: change.cacheId}}, change.change, {}).exec()) + ); + + return Promise.all(promises); + } + + function _saveDomainModel(domain, savedDomains) { + return new Promise((resolve) => { + const caches = domain.caches; + const cacheStoreChanges = domain.cacheStoreChanges; + const domainId = domain._id; + + if (domainId) { + mongo.DomainModel.update({_id: domain._id}, domain, {upsert: true}).exec() + .then(() => mongo.Cache.update({_id: {$in: caches}}, {$addToSet: {domains: domainId}}, {multi: true}).exec()) + .then(() => mongo.Cache.update({_id: {$nin: caches}}, {$pull: {domains: domainId}}, {multi: true}).exec()) + .then(() => { + savedDomains.push(domain); + + _updateCacheStore(cacheStoreChanges); + }) + .then(() => resolve()); + } + else { + mongo.DomainModel.findOne({space: domain.space, valueType: domain.valueType}).exec() + .then((found) => { + if (found) + throw new Error('Domain model with value type: "' + found.valueType + '" already exist.'); + + return (new mongo.DomainModel(domain)).save(); + }) + .then((savedDomain) => { + savedDomains.push(savedDomain); + + return mongo.Cache.update({_id: {$in: caches}}, {$addToSet: {domains: savedDomain._id}}, {multi: true}).exec(); + }) + .then(() => _updateCacheStore(cacheStoreChanges)) + .then(() => resolve()); + } + }); + } + + function _save(domains, res) { + if (domains && domains.length > 0) { + const savedDomains = []; + const generatedCaches = []; + const promises = []; + + _.forEach(domains, (domain) => { + promises.push(new Promise((resolve) => { + if (domain.newCache) { + mongo.Cache.findOne({space: domain.space, name: domain.newCache.name}).exec() + .then((cache) => { + if (cache) { + // Cache already exists, just save domain model. + domain.caches = [cache._id]; + + _saveDomainModel(domain, savedDomains).then(() => resolve()); + } + else { + // If cache not found, then create it and associate with domain model. + const newCache = domain.newCache; + newCache.space = domain.space; + + (new mongo.Cache(newCache)).save() + .then((generatedCache) => { + const cacheId = generatedCache._id; + + generatedCaches.push(generatedCache); + + domain.caches = [cacheId]; + + return mongo.Cluster.update({_id: {$in: generatedCache.clusters}}, {$addToSet: {caches: cacheId}}, {multi: true}).exec(); + }) + .then(() => _saveDomainModel(domain, savedDomains).then(() => resolve())); + } + }); + } + else + _saveDomainModel(domain, savedDomains).then(() => resolve()); + })); + }); + + Promise.all(promises) + .then(() => res.send({savedDomains, generatedCaches})) + .catch((err) => mongo.handleError(res, err)); + } + else + res.status(500).send('Nothing to save!'); + } /** * Save domain model. */ router.post('/save', (req, res) => { - res.status(500).send('Not ready!'); - // _save([req.body], res); + _save([req.body], res); }); /** * Batch save domain models. */ router.post('/save/batch', (req, res) => { - res.status(500).send('Not ready!'); - // _save(req.body, res); + _save(req.body, res); }); /**
