diff options
Diffstat (limited to 'sdnr/wt/odlux/framework/src/utilities/elasticSearch.ts')
-rw-r--r-- | sdnr/wt/odlux/framework/src/utilities/elasticSearch.ts | 63 |
1 files changed, 27 insertions, 36 deletions
diff --git a/sdnr/wt/odlux/framework/src/utilities/elasticSearch.ts b/sdnr/wt/odlux/framework/src/utilities/elasticSearch.ts index 54713f1b1..c18a40b0b 100644 --- a/sdnr/wt/odlux/framework/src/utilities/elasticSearch.ts +++ b/sdnr/wt/odlux/framework/src/utilities/elasticSearch.ts @@ -16,70 +16,61 @@ * ============LICENSE_END========================================================================== */ +import { Result } from '../models'; import { DataCallback } from '../components/material-table'; -import { Result, HitEntry } from '../models'; import { requestRest } from '../services/restService'; +import { convertPropertyNames, convertPropertyValues, replaceUpperCase, replaceHyphen } from './yangHelper'; + type propType = string | number | null | undefined | (string | number)[]; type dataType = { [prop: string]: propType }; -export function createSearchDataHandler<TResult extends {} = dataType>(uri: (() => string) | string, additionalParameters?: {}): DataCallback<(TResult & { _id: string })>; -export function createSearchDataHandler<TResult extends {} = dataType, TData = dataType>(uri: (() => string) | string, additionalParameters: {} | null | undefined, mapResult: (res: HitEntry<TResult>, index: number, arr: HitEntry<TResult>[]) => (TData & { _id: string }), mapRequest?: (name?: string | null) => string): DataCallback<(TData & { _id: string })> -export function createSearchDataHandler<TResult, TData>(uri: (() => string) | string, additionalParameters?: {} | null | undefined, mapResult?: (res: HitEntry<TResult>, index: number, arr: HitEntry<TResult>[]) => (TData & { _id: string }), mapRequest?: (name?: string | null) => string): DataCallback<(TData & { _id: string })> { - const fetchData: DataCallback<(TData & { _id: string })> = async (page, rowsPerPage, orderBy, order, filter) => { - const url = `${ window.location.origin }/database/${typeof uri === "function" ? uri(): uri}/_search`; - const from = rowsPerPage && page != null && !isNaN(+page) - ? (+page) * rowsPerPage - : null; +/** Represents a fabric for the searchDataHandler used by the internal data api. + * @param typeName The name of the entry type to create a searchDataHandler for. + * @param additionalFilters Filterproperties and their values to add permanently. + * @returns The searchDataHandler callback to be used with the material table. +*/ +export function createSearchDataHandler<TResult>(typeName: (() => string) | string, additionalFilters?: {} | null | undefined): DataCallback<(TResult)> { + const fetchData: DataCallback<(TResult)> = async (pageIndex, rowsPerPage, orderBy, order, filter) => { + const url = `/restconf/operations/data-provider:read-${typeof typeName === "function" ? typeName(): typeName}-list`; + + filter = { ...filter, ...additionalFilters }; const filterKeys = filter && Object.keys(filter) || []; const query = { - ...filterKeys.length > 0 ? { - query: { - bool: { - must: filterKeys.reduce((acc, cur) => { - if (acc && filter && filter[cur]) { - acc.push({ [filter[cur].indexOf("*") > -1 || filter[cur].indexOf("?") > -1 ? "wildcard" : "term"]: { [mapRequest ? mapRequest(cur) : cur]: filter[cur] } }); - } - return acc; - }, [] as any[]) - } - } - } : { "query": { "match_all": {} } }, - ...rowsPerPage ? { "size": rowsPerPage } : {}, - ...from ? { "from": from } : {}, - ...orderBy && order ? { "sort": [{ [mapRequest ? mapRequest(orderBy) : orderBy]: order }] } : {}, - ...additionalParameters ? additionalParameters : {} + input: { + filter: filterKeys.filter(f => filter![f] != null && filter![f] !== "").map(property => ({ property, filtervalue: filter![property]})), + sortorder: orderBy ? [{ property: orderBy, sortorder: order === "desc" ? "descending" : "ascending" }] : [], + pagination: { size: rowsPerPage, page: (pageIndex != null && pageIndex > 0 && pageIndex || 0) +1 } + } }; - const result = await requestRest<Result<TResult & { _id: string }>>(url, { + const result = await requestRest<Result<TResult>>(url, { method: "POST", // *GET, POST, PUT, DELETE, etc. - mode: "no-cors", // no-cors, cors, *same-origin + mode: "same-origin", // no-cors, cors, *same-origin cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached headers: { - "Content-Type": "application/json; charset=utf-8", + "Content-Type": "application/json", // "Content-Type": "application/x-www-form-urlencoded", }, - body: JSON.stringify(query), // body data type must match "Content-Type" header + body: JSON.stringify(convertPropertyValues(query, replaceUpperCase)), // body data type must match "Content-Type" header }); if (result) { - let rows: (TData & { _id: string })[] = []; + let rows: TResult[] = []; - if (result && result.hits && result.hits.hits) { - rows = result.hits.hits.map( mapResult ? mapResult : h => ( - { ...(h._source as any as TData), _id: h._id } - )) || [] + if (result && result.output && result.output.data) { + rows = result.output.data.map(obj => convertPropertyNames(obj, replaceHyphen)) || [] } const data = { - page: Math.min(page || 0, result.hits.total || 0 / (rowsPerPage || 1)), rowCount: result.hits.total, rows: rows + page: result.output.pagination && result.output.pagination.page != null && result.output.pagination.page - 1 || 0 , total: result.output.pagination && result.output.pagination.total || 0, rows: rows }; return data; } - return { page: 0, rowCount: 0, rows: [] }; + return { page: 1, total: 0, rows: [] }; }; return fetchData; |