Refactored visits parser to be functional

This commit is contained in:
Alejandro Celaya 2018-08-24 23:55:53 +02:00
parent 4924f24e1a
commit 6d0753481d
3 changed files with 74 additions and 60 deletions

View file

@ -7,13 +7,21 @@ import Moment from 'react-moment'
import { connect } from 'react-redux' import { connect } from 'react-redux'
import { Card, CardBody, CardHeader, UncontrolledTooltip } from 'reactstrap' import { Card, CardBody, CardHeader, UncontrolledTooltip } from 'reactstrap'
import DateInput from '../common/DateInput' import DateInput from '../common/DateInput'
import visitsParser from '../visits/services/VisitsParser' import {
processOsStats,
processBrowserStats,
processCountriesStats,
processReferrersStats,
} from '../visits/services/VisitsParser'
import { getShortUrlVisits } from './reducers/shortUrlVisits' import { getShortUrlVisits } from './reducers/shortUrlVisits'
import './ShortUrlVisits.scss' import './ShortUrlVisits.scss'
import MutedMessage from '../utils/MuttedMessage'; import MutedMessage from '../utils/MuttedMessage';
const defaultProps = { const defaultProps = {
visitsParser, processOsStats,
processBrowserStats,
processCountriesStats,
processReferrersStats,
}; };
export class ShortUrlsVisits extends React.Component { export class ShortUrlsVisits extends React.Component {
@ -34,7 +42,10 @@ export class ShortUrlsVisits extends React.Component {
const { const {
match: { params }, match: { params },
selectedServer, selectedServer,
visitsParser, processOsStats,
processBrowserStats,
processCountriesStats,
processReferrersStats,
shortUrlVisits: { visits, loading, error, shortUrl } shortUrlVisits: { visits, loading, error, shortUrl }
} = this.props; } = this.props;
const serverUrl = selectedServer ? selectedServer.url : ''; const serverUrl = selectedServer ? selectedServer.url : '';
@ -96,10 +107,10 @@ export class ShortUrlsVisits extends React.Component {
return ( return (
<div className="row"> <div className="row">
{renderGraphCard('Operating systems', visitsParser.processOsStats(visits), false)} {renderGraphCard('Operating systems', processOsStats(visits), false)}
{renderGraphCard('Browsers', visitsParser.processBrowserStats(visits), false)} {renderGraphCard('Browsers', processBrowserStats(visits), false)}
{renderGraphCard('Countries', visitsParser.processCountriesStats(visits), true, 'Visits')} {renderGraphCard('Countries', processCountriesStats(visits), true, 'Visits')}
{renderGraphCard('Referrers', visitsParser.processReferrersStats(visits), true, 'Visits')} {renderGraphCard('Referrers', processReferrersStats(visits), true, 'Visits')}
</div> </div>
); );
}; };
@ -173,4 +184,7 @@ export class ShortUrlsVisits extends React.Component {
ShortUrlsVisits.defaultProps = defaultProps; ShortUrlsVisits.defaultProps = defaultProps;
export default connect(pick(['selectedServer', 'shortUrlVisits']), { getShortUrlVisits })(ShortUrlsVisits); export default connect(
pick(['selectedServer', 'shortUrlVisits']),
{ getShortUrlVisits }
)(ShortUrlsVisits);

View file

@ -41,8 +41,7 @@ const extractDomain = url => {
return domain.split(':')[0]; return domain.split(':')[0];
}; };
export class VisitsParser { export const processOsStats = visits =>
processOsStats = visits =>
reduce( reduce(
(stats, visit) => { (stats, visit) => {
const userAgent = visit.userAgent; const userAgent = visit.userAgent;
@ -53,7 +52,7 @@ export class VisitsParser {
visits, visits,
); );
processBrowserStats = visits => export const processBrowserStats = visits =>
reduce( reduce(
(stats, visit) => { (stats, visit) => {
const userAgent = visit.userAgent; const userAgent = visit.userAgent;
@ -64,7 +63,7 @@ export class VisitsParser {
visits, visits,
); );
processReferrersStats = visits => export const processReferrersStats = visits =>
reduce( reduce(
(stats, visit) => { (stats, visit) => {
const notHasDomain = isNil(visit.referer) || isEmpty(visit.referer); const notHasDomain = isNil(visit.referer) || isEmpty(visit.referer);
@ -75,7 +74,7 @@ export class VisitsParser {
visits, visits,
); );
processCountriesStats = visits => export const processCountriesStats = visits =>
reduce( reduce(
(stats, { visitLocation }) => { (stats, { visitLocation }) => {
const notHasCountry = isNil(visitLocation) const notHasCountry = isNil(visitLocation)
@ -87,7 +86,3 @@ export class VisitsParser {
{}, {},
visits, visits,
); );
}
const visitsParser = new VisitsParser();
export default visitsParser;

View file

@ -1,4 +1,9 @@
import visitsParser from '../../../src/visits/services/VisitsParser'; import {
processOsStats,
processBrowserStats,
processReferrersStats,
processCountriesStats,
} from '../../../src/visits/services/VisitsParser';
describe('VisitsParser', () => { describe('VisitsParser', () => {
const visits = [ const visits = [
@ -36,7 +41,7 @@ describe('VisitsParser', () => {
describe('processOsStats', () => { describe('processOsStats', () => {
it('properly parses OS stats', () => { it('properly parses OS stats', () => {
expect(visitsParser.processOsStats(visits)).toEqual({ expect(processOsStats(visits)).toEqual({
'Linux': 3, 'Linux': 3,
'Windows': 1, 'Windows': 1,
'MacOS': 1, 'MacOS': 1,
@ -46,7 +51,7 @@ describe('VisitsParser', () => {
describe('processBrowserStats', () => { describe('processBrowserStats', () => {
it('properly parses browser stats', () => { it('properly parses browser stats', () => {
expect(visitsParser.processBrowserStats(visits)).toEqual({ expect(processBrowserStats(visits)).toEqual({
'Firefox': 2, 'Firefox': 2,
'Chrome': 2, 'Chrome': 2,
'Opera': 1, 'Opera': 1,
@ -56,7 +61,7 @@ describe('VisitsParser', () => {
describe('processReferrersStats', () => { describe('processReferrersStats', () => {
it('properly parses referrer stats', () => { it('properly parses referrer stats', () => {
expect(visitsParser.processReferrersStats(visits)).toEqual({ expect(processReferrersStats(visits)).toEqual({
'Unknown': 2, 'Unknown': 2,
'google.com': 2, 'google.com': 2,
'm.facebook.com': 1, 'm.facebook.com': 1,
@ -66,7 +71,7 @@ describe('VisitsParser', () => {
describe('processCountriesStats', () => { describe('processCountriesStats', () => {
it('properly parses countries stats', () => { it('properly parses countries stats', () => {
expect(visitsParser.processCountriesStats(visits)).toEqual({ expect(processCountriesStats(visits)).toEqual({
'Spain': 3, 'Spain': 3,
'United States': 1, 'United States': 1,
'Unknown': 1, 'Unknown': 1,