From 1d6464fefb69d0e62dd2a5411fc6a99d33ddb638 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 19 Mar 2023 11:44:40 +0100 Subject: [PATCH] Take into consideration types of visits when increasing tags visits --- src/tags/reducers/tagsList.ts | 26 ++++++++--- test/tags/reducers/tagsList.test.ts | 71 +++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 6 deletions(-) diff --git a/src/tags/reducers/tagsList.ts b/src/tags/reducers/tagsList.ts index 9582659f..e92cb589 100644 --- a/src/tags/reducers/tagsList.ts +++ b/src/tags/reducers/tagsList.ts @@ -8,7 +8,7 @@ import type { createShortUrl } from '../../short-urls/reducers/shortUrlCreation' import { supportedFeatures } from '../../utils/helpers/features'; import { createAsyncThunk } from '../../utils/helpers/redux'; import { createNewVisits } from '../../visits/reducers/visitCreation'; -import type { CreateVisit, Stats } from '../../visits/types'; +import type { CreateVisit } from '../../visits/types'; import type { TagStats } from '../data'; import { tagDeleted } from './tagDelete'; import { tagEdited } from './tagEdit'; @@ -39,7 +39,8 @@ const initialState: TagsList = { error: false, }; -type TagIncrease = [string, number]; +type TagIncreaseRecord = Record; +type TagIncrease = [string, { bots: number; nonBots: number }]; const renameTag = (oldName: string, newName: string) => (tag: string) => (tag === oldName ? newName : tag); const rejectTag = (tags: string[], tagToReject: string) => reject((tag) => tag === tagToReject, tags); @@ -48,21 +49,34 @@ const increaseVisitsForTags = (tags: TagIncrease[], stats: TagsStatsMap) => tags return theStats; } + const { bots, nonBots } = increase; const tagStats = theStats[tag]; - // TODO take into consideration bots, nonBots and total return { ...theStats, [tag]: { ...tagStats, - visitsCount: tagStats.visitsCount + increase, + visitsSummary: tagStats.visitsSummary && { + total: tagStats.visitsSummary.total + bots + nonBots, + bots: tagStats.visitsSummary.bots + bots, + nonBots: tagStats.visitsSummary.nonBots + nonBots, + }, + visitsCount: tagStats.visitsCount + bots + nonBots, }, }; }, { ...stats }); const calculateVisitsPerTag = (createdVisits: CreateVisit[]): TagIncrease[] => Object.entries( - createdVisits.reduce((acc, { shortUrl }) => { + createdVisits.reduce((acc, { shortUrl, visit }) => { shortUrl?.tags.forEach((tag) => { - acc[tag] = (acc[tag] || 0) + 1; + if (!acc[tag]) { + acc[tag] = { bots: 0, nonBots: 0 }; + } + + if (visit.potentialBot) { + acc[tag].bots += 1; + } else { + acc[tag].nonBots += 1; + } }); return acc; diff --git a/test/tags/reducers/tagsList.test.ts b/test/tags/reducers/tagsList.test.ts index 54746842..d8f9cfe1 100644 --- a/test/tags/reducers/tagsList.test.ts +++ b/test/tags/reducers/tagsList.test.ts @@ -11,6 +11,8 @@ import { listTags as listTagsCreator, tagsListReducerCreator, } from '../../../src/tags/reducers/tagsList'; +import { createNewVisits } from '../../../src/visits/reducers/visitCreation'; +import type { CreateVisit, Visit } from '../../../src/visits/types'; describe('tagsListReducer', () => { const state = (props: Partial) => Mock.of(props); @@ -118,6 +120,75 @@ describe('tagsListReducer', () => { tags: expectedTags, }); }); + + it('increases amounts when visits are created', () => { + const createdVisits = [ + Mock.of({ + shortUrl: Mock.of({ tags: ['foo', 'bar'] }), + visit: Mock.of({ potentialBot: true }), + }), + Mock.of({ + shortUrl: Mock.of({ tags: ['foo', 'bar'] }), + visit: Mock.all(), + }), + Mock.of({ + shortUrl: Mock.of({ tags: ['bar'] }), + visit: Mock.all(), + }), + Mock.of({ + shortUrl: Mock.of({ tags: ['baz'] }), + visit: Mock.of({ potentialBot: true }), + }), + ]; + const tagStats = (total: number) => ({ + shortUrlsCount: 1, + visitsCount: total, + visitsSummary: { + total, + nonBots: total - 10, + bots: 10, + }, + }); + const stateBefore = state({ + stats: { + foo: tagStats(100), + bar: tagStats(200), + baz: tagStats(150), + }, + }); + + expect(reducer(stateBefore, createNewVisits(createdVisits))).toEqual(expect.objectContaining({ + stats: { + foo: { + shortUrlsCount: 1, + visitsCount: 100 + 2, + visitsSummary: { + total: 100 + 2, + nonBots: 90 + 1, + bots: 10 + 1, + }, + }, + bar: { + shortUrlsCount: 1, + visitsCount: 200 + 3, + visitsSummary: { + total: 200 + 3, + nonBots: 190 + 2, + bots: 10 + 1, + }, + }, + baz: { + shortUrlsCount: 1, + visitsCount: 150 + 1, + visitsSummary: { + total: 150 + 1, + nonBots: 140, + bots: 10 + 1, + }, + }, + }, + })); + }); }); describe('filterTags', () => {