diff --git a/src/server/models/functions/index.ts b/src/server/models/functions.ts similarity index 69% rename from src/server/models/functions/index.ts rename to src/server/models/functions.ts index d277bfd7c..519a29e67 100644 --- a/src/server/models/functions/index.ts +++ b/src/server/models/functions.ts @@ -1,7 +1,7 @@ import { QueryTypes } from 'sequelize' -import { sequelize } from '../../util/sequelize' -import { updateLinkStatistics } from './updateLinkStatistics' +import { sequelize } from '../util/sequelize' +import { updateLinkStatistics } from '../repositories/LinkStatisticsRepository' /** * Syncs database functions. diff --git a/src/server/models/functions/updateLinkStatistics.ts b/src/server/models/functions/updateLinkStatistics.ts deleted file mode 100644 index 8a91cbe30..000000000 --- a/src/server/models/functions/updateLinkStatistics.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { Url } from '../url' -import { Clicks } from '../statistics/daily' -import { Devices } from '../statistics/devices' -import { WeekdayClicks } from '../statistics/weekday' - -// Get the relevant table names from their models. -const urlTable = Url.getTableName() -const devicesTable = Devices.getTableName() -const clicksTable = Clicks.getTableName() -const weekdayTable = WeekdayClicks.getTableName() - -/** - * This function is used to update the relevant link statistics tables, when called. - */ -export const updateLinkStatistics = `CREATE OR REPLACE FUNCTION update_link_statistics (inputShortUrl text, device text) -RETURNS void AS $$ -BEGIN --- Update total clicks. -UPDATE "${urlTable}" SET "clicks" = "${urlTable}"."clicks" + 1 -WHERE "shortUrl" = inputShortUrl; --- Update devices clicks. -IF device='mobile' THEN - INSERT INTO "${devicesTable}" ("shortUrl", "mobile", "tablet", "desktop", "others", "createdAt", "updatedAt") - VALUES (inputShortUrl, 1, 0, 0, 0, current_timestamp, current_timestamp) - ON CONFLICT ("shortUrl") - DO UPDATE SET "mobile" = "${devicesTable}"."mobile" + 1; -ELSIF device='tablet' THEN - INSERT INTO "${devicesTable}" ("shortUrl", "mobile", "tablet", "desktop", "others", "createdAt", "updatedAt") - VALUES (inputShortUrl, 0, 1, 0, 0, current_timestamp, current_timestamp) - ON CONFLICT ("shortUrl") - DO UPDATE SET "tablet" = "${devicesTable}"."tablet" + 1; -ELSIF device='desktop' THEN - INSERT INTO "${devicesTable}" ("shortUrl", "mobile", "tablet", "desktop", "others", "createdAt", "updatedAt") - VALUES (inputShortUrl, 0, 0, 1, 0, current_timestamp, current_timestamp) - ON CONFLICT ("shortUrl") - DO UPDATE SET "desktop" = "${devicesTable}"."desktop" + 1; -ELSIF device='others' THEN - INSERT INTO "${devicesTable}" ("shortUrl", "mobile", "tablet", "desktop", "others", "createdAt", "updatedAt") - VALUES (inputShortUrl, 0, 0, 0, 1, current_timestamp, current_timestamp) - ON CONFLICT ("shortUrl") - DO UPDATE SET "others" = "${devicesTable}"."others" + 1; -END IF; --- Update daily clicks. -INSERT INTO "${clicksTable}" ("shortUrl", "date", "clicks", "createdAt", "updatedAt") -VALUES (inputShortUrl, current_timestamp::date, 1, current_timestamp, current_timestamp) -ON CONFLICT ("shortUrl", "date") -DO UPDATE SET "clicks" = "${clicksTable}"."clicks" + 1; --- Update weekday clicks. -INSERT INTO "${weekdayTable}" ("shortUrl", "weekday", "hours", "clicks", "createdAt", "updatedAt") -VALUES (inputShortUrl, extract(dow from current_timestamp), extract(hour from current_timestamp), 1, current_timestamp, current_timestamp) -ON CONFLICT ("shortUrl", "weekday", "hours") -DO UPDATE SET "clicks" = "${weekdayTable}"."clicks" + 1; -END; $$ LANGUAGE plpgsql; -` - -export default updateLinkStatistics diff --git a/src/server/repositories/LinkStatisticsRepository.ts b/src/server/repositories/LinkStatisticsRepository.ts index af4713450..64cd0732f 100644 --- a/src/server/repositories/LinkStatisticsRepository.ts +++ b/src/server/repositories/LinkStatisticsRepository.ts @@ -12,6 +12,56 @@ import { getLocalDayGroup } from '../util/time' import { sequelize } from '../util/sequelize' import { DeviceType } from '../services/interfaces/DeviceCheckServiceInterface' +// Get the relevant table names from their models. +const urlTable = Url.getTableName() +const devicesTable = Devices.getTableName() +const clicksTable = Clicks.getTableName() +const weekdayTable = WeekdayClicks.getTableName() + +/** + * This function is used to update the relevant link statistics tables, when called. + */ +export const updateLinkStatistics = `CREATE OR REPLACE FUNCTION update_link_statistics (inputShortUrl text, device text) +RETURNS void AS $$ +BEGIN +-- Update total clicks. +UPDATE "${urlTable}" SET "clicks" = "${urlTable}"."clicks" + 1 +WHERE "shortUrl" = inputShortUrl; +-- Update devices clicks. +IF device='mobile' THEN + INSERT INTO "${devicesTable}" ("shortUrl", "mobile", "tablet", "desktop", "others", "createdAt", "updatedAt") + VALUES (inputShortUrl, 1, 0, 0, 0, current_timestamp, current_timestamp) + ON CONFLICT ("shortUrl") + DO UPDATE SET "mobile" = "${devicesTable}"."mobile" + 1; +ELSIF device='tablet' THEN + INSERT INTO "${devicesTable}" ("shortUrl", "mobile", "tablet", "desktop", "others", "createdAt", "updatedAt") + VALUES (inputShortUrl, 0, 1, 0, 0, current_timestamp, current_timestamp) + ON CONFLICT ("shortUrl") + DO UPDATE SET "tablet" = "${devicesTable}"."tablet" + 1; +ELSIF device='desktop' THEN + INSERT INTO "${devicesTable}" ("shortUrl", "mobile", "tablet", "desktop", "others", "createdAt", "updatedAt") + VALUES (inputShortUrl, 0, 0, 1, 0, current_timestamp, current_timestamp) + ON CONFLICT ("shortUrl") + DO UPDATE SET "desktop" = "${devicesTable}"."desktop" + 1; +ELSIF device='others' THEN + INSERT INTO "${devicesTable}" ("shortUrl", "mobile", "tablet", "desktop", "others", "createdAt", "updatedAt") + VALUES (inputShortUrl, 0, 0, 0, 1, current_timestamp, current_timestamp) + ON CONFLICT ("shortUrl") + DO UPDATE SET "others" = "${devicesTable}"."others" + 1; +END IF; +-- Update daily clicks. +INSERT INTO "${clicksTable}" ("shortUrl", "date", "clicks", "createdAt", "updatedAt") +VALUES (inputShortUrl, current_timestamp::date, 1, current_timestamp, current_timestamp) +ON CONFLICT ("shortUrl", "date") +DO UPDATE SET "clicks" = "${clicksTable}"."clicks" + 1; +-- Update weekday clicks. +INSERT INTO "${weekdayTable}" ("shortUrl", "weekday", "hours", "clicks", "createdAt", "updatedAt") +VALUES (inputShortUrl, extract(dow from current_timestamp), extract(hour from current_timestamp), 1, current_timestamp, current_timestamp) +ON CONFLICT ("shortUrl", "weekday", "hours") +DO UPDATE SET "clicks" = "${weekdayTable}"."clicks" + 1; +END; $$ LANGUAGE plpgsql; +` + export type UrlStats = UrlType & { DeviceClicks?: DevicesType DailyClicks: ClicksType[]