Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/cron/updateHighlightedViews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,24 @@ import { TrendingSource } from '../entity/TrendingSource';
import { TrendingTag } from '../entity/TrendingTag';
import { PopularVideoPost } from '../entity/PopularVideoPost';
import { UserStats } from '../entity';
import { TrendingUserPost } from '../entity/TrendingUserPost';
import { TrendingUserSource } from '../entity/TrendingUserSource';
import { PopularUserPost } from '../entity/PopularUserPost';
import { PopularUserSource } from '../entity/PopularUserSource';

const cron: Cron = {
name: 'update-highlighted-views',
handler: async (con, logger) => {
const viewsToRefresh = [
TrendingPost,
TrendingSource,
TrendingUserPost,
TrendingUserSource,
TrendingTag,
PopularPost,
PopularSource,
PopularUserPost,
PopularUserSource,
PopularTag,
PopularVideoPost,
PopularVideoSource,
Expand Down
13 changes: 8 additions & 5 deletions src/entity/PopularPost.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { DataSource, ViewColumn, ViewEntity } from 'typeorm';
import { SourceType } from './Source';

@ViewEntity({
materialized: true,
expression: (dataSource: DataSource) =>
dataSource
.createQueryBuilder()
.select('"sourceId"')
.addSelect('"tagsStr"')
.addSelect('"createdAt"')
.addSelect('upvotes - downvotes r')
.select('p."sourceId"')
.addSelect('p."tagsStr"')
.addSelect('p."createdAt"')
.addSelect('p.upvotes - p.downvotes', 'r')
.from('post', 'p')
.innerJoin('source', 's', 's.id = p."sourceId"')
.where(
'not p.private and p."createdAt" > now() - interval \'60 day\' and upvotes > downvotes',
'not p.private and p."createdAt" > now() - interval \'60 day\' and p.upvotes > p.downvotes',
)
.andWhere('s.type != :type', { type: SourceType.User })
.orderBy('r', 'DESC')
.limit(1000),
})
Expand Down
34 changes: 34 additions & 0 deletions src/entity/PopularUserPost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { DataSource, ViewColumn, ViewEntity } from 'typeorm';
import { SourceType } from './Source';

@ViewEntity({
materialized: true,
expression: (dataSource: DataSource) =>
dataSource
.createQueryBuilder()
.select('p."sourceId"')
.addSelect('p."tagsStr"')
.addSelect('p."createdAt"')
.addSelect('p.upvotes - p.downvotes', 'r')
.from('post', 'p')
.innerJoin('source', 's', 's.id = p."sourceId"')
.where(
'not p.private and p."createdAt" > now() - interval \'60 day\' and p.upvotes > p.downvotes',
)
.andWhere('s.type = :type', { type: SourceType.User })
.orderBy('r', 'DESC')
.limit(1000),
})
export class PopularUserPost {
@ViewColumn()
sourceId: string;

@ViewColumn()
tagsStr: string;

@ViewColumn()
createdAt: Date;

@ViewColumn()
r: number;
}
22 changes: 22 additions & 0 deletions src/entity/PopularUserSource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { DataSource, ViewColumn, ViewEntity } from 'typeorm';
import { PopularUserPost } from './PopularUserPost';

@ViewEntity({
materialized: true,
expression: (dataSource: DataSource) =>
dataSource
.createQueryBuilder()
.select('"sourceId"')
.addSelect('avg(r) r')
.from(PopularUserPost, 'base')
.groupBy('"sourceId"')
.having('count(*) > 5')
.orderBy('r', 'DESC'),
})
export class PopularUserSource {
@ViewColumn()
sourceId: string;

@ViewColumn()
r: number;
}
14 changes: 9 additions & 5 deletions src/entity/TrendingPost.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import { DataSource, ViewColumn, ViewEntity } from 'typeorm';
import { SourceType } from './Source';

@ViewEntity({
materialized: true,
expression: (dataSource: DataSource) =>
dataSource
.createQueryBuilder()
.select('"sourceId"')
.addSelect('"tagsStr"')
.addSelect('"createdAt"')
.select('p."sourceId"')
.addSelect('p."tagsStr"')
.addSelect('p."createdAt"')
.addSelect(
`log(10, upvotes - downvotes) + extract(epoch from ("createdAt" - now() + interval '7 days')) / 200000 r`,
`log(10, p.upvotes - p.downvotes) + extract(epoch from (p."createdAt" - now() + interval '7 days')) / 200000`,
'r',
)
.from('post', 'p')
.innerJoin('source', 's', 's.id = p."sourceId"')
.where(
`not p.private and p."createdAt" > now() - interval '7 day' and upvotes > downvotes`,
`not p.private and p."createdAt" > now() - interval '7 day' and p.upvotes > p.downvotes`,
)
.andWhere('s.type != :type', { type: SourceType.User })
.orderBy('r', 'DESC')
.limit(100),
})
Expand Down
37 changes: 37 additions & 0 deletions src/entity/TrendingUserPost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { DataSource, ViewColumn, ViewEntity } from 'typeorm';
import { SourceType } from './Source';

@ViewEntity({
materialized: true,
expression: (dataSource: DataSource) =>
dataSource
.createQueryBuilder()
.select('p."sourceId"')
.addSelect('p."tagsStr"')
.addSelect('p."createdAt"')
.addSelect(
`log(10, p.upvotes - p.downvotes) + extract(epoch from (p."createdAt" - now() + interval '7 days')) / 200000`,
'r',
)
.from('post', 'p')
.innerJoin('source', 's', 's.id = p."sourceId"')
.where(
`not p.private and p."createdAt" > now() - interval '7 day' and p.upvotes > p.downvotes`,
)
.andWhere('s.type = :type', { type: SourceType.User })
.orderBy('r', 'DESC')
.limit(100),
})
export class TrendingUserPost {
@ViewColumn()
sourceId: string;

@ViewColumn()
tagsStr: string;

@ViewColumn()
createdAt: Date;

@ViewColumn()
r: number;
}
22 changes: 22 additions & 0 deletions src/entity/TrendingUserSource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { DataSource, ViewColumn, ViewEntity } from 'typeorm';
import { TrendingUserPost } from './TrendingUserPost';

@ViewEntity({
materialized: true,
expression: (dataSource: DataSource) =>
dataSource
.createQueryBuilder()
.select('"sourceId"')
.addSelect('avg(r) r')
.from(TrendingUserPost, 'base')
.groupBy('"sourceId"')
.having('count(*) > 1')
.orderBy('r', 'DESC'),
})
export class TrendingUserSource {
@ViewColumn()
sourceId: string;

@ViewColumn()
r: number;
}
22 changes: 22 additions & 0 deletions src/graphorm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1660,6 +1660,28 @@ const obj = new GraphORM({
},
},
},
TrendingUserSource: {
fields: {
handle: {
customQuery: (_, alias, qb) =>
qb
.select('u.username')
.from(User, 'u')
.where(`u.id = ${alias}."sourceId"`),
},
},
},
PopularUserSource: {
fields: {
handle: {
customQuery: (_, alias, qb) =>
qb
.select('u.username')
.from(User, 'u')
.where(`u.id = ${alias}."sourceId"`),
},
},
},
});

export default obj;
Loading
Loading