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: remove console.log and console.time to reduce logging costs in GCP #763

Merged
merged 1 commit into from
Jan 8, 2024
Merged
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
2 changes: 0 additions & 2 deletions src/APIs/FlagsAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export const handleWordFlags = ({
data: { words, contentLength },
flags: { examples, dialects, resolve },
}: HandleFlags) => {
console.time(`Handling word flags - examples: ${examples}, dialects: ${dialects}, resolve: ${resolve}`);
const updatedWords = compact(
words.map((word: Word | WordDocument | LegacyWordDocument) => {
let updatedWord: PartialWordType = assign(word);
Expand All @@ -41,6 +40,5 @@ export const handleWordFlags = ({
return updatedWord;
})
);
console.timeEnd(`Handling word flags - examples: ${examples}, dialects: ${dialects}, resolve: ${resolve}`);
return { words: updatedWords, contentLength };
};
8 changes: 0 additions & 8 deletions src/APIs/RedisAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
type RedisClient = {
get: (value: string) => void;
isFake?: boolean;
set: (key: string, value: string, options: { [key in string]: any }) => void;

Check warning on line 14 in src/APIs/RedisAPI.ts

View workflow job for this annotation

GitHub Actions / Run linters

Unexpected any. Specify a different type
};

type GetValue = {
Expand All @@ -20,11 +20,8 @@
};

export const getCachedWords = async ({ key, redisClient = defaultRedisClient }: GetValue) => {
console.time('Getting cached words');
const rawCachedWords = await redisClient.get(key);
const cachedWords = typeof rawCachedWords === 'string' ? JSON.parse(rawCachedWords) : rawCachedWords;
console.log(`Retrieved cached data for words ${key}:`, !!cachedWords);
console.timeEnd('Getting cached words');
return cachedWords;
};

Expand All @@ -36,7 +33,7 @@
}: {
key: string;
data: {
words: PartialWordType[] | any;

Check warning on line 36 in src/APIs/RedisAPI.ts

View workflow job for this annotation

GitHub Actions / Run linters

Unexpected any. Specify a different type
contentLength: number;
};
redisClient: RedisClient | undefined;
Expand All @@ -53,7 +50,6 @@
export const getCachedExamples = async ({ key, redisClient = defaultRedisClient }: GetValue) => {
const rawCachedExamples = await redisClient.get(key);
const cachedExamples = typeof rawCachedExamples === 'string' ? JSON.parse(rawCachedExamples) : rawCachedExamples;
console.log(`Retrieved cached data for examples ${key}:`, !!cachedExamples);
return cachedExamples;
};

Expand All @@ -73,15 +69,12 @@
};

export const getAllCachedVerbsAndSuffixes = async ({ key, redisClient = defaultRedisClient }: GetValue) => {
console.time(`Searching cached verbs and suffixes: verbs-and-suffixes-${key}`);
const redisAllVerbsAndSuffixesKey = `verbs-and-suffixes-${key}`;
const rawCachedAllVerbsAndSuffixes = await redisClient.get(redisAllVerbsAndSuffixesKey);
const cachedAllVerbsAndSuffixes =
typeof rawCachedAllVerbsAndSuffixes === 'string'
? JSON.parse(rawCachedAllVerbsAndSuffixes)
: rawCachedAllVerbsAndSuffixes;
console.log(`Retrieved cached data for verbs and suffixes ${key}:`, !!cachedAllVerbsAndSuffixes);
console.timeEnd(`Searching cached verbs and suffixes: verbs-and-suffixes-${key}`);
return cachedAllVerbsAndSuffixes;
};

Expand All @@ -101,6 +94,5 @@
if (!redisClient.isFake) {
await redisClient.set(redisAllVerbsAndSuffixesKey, JSON.stringify(updatedData), { EX: REDIS_CACHE_EXPIRATION });
}
console.log(`Setting verbs and suffixes cache: ${JSON.stringify(updatedData, null, 2).length}`);
return updatedData;
};
3 changes: 0 additions & 3 deletions src/controllers/utils/buildDocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export const findWordsWithMatch = async ({
}) => {
const connection = createDbConnection();
const Word = connection.model<WordDocument>('Word', wordSchema);
console.time(`Aggregation completion time: ${queryLabel || 'N/A'}`);
try {
let words = generateAggregationBase<WordDocument>(Word, match);

Expand Down Expand Up @@ -133,12 +132,10 @@ export const findWordsWithMatch = async ({
return cleanedWord as WordDocument;
});

console.timeEnd(`Aggregation completion time: ${queryLabel || 'N/A'}`);
await handleCloseConnection(connection);
return { words: finalWords, contentLength };
} catch (err: any) {
console.log('An error occurred', err);
console.timeEnd(`Aggregation completion time: ${queryLabel || 'N/A'}`);
await handleCloseConnection(connection);
throw err;
}
Expand Down
3 changes: 0 additions & 3 deletions src/controllers/utils/expandNoun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ export default (rawWord: string, wordData: WordData): Solution[] => {
({ wordClass: nestedWordClass }) => nestedWordClass === WordClassEnum.AV || nestedWordClass === WordClassEnum.PV
)
);
console.time('Expand noun time');
helper(word, { verbs }, firstPointer, secondPointer, [], { depth: 0 });
const finalSolution =
compact(
Expand All @@ -92,7 +91,5 @@ export default (rawWord: string, wordData: WordData): Solution[] => {
return solution;
})
) || [];
console.timeEnd('Expand noun time');
console.log('Expanded noun: ', finalSolution);
return finalSolution;
};
4 changes: 0 additions & 4 deletions src/controllers/utils/expandVerb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,6 @@ export default (rawWord: string, wordData: WordData): Solution[] => {
nestedWordClassEnum === WordClassEnum.ESUF || nestedWordClassEnum === WordClassEnum.ISUF
)
);
console.time('Expand verb time');
console.log('Expanding word: ', word);
helper(word, { verbs, suffixes: wordSuffixes }, firstPointer, secondPointer, [], { depth: 0 });
const finalSolution =
compact(
Expand All @@ -297,7 +295,5 @@ export default (rawWord: string, wordData: WordData): Solution[] => {
return solution;
})
) || [];
console.timeEnd('Expand verb time');
console.log('Expanded verb: ', finalSolution);
return finalSolution;
};
21 changes: 2 additions & 19 deletions src/controllers/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ export const handleQueries = async ({
wordClasses: wordClassesQuery,
resolve: resolveQuery,
} = query;
console.time('Handling queries');
const { id } = params;
let allVerbsAndSuffixes: WordData = { verbs: [], suffixes: [] };
const hasQuotes = keywordQuery && keywordQuery.match(/["'].*["']/) !== null;
Expand All @@ -128,12 +127,9 @@ export const handleQueries = async ({
const allVerbsAndSuffixesQuery: PipelineStage.Match['$match'] = searchForAllVerbsAndSuffixesQuery();
const cachedAllVerbsAndSuffixes = await getAllCachedVerbsAndSuffixes({ key: version, redisClient });
if (version === Version.VERSION_2) {
console.time('Searching all verbs and suffixes');
if (cachedAllVerbsAndSuffixes) {
console.log('Getting all verbs and suffixes from cache');
allVerbsAndSuffixes = cachedAllVerbsAndSuffixes;
} else {
console.log('Getting all verbs and suffixes from database');
const allVerbsAndSuffixesDb = (await searchAllVerbsAndSuffixes({ query: allVerbsAndSuffixesQuery, version }))
.words;
allVerbsAndSuffixes = await setAllCachedVerbsAndSuffixes({
Expand All @@ -143,9 +139,7 @@ export const handleQueries = async ({
version,
});
}
console.timeEnd('Searching all verbs and suffixes');
}
console.time('Generating filters, searchWord, and regexes');
const filter = convertFilterToKeyword(filterQuery);
const searchWord = removePrefix(keyword || filter || '').replace(/[Aa]na m /, 'm ');
const searchWordParts = compact(searchWord.split(' '));
Expand All @@ -157,9 +151,6 @@ export const handleQueries = async ({
}),
{}
);
console.timeEnd('Generating filters, searchWord, and regexes');
console.log('Word splits:', searchWordParts);
console.log(`Search word: ${searchWord}`);
let keywords =
version === Version.VERSION_2 && searchWord
? expandVerb(searchWord, allVerbsAndSuffixes).map(({ text, wordClass }) => {
Expand All @@ -180,7 +171,6 @@ export const handleQueries = async ({
: [];
// Attempt to breakdown as noun if there is no breakdown as verb
if (!keywords.length && searchWord) {
console.time('Attempting to breakdown noun');
keywords =
version === Version.VERSION_2
? expandNoun(searchWord, allVerbsAndSuffixes).map(({ text, wordClass }) => ({
Expand All @@ -195,15 +185,12 @@ export const handleQueries = async ({
),
}))
: [];
console.timeEnd('Attempting to breakdown noun');
}
if (!keywords.length && searchWord) {
console.time('Expand phrase time');
keywords = (
version === Version.VERSION_2
? searchWordParts.map((searchWordPart, searchWordPartIndex) => {
? searchWordParts.map((searchWordPart) => {
const expandedVerb = expandVerb(searchWordPart, allVerbsAndSuffixes);
console.time(`Expand phrase part ${searchWordPartIndex}`);
const result = expandedVerb.length
? expandedVerb.map(({ text, wordClass }) => ({
text,
Expand All @@ -218,14 +205,12 @@ export const handleQueries = async ({
}))
: // @ts-expect-error no index signature with parameter type string
[{ text: searchWordPart, wordClass: [], regex: regexes[searchWordPart] }];
console.timeEnd(`Expand phrase part ${searchWordPartIndex}`);

return result;
})
: []
).flat();
console.timeEnd('Expand phrase time');
}
console.time('Generating page, rank, skip, limit, and other flags');
const page = parseInt(pageQuery, 10);
const range = parseRange(rangeQuery);
const { skip, limit } = convertToSkipAndLimit({ page, range });
Expand Down Expand Up @@ -254,8 +239,6 @@ export const handleQueries = async ({
...(tags?.length ? { tags: { $in: tags } } : {}),
...(wordClasses?.length ? { 'definitions.wordClass': { $in: wordClasses } } : {}),
};
console.timeEnd('Generating page, rank, skip, limit, and other flags');
console.timeEnd('Handling queries');
return {
id,
version,
Expand Down
2 changes: 0 additions & 2 deletions src/controllers/utils/minimizeVerbsAndSuffixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const isVerb = (wordClass: WordClassEnum) =>
wordClass === WordClassEnum.MV;

const minimizeVerbsAndSuffixes = (words: Word[], version: Version) => {
console.time('Minimize words');
const minimizedWords = words.reduce(
(finalVerbsAndSuffixes, word) => {
const minimizedWord = pick(assign(word), ['word', 'definitions']) as MinimizedWord;
Expand All @@ -28,7 +27,6 @@ const minimizeVerbsAndSuffixes = (words: Word[], version: Version) => {
},
{ verbs: [], suffixes: [] } as { verbs: MinimizedWord[]; suffixes: MinimizedWord[] }
);
console.timeEnd('Minimize words');
return minimizedWords;
};

Expand Down
2 changes: 0 additions & 2 deletions src/controllers/utils/minimizeWords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ type MinimizedWord = Omit<PartialWordType, 'definitions' | 'examples' | 'dialect
stems?: (string | Partial<{ id: string; _id?: Types.ObjectId }>)[];
};
const minimizeWords = (words: PartialWordType[], version: Version) => {
console.time('Minimize words');
const minimizedWords = words.map((word) => {
let minimizedWord: Partial<MinimizedWord> = assign(word);
minimizedWord = omit(minimizedWord, ['hypernyms', 'hyponyms', 'updatedAt', 'createdAt']);
Expand Down Expand Up @@ -102,7 +101,6 @@ const minimizeWords = (words: PartialWordType[], version: Version) => {
}
return minimizedWord;
});
console.timeEnd('Minimize words');
return minimizedWords;
};

Expand Down
5 changes: 0 additions & 5 deletions src/controllers/utils/searchWordUsingEnglish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,18 @@ const searchWordUsingEnglish = async ({
flags,
filters,
}: EnglishSearch) => {
console.time(`searchWordUsingEnglish for ${searchWord}`);
let responseData = { words: [], contentLength: 0 };
const redisWordsCacheKey = `"${searchWord}"-${version}`;
const cachedWords = await getCachedWords({ key: redisWordsCacheKey, redisClient });

if (cachedWords) {
console.log('Return words from cache for English search');
responseData = {
words: cachedWords.words,
contentLength: cachedWords.contentLength,
};
} else {
const query = searchEnglishRegexQuery({ regex, searchWord, filters });
console.time(`Searching English words for ${searchWord}`);
const { words, contentLength } = await findWordsWithMatch({ match: query, version });
console.timeEnd(`Searching English words for ${searchWord}`);
responseData = await setCachedWords({
key: redisWordsCacheKey,
data: { words, contentLength },
Expand All @@ -61,7 +57,6 @@ const searchWordUsingEnglish = async ({
let sortedWords = sortDocsBy(searchWord, responseData.words, sortKey, version, regex);
sortedWords = sortedWords.slice(skip, skip + limit);

console.timeEnd(`searchWordUsingEnglish for ${searchWord}`);
return handleWordFlags({
data: { words: sortedWords, contentLength: responseData.contentLength },
flags,
Expand Down
5 changes: 0 additions & 5 deletions src/controllers/utils/searchWordUsingIgbo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,11 @@ const searchWordUsingIgbo = async ({
flags,
filters,
}: IgboSearch) => {
console.time(`searchWordUsingIgbo for ${searchWord}`);
let responseData = { words: [], contentLength: 0 };
const redisWordsCacheKey = `${searchWord}-${version}`;
const cachedWords = await getCachedWords({ key: redisWordsCacheKey, redisClient });

if (cachedWords) {
console.log('Return words from cache for Igbo search');
responseData = {
words: cachedWords.words,
contentLength: cachedWords.contentLength,
Expand All @@ -70,12 +68,10 @@ const searchWordUsingIgbo = async ({
searchWord,
filters,
});
console.time(`Searching Igbo words for ${searchWord}`);
const [igboResults, englishResults] = await Promise.all([
findWordsWithMatch({ match: igboQuery, version, queryLabel: 'igbo' }),
findWordsWithMatch({ match: definitionsWithinIgboQuery, version, queryLabel: 'definitions' }),
]);
console.timeEnd(`Searching Igbo words for ${searchWord}`);
// Prevents from duplicate word documents from being included in the final words array
const words = searchWord
? uniqWith(
Expand All @@ -96,7 +92,6 @@ const searchWordUsingIgbo = async ({
let sortedWords = sortDocsBy(searchWord, responseData.words, 'word', version, regex);
sortedWords = sortedWords.slice(skip, skip + limit);

console.timeEnd(`searchWordUsingIgbo for ${searchWord}`);
return handleWordFlags({
data: { words: sortedWords, contentLength: responseData.contentLength },
flags,
Expand Down
3 changes: 0 additions & 3 deletions src/controllers/words.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export const getWordData: MiddleWare = (req, res, next) => {
/* Reuseable base controller function for getting words */
const getWordsFromDatabase: MiddleWare = async (req, res, next) => {
try {
console.time('Getting words from database');
const {
version,
searchWord,
Expand Down Expand Up @@ -81,8 +80,6 @@ const getWordsFromDatabase: MiddleWare = async (req, res, next) => {
...searchQueries,
});
}
console.log(`Number of words for search word "${searchWord}": ${responseData.contentLength}`);
console.timeEnd('Getting words from database');
return packageResponse({
res,
docs: responseData.words,
Expand Down
10 changes: 1 addition & 9 deletions src/middleware/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,38 +35,30 @@ const trackEvent = ({ clientIdentifier, category, action, keyword }: TrackingEve

// Avoid sending GA events
if (isProduction) {
console.time('Sending production tracking data');
axios({
method: 'post',
url: `${GA_URL}?measurement_id=${params.measurement_id}&api_secret=${params.api_secret}`,
data,
})
.then((res) => {
console.log('Logging the data:', JSON.parse(data), JSON.parse(data).events[0].params);
console.log({ status: res.status, response: res.data });
console.timeEnd('Sending production tracking data');
.then(() => {
resolve(true);
})
.catch((err: any) => {
console.log(typeof err?.toJSON === 'function' ? err.toJSON() : err);
console.timeEnd('Sending production tracking data');
reject(new Error(err));
});
} else {
console.time('Sending development tracking data');
axios({
method: 'post',
url: `${DEBUG_GA_URL}?measurement_id=${params.measurement_id}&api_secret=${params.api_secret}`,
data,
})
.then(() => {
console.timeEnd('Sending development tracking data');
resolve(true);
})
.catch((err: any) => {
if (isProduction) {
console.log(typeof err?.toJSON === 'function' ? err.toJSON() : err);
console.timeEnd('Sending development tracking data');
reject(new Error(err));
}
});
Expand Down
4 changes: 0 additions & 4 deletions src/middleware/validateApiKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@ const handleDeveloperUsage = async (developer: DeveloperDocument) => {

/* Finds a developer with provided information */
const findDeveloper = async (apiKey: string) => {
console.time('Finding developer account');
const connection = createDbConnection();
const Developer = connection.model<DeveloperDocument>('Developer', developerSchema);
let developer = await Developer.findOne({ apiKey });
if (developer) {
console.timeEnd('Finding developer account');
return developer;
}
// Legacy implementation: hashed API tokens can't be indexed
Expand All @@ -47,10 +45,8 @@ const findDeveloper = async (apiKey: string) => {
if (developer) {
developer.apiKey = apiKey;
const updatedDeveloper = await developer.save();
console.timeEnd('Finding developer account');
return updatedDeveloper;
}
console.timeEnd('Finding developer account');
return developer;
};

Expand Down
Loading
Loading