Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: try_find_clients_endpoints #184

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@
"typecheck:src": "tsc --noEmit -p ./tsconfig.prod.json",
"typecheck:examples": "tsc --noEmit -p ./examples/tsconfig.json",
"typecheck": "npm run typecheck:src && npm run typecheck:examples",
"cloudapi:update": "git submodule update --remote"
"cloudapi:update": "git submodule update --remote",
"discover_endpoints": "ts-node scripts/discover_endpoints"
},
"engines": {
"node": ">=18.0.0"
Expand Down
4 changes: 2 additions & 2 deletions scripts/detect_services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ export const writeToFile = (serviceMap: ServiceMapType) => {
});
};

export type ClientsMapType = Record<string, undefined | { rootServiceList: string[] }>;
export const readFile = () => {
const data = fs.readFileSync(FILE_PATH, 'utf8');
const jsonData = JSON.parse(data);

return jsonData as Record<string, undefined | { rootServiceList: string[] }>;
return jsonData as ClientsMapType;
};

const main = async () => {
Expand Down
71 changes: 71 additions & 0 deletions scripts/discover_endpoints/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { readFile } from '../detect_services';

type EndpointType = { id: string; address: string };

const buildEndpointsMap = (endpoints: EndpointType[]) => {
return new Map(endpoints.map(({ id, address }) => [id.replaceAll('-', ''), { address, id }]));
};

const subsequenceLenghth = (target: string, subsequenceStr: string) => {
let maxCount = 0;

for (let i = 0; i !== target.length; i++) {
if (subsequenceStr[0] === target[i]) {
let count = 0;
for (let j = 0, k = i; j !== subsequenceStr.length && k !== target.length; j++, k++) {
if (subsequenceStr[j] === target[k]) count++;
else break;
}

if (count > maxCount) maxCount = count;
}
}

return maxCount;
};

const main = async () => {
const resp = await fetch('https://api.cloud.yandex.net/endpoints', {
method: 'get',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
}).then((response) => response.json());

const endpointsMap = buildEndpointsMap(resp.endpoints);

const serviceMap = readFile();

Object.keys(serviceMap).forEach((serviceName) => {
let maxCount = 0;
let res: string[] = [];
let resId: string[] = [];

endpointsMap.forEach(({ address, id }, endpointId) => {
const count = subsequenceLenghth(serviceName.split('/').join(''), endpointId);

if (count === 0) return;

if (count > maxCount) {
maxCount = count;
res = [address];
resId = [id];
return;
}

if (count === maxCount) {
res.push(address);
resId.push(id);
}
});

console.log(`\n ${serviceName} ${maxCount}`);
console.log(res, resId);
console.log(`\n`);
});
};

if (require.main === module) {
main();
}
17 changes: 17 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,22 @@
"lib": ["es6"],
"disableSizeLimit": true,
"moduleResolution": "nodenext"
},
"include": ["./src"],
"ts-node": {
"compilerOptions": {
"target": "ES2021",
"module": "NodeNext",
"allowJs": true,
"declaration": true,
"outDir": "./dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"resolveJsonModule": true,
"lib": ["ES2021", "DOM"],
"disableSizeLimit": true
}
}
}
Loading