-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add maps metrics api from saved object
Signed-off-by: Junqiu Lei <junqiu@amazon.com>
- Loading branch information
1 parent
2259b6f
commit b838c1a
Showing
7 changed files
with
150 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { SavedObjectsFindResponse } from '../../../../../src/core/server'; | ||
import { MapLayerSpecification } from '../../../public/model/mapLayerType'; | ||
import { MapSavedObjectAttributes } from '../../../common/map_saved_object_attributes'; | ||
import { MAPS_INFO, MAPS_METRICS } from './type'; | ||
import { DASHBOARDS_MAPS_LAYER_TYPE } from '../../../common'; | ||
|
||
export const getMetrics = ( | ||
mapsSavedObjects: SavedObjectsFindResponse<MapSavedObjectAttributes> | ||
) => { | ||
let totalLayersCount = 0; | ||
let totalDocumentsLayersCount = 0; | ||
let totalOpensearchBaseLayersCount = 0; | ||
let totalCustomTmsLayersCount = 0; | ||
let totalCustomWmsLayersCount = 0; | ||
const mapsInfo: MAPS_INFO[] = []; | ||
mapsSavedObjects.saved_objects.forEach((mapRes) => { | ||
let documentsLayersCount = 0; | ||
let opensearchBaseLayersCount = 0; | ||
let customTmsLayersCount = 0; | ||
let customWmsLayersCount = 0; | ||
const layerList: MapLayerSpecification[] = JSON.parse(mapRes.attributes.layerList as string); | ||
const layersCount = layerList.length; | ||
totalLayersCount += layerList.length; | ||
|
||
layerList.forEach((layer) => { | ||
switch (layer.type) { | ||
case DASHBOARDS_MAPS_LAYER_TYPE.DOCUMENTS: | ||
totalDocumentsLayersCount++; | ||
documentsLayersCount++; | ||
break; | ||
case DASHBOARDS_MAPS_LAYER_TYPE.OPENSEARCH_MAP: | ||
totalOpensearchBaseLayersCount++; | ||
opensearchBaseLayersCount++; | ||
break; | ||
case DASHBOARDS_MAPS_LAYER_TYPE.CUSTOM_MAP: | ||
if (layer.source.customType === 'wms') { | ||
totalCustomWmsLayersCount++; | ||
customWmsLayersCount++; | ||
} else { | ||
totalCustomTmsLayersCount++; | ||
customTmsLayersCount++; | ||
} | ||
break; | ||
} | ||
}); | ||
|
||
mapsInfo.push({ | ||
id: mapRes.id, | ||
title: mapRes.attributes.title, | ||
layers_total_count: layersCount, | ||
documents_layers_total_count: documentsLayersCount, | ||
custom_tms_layers_total_count: customTmsLayersCount, | ||
custom_wms_layers_total_count: customWmsLayersCount, | ||
opensearch_base_map_layers_total_count: opensearchBaseLayersCount, | ||
}); | ||
}); | ||
|
||
const metrics: MAPS_METRICS = { | ||
maps_total_count: mapsSavedObjects.total, | ||
layers_total_count: totalLayersCount, | ||
documents_layers_total_count: totalDocumentsLayersCount, | ||
custom_tms_layers_total_count: totalCustomTmsLayersCount, | ||
custom_wms_layers_total_count: totalCustomWmsLayersCount, | ||
opensearch_base_map_layers_total_count: totalOpensearchBaseLayersCount, | ||
maps_info: mapsInfo, | ||
}; | ||
return metrics; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export interface MAPS_METRICS { | ||
maps_total_count: number; | ||
layers_total_count: number; | ||
documents_layers_total_count: number; | ||
opensearch_base_map_layers_total_count: number; | ||
custom_tms_layers_total_count: number; | ||
custom_wms_layers_total_count: number; | ||
maps_info: Array<{ id: string; title: string }>; | ||
} | ||
|
||
export interface MAPS_INFO { | ||
id: string; | ||
title: string; | ||
layers_total_count: number; | ||
documents_layers_total_count: number; | ||
opensearch_base_map_layers_total_count: number; | ||
custom_tms_layers_total_count: number; | ||
custom_wms_layers_total_count: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { ResponseError } from '@opensearch-project/opensearch/lib/errors'; | ||
import { | ||
IOpenSearchDashboardsResponse, | ||
IRouter, | ||
SavedObjectsFindResponse, | ||
} from '../../../../src/core/server'; | ||
import { APP_API, APP_PATH } from '../../common'; | ||
import { getMetrics } from '../common/metrics/metrics_helper'; | ||
import { MapSavedObjectAttributes } from '../../common/map_saved_object_attributes'; | ||
|
||
export function metricsRoute(router: IRouter) { | ||
router.get( | ||
{ | ||
path: `${APP_API}${APP_PATH.METRICS}`, | ||
validate: false, | ||
}, | ||
async ( | ||
context, | ||
request, | ||
response | ||
): Promise<IOpenSearchDashboardsResponse<any | ResponseError>> => { | ||
try { | ||
const savedObjectsClient = context.core.savedObjects.client; | ||
const mapsSavedObjects: SavedObjectsFindResponse<MapSavedObjectAttributes> = | ||
await savedObjectsClient?.find({ | ||
type: 'map', | ||
}); | ||
const metrics = getMetrics(mapsSavedObjects); | ||
return response.ok({ | ||
body: metrics, | ||
}); | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error(error); | ||
return response.custom({ | ||
statusCode: error.statusCode || 500, | ||
body: error.message, | ||
}); | ||
} | ||
} | ||
); | ||
} |