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

Open
wants to merge 1 commit into
base: alpha
Choose a base branch
from
Open
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 @@ -169,7 +169,8 @@
"build:services": "ts-node scripts/build_services",
"build:mono-service": "cross-env NODE_OPTIONS=\"--max-old-space-size=4096\" tsc -p .",
"build": "npm run build:mono-service && npm run build:services",
"typecheck:clients": "tsc -p ./clients --noEmit"
"typecheck:clients": "tsc -p ./clients --noEmit",
"try_find_clients_endpoints": "ts-node scripts/try_find_clients_endpoints"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"try_find_clients_endpoints": "ts-node scripts/try_find_clients_endpoints"
"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/try_find_clients_endpoints/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { ClientsMapType, readFile } from '../detect_services';

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Какой интересный способ сделать .replaceAll() :)

};

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', {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Было бы круто еще endpoint принимать аргументом, чтобы другие инсталляции тыкать можно было.

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();
}
18 changes: 17 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,21 @@
"lib": ["es6"],
"disableSizeLimit": true
},
"include": ["./src"]
"include": ["./src"],
"ts-node": {
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"allowJs": true,
"declaration": true,
"outDir": "./dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"resolveJsonModule": true,
"lib": ["es6", "DOM"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А зачем DOM?

"disableSizeLimit": true
}
}
}
Loading