-
Notifications
You must be signed in to change notification settings - Fork 731
Expand file tree
/
Copy pathintegrationProgressRepository.ts
More file actions
194 lines (171 loc) · 5.06 KB
/
integrationProgressRepository.ts
File metadata and controls
194 lines (171 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { QueryTypes } from 'sequelize'
import { Repos } from '@/serverless/integrations/types/regularTypes'
import { GitHubStats } from '@/serverless/integrations/usecases/github/rest/getRemoteStats'
import { IRepositoryOptions } from './IRepositoryOptions'
import SequelizeRepository from './sequelizeRepository'
class IntegrationProgressRepository {
static createPayloadWithActivityType(
activityTypes: string[],
repos: Repos,
segments: string[] = [],
) {
return {
filter: {
and: [
{ platform: { in: ['github'] } },
{ or: repos.map((repo) => ({ channel: { eq: repo.url } })) },
{ type: { in: activityTypes } },
],
},
segmentIds: segments,
}
}
static async getPendingStreamsCount(integrationId: string, options: IRepositoryOptions) {
const transaction = options.transaction
const seq = SequelizeRepository.getSequelize(options)
const lastRunId = await IntegrationProgressRepository.getLastRunId(integrationId, options)
if (!lastRunId) {
return 0
}
const result = await seq.query(
`
select count(*) as "total"
from integration.streams
where "integrationId" = :integrationId
and "runId" = :lastRunId
and "state" = 'pending'
`,
{
replacements: {
integrationId,
lastRunId,
},
type: QueryTypes.SELECT,
transaction,
},
)
return (result[0] as any).total as number
}
static async getLastRunId(integrationId: string, options: IRepositoryOptions) {
const transaction = options.transaction
const seq = SequelizeRepository.getSequelize(options)
const result = await seq.query(
`
select id
from integration.runs
where "integrationId" = :integrationId
order by "createdAt" desc
limit 1
`,
{
replacements: {
integrationId,
},
type: QueryTypes.SELECT,
transaction,
},
)
if (result.length === 0) {
return null
}
return (result[0] as any).id as string
}
static async getDbStatsForGithub(): Promise<GitHubStats> {
// const tb = new TinybirdClient()
// const promises: Promise<{ data: Counter }>[] = [
// queryActivitiesCounter(
// IntegrationProgressRepository.createPayloadWithActivityType(['star'], repos, segments),
// tb,
// ),
// queryActivitiesCounter(
// IntegrationProgressRepository.createPayloadWithActivityType(['unstar'], repos, segments),
// tb,
// ),
// queryActivitiesCounter(
// {
// ...IntegrationProgressRepository.createPayloadWithActivityType(['fork'], repos, segments),
// indirectFork: 1,
// },
// tb,
// ),
// queryActivitiesCounter(
// IntegrationProgressRepository.createPayloadWithActivityType(
// ['issues-opened'],
// repos,
// segments,
// ),
// tb,
// ),
// queryActivitiesCounter(
// IntegrationProgressRepository.createPayloadWithActivityType(
// ['pull_request-opened'],
// repos,
// segments,
// ),
// tb,
// ),
// ]
// const result = await Promise.all(promises)
return {
// stars: (result[0]?.data?.[0]?.count ?? 0) - (result[1]?.data?.[0]?.count ?? 0),
// forks: result[2]?.data?.[0]?.count ?? 0,
// totalIssues: result[3]?.data?.[0]?.count ?? 0,
// totalPRs: result[4]?.data?.[0]?.count ?? 0,
stars: 0,
forks: 0,
totalIssues: 0,
totalPRs: 0,
}
}
static async getAllIntegrationsInProgressForSegment(
options: IRepositoryOptions,
): Promise<string[]> {
const transaction = options.transaction
const seq = SequelizeRepository.getSequelize(options)
const segment = SequelizeRepository.getStrictlySingleActiveSegment(options)
const result = await seq.query(
`
select id
from integrations
where
"status" = 'in-progress'
and "segmentId" = :segmentId
and "deletedAt" is null
`,
{
replacements: {
segmentId: segment.id,
},
type: QueryTypes.SELECT,
transaction,
},
)
return result.map((r: any) => r.id)
}
static async getAllIntegrationsInProgressForMultipleSegments(
options: IRepositoryOptions,
): Promise<string[]> {
const transaction = options.transaction
const seq = SequelizeRepository.getSequelize(options)
const segments = SequelizeRepository.getCurrentSegments(options)
const result = await seq.query(
`
select id
from integrations
where
"status" = 'in-progress'
and "segmentId" in (:segmentIds)
and "deletedAt" is null
`,
{
replacements: {
segmentIds: segments.map((s) => s.id),
},
type: QueryTypes.SELECT,
transaction,
},
)
return result.map((r: any) => r.id)
}
}
export default IntegrationProgressRepository