-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathmetrics.ts
executable file
·236 lines (218 loc) · 7.5 KB
/
metrics.ts
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
import {
getGroupArrayInsertAtClause,
getGroupTimeClause,
getGroupIdClause,
getInnerOrderAndLimit,
getMergedConfig,
getOutterOrderAndLimit,
getRepoWhereClause,
getTimeRangeWhereClause,
getUserWhereClause,
QueryConfig,
processQueryResult,
getTopLevelPlatform,
getInnerGroupBy
} from "./basic";
import * as clickhouse from '../db/clickhouse';
export const repoStars = async (config: QueryConfig) => {
config = getMergedConfig(config);
const whereClauses: string[] = ["type = 'WatchEvent'"];
const repoWhereClause = await getRepoWhereClause(config);
if (repoWhereClause) whereClauses.push(repoWhereClause);
whereClauses.push(getTimeRangeWhereClause(config));
const sql = `
SELECT
id,
${getTopLevelPlatform(config)},
argMax(name, time) AS name,
${getGroupArrayInsertAtClause(config, { key: 'count' })}
FROM
(
SELECT
${getGroupTimeClause(config)},
${getGroupIdClause(config)},
COUNT() AS count
FROM events
WHERE ${whereClauses.join(' AND ')}
${getInnerGroupBy(config)}
${getInnerOrderAndLimit(config, 'count')}
)
GROUP BY id, platform
${getOutterOrderAndLimit(config, 'count')}`;
const result: any = await clickhouse.query(sql);
return processQueryResult(result, ['count']);
};
export const repoIssueComments = async (config: QueryConfig) => {
config = getMergedConfig(config);
const whereClauses: string[] = ["type = 'IssueCommentEvent' AND action = 'created'"];
const repoWhereClause = await getRepoWhereClause(config);
if (repoWhereClause) whereClauses.push(repoWhereClause);
whereClauses.push(getTimeRangeWhereClause(config));
const sql = `
SELECT
id,
${getTopLevelPlatform(config)},
argMax(name, time) AS name,
${getGroupArrayInsertAtClause(config, { key: 'count' })}
FROM
(
SELECT
${getGroupTimeClause(config)},
${getGroupIdClause(config)},
COUNT() AS count
FROM events
WHERE ${whereClauses.join(' AND ')}
${getInnerGroupBy(config)}
${getInnerOrderAndLimit(config, 'count')}
)
GROUP BY id, platform
${getOutterOrderAndLimit(config, 'count')}`;
const result: any = await clickhouse.query(sql);
return processQueryResult(result, ['count']);
};
export const repoCount = async (config: QueryConfig) => {
config = getMergedConfig(config);
const whereClauses: string[] = [];
const repoWhereClause = await getRepoWhereClause(config);
if (repoWhereClause) whereClauses.push(repoWhereClause);
whereClauses.push(getTimeRangeWhereClause(config));
const sql = `
SELECT
id,
${getTopLevelPlatform(config)},
argMax(name, time) AS name,
${getGroupArrayInsertAtClause(config, { key: 'count' })}
FROM
(
SELECT
${getGroupTimeClause(config)},
${getGroupIdClause(config)},
COUNT(DISTINCT repo_id) AS count
FROM events
WHERE ${whereClauses.join(' AND ')}
${getInnerGroupBy(config)}
${getInnerOrderAndLimit(config, 'count')}
)
GROUP BY id, platform
${getOutterOrderAndLimit(config, 'count')}`;
const result: any = await clickhouse.query(sql);
return processQueryResult(result, ['count']);
};
export const repoParticipants = async (config: QueryConfig) => {
config = getMergedConfig(config);
const whereClauses: string[] = ["type IN ('IssuesEvent', 'IssueCommentEvent', 'PullRequestEvent', 'PullRequestReviewCommentEvent')"];
const repoWhereClause = await getRepoWhereClause(config);
if (repoWhereClause) whereClauses.push(repoWhereClause);
whereClauses.push(getTimeRangeWhereClause(config));
const sql = `
SELECT
id,
${getTopLevelPlatform(config)},
argMax(name, time) AS name,
${getGroupArrayInsertAtClause(config, { key: 'count' })}
FROM
(
SELECT
${getGroupTimeClause(config)},
${getGroupIdClause(config)},
COUNT(DISTINCT actor_id) AS count
FROM events
WHERE ${whereClauses.join(' AND ')}
${getInnerGroupBy(config)}
${getInnerOrderAndLimit(config, 'count')}
)
GROUP BY id, platform
${getOutterOrderAndLimit(config, 'count')}`;
const result: any = await clickhouse.query(sql);
return processQueryResult(result, ['count']);
};
interface EquivalentTimeZoneOptions {
// how many hours to accumulate the activity, default to 12
continousHours: number;
// what is the start time of the local working hour, default to 9
startHour: number;
}
export const userEquivalentTimeZone = async (config: QueryConfig<EquivalentTimeZoneOptions>) => {
config = getMergedConfig(config);
const whereClauses: string[] = [getTimeRangeWhereClause(config)];
const userWhereClause = await getUserWhereClause(config);
if (userWhereClause) whereClauses.push(userWhereClause);
const continousHours = config.options?.continousHours ?? 12;
const startHour = config.options?.startHour ?? 9;
/*
* The procedure is
* - Accumulate the users' event data for a certain time period and collect as 0am - 23pm in UTC time.
* - Find out the maximum cumulative active value of ${continousHour} hours continuous interval
* - Assume the hours should be started at ${startHour} in the local time zone for the user.
* - Calculate the time zone of the user.
* The procedure will return `13` which is an invalid result for time period without data
*/
const sql = `
SELECT
id,
${getTopLevelPlatform(config)},
argMax(name, time) AS name,
${getGroupArrayInsertAtClause(config, { key: 'time_zone', defaultValue: '13', noPrecision: true })}
FROM
(
SELECT
id,
platform,
anyHeavy(name) AS name,
time,
groupArrayInsertAt(0, 24)(count, hour) AS arr,
arraySort(x -> -x[2], arrayMap(h -> [h, if(h <= ${continousHours + 1}, arraySum(arraySlice(arr, h, ${continousHours})), arraySum(arrayConcat(arraySlice(arr, h), arraySlice(arr, 1, h - ${continousHours + 1}))))], range(1, 25)))[1][1] - 1 AS maxHour,
if(maxHour > ${startHour + 12}, ${startHour} + 24 - maxHour, ${startHour} - maxHour) AS time_zone
FROM
(
SELECT
${getGroupTimeClause(config)},
${getGroupIdClause(config, 'user')},
toHour(created_at) AS hour,
COUNT() AS count
FROM events
WHERE ${whereClauses.join(' AND ')}
GROUP BY id, platform, time, hour
ORDER BY hour
)
${getInnerGroupBy(config)}
)
GROUP BY id, platform`;
const result: any = await clickhouse.query(sql);
return processQueryResult(result, ['time_zone']);
};
export const contributorEmailSuffixes = async (config: QueryConfig) => {
config = getMergedConfig(config);
const whereClauses: string[] = ["type='PushEvent'"];
const repoWhereClause = await getRepoWhereClause(config);
if (repoWhereClause) whereClauses.push(repoWhereClause);
whereClauses.push(getTimeRangeWhereClause(config));
const sql = `
SELECT
id,
${getTopLevelPlatform(config)},
argMax(name, time) AS name,
${getGroupArrayInsertAtClause(config, { key: 'suffixes', noPrecision: true, defaultValue: '[]' })}
FROM
(
SELECT
time, id, argMax(name, time) AS name, ${(config.groupBy && config.groupBy !== 'org' && config.groupBy !== 'repo') ? '' : 'platform, '}
groupArray(DISTINCT suffix) AS distinct_suffixes,
arraySort(x -> -tupleElement(x, 2), arrayZip(distinct_suffixes, arrayMap(s -> length(arrayFilter(x -> x = s, groupArray(suffix))), distinct_suffixes))) AS suffixes
FROM
(
SELECT
${getGroupTimeClause(config)},
${getGroupIdClause(config)},
anyHeavy(arrayJoin(arrayMap(x -> splitByChar('@', x)[2], push_commits.email))) AS suffix,
arrayJoin(push_commits.name) AS author
FROM events
WHERE ${whereClauses.join(' AND ')}
GROUP BY platform, repo_id, org_id, author, time
)
${getInnerGroupBy(config)}
)
GROUP BY id, platform`;
const result: any = await clickhouse.query(sql);
return processQueryResult(result, ['suffixes']);
}