Skip to content

Commit 2aede8c

Browse files
committed
feat: add trending and popular user sources
1 parent f6008dc commit 2aede8c

File tree

8 files changed

+484
-10
lines changed

8 files changed

+484
-10
lines changed

src/entity/PopularPost.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import { DataSource, ViewColumn, ViewEntity } from 'typeorm';
2+
import { SourceType } from './Source';
23

34
@ViewEntity({
45
materialized: true,
56
expression: (dataSource: DataSource) =>
67
dataSource
78
.createQueryBuilder()
8-
.select('"sourceId"')
9-
.addSelect('"tagsStr"')
10-
.addSelect('"createdAt"')
11-
.addSelect('upvotes - downvotes r')
9+
.select('p."sourceId"')
10+
.addSelect('p."tagsStr"')
11+
.addSelect('p."createdAt"')
12+
.addSelect('p.upvotes - p.downvotes', 'r')
1213
.from('post', 'p')
14+
.innerJoin('source', 's', 's.id = p."sourceId"')
1315
.where(
14-
'not p.private and p."createdAt" > now() - interval \'60 day\' and upvotes > downvotes',
16+
'not p.private and p."createdAt" > now() - interval \'60 day\' and p.upvotes > p.downvotes',
1517
)
18+
.andWhere('s.type != :type', { type: SourceType.User })
1619
.orderBy('r', 'DESC')
1720
.limit(1000),
1821
})

src/entity/PopularUserPost.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { DataSource, ViewColumn, ViewEntity } from 'typeorm';
2+
import { SourceType } from './Source';
3+
4+
@ViewEntity({
5+
materialized: true,
6+
expression: (dataSource: DataSource) =>
7+
dataSource
8+
.createQueryBuilder()
9+
.select('p."sourceId"')
10+
.addSelect('p."tagsStr"')
11+
.addSelect('p."createdAt"')
12+
.addSelect('p.upvotes - p.downvotes', 'r')
13+
.from('post', 'p')
14+
.innerJoin('source', 's', 's.id = p."sourceId"')
15+
.where(
16+
'not p.private and p."createdAt" > now() - interval \'60 day\' and p.upvotes > p.downvotes',
17+
)
18+
.andWhere('s.type = :type', { type: SourceType.User })
19+
.orderBy('r', 'DESC')
20+
.limit(1000),
21+
})
22+
export class PopularUserPost {
23+
@ViewColumn()
24+
sourceId: string;
25+
26+
@ViewColumn()
27+
tagsStr: string;
28+
29+
@ViewColumn()
30+
createdAt: Date;
31+
32+
@ViewColumn()
33+
r: number;
34+
}

src/entity/PopularUserSource.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { DataSource, ViewColumn, ViewEntity } from 'typeorm';
2+
import { PopularUserPost } from './PopularUserPost';
3+
4+
@ViewEntity({
5+
materialized: true,
6+
expression: (dataSource: DataSource) =>
7+
dataSource
8+
.createQueryBuilder()
9+
.select('"sourceId"')
10+
.addSelect('avg(r) r')
11+
.from(PopularUserPost, 'base')
12+
.groupBy('"sourceId"')
13+
.having('count(*) > 5')
14+
.orderBy('r', 'DESC'),
15+
})
16+
export class PopularUserSource {
17+
@ViewColumn()
18+
sourceId: string;
19+
20+
@ViewColumn()
21+
r: number;
22+
}

src/entity/TrendingPost.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
import { DataSource, ViewColumn, ViewEntity } from 'typeorm';
2+
import { SourceType } from './Source';
23

34
@ViewEntity({
45
materialized: true,
56
expression: (dataSource: DataSource) =>
67
dataSource
78
.createQueryBuilder()
8-
.select('"sourceId"')
9-
.addSelect('"tagsStr"')
10-
.addSelect('"createdAt"')
9+
.select('p."sourceId"')
10+
.addSelect('p."tagsStr"')
11+
.addSelect('p."createdAt"')
1112
.addSelect(
12-
`log(10, upvotes - downvotes) + extract(epoch from ("createdAt" - now() + interval '7 days')) / 200000 r`,
13+
`log(10, p.upvotes - p.downvotes) + extract(epoch from (p."createdAt" - now() + interval '7 days')) / 200000`,
14+
'r',
1315
)
1416
.from('post', 'p')
17+
.innerJoin('source', 's', 's.id = p."sourceId"')
1518
.where(
16-
`not p.private and p."createdAt" > now() - interval '7 day' and upvotes > downvotes`,
19+
`not p.private and p."createdAt" > now() - interval '7 day' and p.upvotes > p.downvotes`,
1720
)
21+
.andWhere('s.type != :type', { type: SourceType.User })
1822
.orderBy('r', 'DESC')
1923
.limit(100),
2024
})

src/entity/TrendingUserPost.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { DataSource, ViewColumn, ViewEntity } from 'typeorm';
2+
import { SourceType } from './Source';
3+
4+
@ViewEntity({
5+
materialized: true,
6+
expression: (dataSource: DataSource) =>
7+
dataSource
8+
.createQueryBuilder()
9+
.select('p."sourceId"')
10+
.addSelect('p."tagsStr"')
11+
.addSelect('p."createdAt"')
12+
.addSelect(
13+
`log(10, p.upvotes - p.downvotes) + extract(epoch from (p."createdAt" - now() + interval '7 days')) / 200000`,
14+
'r',
15+
)
16+
.from('post', 'p')
17+
.innerJoin('source', 's', 's.id = p."sourceId"')
18+
.where(
19+
`not p.private and p."createdAt" > now() - interval '7 day' and p.upvotes > p.downvotes`,
20+
)
21+
.andWhere('s.type = :type', { type: SourceType.User })
22+
.orderBy('r', 'DESC')
23+
.limit(100),
24+
})
25+
export class TrendingUserPost {
26+
@ViewColumn()
27+
sourceId: string;
28+
29+
@ViewColumn()
30+
tagsStr: string;
31+
32+
@ViewColumn()
33+
createdAt: Date;
34+
35+
@ViewColumn()
36+
r: number;
37+
}

src/entity/TrendingUserSource.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { DataSource, ViewColumn, ViewEntity } from 'typeorm';
2+
import { TrendingUserPost } from './TrendingUserPost';
3+
4+
@ViewEntity({
5+
materialized: true,
6+
expression: (dataSource: DataSource) =>
7+
dataSource
8+
.createQueryBuilder()
9+
.select('"sourceId"')
10+
.addSelect('avg(r) r')
11+
.from(TrendingUserPost, 'base')
12+
.groupBy('"sourceId"')
13+
.having('count(*) > 1')
14+
.orderBy('r', 'DESC'),
15+
})
16+
export class TrendingUserSource {
17+
@ViewColumn()
18+
sourceId: string;
19+
20+
@ViewColumn()
21+
r: number;
22+
}

0 commit comments

Comments
 (0)