Skip to content

Commit

Permalink
fix endpoint generate from traces for host and method
Browse files Browse the repository at this point in the history
  • Loading branch information
NikhilShahi committed Aug 5, 2022
1 parent ba587c1 commit 19b3fd9
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions backend/src/services/endpoints/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { RestMethod } from "../../enums";
interface GenerateEndpoint {
parameterizedPath: string;
host: string;
regex: string;
method: RestMethod;
traces: ApiTrace[];
}
Expand All @@ -26,17 +27,15 @@ export class EndpointsService {
const regexes = Object.keys(regexToTracesMap);
for (let x = 0; x < regexes.length && !found; x++) {
const regex = regexes[x];
const curr = regexToTracesMap[regex];
if (
RegExp(regex).test(trace.path) &&
trace.host === curr.host &&
trace.method === curr.method
RegExp(regex).test(`${trace.host}-${trace.method}-${trace.path}`)
) {
found = true;
regexToTracesMap[regex] = {
regexToTracesMap[regex].traces.push(trace);
/*regexToTracesMap[regex] = {
...regexToTracesMap[regex],
traces: [...regexToTracesMap[regex].traces, trace],
};
};*/
}
}
if (!found) {
Expand All @@ -46,25 +45,30 @@ export class EndpointsService {
let pathRegex = String.raw``;
for (let j = 0; j < pathTokens.length; j++) {
const tokenString = pathTokens[j];
if (isSuspectedParamater(tokenString)) {
parameterizedPath += `/{param${paramNum}}`;
pathRegex += String.raw`/[^/]+`;
paramNum += 1;
} else {
parameterizedPath += `/${tokenString}`;
pathRegex += String.raw`/${tokenString}`;
if (tokenString.length > 0) {
if (isSuspectedParamater(tokenString)) {
parameterizedPath += `/{param${paramNum}}`;
pathRegex += String.raw`/[^/]+`;
paramNum += 1;
} else {
parameterizedPath += `/${tokenString}`;
pathRegex += String.raw`/${tokenString}`;
}
}
}
if (pathRegex.length > 0) {
if (regexToTracesMap[pathRegex]) {
regexToTracesMap[pathRegex] = {
const regexKey = `${trace.host}-${trace.method}-${pathRegex}`;
if (regexToTracesMap[regexKey]) {
regexToTracesMap[regexKey].traces.push(trace);
/*regexToTracesMap[pathRegex] = {
...regexToTracesMap[pathRegex],
traces: [...regexToTracesMap[pathRegex].traces, trace],
};
};*/
} else {
regexToTracesMap[pathRegex] = {
regexToTracesMap[regexKey] = {
parameterizedPath,
host: trace.host,
regex: pathRegex,
method: trace.method,
traces: [trace],
};
Expand All @@ -75,13 +79,13 @@ export class EndpointsService {
Object.entries(regexToTracesMap).map(async ([regex, value], idx) => {
const apiEndpoint = new ApiEndpoint();
apiEndpoint.path = value.parameterizedPath;
apiEndpoint.pathRegex = regex;
apiEndpoint.pathRegex = value.regex;
apiEndpoint.host = value.traces[0].host;
apiEndpoint.totalCalls = value.traces.length;
apiEndpoint.method = value.traces[0].method;
apiEndpoint.owner = value.traces[0].owner;
await apiEndpointRepository.save(apiEndpoint);
// TODO: Do something with setting sensitive data classes during iteration of traces
// TODO: Do something with setting sensitive data classes during iteration of traces and add auto generated open api spec for inferred endpoints
for (let i = 0; i < value.traces.length; i++) {
const trace = value.traces[i];
trace.apiEndpoint = apiEndpoint;
Expand Down

0 comments on commit 19b3fd9

Please sign in to comment.