-
Notifications
You must be signed in to change notification settings - Fork 4
/
cip-categories.js
243 lines (205 loc) · 6.19 KB
/
cip-categories.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
'use strict';
const cip = require('./services/cip');
const Q = require('q');
const config = require('collections-online/lib/config');
const ds = require('collections-online/lib/services/documents');
function Categories(tree) {
this.getPath = function(x) {
function traverse(tree, path) {
path.push(tree);
if (tree.id === x) {
return path;
}
for (var i = 0; i < tree.children.length; ++i) {
var result = traverse(tree.children[i], path.slice(0));
if (result !== null) {
return result;
}
}
return null;
}
return traverse(this.tree, []);
};
this.getNode = function(x) {
function traverse(tree) {
if (tree.id === x) {
return tree;
}
for (var i = 0; i < tree.children.length; ++i) {
var result = traverse(tree.children[i]);
if (result !== null) {
return result;
}
}
return null;
}
return traverse(this.tree);
};
this.dumpTree = function(tree) {
console.log(tree.id + ':' + tree.name);
for (var i = 0; i < tree.children.length; ++i) {
this.dumpTree(tree.children[i]);
}
};
this.buildTree = function(tree) {
// If the category id is in the blacklist, just return null.
if (config.categoryBlacklist.indexOf(tree.id) !== -1) {
return null;
}
var name = tree['Category Name'] || tree['CategoryName'];
var result = {
id: tree.id,
name: name,
children: []
};
if (!tree.hassubcategories) {
return result;
}
for (var i = 0; i < tree.subcategories.length; ++i) {
var subcategories = this.buildTree(tree.subcategories[i]);
if (subcategories !== null) {
result.children.push(subcategories);
}
}
return result;
};
this.tree = {};
this.tree = this.buildTree(tree);
}
function loadCategories() {
// First let's create a single session with the CIP
return cip.initSession().then(() => {
// Then - let's fetch some categories
var catalogPromises = Object.keys(config.cip.catalogs).map((alias) => {
return cip.request([
'metadata',
'getcategories',
alias,
'categories'
], {
levels: 'all'
}).then((response) => {
var categories = new Categories(response.body);
categories.id = alias;
return categories;
});
});
console.log('Fetching categories for', catalogPromises.length, 'catalogs.');
return Q.allSettled(catalogPromises).then(function(result) {
var finalResult = [];
for (var i = 0; i < result.length; ++i) {
if (result[i].state === 'fulfilled') {
finalResult.push(result[i].value);
} else {
console.error('Error fetching categories:', result[i].reason);
}
}
console.log('Got categories for', finalResult.length, 'catalogs.');
if (catalogPromises.length !== finalResult.length &&
config.env !== 'development') {
throw new Error('Could not load categories for all the catalogs.');
}
return finalResult;
});
});
}
exports.loadCategories = loadCategories;
function fetchCategoryCounts(esClient, catalogs) {
function handleCategoryNode(categoryCounts, node) {
var categoryCount = categoryCounts[node.id];
if (categoryCount > 0) {
node.count = categoryCount;
} else {
node.count = 0;
}
// Progress recursively ..
for (var c in node.children) {
handleCategoryNode(categoryCounts, node.children[c]);
}
}
function handleEsAggregations(response) {
var categoryCounts = {};
var categoryAggregations = response.aggregations.catalog.categories.buckets;
for (var f in categoryAggregations) {
var categoryId = categoryAggregations[f].key;
var categoryCount = categoryAggregations[f].doc_count;
categoryCounts[categoryId] = categoryCount;
}
handleCategoryNode(categoryCounts, this.tree);
}
var promises = [];
for (var c in catalogs) {
var catalog = catalogs[c];
var catalogAlias = catalog.id;
var countPromise = esClient.search({
index: config.types.asset.index,
body: {
'size': 0,
'aggs': {
'catalog': {
'filter': {
'and': [
{'query': {'match': {'catalog': catalogAlias}}},
{'query': {'match': {'is_searchable': true}}}
]
},
'aggs': {
'categories': {
'terms': {
'field': 'categories_int',
'size': 1000000000 // A very large number
},
}
}
}
}
}
}).then(handleEsAggregations.bind(catalog));
promises.push(countPromise);
}
// When all the facet searches for assets are ready.
return Q.all(promises).then(function() {
// Return the new catalogs with counts.
return catalogs;
});
}
exports.formatCategories = function(allCategories, categories) {
var result = [];
for (var c in categories) {
var category = categories[c];
if (category.path.indexOf('$Categories') === 0 && category.id !== 1) {
result.push(allCategories.getPath(category.id));
}
}
// Sort by lexicographical order of the concatinated names.
result.sort(function(x, y) {
if (x && y) {
var xStr = x.map(function(value) { return value.name; }).join(':');
var yStr = y.map(function(value) { return value.name; }).join(':');
return xStr.localeCompare(yStr);
}
});
return result;
};
exports.initialize = (app) => {
var categories = {};
return loadCategories().then(function(result) {
for (var i = 0; i < result.length; ++i) {
if (result[i] && result[i].id) {
categories[result[i].id] = result[i];
} else {
console.error(result);
throw new Error('Could not read id from the result of loadCategories');
}
}
// Fetch the number of assets in the category.
return fetchCategoryCounts(ds, categories)
.then(function(categoriesWithCounts) {
app.set('categories', categoriesWithCounts);
});
}).then(function() {
return cip.getCatalogs().then(function(catalogs) {
app.set('catalogs', catalogs);
});
});
};