UNPKG

12.2 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || function () {
3 __assign = Object.assign || function(t) {
4 for (var s, i = 1, n = arguments.length; i < n; i++) {
5 s = arguments[i];
6 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7 t[p] = s[p];
8 }
9 return t;
10 };
11 return __assign.apply(this, arguments);
12};
13var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14 return new (P || (P = Promise))(function (resolve, reject) {
15 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
16 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
17 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
18 step((generator = generator.apply(thisArg, _arguments || [])).next());
19 });
20};
21var __generator = (this && this.__generator) || function (thisArg, body) {
22 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
23 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
24 function verb(n) { return function (v) { return step([n, v]); }; }
25 function step(op) {
26 if (f) throw new TypeError("Generator is already executing.");
27 while (_) try {
28 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
29 if (y = 0, t) op = [op[0] & 2, t.value];
30 switch (op[0]) {
31 case 0: case 1: t = op; break;
32 case 4: _.label++; return { value: op[1], done: false };
33 case 5: _.label++; y = op[1]; op = [0]; continue;
34 case 7: op = _.ops.pop(); _.trys.pop(); continue;
35 default:
36 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
37 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
38 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
39 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
40 if (t[2]) _.ops.pop();
41 _.trys.pop(); continue;
42 }
43 op = body.call(thisArg, _);
44 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
45 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46 }
47};
48var __importDefault = (this && this.__importDefault) || function (mod) {
49 return (mod && mod.__esModule) ? mod : { "default": mod };
50};
51Object.defineProperty(exports, "__esModule", { value: true });
52// (C) 2007-2018 GoodData Corporation
53var get_1 = __importDefault(require("lodash/get"));
54var find_1 = __importDefault(require("lodash/find"));
55var omit_1 = __importDefault(require("lodash/omit"));
56var cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
57var REQUEST_DEFAULTS = {
58 types: ["attribute", "metric", "fact"],
59 paging: {
60 offset: 0,
61 },
62};
63var LOAD_DATE_DATASET_DEFAULTS = {
64 includeUnavailableDateDataSetsCount: true,
65 includeAvailableDateAttributes: true,
66};
67/**
68 * Convert specific params in options to "requiredDataSets" structure. For more details look into
69 * res file https://github.com/gooddata/gdc-bear/blob/develop/resources/specification/internal/catalog.res
70 *
71 * @param options Supported keys in options are:
72 * <ul>
73 * <li>dataSetIdentifier - in value is string identifier of dataSet - this leads to CUSTOM type
74 * <li>returnAllDateDataSets - true value means to return ALL values without dataSet differentiation
75 * <li>returnAllRelatedDateDataSets - only related date dataSets are loaded across all dataSets
76 * <li>by default we get PRODUCTION dataSets
77 * </ul>
78 * @returns {Object} "requiredDataSets" object hash.
79 */
80var getRequiredDataSets = function (options) {
81 if (get_1.default(options, "returnAllRelatedDateDataSets")) {
82 return {};
83 }
84 if (get_1.default(options, "returnAllDateDataSets")) {
85 return { requiredDataSets: { type: "ALL" } };
86 }
87 if (get_1.default(options, "dataSetIdentifier")) {
88 return {
89 requiredDataSets: {
90 type: "CUSTOM",
91 customIdentifiers: [get_1.default(options, "dataSetIdentifier")],
92 },
93 };
94 }
95 return { requiredDataSets: { type: "PRODUCTION" } };
96};
97var buildItemDescriptionObjects = function (_a) {
98 var columns = _a.columns, definitions = _a.definitions;
99 if (!columns) {
100 return [];
101 }
102 return columns.map(function (column) {
103 var definition = find_1.default(definitions, function (_a) {
104 var metricDefinition = _a.metricDefinition;
105 return metricDefinition.identifier === column;
106 });
107 var maql = get_1.default(definition, "metricDefinition.expression");
108 if (maql) {
109 return { expression: maql };
110 }
111 return { uri: column };
112 });
113};
114var isStoredItemDescription = function (itemDescription) {
115 return !!itemDescription.uri;
116};
117var isAdHocItemDescription = function (itemDescription) {
118 return !!itemDescription.expression;
119};
120var unwrapItemDescriptionObject = function (itemDescription) {
121 if (isStoredItemDescription(itemDescription)) {
122 return itemDescription.uri;
123 }
124 if (isAdHocItemDescription(itemDescription)) {
125 return itemDescription.expression;
126 }
127 throw new Error("Item description can only have expression or uri");
128};
129var CatalogueModule = /** @class */ (function () {
130 function CatalogueModule(xhr, execution) {
131 this.xhr = xhr;
132 this.execution = execution;
133 }
134 CatalogueModule.prototype.loadItems = function (projectId, options) {
135 var _this = this;
136 if (options === void 0) { options = {}; }
137 var request = omit_1.default(__assign({}, REQUEST_DEFAULTS, options, getRequiredDataSets(options)), ["dataSetIdentifier", "returnAllDateDataSets", "attributesMap"]);
138 var mdObj = get_1.default(cloneDeep_1.default(options), "bucketItems");
139 var attributesMap = get_1.default(options, "attributesMap");
140 var hasBuckets = get_1.default(mdObj, "buckets") !== undefined;
141 if (hasBuckets) {
142 return this.loadItemDescriptions(projectId, mdObj, attributesMap).then(function (bucketItems) {
143 return _this.loadCatalog(projectId, __assign({}, request, { bucketItems: bucketItems }));
144 });
145 }
146 return this.loadCatalog(projectId, request);
147 };
148 CatalogueModule.prototype.loadDateDataSets = function (projectId, options) {
149 return __awaiter(this, void 0, void 0, function () {
150 var mdObj, bucketItems, _a, omittedOptions, request;
151 return __generator(this, function (_b) {
152 switch (_b.label) {
153 case 0:
154 mdObj = cloneDeep_1.default(options).bucketItems;
155 if (!mdObj) return [3 /*break*/, 2];
156 return [4 /*yield*/, this.loadItemDescriptions(projectId, mdObj, get_1.default(options, "attributesMap"), true)];
157 case 1:
158 _a = _b.sent();
159 return [3 /*break*/, 3];
160 case 2:
161 _a = undefined;
162 _b.label = 3;
163 case 3:
164 bucketItems = _a;
165 omittedOptions = [
166 "filter",
167 "types",
168 "paging",
169 "dataSetIdentifier",
170 "returnAllDateDataSets",
171 "returnAllRelatedDateDataSets",
172 "attributesMap",
173 ];
174 // includeObjectsWithTags has higher priority than excludeObjectsWithTags,
175 // so when present omit excludeObjectsWithTags
176 if (options.includeObjectsWithTags) {
177 omittedOptions.push("excludeObjectsWithTags");
178 }
179 request = omit_1.default(__assign({}, LOAD_DATE_DATASET_DEFAULTS, REQUEST_DEFAULTS, options, getRequiredDataSets(options), { bucketItems: bucketItems }), omittedOptions);
180 return [2 /*return*/, this.requestDateDataSets(projectId, request)];
181 }
182 });
183 });
184 };
185 /**
186 * Loads item description objects and returns them
187 *
188 * @internal
189 * @private
190 *
191 * @param projectId {string}
192 * @param mdObj metadata object containing buckets, visualization class, properties etc.
193 * @param attributesMap contains map of attributes where the keys are the attributes display forms URIs
194 * @param removeDateItems {boolean} skip date items
195 * @return ItemDescription which is either `{ uri: string }` or `{ expression: string }`
196 */
197 CatalogueModule.prototype.loadItemDescriptionObjects = function (projectId, mdObj, attributesMap, removeDateItems) {
198 if (removeDateItems === void 0) { removeDateItems = false; }
199 return __awaiter(this, void 0, void 0, function () {
200 var definitionsAndColumns;
201 return __generator(this, function (_a) {
202 switch (_a.label) {
203 case 0: return [4 /*yield*/, this.execution.mdToExecutionDefinitionsAndColumns(projectId, mdObj, { attributesMap: attributesMap, removeDateItems: removeDateItems })];
204 case 1:
205 definitionsAndColumns = _a.sent();
206 return [2 /*return*/, buildItemDescriptionObjects(definitionsAndColumns)];
207 }
208 });
209 });
210 };
211 /**
212 * ItemDescription is either URI or MAQL expression
213 * https://github.com/gooddata/gdc-bear/blob/185.4/resources/specification/md/obj.res#L284
214 *
215 * @param projectId {string}
216 * @param mdObj metadata object containing buckets, visualization class, properties etc.
217 * @param attributesMap contains map of attributes where the keys are the attributes display forms URIs
218 * @param removeDateItems {boolean} skip date items
219 * @deprecated
220 */
221 CatalogueModule.prototype.loadItemDescriptions = function (projectId, mdObj, attributesMap, removeDateItems) {
222 if (removeDateItems === void 0) { removeDateItems = false; }
223 return __awaiter(this, void 0, void 0, function () {
224 var itemDescriptions;
225 return __generator(this, function (_a) {
226 switch (_a.label) {
227 case 0: return [4 /*yield*/, this.loadItemDescriptionObjects(projectId, mdObj, attributesMap, removeDateItems)];
228 case 1:
229 itemDescriptions = _a.sent();
230 return [2 /*return*/, itemDescriptions.map(unwrapItemDescriptionObject)];
231 }
232 });
233 });
234 };
235 CatalogueModule.prototype.requestDateDataSets = function (projectId, dateDataSetsRequest) {
236 var uri = "/gdc/internal/projects/" + projectId + "/loadDateDataSets";
237 return this.xhr
238 .post(uri, { data: { dateDataSetsRequest: dateDataSetsRequest } })
239 .then(function (r) { return r.getData(); })
240 .then(function (data) { return data.dateDataSetsResponse; });
241 };
242 CatalogueModule.prototype.loadCatalog = function (projectId, catalogRequest) {
243 var uri = "/gdc/internal/projects/" + projectId + "/loadCatalog";
244 return this.xhr
245 .post(uri, { data: { catalogRequest: catalogRequest } })
246 .then(function (r) { return r.getData(); })
247 .then(function (data) { return data.catalogResponse; });
248 };
249 return CatalogueModule;
250}());
251exports.CatalogueModule = CatalogueModule;