Skip to content

Commit

Permalink
refactor(daily-clicks): rename clicks to daily clicks
Browse files Browse the repository at this point in the history
To make way for the new url click table
  • Loading branch information
yong-jie committed Dec 6, 2020
1 parent b71bb37 commit 8ed6c13
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/server/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { sequelize } from '../util/sequelize'
import { Url, UrlHistory } from './url'
import { User } from './user'
import { Clicks } from './statistics/daily'
import { DailyClicks } from './statistics/daily'
import { WeekdayClicks } from './statistics/weekday'
import { Devices } from './statistics/devices'
import { syncFunctions } from './functions'
Expand All @@ -15,10 +15,10 @@ User.hasMany(UrlHistory, { foreignKey: { allowNull: false } })
UrlHistory.belongsTo(User, { foreignKey: { allowNull: false } })

// A Url record can have various rows of relevant statistics.
Url.hasMany(Clicks, { foreignKey: 'shortUrl', as: 'DailyClicks' })
Url.hasMany(DailyClicks, { foreignKey: 'shortUrl', as: 'DailyClicks' })
Url.hasMany(WeekdayClicks, { foreignKey: 'shortUrl', as: 'WeekdayClicks' })
Url.hasOne(Devices, { foreignKey: 'shortUrl', as: 'DeviceClicks' })
Clicks.belongsTo(Url, { foreignKey: 'shortUrl' })
DailyClicks.belongsTo(Url, { foreignKey: 'shortUrl' })
WeekdayClicks.belongsTo(Url, { foreignKey: 'shortUrl' })
Devices.belongsTo(Url, { foreignKey: 'shortUrl' })

Expand Down
39 changes: 21 additions & 18 deletions src/server/models/statistics/daily.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,35 @@ import Sequelize from 'sequelize'
import { sequelize } from '../../util/sequelize'
import { IdType } from '../../../types/server/models'

export interface ClicksType extends IdType, Sequelize.Model {
export interface DailyClicksType extends IdType, Sequelize.Model {
readonly shortUrl: string
readonly date: string
readonly clicks: number
readonly createdAt: string
readonly updatedAt: string
}

type ClicksTypeStatic = typeof Sequelize.Model & {
new (values?: object, options?: Sequelize.BuildOptions): ClicksType
type DailyClicksTypeStatic = typeof Sequelize.Model & {
new (values?: object, options?: Sequelize.BuildOptions): DailyClicksType
}

export const Clicks = <ClicksTypeStatic>sequelize.define('daily_stats', {
shortUrl: {
type: Sequelize.STRING,
primaryKey: true,
validate: {
is: /^[a-z0-9-]+$/,
export const DailyClicks = <DailyClicksTypeStatic>sequelize.define(
'daily_stats',
{
shortUrl: {
type: Sequelize.STRING,
primaryKey: true,
validate: {
is: /^[a-z0-9-]+$/,
},
},
date: {
type: Sequelize.DATEONLY,
primaryKey: true,
},
clicks: {
type: Sequelize.INTEGER,
defaultValue: 0,
},
},
date: {
type: Sequelize.DATEONLY,
primaryKey: true,
},
clicks: {
type: Sequelize.INTEGER,
defaultValue: 0,
},
})
)
10 changes: 5 additions & 5 deletions src/server/repositories/LinkStatisticsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Op, QueryTypes } from 'sequelize'
import _ from 'lodash'

import { Url, UrlType } from '../models/url'
import { Clicks, ClicksType } from '../models/statistics/daily'
import { DailyClicks, DailyClicksType } from '../models/statistics/daily'
import { Devices, DevicesType } from '../models/statistics/devices'
import { WeekdayClicks, WeekdayClicksType } from '../models/statistics/weekday'
import { LinkStatisticsInterface } from '../../shared/interfaces/link-statistics'
Expand All @@ -15,7 +15,7 @@ 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 clicksTable = DailyClicks.getTableName()
const weekdayTable = WeekdayClicks.getTableName()
const timeZone = 'Asia/Singapore'

Expand Down Expand Up @@ -66,7 +66,7 @@ END; $$ LANGUAGE plpgsql;
export type UrlStats = UrlType & {
Url: UrlType
DeviceClicks?: DevicesType
DailyClicks: ClicksType[]
DailyClicks: DailyClicksType[]
WeekdayClicks: WeekdayClicksType[]
}

Expand All @@ -81,7 +81,7 @@ export class LinkStatisticsRepository
include: [
{ model: Devices, as: 'DeviceClicks' },
{
model: Clicks,
model: DailyClicks,
as: 'DailyClicks',
where: {
date: {
Expand All @@ -98,7 +98,7 @@ export class LinkStatisticsRepository
},
],
order: [
[{ model: Clicks, as: 'DailyClicks' }, 'date', 'ASC'],
[{ model: DailyClicks, as: 'DailyClicks' }, 'date', 'ASC'],
[{ model: WeekdayClicks, as: 'WeekdayClicks' }, 'weekday', 'ASC'],
[{ model: WeekdayClicks, as: 'WeekdayClicks' }, 'hours', 'ASC'],
],
Expand Down

0 comments on commit 8ed6c13

Please sign in to comment.