IGNITE-7927 Web Console: Fixed demo for non-collocated joins.
[ignite.git] / modules / web-console / backend / routes / demo.js
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 'use strict';
19
20 const express = require('express');
21 const _ = require('lodash');
22
23 // Fire me up!
24
25 const clusters = require('./demo/clusters.json');
26 const caches = require('./demo/caches.json');
27 const domains = require('./demo/domains.json');
28 const igfss = require('./demo/igfss.json');
29
30 module.exports = {
31 implements: 'routes/demo',
32 inject: ['errors', 'settings', 'mongo', 'services/spaces']
33 };
34
35 /**
36 *
37 * @param _
38 * @param express
39 * @param errors
40 * @param settings
41 * @param mongo
42 * @param spacesService
43 * @return {Promise}
44 */
45 module.exports.factory = (errors, settings, mongo, spacesService) => {
46 return new Promise((factoryResolve) => {
47 const router = new express.Router();
48
49 /**
50 * Reset demo configuration.
51 */
52 router.post('/reset', (req, res) => {
53 spacesService.spaces(req.user._id, true)
54 .then((spaces) => {
55 const spaceIds = _.map(spaces, '_id');
56
57 return spacesService.cleanUp(spaceIds)
58 .then(() => mongo.Space.remove({_id: {$in: _.tail(spaceIds)}}).exec())
59 .then(() => _.head(spaces));
60 })
61 .catch((err) => {
62 if (err instanceof errors.MissingResourceException)
63 return spacesService.createDemoSpace(req.user._id);
64
65 throw err;
66 })
67 .then((space) => {
68 return Promise.all(_.map(clusters, (cluster) => {
69 const clusterDoc = new mongo.Cluster(cluster);
70
71 clusterDoc.space = space._id;
72
73 return clusterDoc.save();
74 }));
75 })
76 .then((clusterDocs) => {
77 return _.map(clusterDocs, (cluster) => {
78 const addCacheToCluster = (cacheDoc) => cluster.caches.push(cacheDoc._id);
79 const addIgfsToCluster = (igfsDoc) => cluster.igfss.push(igfsDoc._id);
80
81 if (cluster.name.endsWith('-caches')) {
82 const cachePromises = _.map(caches, (cacheData) => {
83 const cache = new mongo.Cache(cacheData);
84
85 cache.space = cluster.space;
86 cache.clusters.push(cluster._id);
87
88 return cache.save()
89 .then((cacheDoc) => {
90 const domainData = _.find(domains, (item) =>
91 item.databaseTable === cacheDoc.name.slice(0, -5).toUpperCase());
92
93 if (domainData) {
94 const domain = new mongo.DomainModel(domainData);
95
96 domain.space = cacheDoc.space;
97 domain.caches.push(cacheDoc._id);
98 domain.clusters.push(cluster._id);
99
100 return domain.save()
101 .then((domainDoc) => {
102 cacheDoc.domains.push(domainDoc._id);
103 cluster.models.push(domainDoc._id);
104
105 return cacheDoc.save();
106 });
107 }
108
109 return cacheDoc;
110 });
111 });
112
113 return Promise.all(cachePromises)
114 .then((cacheDocs) => {
115 _.forEach(cacheDocs, addCacheToCluster);
116
117 return cluster.save();
118 });
119 }
120
121 if (cluster.name.endsWith('-igfs')) {
122 return Promise.all(_.map(igfss, (igfs) => {
123 const igfsDoc = new mongo.Igfs(igfs);
124
125 igfsDoc.space = cluster.space;
126 igfsDoc.clusters.push(cluster._id);
127
128 return igfsDoc.save();
129 }))
130 .then((igfsDocs) => {
131 _.forEach(igfsDocs, addIgfsToCluster);
132
133 return cluster.save();
134 });
135 }
136 });
137 })
138 .then(() => res.sendStatus(200))
139 .catch((err) => res.status(500).send(err.message));
140 });
141
142 factoryResolve(router);
143 });
144 };
145