Skip to content

Commit 30fd74c

Browse files
committed
add DEVELOPER_GUIDE.md for middleware layer router setup for APIs
Signed-off-by: Fen Qin <[email protected]>
1 parent 4ce3c77 commit 30fd74c

File tree

3 files changed

+162
-1
lines changed

3 files changed

+162
-1
lines changed

server/DEVELOPER_GUIDE.md

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
## Developer Guide
2+
3+
This developer guide explains how to add new APIs to the middleware layer.
4+
5+
6+
To add search-relevance related operations, directory structure should look like this:
7+
8+
```md
9+
.
10+
├── dashboards-search-relevance
11+
│ └── common
12+
│ └── index.ts
13+
│ └── public
14+
│ └── services.ts
15+
│ └── server
16+
│ └── routes
17+
│ └── search_relevance_route_service.ts
18+
19+
```
20+
#### Step1 - add common index
21+
define your backend APIs and its node APIs as common index under `common/index.ts`
22+
- [backend search relevance APIs](https://github.com/opensearch-project/dashboards-search-relevance/blob/evaluation_lab/common/index.ts#L12)
23+
```
24+
/**
25+
* BACKEND SEARCH RELEVANCE APIs
26+
*/
27+
export const SEARCH_RELEVANCE_QUERY_SET_API = `${SEARCH_RELEVANCE_BASE_API}/queryset`;
28+
```
29+
- [node APIs](https://github.com/opensearch-project/dashboards-search-relevance/blob/evaluation_lab/common/index.ts#L23)
30+
```
31+
/**
32+
* Node APIs
33+
*/
34+
export const BASE_QUERYSET_NODE_API_PATH = `${BASE_NODE_API_PATH}/queryset`;
35+
```
36+
37+
38+
#### Step2 - client-side API
39+
add public-facing API routing under `public/service.ts`. When a user create a queryset, the application makes a POST request to ../api/relevancy/queryset path
40+
```
41+
export const postQuerySet = async (id: string, http: any) => {
42+
try {
43+
return await http.post(`..${BASE_QUERYSET_NODE_API_PATH}`, {
44+
body: JSON.stringify({
45+
querySetId: id,
46+
}),
47+
});
48+
} catch (e) {
49+
return e;
50+
}
51+
};
52+
```
53+
54+
55+
#### Step3 - server-side API
56+
add router `server/routes/search_relevance_route_service.ts` to route your node APIs to the functions. It's recommended to add data_source_id enabled router as well.
57+
```
58+
router.post(
59+
{
60+
path: BASE_QUERYSET_NODE_API_PATH,
61+
validate: {
62+
body: schema.any(),
63+
},
64+
options: {
65+
body: {
66+
accepts: 'application/json',
67+
},
68+
},
69+
},
70+
searchRelevanceRoutesService.createQuerySet
71+
);
72+
```
73+
add function definition under the same path `server/routes/search_relevance_route_service.ts`
74+
```
75+
createQuerySet = async (
76+
context: RequestHandlerContext,
77+
req: OpenSearchDashboardsRequest,
78+
res: OpenSearchDashboardsResponseFactory
79+
): Promise<IOpenSearchDashboardsResponse<any>> => {
80+
const body = req.body;
81+
const { data_source_id = '' } = req.params as { data_source_id?: string };
82+
try {
83+
const callWithRequest = getClientBasedOnDataSource(
84+
context,
85+
this.dataSourceEnabled,
86+
req,
87+
data_source_id,
88+
this.client
89+
);
90+
91+
const querysetResponse = await callWithRequest('searchRelevance.createQuerySet', {
92+
body,
93+
});
94+
95+
return res.ok({
96+
body: {
97+
ok: true,
98+
resp: querysetResponse,
99+
},
100+
});
101+
} catch (err) {
102+
return res.ok({
103+
body: {
104+
ok: false,
105+
resp: err.message,
106+
},
107+
});
108+
}
109+
};
110+
```
111+
112+
add server-side API routing under `server/clusters/search_relevance_plugin.ts` to make calls to backend APIs.
113+
```
114+
searchRelevance.createQuerySet = ca({
115+
url: {
116+
fmt: `${SEARCH_RELEVANCE_QUERY_SET_API}`,
117+
},
118+
method: 'POST',
119+
});
120+
```
121+
122+
### Simple page testing
123+
This page testing is not recommended after we have a page/workflow.
124+
jest tests should be added components by components, this is a quick example for connector tests, since we don't have any workflows/pages ready yet.
125+
126+
127+
To verify your api connection, your directory should look like this:
128+
129+
```md
130+
.
131+
├── dashboards-search-relevance
132+
│ └── public
133+
│ └── components
134+
│ └── api
135+
│ └── search_relevance_testing_page.tsx
136+
│ └── app.tsx
137+
138+
```
139+
#### step1 - change your API calls
140+
The original testing page `search_relevance_testing_page.tsx` is pointing to postQuerySet function. you can replace with your new function.
141+
```
142+
const result = await postQuerySet(querySetId, http);
143+
```
144+
145+
#### step2 - change your application landing page
146+
You can change your landing page to point to your testing page under `app.tsx`.
147+
Currently, the application is pointing to ExperimentPage if user opted in for the new version or QueryCompareHome.
148+
149+
By replacing ~~`<ExperimentPage application={application} chrome={chrome} />`~~
150+
with `<QuerySetTester http={http} />`
151+
152+
- spin up your backend cluster, for example for search-relevance plugin, run `./gradlew run`
153+
- start your frontend server, make sure your frontend plugins under OpenSearch-Dashboard like `OpenSearch-Dashboard/plugins/dashboard-search-relevance`, then under `OpenSearch-Dashboard` run `yarn start`
154+
- now, you able to open a page `http://localhost:5603/{uuid}`
155+
- you can now test your api and your backend api response should be returned and rendered after click.
156+
- more detailed can be found from this [issue](https://github.com/opensearch-project/dashboards-search-relevance/pull/490)
157+
158+
159+
160+
161+

server/routes/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ export function defineRoutes(
1616
registerMetricsRoute(router);
1717
}
1818

19-
export * from './search_relevance_route_servce';
19+
export * from './search_relevance_route_service';

0 commit comments

Comments
 (0)