mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2024-12-23 09:30:31 +03:00
Refactored VisitsParser taking advantage of reduce function
This commit is contained in:
parent
26a3fdfd3d
commit
4924f24e1a
1 changed files with 43 additions and 52 deletions
|
@ -1,4 +1,4 @@
|
||||||
import { forEach, isNil, isEmpty } from 'ramda';
|
import { assoc, isNil, isEmpty, reduce } from 'ramda';
|
||||||
|
|
||||||
const osFromUserAgent = userAgent => {
|
const osFromUserAgent = userAgent => {
|
||||||
const lowerUserAgent = userAgent.toLowerCase();
|
const lowerUserAgent = userAgent.toLowerCase();
|
||||||
|
@ -41,61 +41,52 @@ const extractDomain = url => {
|
||||||
return domain.split(':')[0];
|
return domain.split(':')[0];
|
||||||
};
|
};
|
||||||
|
|
||||||
// FIXME Refactor these foreach statements which mutate a stats object
|
|
||||||
export class VisitsParser {
|
export class VisitsParser {
|
||||||
processOsStats = visits => {
|
processOsStats = visits =>
|
||||||
const stats = {};
|
reduce(
|
||||||
|
(stats, visit) => {
|
||||||
forEach(visit => {
|
|
||||||
const userAgent = visit.userAgent;
|
const userAgent = visit.userAgent;
|
||||||
const os = isNil(userAgent) ? 'Others' : osFromUserAgent(userAgent);
|
const os = isNil(userAgent) ? 'Others' : osFromUserAgent(userAgent);
|
||||||
|
return assoc(os, (stats[os] || 0) + 1, stats);
|
||||||
|
},
|
||||||
|
{},
|
||||||
|
visits,
|
||||||
|
);
|
||||||
|
|
||||||
stats[os] = typeof stats[os] === 'undefined' ? 1 : stats[os] + 1;
|
processBrowserStats = visits =>
|
||||||
}, visits);
|
reduce(
|
||||||
|
(stats, visit) => {
|
||||||
return stats;
|
|
||||||
};
|
|
||||||
|
|
||||||
processBrowserStats = visits => {
|
|
||||||
const stats = {};
|
|
||||||
|
|
||||||
forEach(visit => {
|
|
||||||
const userAgent = visit.userAgent;
|
const userAgent = visit.userAgent;
|
||||||
const browser = isNil(userAgent) ? 'Others' : browserFromUserAgent(userAgent);
|
const browser = isNil(userAgent) ? 'Others' : browserFromUserAgent(userAgent);
|
||||||
|
return assoc(browser, (stats[browser] || 0) + 1, stats);
|
||||||
|
},
|
||||||
|
{},
|
||||||
|
visits,
|
||||||
|
);
|
||||||
|
|
||||||
stats[browser] = typeof stats[browser] === 'undefined' ? 1 : stats[browser] + 1;
|
processReferrersStats = visits =>
|
||||||
}, visits);
|
reduce(
|
||||||
|
(stats, visit) => {
|
||||||
return stats;
|
|
||||||
};
|
|
||||||
|
|
||||||
processReferrersStats = visits => {
|
|
||||||
const stats = {};
|
|
||||||
|
|
||||||
forEach(visit => {
|
|
||||||
const notHasDomain = isNil(visit.referer) || isEmpty(visit.referer);
|
const notHasDomain = isNil(visit.referer) || isEmpty(visit.referer);
|
||||||
const domain = notHasDomain ? 'Unknown' : extractDomain(visit.referer);
|
const domain = notHasDomain ? 'Unknown' : extractDomain(visit.referer);
|
||||||
|
return assoc(domain, (stats[domain]|| 0) + 1, stats);
|
||||||
|
},
|
||||||
|
{},
|
||||||
|
visits,
|
||||||
|
);
|
||||||
|
|
||||||
stats[domain] = typeof stats[domain] === 'undefined' ? 1 : stats[domain] + 1;
|
processCountriesStats = visits =>
|
||||||
}, visits);
|
reduce(
|
||||||
|
(stats, { visitLocation }) => {
|
||||||
return stats;
|
|
||||||
};
|
|
||||||
|
|
||||||
processCountriesStats = visits => {
|
|
||||||
const stats = {};
|
|
||||||
|
|
||||||
forEach(({ visitLocation }) => {
|
|
||||||
const notHasCountry = isNil(visitLocation)
|
const notHasCountry = isNil(visitLocation)
|
||||||
|| isNil(visitLocation.countryName)
|
|| isNil(visitLocation.countryName)
|
||||||
|| isEmpty(visitLocation.countryName);
|
|| isEmpty(visitLocation.countryName);
|
||||||
const country = notHasCountry ? 'Unknown' : visitLocation.countryName;
|
const country = notHasCountry ? 'Unknown' : visitLocation.countryName;
|
||||||
|
return assoc(country, (stats[country]|| 0) + 1, stats);
|
||||||
stats[country] = typeof stats[country] === 'undefined' ? 1 : stats[country] + 1;
|
},
|
||||||
}, visits);
|
{},
|
||||||
|
visits,
|
||||||
return stats;
|
);
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const visitsParser = new VisitsParser();
|
const visitsParser = new VisitsParser();
|
||||||
|
|
Loading…
Reference in a new issue