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

Fix: Create random users in batches in create-data script #2635

Merged
merged 2 commits into from
Jul 3, 2024
Merged
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
169 changes: 102 additions & 67 deletions scripts/create-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ const DOMAIN_COLORS = [
'YELLOW',
];

const CHAR_LIMITS = {
USER: {
MAX_DISPLAYNAME_CHARS: 30,
MAX_BIO_CHARS: 200,
MAX_LOCATION_CHARS: 200
},
COLONY: {
MAX_COLONY_DISPLAY_NAME: 20,
MAX_COLONY_OBJECTIVE_TITLE: 60,
MAX_COLONY_OBJECTIVE_DESCRIPTION: 200,
}
};

/*
* Mutations
*/
Expand Down Expand Up @@ -195,6 +208,9 @@ const getColonyMetadata = /* GraphQL */ `
}
`;

const graphqlRequestPreconfigured = async (queryOrMutation, variables) =>
graphqlRequest(queryOrMutation, variables, GRAPHQL_URI, API_KEY)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice! Big fan of this. 📈


/*
* Helper methods
*/
Expand Down Expand Up @@ -222,7 +238,7 @@ const readFile = (path) => {
*/
const subscribeUserToColony = async (userAddress, colonyAddress) => {
// subscribe user to colony
await graphqlRequest(
await graphqlRequestPreconfigured(
createContributor,
{
input: {
Expand All @@ -234,8 +250,6 @@ const subscribeUserToColony = async (userAddress, colonyAddress) => {
isWatching: true,
},
},
GRAPHQL_URI,
API_KEY,
);
await delay();

Expand Down Expand Up @@ -273,31 +287,32 @@ const createUser = async (
}

if (description) {
metadata.bio = description;
metadata.bio = description.slice(0, CHAR_LIMITS.USER.MAX_BIO_CHARS);
}

if (website) {
metadata.website = website;
}

if (location) {
metadata.location = location;
metadata.location = location.slice(0, CHAR_LIMITS.USER.MAX_LOCATION_CHARS);
}

const userQuery = await graphqlRequest(
const displayName = username.slice(0, CHAR_LIMITS.USER.MAX_DISPLAYNAME_CHARS);
const email = `${displayName}@colony.io`;

const userQuery = await graphqlRequestPreconfigured(
createUniqueUser,
{
input: {
id: userAddress,
profile: {
displayName: username,
email: `${username}@colony.io`,
displayName,
email,
...metadata,
},
},
},
GRAPHQL_URI,
API_KEY,
);
await delay();

Expand All @@ -309,7 +324,7 @@ const createUser = async (
);
} else {
console.log(
`Creating user { walletAddress: "${userAddress}", profile: { displayName: "${username}", email: "${username}@colony.io" } }`,
`Creating user { walletAddress: "${userAddress}", profile: { displayName: "${displayName}", email: "${email}" } }`,
);
}
} catch (error) {
Expand All @@ -325,16 +340,14 @@ const createUser = async (

const addTokenToColonyTokens = async (colonyAddress, tokenAddress) => {
// add token to colony's token list
await graphqlRequest(
await graphqlRequestPreconfigured(
createColonyTokens,
{
input: {
colonyID: colonyAddress,
tokenID: tokenAddress,
},
},
GRAPHQL_URI,
API_KEY,
);
await delay();

Expand All @@ -345,7 +358,7 @@ const addTokenToColonyTokens = async (colonyAddress, tokenAddress) => {

const addTokenToDB = async (tokenAddress, avatar) => {
// create token entry in the db
await graphqlRequest(
await graphqlRequestPreconfigured(
getTokenFromEverywhere,
{
input: {
Expand All @@ -354,8 +367,6 @@ const addTokenToDB = async (tokenAddress, avatar) => {
thumbnail: avatar || null,
},
},
GRAPHQL_URI,
API_KEY,
);
};

Expand Down Expand Up @@ -441,23 +452,22 @@ const createColony = async (
await signerOrWallet.signTransaction(populatedTransaction);
const hash = utils.keccak256(signedTransaction);

const displayName = (colonyDisplayName || `Colony ${colonyName.toUpperCase()}`).slice(0, CHAR_LIMITS.COLONY.MAX_COLONY_DISPLAY_NAME);

// create the colony
const colonyQuery = await graphqlRequest(
const colonyQuery = await graphqlRequestPreconfigured(
createColonyEtherealMetadata,
{
input: {
colonyName,
colonyDisplayName:
colonyDisplayName || `Colony ${colonyName.toUpperCase()}`,
colonyDisplayName: displayName,
tokenAvatar,
tokenThumbnail: tokenAvatar,
initiatorAddress: utils.getAddress(signerOrWallet.address),
transactionHash: hash,
inviteCode: 'dev',
},
},
GRAPHQL_URI,
API_KEY,
);

if (colonyQuery?.errors) {
Expand Down Expand Up @@ -513,7 +523,11 @@ const createColony = async (
metadata.externalLinks = colonySocialLinks;
}
if (colonyObjective?.title && colonyObjective?.description) {
metadata.objective = colonyObjective;
metadata.objective = {
...colonyObjective,
mmioana marked this conversation as resolved.
Show resolved Hide resolved
title: colonyObjective.title.slice(0, CHAR_LIMITS.COLONY.MAX_COLONY_OBJECTIVE_TITLE),
description: colonyObjective.description.slice(0, CHAR_LIMITS.COLONY.MAX_COLONY_OBJECTIVE_DESCRIPTION),
};
}

const colonyExists = await tryFetchGraphqlQuery(getColonyMetadata, {
Expand All @@ -528,16 +542,14 @@ const createColony = async (
}

// Colony metadata
const metadataMutation = await graphqlRequest(
const metadataMutation = await graphqlRequestPreconfigured(
updateColonyMetadata,
{
input: {
id: utils.getAddress(colonyAddress),
...metadata,
},
},
GRAPHQL_URI,
API_KEY,
);
await delay();

Expand Down Expand Up @@ -597,7 +609,7 @@ const createColony = async (
DOMAIN_COLORS[randomBetweenNumbers(0, DOMAIN_COLORS.length - 1)] ||
'LIGHT_PINK';

const domainMetadataMutation = await graphqlRequest(
const domainMetadataMutation = await graphqlRequestPreconfigured(
createDomainMetadata,
{
input: {
Expand All @@ -607,8 +619,6 @@ const createColony = async (
description: domains[index].description || '',
},
},
GRAPHQL_URI,
API_KEY,
);

await delay();
Expand Down Expand Up @@ -641,21 +651,17 @@ const createColony = async (
const oneTxHash = getExtensionHash('OneTxPayment');
const stakedExpenditureHash = getExtensionHash('StakedExpenditure');

const { data: oneTxVersionData } = await graphqlRequest(
const { data: oneTxVersionData } = await graphqlRequestPreconfigured(
getCurrentVersion,
{
key: oneTxHash,
},
GRAPHQL_URI,
API_KEY,
);
const { data: stakedExpenditureVersionData } = await graphqlRequest(
const { data: stakedExpenditureVersionData } = await graphqlRequestPreconfigured(
getCurrentVersion,
{
key: stakedExpenditureHash,
},
GRAPHQL_URI,
API_KEY,
);
const latestOneTxVersion =
oneTxVersionData?.getCurrentVersionByKey?.items[0]?.version || 1;
Expand Down Expand Up @@ -1142,11 +1148,9 @@ const tryFetchGraphqlQuery = async (
) => {
let currentTry = 0;
while (true) {
const { data } = await graphqlRequest(
const { data } = await graphqlRequestPreconfigured(
queryOrMutation,
variables,
GRAPHQL_URI,
API_KEY,
);

/*
Expand All @@ -1166,6 +1170,51 @@ const tryFetchGraphqlQuery = async (
}
};



const createRandomUser = async ({ username, index }) => {
const avatarURL = `http://xsgames.co/randomusers/assets/avatars/${
(index + 1) % 2 === 0 ? 'female' : 'male'
}/${index + 1}.jpg`;
const avatar = await imageUrlToBase64(avatarURL);
return createUser({
username,
avatar: (index + 1) % 5 === 0 ? null : avatar,
});
};

const createRandomUsersBatch = async (start, size = 3) => {
const batch = [];

for (let index = start; index < start + size; index++) {
if (index > usersTempData.randomUsernames.length - 1) {
break;
}
batch.push(
createRandomUser({
username: usersTempData.randomUsernames[index],
index
})
);
}

return Promise.all(batch);
};

const createRandomUsersInBatches = async () => {
const batchSize = 3;
const randomUsersBatchCount = Math.ceil(usersTempData.randomUsernames.length / batchSize);
const randomUsers = [];

for (let batchCount = 0; batchCount < randomUsersBatchCount; batchCount++) {
const randomUsersBatch = await createRandomUsersBatch(batchCount * batchSize, batchSize);
randomUsers.push(...randomUsersBatch);
delay(1000);
}

return randomUsers;
};

/*
* Orchestration
*/
Expand All @@ -1189,28 +1238,20 @@ const createUserAndColonyData = async () => {
let reputationMining = false;

const { leela, amy, fry } = usersTempData;
await Promise.all(
[leela, amy, fry].map(async (user, index) => {
const newUser = await createUser(user, index);
availableUsers.walletUsers[newUser.address] = newUser;
delay(100);
}),
const walletUsers = await Promise.all(
[leela, amy, fry].map((user, index) => createUser(user, index)),
);

await Promise.all(
usersTempData.randomUsernames.map(async (username, index) => {
const avatarURL = `http://xsgames.co/randomusers/assets/avatars/${
(index + 1) % 2 === 0 ? 'female' : 'male'
}/${index + 1}.jpg`;
const avatar = await imageUrlToBase64(avatarURL);
const user = await createUser({
username,
avatar: (index + 1) % 5 === 0 ? null : avatar,
});
availableUsers.randomUsers[user.address] = user;
delay(100);
}),
);
walletUsers.forEach((user) => {
availableUsers.walletUsers[user.address] = user;
})

delay(1000);

const randomUsers = await createRandomUsersInBatches();
randomUsers.forEach((user) => {
availableUsers.randomUsers[user.address] = user;
})

const colonyNamesToCreate = Object.keys(coloniesTempData).slice(
0,
Expand Down Expand Up @@ -1268,16 +1309,14 @@ const createUserAndColonyData = async () => {
// verify users
await Promise.all(
Object.keys(availableUsers.walletUsers).map(async (userAddress) => {
await graphqlRequest(
await graphqlRequestPreconfigured(
updateColonyContributor,
{
input: {
id: `${newColonyAddress}_${userAddress}`,
isVerified: true,
},
},
GRAPHQL_URI,
API_KEY,
);
}),
);
Expand All @@ -1290,11 +1329,9 @@ const createUserAndColonyData = async () => {
availableUsers.walletUsers[leela.address],
);

const { data: colonyDomainsdata } = await graphqlRequest(
const { data: colonyDomainsdata } = await graphqlRequestPreconfigured(
getColonyDomains,
{ address: newColonyAddress },
GRAPHQL_URI,
API_KEY,
);

const domains =
Expand All @@ -1312,11 +1349,9 @@ const createUserAndColonyData = async () => {
}

if (domains.length > 0) {
const { data: colonyContributorsData } = await graphqlRequest(
const { data: colonyContributorsData } = await graphqlRequestPreconfigured(
getColonyContributors,
{ address: newColonyAddress },
GRAPHQL_URI,
API_KEY,
);
const contributors = (
colonyContributorsData?.listColonyContributors?.items || []
Expand Down
Loading