-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasicUserSearch.ts
206 lines (175 loc) · 6.18 KB
/
basicUserSearch.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
import {v4 as uuidv4} from "uuid";
import {Command} from "../../graphql/command.js";
import {TaskContext} from "../../graphql/context.js";
import {Task} from "../../graphql/task.js";
import {TaskResult} from "../../graphql/taskResult.js";
import {TaskSpec} from "../../graphql/taskSpec.js";
import {formatDate, parseDate} from "../../utils.js";
/**
* This example task searches for users in a given location, who signed up in a given period.
*
* The query at the end of this file is used to search for users.
* Used variables:
* {
* "searchString": "location:Istanbul created:2020-01-01..2020-01-31",
* "first": 100,
* "after": null
* }
*
* There are more than 100 users in Istanbul who signed up in January 2020. Thus, the program will create a new task
* for the next page of the search results.
*
* To execute this task, run:
* > ts-node-esm src/index.ts --command-file=/path/to/this/file/basicUserSearch.ts --data-directory=/tmp/foo/basicUserSearch --github-token=`gh auth login`
*
* The result of the task will be saved in the /tmp/foo/basicUserSearch directory.
*/
const LOCATION = "Istanbul";
const EXCLUDE_USERS_SIGNED_UP_BEFORE = "2020-01-01";
const EXCLUDE_USERS_SIGNED_UP_AFTER = "2020-01-31";
const PAGE_SIZE = 100;
interface UserFragment {
login:string;
company:string | null;
name:string | null;
}
export interface BasicUserSearchTaskResult extends TaskResult {
search:{
pageInfo:{
startCursor:string | null;
hasNextPage:boolean;
endCursor:string | null;
};
nodes:UserFragment[];
};
}
export interface BasicUserSearchTaskSpec extends TaskSpec {
location:string;
signedUpAfter:string;
signedUpBefore:string;
pageSize:number;
startCursor:string | null;
}
export default class BasicUserSearchCommand implements Command<BasicUserSearchTaskResult, BasicUserSearchTaskSpec, BasicUserSearchTask> {
createTask(_:TaskContext, spec:BasicUserSearchTaskSpec):BasicUserSearchTask {
return new BasicUserSearchTask(spec);
}
createNewQueueItems(context:TaskContext):BasicUserSearchTaskSpec[] {
const logger = context.logger;
const startDate = parseDate(EXCLUDE_USERS_SIGNED_UP_BEFORE);
const endDate = parseDate(EXCLUDE_USERS_SIGNED_UP_AFTER);
logger.info(`Creating a new process state, startDate: ${formatDate(startDate)}, endDate: ${formatDate(endDate)}`);
const newTaskSpecs:BasicUserSearchTaskSpec[] = [];
const signedUpAfter = formatDate(startDate);
const signedUpBefore = formatDate(endDate);
const key = uuidv4();
const newTaskSpec = {
id: key,
parentId: null,
originatingTaskId: null,
//
location: LOCATION,
signedUpAfter: signedUpAfter,
signedUpBefore: signedUpBefore,
//
pageSize: PAGE_SIZE,
startCursor: null,
};
newTaskSpecs.push(newTaskSpec);
logger.info(`Created ${newTaskSpecs.length} new task specs.`);
return newTaskSpecs;
}
}
export class BasicUserSearchTask extends Task<BasicUserSearchTaskResult, BasicUserSearchTaskSpec> {
protected getGraphqlQuery():string {
return QUERY;
}
protected buildQueryParameters(context:TaskContext) {
const spec = this.getSpec(context);
const searchString =
`location:${this.spec.location} ` +
// both ends are inclusive
`created:${this.spec.signedUpAfter}..${this.spec.signedUpBefore}`;
return {
"searchString": searchString,
"first": spec.pageSize,
"after": spec.startCursor,
};
}
nextTask(context:TaskContext, result:BasicUserSearchTaskResult):BasicUserSearchTask | null {
// return a new task if there is a next page
if (result.search.pageInfo.hasNextPage) {
context.logger.debug(`Next page available for task: ${this.getId(context)}`);
const newTaskSpec = {
id: uuidv4(),
parentId: null,
originatingTaskId: this.getId(context),
//
location: this.spec.location,
signedUpAfter: this.spec.signedUpAfter,
signedUpBefore: this.spec.signedUpBefore,
//
pageSize: this.spec.pageSize,
//
startCursor: result.search.pageInfo.endCursor,
};
return new BasicUserSearchTask(newTaskSpec);
}
return null;
}
saveOutput(context:TaskContext, output:BasicUserSearchTaskResult):void {
const logger = context.logger;
logger.debug(`Saving output of the task: ${this.getId(context)}`);
let nodes = output.search.nodes;
if (!nodes || nodes.length == 0) {
logger.debug(`No nodes found for ${this.getId(context)}.`);
nodes = [];
}
logger.debug(`Number of nodes found for ${this.getId(context)}: ${nodes.length}`);
for (let i = 0; i < nodes.length; i++) {
const userInfo = <UserFragment>nodes[i];
// items in the array might be null, in case of partial responses
if (userInfo) {
context.currentRunOutput.push({
taskId: this.getId(context),
result: userInfo,
});
}
}
}
narrowedDownTasks(_:TaskContext):BasicUserSearchTask[] | null {
// this task doesn't create narrowed down tasks.
// that's because this simple search will not time out.
throw new Error("Not implemented.");
}
}
export const QUERY = `
query BasicUserSearch($searchString: String!, $first: Int!, $after:String){
rateLimit {
cost
limit
nodeCount
remaining
resetAt
used
}
search(type: USER, query:$searchString, first:$first, after:$after) {
pageInfo {
startCursor
hasNextPage
endCursor
}
userCount
nodes {
... on User {
...UserFragment
}
}
}
}
fragment UserFragment on User {
login
company
name
}
`;