UNPKG

@ultipa-graph/ultipa-node-sdk

Version:

NodeJS SDK for ultipa-server 4.0

1,012 lines (1,010 loc) 40.3 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FormatResponse = exports.FormatType = exports.mergeUqlResponse = void 0; const ultipa_pb_1 = require("../proto/ultipa_pb"); const types_1 = require("../types"); const lodash_1 = __importDefault(require("lodash")); const index_1 = require("../utils/index"); /** let a = "123" let aBuffer = new DataView(new ArrayBuffer(4)) aBuffer.setInt32(0, parseInt(a)) let aValue = new DataView(aBuffer.buffer).getInt32(aBuffer.byteOffset) console.log( a, aBuffer, aValue ) console.log(new ArrayBuffer(4)) let b = "!@3鼎折覆餗" let bBuffer = Buffer.from(b, 'utf8'); let bString = new TextDecoder("utf-8").decode(bBuffer); console.log({ b, bBuffer, bString }) */ let ULITPA_PRIVATE_KEYS = { id: "_id", from: "_from", to: "_to", uuid: "_uuid", from_uuid: "_from_uuid", to_uuid: "_to_uuid", }; const _mapValues = (values, types, timeZone) => { let result = {}; values.forEach((v, index) => { let pty = types[index]; result[pty.getPropertyName()] = index_1.DESERIALIZE.deserialize(v, pty.getPropertyType(), pty.getSubTypesList(), timeZone); }); // console.log(result) return result; }; const DataMerge = { arrayDiff: (arr1, arr2) => { let diff = []; arr2.forEach((s) => { if (!arr1.includes(s)) { diff.push(s); } }); return diff; }, merge: (o1, o2, ignoreKeys) => { if (!o2) { return o1; } function customizer(objValue, srcValue, key) { if (ignoreKeys && ignoreKeys.includes(key)) { return srcValue; } if (lodash_1.default.isArray(srcValue)) { return srcValue.concat(objValue); } } return lodash_1.default.mergeWith(o2, o1, customizer); }, concat: (arr1, arr2, byKey, ignoreKeys) => { if (!(arr2 === null || arr2 === void 0 ? void 0 : arr2.length)) { return arr1; } if (byKey) { let newArray = [...arr1]; let diff = DataMerge.arrayDiff(arr1.map((o) => o[byKey]), arr2.map((o) => o[byKey])); diff.forEach((k) => newArray.push({ [byKey]: k, })); let results = []; newArray.forEach((o) => { let newObj = o; let finds = arr2.filter((o2) => { return o2[byKey] == o[byKey]; }); finds.forEach((f) => { newObj = DataMerge.merge(newObj, f, ignoreKeys); }); results.push(newObj); }); return results; } return [...arr1, ...arr2]; }, }; const mergeUqlResponse = (res1, res2) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o; if (!(res2 === null || res2 === void 0 ? void 0 : res2.data)) { return res1; } // res1.data.results = DataMerge.concat(res1.data?.results, res2.data?.results); res1.data.paths = DataMerge.concat((_a = res1.data) === null || _a === void 0 ? void 0 : _a.paths, (_b = res2.data) === null || _b === void 0 ? void 0 : _b.paths, "alias"); res1.data.nodes = DataMerge.concat((_c = res1.data) === null || _c === void 0 ? void 0 : _c.nodes, (_d = res2.data) === null || _d === void 0 ? void 0 : _d.nodes, "alias"); res1.data.edges = DataMerge.concat((_e = res1.data) === null || _e === void 0 ? void 0 : _e.edges, (_f = res2.data) === null || _f === void 0 ? void 0 : _f.edges, "alias"); res1.data.attrs = DataMerge.concat((_g = res1.data) === null || _g === void 0 ? void 0 : _g.attrs, (_h = res2.data) === null || _h === void 0 ? void 0 : _h.attrs, "alias"); res1.data.tables = DataMerge.concat((_j = res1.data) === null || _j === void 0 ? void 0 : _j.tables, (_k = res2.data) === null || _k === void 0 ? void 0 : _k.tables, "name", ["headers"]); res1.data.graphs = DataMerge.concat((_l = res1.data) === null || _l === void 0 ? void 0 : _l.graphs, (_m = res2.data) === null || _m === void 0 ? void 0 : _m.graphs, "alias"); if ((_o = res2 === null || res2 === void 0 ? void 0 : res2.data) === null || _o === void 0 ? void 0 : _o.statistics) { res1.data.statistics = res2.data.statistics; } return res1; }; exports.mergeUqlResponse = mergeUqlResponse; exports.FormatType = { uqlResponse: (res, timeZone) => { var _a, _b; // console.log(JSON.stringify(res.toObject())) let response = new types_1.ULTIPA.UQLBaseResponse(); response.status = exports.FormatType.status(res.getStatus()); let statistics = (_b = (_a = exports.FormatType.table(res.getStatistics(), timeZone)) === null || _a === void 0 ? void 0 : _a.toKV()) === null || _b === void 0 ? void 0 : _b[0]; if (statistics) { statistics = { totalCost: +statistics.total_time_cost || 0, engineCost: +statistics.engine_time_cost || 0, nodeAffected: +statistics.node_affected || 0, edgeAffected: +statistics.edge_affected || 0, }; } let attrs = exports.FormatType.attrAliases(res.getAttrsList(), timeZone); let attrs_map = {}; let attrs_format = []; let addAttrMap = (alias, type, values, p) => { if (!attrs_map[alias]) { if (p.has_attr_data) { type = types_1.ULTIPA.ResultType.RESULT_TYPE_ATTR; } let v = { alias, type: type, type_desc: types_1.ULTIPA.ResultType[type], values: [], }; attrs_map[alias] = v; attrs_format.push(v); } if (type === types_1.ULTIPA.ResultType.RESULT_TYPE_ATTR && p.only_attr_list) { attrs_map[alias].values = values; } else { attrs_map[alias].values.push(values); } }; attrs === null || attrs === void 0 ? void 0 : attrs.forEach(attrAlias => { var _a; let alias = attrAlias.alias; let { has_attr_data, has_ultipa_data, } = attrAlias.attr; let is_unset = attrAlias.attr.type == types_1.ULTIPA.PropertyType.PROPERTY_UNSET; if (is_unset) { return; } let only_attr_list = has_attr_data && !has_ultipa_data; let p = { only_attr_list, has_attr_data, has_ultipa_data, }; if (attrAlias.attr.type == types_1.ULTIPA.PropertyType.PROPERTY_LIST || attrAlias.attr.type == types_1.ULTIPA.PropertyType.PROPERTY_SET) { if (only_attr_list) { addAttrMap(alias, types_1.ULTIPA.ResultType.RESULT_TYPE_ATTR, attrAlias.attr.values, p); return; } (_a = attrAlias.attr.values) === null || _a === void 0 ? void 0 : _a.forEach(o => { if (!o || !o.type) { addAttrMap(alias, types_1.ULTIPA.ResultType.RESULT_TYPE_ATTR, o, p); return; } switch (o.type) { case types_1.ULTIPA.ResultType.RESULT_TYPE_EDGE: { addAttrMap(alias, o.type, o.edges, p); } break; case types_1.ULTIPA.ResultType.RESULT_TYPE_NODE: { addAttrMap(alias, o.type, o.nodes, p); } break; case types_1.ULTIPA.ResultType.RESULT_TYPE_PATH: { addAttrMap(alias, o.type, o.paths, p); } break; } }); } else { addAttrMap(alias, types_1.ULTIPA.ResultType.RESULT_TYPE_ATTR, attrAlias.attr.values, p); } }); response.data = { total_time_cost: res.getTotalTimeCost(), engine_time_cost: res.getEngineTimeCost(), results: exports.FormatType.resultAliases(res.getAliasList()), paths: exports.FormatType.pathAliases(res.getPathsList(), timeZone), nodes: exports.FormatType.nodeAliases(res.getNodesList(), timeZone), edges: exports.FormatType.edgeAliases(res.getEdgesList(), timeZone), attrs: attrs_format, tables: exports.FormatType.tables(res.getTablesList(), timeZone), graphs: exports.FormatType.graphAliases(res.getGraphsList(), timeZone), statistics, explainPlans: exports.FormatType.explainPlan(res.getExplainPlan()), }; response.statistics = response.data.statistics; response.explainPlans = response.data.explainPlans; return response; }, status: (statusData, p) => { let status = { code: types_1.ULTIPA.Code.SUCCESS, message: "", }; if (statusData) { status.code = statusData.getErrorCode(); status.message = statusData.getMsg(); let clusterInfo = statusData.getClusterInfo(); let isNotRaftMode = status.code == types_1.ULTIPA.Code.NOT_RAFT_MODE; if (clusterInfo || isNotRaftMode) { let newRaftPeers = []; if (status.code == types_1.ULTIPA.Code.SUCCESS || isNotRaftMode) { newRaftPeers.push({ host: (clusterInfo === null || clusterInfo === void 0 ? void 0 : clusterInfo.getLeaderAddress()) || (p === null || p === void 0 ? void 0 : p.host), status: true, isLeader: true, isAlgoExecutable: isNotRaftMode ? true : false, isFollowerReadable: false, isUnset: false, }); } for (const item of (clusterInfo === null || clusterInfo === void 0 ? void 0 : clusterInfo.getFollowersList()) || []) { let role = item.getRole(); let status = item.getStatus() === 1; let isAlgoExecutable = false; let isFollowerReadable = false; if (status) { isAlgoExecutable = role & types_1.ULTIPA.RaftFollowerRole.ROLE_ALGO_EXECUTABLE ? true : false; isFollowerReadable = role & types_1.ULTIPA.RaftFollowerRole.ROLE_READABLE ? true : false; } newRaftPeers.push({ host: item.getAddress(), status, isLeader: false, isAlgoExecutable, isFollowerReadable, isUnset: role === types_1.ULTIPA.RaftFollowerRole.ROLE_UNSET }); } status.clusterInfo = { redirect: clusterInfo === null || clusterInfo === void 0 ? void 0 : clusterInfo.getRedirect(), raftPeers: newRaftPeers }; } } status.code_desc = types_1.ULTIPA.Code[status.code]; return status; }, keyValue: (keyValuesData) => { if (!keyValuesData || keyValuesData.length == 0) { return {}; } let keyValue = {}; keyValuesData.forEach((kv) => { keyValue[kv.getKey()] = kv.getValue(); }); return keyValue; }, graph: (graph, timeZone) => { let newGraph = new types_1.ULTIPA.Graph(); newGraph.nodes = exports.FormatType.nodeTable(graph.getNodeTable(), timeZone); newGraph.edges = exports.FormatType.edgeTable(graph.getEdgeTable(), timeZone); return newGraph; }, paths: (paths, timeZone) => { let newPaths = []; paths.forEach(o => { let p = new types_1.ULTIPA.Path(); p.nodes = exports.FormatType.nodeTable(o.getNodeTable(), timeZone); p.edges = exports.FormatType.edgeTable(o.getEdgeTable(), timeZone); p.length = p.edges.length; newPaths.push(p); }); return newPaths; }, pathAliases: (pathsData, timeZone) => { if (!pathsData || pathsData.length == 0) { return []; } let pathAliases = []; pathsData.forEach((pathData) => { let pathAlias = { alias: pathData.getAlias(), paths: exports.FormatType.paths(pathData.getPathsList(), timeZone) }; pathAliases.push(pathAlias); }); return pathAliases; }, graphAliases: (graphDatas, timeZone) => { if (!graphDatas || graphDatas.length == 0) { return []; } let graphAliases = []; graphDatas.forEach((data) => { let alias = { alias: data.getAlias(), graph: exports.FormatType.graph(data.getGraph(), timeZone) }; graphAliases.push(alias); }); return graphAliases; }, explainPlan: (data) => { if (!data) { return null; } let explainPlan = { plan_nodes: [] }; let planNodes = []; data.getPlanNodesList().forEach(plan => { let one = { alias: plan.getAlias(), children_num: plan.getChildrenNum(), infos: plan.getInfos(), uql: plan.getUql(), }; planNodes.push(one); }); explainPlan.plan_nodes = planNodes; return explainPlan; }, table: (tableData, timeZone) => { if (!tableData) { return null; } let tableRows = []; let tableHeaders = []; tableData.getHeadersList().forEach(h => { tableHeaders.push({ name: h.getPropertyName(), type: h.getPropertyType(), }); }); tableData.getTableRowsList().forEach((tableRowData) => { let values = []; tableRowData.getValuesList_asU8().forEach((v, i) => { let header = tableHeaders[i]; values.push(index_1.DESERIALIZE.deserialize(v, header.type, header.subTypes, timeZone)); }); tableRows.push(values); }); let t = new types_1.ULTIPA.Table(); t.name = tableData.getTableName(); t.alias = t.name; t.headers = tableHeaders.map(o => o.name); t.rows = tableRows; return t; }, tables: (tablesData, timeZone) => { if (!tablesData || tablesData.length == 0) { return []; } let tables = []; tablesData.forEach((tableData) => { tables.push(exports.FormatType.table(tableData, timeZone)); }); return tables; }, resultAliases: (resultAliases) => { if (!resultAliases || resultAliases.length == 0) { return []; } let rs = []; resultAliases.forEach(one => { rs.push({ alias: one.getAlias(), result_type: one.getResultType() }); }); return rs; }, // arrayAliases: (arrayAliases: ArrayAlias[], timeZone: ULTIPA.TimeZone) => { // if (!arrayAliases || arrayAliases.length == 0) { // return []; // } // let rs :ULTIPA.ArrayAliases = [] // arrayAliases.forEach(one => { // let eles = [] // let type = one.getPropertyType() // one.getElementsList().map(o => { // let rs = [] // o.getValuesList_asU8().map(o1 => { // rs.push(DESERIALIZE.base(o1, type, timeZone)) // }) // eles.push(rs) // }) // rs.push({ // alias: one.getAlias(), // elements: eles, // }) // }) // return rs // }, nodeAliases: (nodesListData, timeZone) => { if (!nodesListData || nodesListData.length == 0) { return []; } let nodeAliases = []; nodesListData.forEach((nodeData) => { nodeAliases.push({ alias: nodeData.getAlias(), nodes: exports.FormatType.nodeTable(nodeData.getNodeTable(), timeZone), }); }); return nodeAliases; }, edgeAliases: (edgesListData, timeZone) => { if (!edgesListData || edgesListData.length == 0) { return []; } let nodeAliases = []; edgesListData.forEach((nodeData) => { nodeAliases.push({ alias: nodeData.getAlias(), edges: exports.FormatType.edgeTable(nodeData.getEdgeTable(), timeZone), }); }); return nodeAliases; }, toEdgeTable: (propertyTypeObject, schemaName, headers, rows, timeZone) => { let edgeTable = new ultipa_pb_1.EntityTable(); let handles = [ULITPA_PRIVATE_KEYS.uuid, ULITPA_PRIVATE_KEYS.from, ULITPA_PRIVATE_KEYS.to, ULITPA_PRIVATE_KEYS.from_uuid, ULITPA_PRIVATE_KEYS.to_uuid]; let schemaHeaders = []; let schemaHeader = new ultipa_pb_1.Schema(); schemaHeader.setSchemaName(schemaName); let headerList = []; headers.forEach((h) => { if (handles.includes(h)) { return; } let header = new ultipa_pb_1.Property(); header.setPropertyName(h); let typeCfg = index_1.PropertyUtils.GetPropertyType(propertyTypeObject[h]); header.setPropertyType(typeCfg.type); if (typeCfg.subTypes) { header.setSubTypesList(typeCfg.subTypes); } headerList.push(header); }); schemaHeader.setPropertiesList(headerList); schemaHeaders.push(schemaHeader); edgeTable.setSchemasList(schemaHeaders); let edgeRowsList = []; rows.forEach(rowsObj => { let edgeRow = new ultipa_pb_1.EntityRow(); if (!rowsObj) { edgeRow.setIsNull(true); edgeRowsList.push(edgeRow); return; } edgeRow.setSchemaName(schemaName); let rowValues = rowsObj; let values = []; headers.forEach((h, index) => { if (h === ULITPA_PRIVATE_KEYS.from) { edgeRow.setFromId(rowValues[index]); return; } if (h === ULITPA_PRIVATE_KEYS.to) { edgeRow.setToId(rowValues[index]); return; } if (h === ULITPA_PRIVATE_KEYS.uuid) { let uuid = rowValues[index]; if (uuid) { uuid += ""; } edgeRow.setUuid(uuid); return; } if (h === ULITPA_PRIVATE_KEYS.from_uuid) { edgeRow.setFromUuid(rowValues[index]); return; } if (h === ULITPA_PRIVATE_KEYS.to_uuid) { edgeRow.setToUuid(rowValues[index]); return; } let typeCfg = index_1.PropertyUtils.GetPropertyType(propertyTypeObject[h]); values.push(index_1.SERIALIZE.serialize(rowValues[index], typeCfg.type, typeCfg.subTypes, timeZone)); }); edgeRow.setValuesList(values); edgeRowsList.push(edgeRow); }); edgeTable.setEntityRowsList(edgeRowsList); return edgeTable; }, toPropertyTypeObject: (properties) => { let object = {}; properties.forEach(p => { object[p.name] = p.type; }); return object; }, toNodeTable: (propertyTypeObject, schemaName, headers, rows, timeZone) => { let nodeTable = new ultipa_pb_1.EntityTable(); let handles = [ULITPA_PRIVATE_KEYS.id, ULITPA_PRIVATE_KEYS.uuid]; let schemaHeaders = []; let schemaHeader = new ultipa_pb_1.Schema(); schemaHeader.setSchemaName(schemaName); let headerList = []; headers.forEach((h) => { if (handles.includes(h)) { return; } let header = new ultipa_pb_1.Property(); header.setPropertyName(h); let typeCfg = index_1.PropertyUtils.GetPropertyType(propertyTypeObject[h]); header.setPropertyType(typeCfg.type); if (typeCfg.subTypes) { header.setSubTypesList(typeCfg.subTypes); } headerList.push(header); }); schemaHeader.setPropertiesList(headerList); schemaHeaders.push(schemaHeader); nodeTable.setSchemasList(schemaHeaders); let nodeRowsList = []; rows.forEach(rowObj => { let nodeRow = new ultipa_pb_1.EntityRow(); if (!rowObj) { nodeRow.setIsNull(true); nodeRowsList.push(nodeRow); return; } nodeRow.setSchemaName(schemaName); let values = []; let rowValues = rowObj; headers.forEach((h, index) => { if (h === ULITPA_PRIVATE_KEYS.id) { nodeRow.setId(rowValues[index]); return; } if (h === ULITPA_PRIVATE_KEYS.uuid) { let uuid = rowValues[index]; if (uuid) { uuid += ""; } nodeRow.setUuid(uuid); return; } let typeCfg = index_1.PropertyUtils.GetPropertyType(propertyTypeObject[h]); values.push(index_1.SERIALIZE.serialize(rowValues[index], typeCfg.type, typeCfg.subTypes, timeZone)); }); nodeRow.setValuesList(values); nodeRowsList.push(nodeRow); }); nodeTable.setEntityRowsList(nodeRowsList); return nodeTable; }, toEdgeTableWithSchema: (schema, rows, timeZone) => { let table = new ultipa_pb_1.EntityTable(); let handles = [ULITPA_PRIVATE_KEYS.uuid, ULITPA_PRIVATE_KEYS.from, ULITPA_PRIVATE_KEYS.to, ULITPA_PRIVATE_KEYS.from_uuid, ULITPA_PRIVATE_KEYS.to_uuid]; let schemaHeaders = []; let schemaHeader = new ultipa_pb_1.Schema(); schemaHeader.setSchemaName(schema.name); let headerList = []; schema.properties.forEach((o) => { let { name, type, subTypes } = o; if (handles.includes(name)) { return; } let header = new ultipa_pb_1.Property(); header.setPropertyName(name); header.setPropertyType(type); if (subTypes) { header.setSubTypesList(subTypes); } headerList.push(header); }); schemaHeader.setPropertiesList(headerList); schemaHeaders.push(schemaHeader); table.setSchemasList(schemaHeaders); let rowList = []; rows.forEach(rowObj => { let row = new ultipa_pb_1.EntityRow(); if (!rowObj) { row.setIsNull(true); rowList.push(row); return; } row.setSchemaName(schema.name); if (!!rowObj.uuid) { row.setUuid(rowObj.uuid); } if (!!rowObj.from) { row.setFromId(rowObj.from); } if (!!rowObj.to) { row.setToId(rowObj.to); } if (!!rowObj.from_uuid) { row.setFromUuid(rowObj.from_uuid); } if (!!rowObj.to_uuid) { row.setToUuid(rowObj.to_uuid); } let values = []; let rowValues = rowObj.values || {}; headerList.forEach((o, index) => { let name = o.getPropertyName(); let type = o.getPropertyType(); let subTypes = o.getSubTypesList(); values.push(index_1.SERIALIZE.serialize(rowValues[name], type, subTypes, timeZone)); }); row.setValuesList(values); rowList.push(row); }); table.setEntityRowsList(rowList); return table; }, toNodeTableWithSchema: (schema, rows, timeZone) => { let nodeTable = new ultipa_pb_1.EntityTable(); let handles = [ULITPA_PRIVATE_KEYS.id, ULITPA_PRIVATE_KEYS.uuid]; let schemaHeaders = []; let schemaHeader = new ultipa_pb_1.Schema(); schemaHeader.setSchemaName(schema.name); let headerList = []; schema.properties.forEach((o) => { let { name, type, subTypes } = o; if (handles.includes(name)) { return; } let header = new ultipa_pb_1.Property(); header.setPropertyName(name); header.setPropertyType(type); if (subTypes) { header.setSubTypesList(subTypes); } headerList.push(header); }); schemaHeader.setPropertiesList(headerList); schemaHeaders.push(schemaHeader); nodeTable.setSchemasList(schemaHeaders); let nodeRowsList = []; rows.forEach(rowObj => { let nodeRow = new ultipa_pb_1.EntityRow(); if (!rowObj) { nodeRow.setIsNull(true); nodeRowsList.push(nodeRow); return; } if (!!rowObj.id) { nodeRow.setId(rowObj.id); } if (!!rowObj.uuid) { nodeRow.setUuid(rowObj.uuid); } nodeRow.setSchemaName(schema.name); let values = []; let rowValues = rowObj.values || {}; headerList.forEach((o, index) => { let name = o.getPropertyName(); let type = o.getPropertyType(); let subTypes = o.getSubTypesList(); values.push(index_1.SERIALIZE.serialize(rowValues[name], type, subTypes, timeZone)); }); nodeRow.setValuesList(values); nodeRowsList.push(nodeRow); }); nodeTable.setEntityRowsList(nodeRowsList); return nodeTable; }, nodeTable: (nodeTableData, timeZone) => { let tables = []; if (!nodeTableData) { return tables; } let headerMap = {}; nodeTableData.getSchemasList().forEach((sh) => { let schema = sh.getSchemaName(); headerMap[schema] = sh.getPropertiesList(); }); let nodeRowsData = nodeTableData.getEntityRowsList(); nodeRowsData.forEach((nodeRowData) => { if (nodeRowData.getIsNull()) { tables.push(null); return; } let schema = nodeRowData.getSchemaName(); let ptys = headerMap[schema]; let values = nodeRowData.getValuesList_asU8(); let _v = _mapValues(values, ptys, timeZone); let nodeRow = new types_1.ULTIPA.Node(); nodeRow.id = nodeRowData.getId(), nodeRow.uuid = nodeRowData.getUuid(), nodeRow.schema = schema, nodeRow.values = _v, tables.push(nodeRow); }); return tables; }, edgeTable: (edgeTableData, timeZone) => { let tables = []; if (!edgeTableData) { return tables; } let headerMap = {}; edgeTableData.getSchemasList().forEach((sh) => { let schema = sh.getSchemaName(); headerMap[schema] = sh.getPropertiesList(); }); edgeTableData.getSchemasList().forEach((sh) => { let schema = sh.getSchemaName(); headerMap[schema] = sh.getPropertiesList(); }); let edgeRowsData = edgeTableData.getEntityRowsList(); edgeRowsData.forEach((rowData) => { if (rowData.getIsNull()) { tables.push(null); return; } let schema = rowData.getSchemaName(); let ptys = headerMap[schema]; let values = rowData.getValuesList_asU8(); let _v = _mapValues(values, ptys, timeZone); let edgeRow = new types_1.ULTIPA.Edge(); // id: rowData.getId(), edgeRow.from = rowData.getFromId(); edgeRow.to = rowData.getToId(); edgeRow.uuid = rowData.getUuid(); edgeRow.from_uuid = rowData.getFromUuid(); edgeRow.to_uuid = rowData.getToUuid(); edgeRow.schema = schema; edgeRow.values = _v; tables.push(edgeRow); }); return tables; }, attr(attr, timeZone) { let type = attr.getValueType(); let result = { type, type_desc: index_1.PropertyUtils.propertyGet(type), values: null, has_attr_data: false, has_ultipa_data: false, }; result.values = []; attr.getValuesList_asU8().forEach((v) => { if (type == types_1.ULTIPA.PropertyType.PROPERTY_NULL) { result.has_attr_data = true; return result.values.push(null); } if (type == types_1.ULTIPA.PropertyType.PROPERTY_LIST || type == types_1.ULTIPA.PropertyType.PROPERTY_SET) { let attrListData = ultipa_pb_1.AttrListData.deserializeBinary(v); if (attrListData.getIsNull()) { return result.values.push(null); } let result_type = attrListData.getType(); switch (result_type) { case ultipa_pb_1.ResultType.RESULT_TYPE_NODE: { let v = { type: result_type, type_desc: types_1.ULTIPA.ResultType[result_type], nodes: exports.FormatType.nodeTable(attrListData.getNodes(), timeZone) }; result.has_ultipa_data = true; result.values.push(v); } break; case ultipa_pb_1.ResultType.RESULT_TYPE_EDGE: { let v = { type: result_type, type_desc: types_1.ULTIPA.ResultType[result_type], edges: exports.FormatType.edgeTable(attrListData.getEdges(), timeZone) }; result.has_ultipa_data = true; result.values.push(v); } break; case ultipa_pb_1.ResultType.RESULT_TYPE_PATH: { let v = { type: result_type, type_desc: types_1.ULTIPA.ResultType[result_type], paths: exports.FormatType.paths(attrListData.getPathsList(), timeZone) }; result.has_ultipa_data = true; result.values.push(v); } break; case ultipa_pb_1.ResultType.RESULT_TYPE_ATTR: { { let attrs = []; attrListData.getAttrsList().forEach(o => { let attr = exports.FormatType.attr(o, timeZone); if (!attr.values) { attrs.push(attr.values); } else { attrs.push(...attr.values); } }); // let v: ULTIPA.AttrListData = { // type: result_type, // type_desc: ULTIPA.ResultType[result_type], // attrs: attrs, // } if (attrs.length != 0) { result.has_attr_data = true; } result.values.push(attrs); } break; } } } else { let v1 = index_1.DESERIALIZE.base(v, type, timeZone); result.has_attr_data = true; result.values.push(v1); } }); return result; }, attrAliases: (attrAliasesData, timeZone) => { if (!attrAliasesData || attrAliasesData.length == 0) { return []; } let attrAliases = []; attrAliasesData.forEach((attrAliasData) => { let v = exports.FormatType.attr(attrAliasData.getAttr(), timeZone); attrAliases.push({ alias: attrAliasData.getAlias(), attr: v, }); }); return attrAliases; }, // graphPrivileges(graphPrivileges: object[]) { // let new_graphPrivileges = []; // if (graphPrivileges) { // graphPrivileges.forEach((graphPrivilege) => { // let new_graphPrivilege = { ...graphPrivilege }; // Object.keys(new_graphPrivilege).forEach((key) => { // new_graphPrivilege["name"] = key; // new_graphPrivilege["values"] = new_graphPrivilege[key]; // delete new_graphPrivilege[key]; // }); // new_graphPrivileges.push(new_graphPrivilege); // }); // } // return new_graphPrivileges; // }, }; exports.FormatResponse = { tableToArray: (table) => { let res = []; if (table) { try { table.rows.forEach((row) => { let item = {}; row.forEach((value, index) => { let header = table.headers[index]; item[header] = value; }); res.push(item); }); } catch (error) { console.log(error); } } return res; }, formatObjects: (objs, keysFormat) => { if (!keysFormat) { return objs; } let newObjs = []; if ((objs === null || objs === void 0 ? void 0 : objs.length) > 0) { objs.forEach((obj) => { newObjs.push(exports.FormatResponse.formatObject(obj, keysFormat)); }); } return newObjs; }, formatObject: (_obj, keysFormat) => { if (!keysFormat) { return _obj; } let obj = Object.assign({}, _obj); if (keysFormat === null || keysFormat === void 0 ? void 0 : keysFormat.jsonKeys) { keysFormat.jsonKeys.forEach((k) => { try { if (obj[k]) { obj[k] = JSON.parse(obj[k]); } } catch (error) { console.log(error, k, obj); } }); } if (keysFormat === null || keysFormat === void 0 ? void 0 : keysFormat.boolKeys) { keysFormat.boolKeys.forEach((k) => { try { if (obj[k]) { obj[k] = obj[k] == "true"; } } catch (error) { console.log(error, k, obj); } }); } if (keysFormat === null || keysFormat === void 0 ? void 0 : keysFormat.keyReplace) { Object.keys(keysFormat.keyReplace).forEach((k) => { if (obj[k]) { obj[keysFormat.keyReplace[k]] = obj[k]; delete obj[k]; } }); } if (keysFormat === null || keysFormat === void 0 ? void 0 : keysFormat.dataFormat) { obj = keysFormat.dataFormat(obj); } return obj; }, // formatData: (objs :object[] | object, keysFormat?: ResponseKeyFormat) => { // if (objs && keysFormat) { // let array = Array.isArray(objs) ? objs : [objs] // array.forEach(obj => { // if (keysFormat.jsonKeys) { // keysFormat.jsonKeys.forEach(k => { // try { // if (obj[k]) { // obj[k] = JSON.parse(obj[k]) // } // } catch (error) { // console.log(error, k, obj) // } // }) // } // if (keysFormat.boolKeys) { // keysFormat.boolKeys.forEach(k => { // try { // if (obj[k]) { // obj[k] = obj[k] == "true" // } // } catch (error) { // console.log(error, k, obj) // } // }) // } // if (keysFormat.keyReplace) { // Object.keys(keysFormat.keyReplace).forEach(k => { // if (obj[k]) { // obj[keysFormat.keyReplace[k]] = obj[k] // delete obj[k] // } // }) // } // if (keysFormat.dataFormat) { // keysFormat.dataFormat(obj) // } // }) // } // }, successStatus: () => { return { code: types_1.ULTIPA.Code.SUCCESS, code_desc: types_1.ULTIPA.Code[types_1.ULTIPA.Code.SUCCESS], message: "ok", }; }, statisticsSum: (one, sumOne) => { return { totalCost: ((one === null || one === void 0 ? void 0 : one.totalCost) || 0) + ((sumOne === null || sumOne === void 0 ? void 0 : sumOne.totalCost) || 0), engineCost: ((one === null || one === void 0 ? void 0 : one.engineCost) || 0) + ((sumOne === null || sumOne === void 0 ? void 0 : sumOne.engineCost) || 0), nodeAffected: ((one === null || one === void 0 ? void 0 : one.nodeAffected) || 0) + ((sumOne === null || sumOne === void 0 ? void 0 : sumOne.nodeAffected) || 0), edgeAffected: ((one === null || one === void 0 ? void 0 : one.edgeAffected) || 0) + ((sumOne === null || sumOne === void 0 ? void 0 : sumOne.edgeAffected) || 0), }; }, unknownError: (err, res) => { res.status = { code: types_1.ULTIPA.Code.UNKNOWN_ERROR, code_desc: types_1.ULTIPA.Code[types_1.ULTIPA.Code.UNKNOWN_ERROR], message: err.message || err.detail, }; return res; }, catchUltipaUqlError: (error) => { let response = new types_1.ULTIPA.UQLBaseResponse(); response = exports.FormatResponse.unknownError(error, response); let res = types_1.ULTIPA.formatBaseResponse(response); return res; }, }; //# sourceMappingURL=format.js.map