Repository navigation
Expand file tree
/
Copy pathindices.ts
More file actions
279 lines (261 loc) · 9.47 KB
/
Copy pathindices.ts
File metadata and controls
279 lines (261 loc) · 9.47 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import {
QueryConfig,
getMergedConfig,
getRepoWhereClauseForClickhouse,
getUserWhereClauseForClickhouse,
getTimeRangeWhereClauseForClickhouse,
getGroupArrayInsertAtClauseForClickhouse,
getGroupTimeClauseForClickhouse,
getGroupIdClauseForClickhouse,
getInnerOrderAndLimit,
getOutterOrderAndLimit
} from './basic';
import * as clickhouse from '../db/clickhouse';
export const ISSUE_COMMENT_WEIGHT = 1;
export const OPEN_ISSUE_WEIGHT = 2;
export const OPEN_PULL_WEIGHT = 3;
export const REVIEW_COMMENT_WEIGHT = 4;
export const PULL_MERGED_WEIGHT = 2;
export const getRepoOpenrank = async (config: QueryConfig) => {
config = getMergedConfig(config);
const whereClause: string[] = [];
const repoWhereClause = await getRepoWhereClauseForClickhouse(config);
if (repoWhereClause) whereClause.push(repoWhereClause);
const timeRangeClause = getTimeRangeWhereClauseForClickhouse(config);
if (timeRangeClause) whereClause.push(timeRangeClause);
const sql = `
SELECT
id,
argMax(name, time) AS name,
${getGroupArrayInsertAtClauseForClickhouse(config, { key: 'openrank' })}
FROM
(
SELECT
${getGroupTimeClauseForClickhouse(config)},
${getGroupIdClauseForClickhouse(config)},
SUM(openrank) AS openrank
FROM gh_repo_openrank
WHERE ${whereClause.join(' AND ')}
GROUP BY id, time
${getInnerOrderAndLimit(config, 'openrank')}
)
GROUP BY id
${getOutterOrderAndLimit(config, 'openrank')}`;
const result: any = await clickhouse.query(sql);
return result.map(row => {
const [id, name, openrank] = row;
return {
id,
name,
openrank,
}
});
}
export const getUserOpenrank = async (config: QueryConfig) => {
config = getMergedConfig(config);
const whereClause: string[] = [];
const userWhereClause = await getUserWhereClauseForClickhouse(config);
if (userWhereClause) whereClause.push(userWhereClause);
const timeRangeClause = getTimeRangeWhereClauseForClickhouse(config);
if (timeRangeClause) whereClause.push(timeRangeClause);
const sql = `
SELECT
id,
argMax(name, time) AS name,
${getGroupArrayInsertAtClauseForClickhouse(config, { key: 'openrank' })}
FROM
(
SELECT
${getGroupTimeClauseForClickhouse(config)},
${getGroupIdClauseForClickhouse(config, 'user')},
SUM(openrank) AS openrank
FROM gh_user_openrank
WHERE ${whereClause.join(' AND ')}
GROUP BY id, time
${getInnerOrderAndLimit(config, 'openrank')}
)
GROUP BY id
${getOutterOrderAndLimit(config, 'openrank')}`;
const result: any = await clickhouse.query(sql);
return result.map(row => {
const [id, name, openrank] = row;
return {
id,
name,
openrank,
}
});
}
export const basicActivitySqlComponent = `
if(type='PullRequestEvent' AND action='closed' AND pull_merged=1, issue_author_id, actor_id) AS actor_id,
argMax(if(type='PullRequestEvent' AND action='closed' AND pull_merged=1, issue_author_login, actor_login), created_at) AS actor_login,
countIf(type='IssueCommentEvent' AND action='created') AS issue_comment,
countIf(type='IssuesEvent' AND action='opened') AS open_issue,
countIf(type='PullRequestEvent' AND action='opened') AS open_pull,
countIf(type='PullRequestReviewCommentEvent' AND action='created') AS review_comment,
countIf(type='PullRequestEvent' AND action='closed' AND pull_merged=1) AS merged_pull,
sqrt(${ISSUE_COMMENT_WEIGHT}*issue_comment + ${OPEN_ISSUE_WEIGHT}*open_issue + ${OPEN_PULL_WEIGHT}*open_pull + ${REVIEW_COMMENT_WEIGHT}*review_comment + ${PULL_MERGED_WEIGHT}*merged_pull) AS activity
`;
interface RepoActivityOption {
developerDetail: boolean;
}
export const getRepoActivity = async (config: QueryConfig<RepoActivityOption>) => {
config = getMergedConfig(config);
const whereClauses: string[] = ["type IN ('IssuesEvent', 'IssueCommentEvent', 'PullRequestEvent', 'PullRequestReviewCommentEvent')"]; // specify types to reduce memory usage and calculation
const repoWhereClause = await getRepoWhereClauseForClickhouse(config);
if (repoWhereClause) whereClauses.push(repoWhereClause);
whereClauses.push(getTimeRangeWhereClauseForClickhouse(config));
const developerDetail = config.options?.developerDetail;
const sql = `
SELECT
id,
argMax(name, time) AS name,
${getGroupArrayInsertAtClauseForClickhouse(config, { key: 'activity', value: 'agg_activity' })},
${getGroupArrayInsertAtClauseForClickhouse(config, { key: 'participants' })},
${getGroupArrayInsertAtClauseForClickhouse(config, { key: 'issue_comment' })},
${getGroupArrayInsertAtClauseForClickhouse(config, { key: 'open_issue' })},
${getGroupArrayInsertAtClauseForClickhouse(config, { key: 'open_pull' })},
${getGroupArrayInsertAtClauseForClickhouse(config, { key: 'review_comment' })},
${getGroupArrayInsertAtClauseForClickhouse(config, { key: 'merged_pull' })}
${developerDetail === true ? ',' + getGroupArrayInsertAtClauseForClickhouse(config, { key: 'details', noPrecision: true, defaultValue: '[]' }) : ''}
FROM
(
SELECT
${getGroupTimeClauseForClickhouse(config, 'month')},
${getGroupIdClauseForClickhouse(config, 'repo', 'month')},
ROUND(SUM(activity), 2) AS agg_activity,
COUNT(actor_id) AS participants,
SUM(issue_comment) AS issue_comment,
SUM(open_issue) AS open_issue,
SUM(open_pull) AS open_pull,
SUM(review_comment) AS review_comment,
SUM(merged_pull) AS merged_pull
${developerDetail === true ? ',arraySort(x -> -tupleElement(x, 2), groupArray((actor_login, ROUND(activity, 2)))) AS details' : ''}
FROM
(
SELECT
toStartOfMonth(created_at) AS month,
repo_id, argMax(repo_name, created_at) AS repo_name,
org_id, argMax(org_login, created_at) AS org_login,
${basicActivitySqlComponent}
FROM gh_events
WHERE ${whereClauses.join(' AND ')}
GROUP BY repo_id, org_id, actor_id, month
HAVING activity > 0
)
GROUP BY id, time
${getInnerOrderAndLimit(config, 'agg_activity')}
)
GROUP BY id
${getOutterOrderAndLimit(config, 'activity')}`;
const result: any = await clickhouse.query(sql);
return result.map(row => {
const [id, name, activity, participants, issue_comment, open_issue, open_pull, review_comment, merged_pull, details] = row;
return {
id,
name,
activity,
participants,
issue_comment,
open_issue,
open_pull,
review_comment,
merged_pull,
details,
}
});
}
export const getUserActivity = async (config: QueryConfig, withBot: boolean = true) => {
config = getMergedConfig(config);
const whereClauses: string[] = ["type IN ('IssuesEvent', 'IssueCommentEvent', 'PullRequestEvent', 'PullRequestReviewCommentEvent')"]; // specify types to reduce memory usage and calculation
const userWhereClause = await getUserWhereClauseForClickhouse(config);
if (userWhereClause) whereClauses.push(userWhereClause);
whereClauses.push(getTimeRangeWhereClauseForClickhouse(config));
const sql = `
SELECT
id,
argMax(name, time) AS name,
${getGroupArrayInsertAtClauseForClickhouse(config, { key: 'activity' })},
${getGroupArrayInsertAtClauseForClickhouse(config, { key: 'issue_comment' })},
${getGroupArrayInsertAtClauseForClickhouse(config, { key: 'open_issue' })},
${getGroupArrayInsertAtClauseForClickhouse(config, { key: 'open_pull' })},
${getGroupArrayInsertAtClauseForClickhouse(config, { key: 'review_comment' })},
${getGroupArrayInsertAtClauseForClickhouse(config, { key: 'merged_pull' })}
FROM
(
SELECT
${getGroupTimeClauseForClickhouse(config, 'month')},
${getGroupIdClauseForClickhouse(config, 'user')},
ROUND(SUM(activity), 2) AS activity,
SUM(issue_comment) AS issue_comment,
SUM(open_issue) AS open_issue,
SUM(open_pull) AS open_pull,
SUM(review_comment) AS review_comment,
SUM(merged_pull) AS merged_pull
FROM
(
SELECT
toStartOfMonth(created_at) AS month,
repo_id,
${basicActivitySqlComponent}
FROM gh_events
WHERE ${whereClauses.join(' AND ')}
GROUP BY repo_id, actor_id, month
HAVING activity > 0 ${withBot ? '' : `AND actor_login NOT LIKE '%[bot]'`}
)
GROUP BY id, time
${getInnerOrderAndLimit(config, 'activity')}
)
GROUP BY id
${getOutterOrderAndLimit(config, 'activity')}`;
const result: any = await clickhouse.query(sql);
return result.map(row => {
const [id, name, activity, issue_comment, open_issue, open_pull, review_comment, merged_pull] = row;
return {
id,
name,
activity,
issue_comment,
open_issue,
open_pull,
review_comment,
merged_pull,
}
});
}
export const getAttention = async (config: QueryConfig) => {
config = getMergedConfig(config);
const whereClauses: string[] = ["type IN ('WatchEvent', 'ForkEvent')"];
const repoWhereClause = await getRepoWhereClauseForClickhouse(config);
if (repoWhereClause) whereClauses.push(repoWhereClause);
whereClauses.push(getTimeRangeWhereClauseForClickhouse(config));
const sql = `
SELECT
id,
argMax(name, time) AS name,
${getGroupArrayInsertAtClauseForClickhouse(config, { key: 'attention' })}
FROM
(
SELECT
${getGroupTimeClauseForClickhouse(config)},
${getGroupIdClauseForClickhouse(config)},
countIf(type='WatchEvent') AS stars,
countIf(type='ForkEvent') AS forks,
stars + 2 * forks AS attention
FROM gh_events
WHERE ${whereClauses.join(' AND ')}
GROUP BY id, time
${getInnerOrderAndLimit(config, 'attention')}
)
GROUP BY id
${getOutterOrderAndLimit(config, 'attention')}`;
const result: any = await clickhouse.query(sql);
return result.map(row => {
const [id, name, attention] = row;
return {
id,
name,
attention,
}
});
};