mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2024-12-23 01:20:24 +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) => {
|
||||||
|
const userAgent = visit.userAgent;
|
||||||
|
const os = isNil(userAgent) ? 'Others' : osFromUserAgent(userAgent);
|
||||||
|
return assoc(os, (stats[os] || 0) + 1, stats);
|
||||||
|
},
|
||||||
|
{},
|
||||||
|
visits,
|
||||||
|
);
|
||||||
|
|
||||||
forEach(visit => {
|
processBrowserStats = visits =>
|
||||||
const userAgent = visit.userAgent;
|
reduce(
|
||||||
const os = isNil(userAgent) ? 'Others' : osFromUserAgent(userAgent);
|
(stats, visit) => {
|
||||||
|
const userAgent = visit.userAgent;
|
||||||
|
const browser = isNil(userAgent) ? 'Others' : browserFromUserAgent(userAgent);
|
||||||
|
return assoc(browser, (stats[browser] || 0) + 1, stats);
|
||||||
|
},
|
||||||
|
{},
|
||||||
|
visits,
|
||||||
|
);
|
||||||
|
|
||||||
stats[os] = typeof stats[os] === 'undefined' ? 1 : stats[os] + 1;
|
processReferrersStats = visits =>
|
||||||
}, visits);
|
reduce(
|
||||||
|
(stats, visit) => {
|
||||||
|
const notHasDomain = isNil(visit.referer) || isEmpty(visit.referer);
|
||||||
|
const domain = notHasDomain ? 'Unknown' : extractDomain(visit.referer);
|
||||||
|
return assoc(domain, (stats[domain]|| 0) + 1, stats);
|
||||||
|
},
|
||||||
|
{},
|
||||||
|
visits,
|
||||||
|
);
|
||||||
|
|
||||||
return stats;
|
processCountriesStats = visits =>
|
||||||
};
|
reduce(
|
||||||
|
(stats, { visitLocation }) => {
|
||||||
processBrowserStats = visits => {
|
const notHasCountry = isNil(visitLocation)
|
||||||
const stats = {};
|
|| isNil(visitLocation.countryName)
|
||||||
|
|| isEmpty(visitLocation.countryName);
|
||||||
forEach(visit => {
|
const country = notHasCountry ? 'Unknown' : visitLocation.countryName;
|
||||||
const userAgent = visit.userAgent;
|
return assoc(country, (stats[country]|| 0) + 1, stats);
|
||||||
const browser = isNil(userAgent) ? 'Others' : browserFromUserAgent(userAgent);
|
},
|
||||||
|
{},
|
||||||
stats[browser] = typeof stats[browser] === 'undefined' ? 1 : stats[browser] + 1;
|
visits,
|
||||||
}, visits);
|
);
|
||||||
|
|
||||||
return stats;
|
|
||||||
};
|
|
||||||
|
|
||||||
processReferrersStats = visits => {
|
|
||||||
const stats = {};
|
|
||||||
|
|
||||||
forEach(visit => {
|
|
||||||
const notHasDomain = isNil(visit.referer) || isEmpty(visit.referer);
|
|
||||||
const domain = notHasDomain ? 'Unknown' : extractDomain(visit.referer);
|
|
||||||
|
|
||||||
stats[domain] = typeof stats[domain] === 'undefined' ? 1 : stats[domain] + 1;
|
|
||||||
}, visits);
|
|
||||||
|
|
||||||
return stats;
|
|
||||||
};
|
|
||||||
|
|
||||||
processCountriesStats = visits => {
|
|
||||||
const stats = {};
|
|
||||||
|
|
||||||
forEach(({ visitLocation }) => {
|
|
||||||
const notHasCountry = isNil(visitLocation)
|
|
||||||
|| isNil(visitLocation.countryName)
|
|
||||||
|| isEmpty(visitLocation.countryName);
|
|
||||||
const country = notHasCountry ? 'Unknown' : visitLocation.countryName;
|
|
||||||
|
|
||||||
stats[country] = typeof stats[country] === 'undefined' ? 1 : stats[country] + 1;
|
|
||||||
}, visits);
|
|
||||||
|
|
||||||
return stats;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const visitsParser = new VisitsParser();
|
const visitsParser = new VisitsParser();
|
||||||
|
|
Loading…
Reference in a new issue