Skip to content

Commit

Permalink
Merge pull request #74 from ciorg/webstats-query
Browse files Browse the repository at this point in the history
updated mongo search
  • Loading branch information
ciorg authored Dec 13, 2021
2 parents 27f0c7f + 19657a3 commit 31e62d0
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 30 deletions.
16 changes: 8 additions & 8 deletions src/controllers/crypto_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class CryptoData {
res: undefined
}

const response = await this.dbExchanges.search('name', name);
const response = await this.dbExchanges.search({ name });

const { res } = response;

Expand Down Expand Up @@ -127,7 +127,7 @@ class CryptoData {
res: undefined
};

const response = await this.dbCoins.search('symbol', symbol);
const response = await this.dbCoins.search({ symbol });

const { res } = response;

Expand Down Expand Up @@ -291,7 +291,7 @@ class CryptoData {
}

private async _getCategoriesFromCode(code: string): Promise<string[]> {
const result = await this.dbCats.search('code', code);
const result = await this.dbCats.search({ code });

if (result.res.length > 0) {
return result.res.map((res: any) => res.full);
Expand All @@ -301,21 +301,21 @@ class CryptoData {
}

private async _getCoinIdsInCategory(categories: string): Promise<string[]> {
const result = await this.dbCoins.search('categories', categories);
const result = await this.dbCoins.search({ categories });

if (result.res.length === 0) return [];

return result.res.map((res: any) => res.coin_id)
}

private async _getCategories(coin_id: string): Promise<string[]> {
const result = await this.dbCoins.search('coin_id', coin_id);
const result = await this.dbCoins.search({ coin_id });

if (result.res.length > 0) {
const { categories } = result.res[0]

const pResp: string | null[] = await Promise.all(
categories.map((cat: any) => this._getCodeFromCategory(cat))
const pResp: (string | null)[] = await Promise.all(
categories.map((cat: string) => this._getCodeFromCategory(cat))
);

return pResp.reduce((coinCats: string[], r: string | null) => {
Expand All @@ -331,7 +331,7 @@ class CryptoData {
}

private async _getCodeFromCategory(category: string): Promise<string | null> {
const codeResult = await this.dbCats.search('key', this._normalizeCategory(category));
const codeResult = await this.dbCats.search({ key: this._normalizeCategory(category) });

if (codeResult.res.length) {
return codeResult.res[0].code;
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/crypto_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class CoinPurchase {
async getTransactions(req: Request): Promise<[I.TransactionsWithSummary[], I.GrandTally, Date]> {
const { user }: any = req;

const results = await this.action.search('user_id', user._id);
const results = await this.action.search({ user_id: user._id });

const transactionsByCoin = await this.organizeTransactions(results.res);

Expand Down
4 changes: 2 additions & 2 deletions src/controllers/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Utils {

async addRbName(docArray: I.Rating[]) {
for (const i of docArray) {
const result = await this.rbActions.search('_id', i.rb_id);
const result = await this.rbActions.search({ _id: i.rb_id });

if (result.error) continue;
if (result.res.length === 0) continue;
Expand Down Expand Up @@ -82,7 +82,7 @@ class Utils {
}

getRatingsByRbId(rbId: string) {
return this.ratingActions.search('rb_id', rbId);
return this.ratingActions.search({ rb_id: rbId });
}

async getTotalAvg(rbDocs: I.RootBeer[]) {
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Notes {
async getUsersNotes(req: Request): Promise<I.Result> {
const { user }: any = req;

const notes = await this.action.search('user', user._id);
const notes = await this.action.search({ user: user._id });

return this.formatNotes(notes);
}
Expand All @@ -69,7 +69,7 @@ class Notes {
}

async searchTag(req: Request): Promise<I.Result> {
const notes = await this.action.search('tags', req.params.tag);
const notes = await this.action.search({ tags: req.params.tag });

return this.formatNotes(notes);
}
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/ratings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ class Ratings {
}

getRbRatings(req: Request) {
return this.action.search('rb_id', req.params.rb_id);
return this.action.search({ rb_id: req.params.rb_id });
}

async ratingsByUser(req: Request) {
const { user }: any = req;

const ratings = await this.action.search('user', user._id);
const ratings = await this.action.search({ user: user._id });

const ratingsDocs = this.utils.getDocs(ratings.res);

Expand Down
4 changes: 2 additions & 2 deletions src/controllers/rb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Rootbeer {

async getUsersRb(req: Request) {
const { user }: any = req;
const result = await this.rb_actions.search('user', user._id);
const result = await this.rb_actions.search({ user: user._id });

if (result.error) return result;

Expand All @@ -79,7 +79,7 @@ class Rootbeer {
const searchTerms = this.utils.makeRegex(req.body.rb_search);

if (searchTerms) {
const result = await this.rb_actions.search(field, searchTerms)
const result = await this.rb_actions.search({ [field]: searchTerms });

if (result.error) return result;

Expand Down
19 changes: 15 additions & 4 deletions src/controllers/web_stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,24 @@ class SiteStats {
tallyByOs: this._sortTallies(tallyByOs),
tallyByBrowser: this._sortTallies(tallyByBrowser),
tallyByIp: enhancedIp,
tallyByCountry: this._sortTallies(countByCountry)
tallyByCountry: this._sortTallies(countByCountry)
};
}

private async _mongoQuery(startDate: string) {
return this.visit_actions.search('timestamp', { $gte: startDate })
// return Visit.find( { timestamp: { $gte: startDate } });
const botQuery = this.bot.platform_fields.reduce((bQ: any[], field) => {
const fieldQ = { [field]: { $ne: 'unknown' } };

bQ.push(fieldQ);

return bQ;
}, []);

return this.visit_actions.search({
timestamp: { $gte: startDate },
path: { $ne: '/crypto/cached_markets' },
$or: botQuery
});
}

private _removeBots(visits: Document[]): Document[] {
Expand Down Expand Up @@ -157,7 +168,7 @@ class SiteStats {
const enhancedIp:{ [prop: string]: [string, number] } = {};

for (const ip of Object.keys(data)) {
const result = await this.ip_actions.search('ip_address', ip);
const result = await this.ip_actions.search({ ip_address: ip });

if (result.error || result.res.length === 0) continue;

Expand Down
2 changes: 1 addition & 1 deletion src/scripts/metadata_utils/_lib/save_categories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class SaveCryptoCategories {
}

async _saveToDb( key: string, value: CodeValue) {
const result = await this.dbActions.search('key', key);
const result = await this.dbActions.search({ key });

const [cat] = result.res;

Expand Down
6 changes: 5 additions & 1 deletion src/utils/bot_detector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ class BotDetector {
'version.js',
'sql',
'exe',
'ncdh'
'ncdh',
'env',
'mifs',
'console',
'git'
];

this.platform_fields = [
Expand Down
12 changes: 6 additions & 6 deletions src/utils/db_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ class Actions {
this.model = model;
}

protected async _modelAction(action: string, params: any): Promise<I.Result> {
protected async _modelAction(action: string, params: any, fields?: any): Promise<I.Result> {
const result: I.Result = { res: null };

try {
const res: Document[] = await this.model[action](params);
const res: Document[] = await this.model[action](params, fields);

this.log.debug(`successfully completed ${action} for ${JSON.stringify(params)}`);

Expand All @@ -28,8 +28,8 @@ class Actions {
return result;
}

create(params: any) {
return this._modelAction('create', params);
create(params: any, fields?: any) {
return this._modelAction('create', params, fields);
};

async update(id: string, params: any): Promise<I.Result> {
Expand Down Expand Up @@ -81,8 +81,8 @@ class Actions {
return this._modelAction('deleteOne', { _id: id });
}

search(field: string, value: any) {
return this._modelAction('find', { [field]: value });
search(params: { [key: string]: any }, fields = {}) {
return this._modelAction('find', params, fields);
}

searchById(id: string) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/ip_address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class IpAddress {
}

async save(ipAddress: string): Promise<void> {
const savedIpInfo = await this.ip_actions.search('ip_address', ipAddress);
const savedIpInfo = await this.ip_actions.search({ ip_address: ipAddress });

if (savedIpInfo.res && savedIpInfo.res.length > 0
&& this._within30Days(savedIpInfo.res[0].updated)) return;
Expand Down

0 comments on commit 31e62d0

Please sign in to comment.