|
1 | 1 | import { QueryTypes } from 'sequelize' |
2 | 2 |
|
| 3 | +import { queryActivitiesCounter } from '@crowd/data-access-layer' |
| 4 | +import { Counter, TinybirdClient } from '@crowd/data-access-layer/src/database' |
| 5 | + |
3 | 6 | import { Repos } from '@/serverless/integrations/types/regularTypes' |
4 | 7 | import { GitHubStats } from '@/serverless/integrations/usecases/github/rest/getRemoteStats' |
5 | 8 |
|
6 | 9 | import { IRepositoryOptions } from './IRepositoryOptions' |
7 | 10 | import SequelizeRepository from './sequelizeRepository' |
8 | 11 |
|
9 | 12 | class IntegrationProgressRepository { |
| 13 | + static createPayloadWithActivityType( |
| 14 | + activityTypes: string[], |
| 15 | + repos: Repos, |
| 16 | + segments: string[] = [], |
| 17 | + ) { |
| 18 | + return { |
| 19 | + filter: { |
| 20 | + and: [ |
| 21 | + { platform: { in: ['github'] } }, |
| 22 | + { or: repos.map((repo) => ({ channel: { eq: repo.url } })) }, |
| 23 | + { type: { in: activityTypes } }, |
| 24 | + ], |
| 25 | + }, |
| 26 | + segmentIds: segments, |
| 27 | + } |
| 28 | + } |
| 29 | + |
10 | 30 | static async getPendingStreamsCount(integrationId: string, options: IRepositoryOptions) { |
11 | 31 | const transaction = options.transaction |
12 | 32 | const seq = SequelizeRepository.getSequelize(options) |
@@ -66,89 +86,56 @@ class IntegrationProgressRepository { |
66 | 86 | return (result[0] as any).id as string |
67 | 87 | } |
68 | 88 |
|
69 | | - static async getDbStatsForGithub( |
70 | | - repos: Repos, |
71 | | - options: IRepositoryOptions, |
72 | | - ): Promise<GitHubStats> { |
73 | | - // TODO questdb to tinybird remove - it's here for linter to be happy |
74 | | - options.log.info('getDbStatsForGithub', { repos }) |
75 | | - // TODO questdb to tinybird |
76 | | - // const starsQuery = ` |
77 | | - // SELECT COUNT_DISTINCT("sourceId") AS count |
78 | | - // FROM activities |
79 | | - // WHERE platform = 'github' |
80 | | - // AND type = 'star' |
81 | | - // AND "deletedAt" IS NULL |
82 | | - // AND channel IN ($(remotes:csv)) |
83 | | - // ` |
84 | | - |
85 | | - // const unstarsQuery = ` |
86 | | - // SELECT COUNT_DISTINCT("sourceId") AS count |
87 | | - // FROM activities |
88 | | - // WHERE platform = 'github' |
89 | | - // AND type = 'unstar' |
90 | | - // AND "deletedAt" IS NULL |
91 | | - // AND channel IN ($(remotes:csv)) |
92 | | - // ` |
93 | | - |
94 | | - // const forksQuery = ` |
95 | | - // SELECT COUNT_DISTINCT("sourceId") AS count |
96 | | - // FROM activities |
97 | | - // WHERE platform = 'github' |
98 | | - // AND type = 'fork' |
99 | | - // AND "deletedAt" IS NULL |
100 | | - // AND "gitIsIndirectFork" != TRUE |
101 | | - // AND channel IN ($(remotes:csv)) |
102 | | - // ` |
103 | | - |
104 | | - // const issuesOpenedQuery = ` |
105 | | - // SELECT COUNT_DISTINCT("sourceId") AS count |
106 | | - // FROM activities |
107 | | - // WHERE platform = 'github' |
108 | | - // AND type = 'issues-opened' |
109 | | - // AND "deletedAt" IS NULL |
110 | | - // AND channel IN ($(remotes:csv)) |
111 | | - // ` |
112 | | - |
113 | | - // const prOpenedQuery = ` |
114 | | - // SELECT COUNT_DISTINCT("sourceId") AS count |
115 | | - // FROM activities |
116 | | - // WHERE platform = 'github' |
117 | | - // AND type = 'pull_request-opened' |
118 | | - // AND "deletedAt" IS NULL |
119 | | - // AND channel IN ($(remotes:csv)) |
120 | | - // ` |
121 | | - |
122 | | - // const remotes = repos.map((r) => r.url) |
123 | | - |
124 | | - // const promises: Promise<any[]>[] = [ |
125 | | - // options.qdb.query(starsQuery, { |
126 | | - // remotes, |
127 | | - // }), |
128 | | - // options.qdb.query(unstarsQuery, { |
129 | | - // remotes, |
130 | | - // }), |
131 | | - // options.qdb.query(forksQuery, { |
132 | | - // remotes, |
133 | | - // }), |
134 | | - // options.qdb.query(issuesOpenedQuery, { |
135 | | - // remotes, |
136 | | - // }), |
137 | | - // options.qdb.query(prOpenedQuery, { |
138 | | - // remotes, |
139 | | - // }), |
140 | | - // ] |
141 | | - |
142 | | - // const results = await Promise.all(promises) |
| 89 | + static async getDbStatsForGithub({ |
| 90 | + repos, |
| 91 | + segments, |
| 92 | + }: { |
| 93 | + repos: Repos |
| 94 | + segments: string[] |
| 95 | + }): Promise<GitHubStats> { |
| 96 | + const tb = new TinybirdClient() |
| 97 | + |
| 98 | + const promises: Promise<{ data: Counter }>[] = [ |
| 99 | + queryActivitiesCounter( |
| 100 | + IntegrationProgressRepository.createPayloadWithActivityType(['star'], repos, segments), |
| 101 | + tb, |
| 102 | + ), |
| 103 | + queryActivitiesCounter( |
| 104 | + IntegrationProgressRepository.createPayloadWithActivityType(['unstar'], repos, segments), |
| 105 | + tb, |
| 106 | + ), |
| 107 | + queryActivitiesCounter( |
| 108 | + { |
| 109 | + ...IntegrationProgressRepository.createPayloadWithActivityType(['fork'], repos, segments), |
| 110 | + indirectFork: 1, |
| 111 | + }, |
| 112 | + tb, |
| 113 | + ), |
| 114 | + queryActivitiesCounter( |
| 115 | + IntegrationProgressRepository.createPayloadWithActivityType( |
| 116 | + ['issues-opened'], |
| 117 | + repos, |
| 118 | + segments, |
| 119 | + ), |
| 120 | + tb, |
| 121 | + ), |
| 122 | + queryActivitiesCounter( |
| 123 | + IntegrationProgressRepository.createPayloadWithActivityType( |
| 124 | + ['pull_request-opened'], |
| 125 | + repos, |
| 126 | + segments, |
| 127 | + ), |
| 128 | + tb, |
| 129 | + ), |
| 130 | + ] |
| 131 | + |
| 132 | + const result = await Promise.all(promises) |
| 133 | + |
143 | 134 | return { |
144 | | - // stars: parseInt(results[0][0].count, 10) - parseInt(results[1][0].count, 10), |
145 | | - // forks: parseInt(results[2][0].count, 10), |
146 | | - // totalIssues: parseInt(results[3][0].count, 10), |
147 | | - // totalPRs: parseInt(results[4][0].count, 10), |
148 | | - stars: 0, |
149 | | - forks: 0, |
150 | | - totalIssues: 0, |
151 | | - totalPRs: 0, |
| 135 | + stars: (result[0]?.data?.[0]?.count ?? 0) - (result[1]?.data?.[0]?.count ?? 0), |
| 136 | + forks: result[2]?.data?.[0]?.count ?? 0, |
| 137 | + totalIssues: result[3]?.data?.[0]?.count ?? 0, |
| 138 | + totalPRs: result[4]?.data?.[0]?.count ?? 0, |
152 | 139 | } |
153 | 140 | } |
154 | 141 |
|
|
0 commit comments