-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathupload.js
212 lines (199 loc) · 5.84 KB
/
upload.js
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
/**
* Usage:
*
* node ./upload.js
*
* - Uses package.json config to know about data sets.
* - Expects output files to end with .out.txt
* - Expects to find files ordered by creation time in .builds
*/
const path = require("path");
const _ = require("lodash");
const debug = require("debug")("upload");
const fs = require("fs");
const joi = require("joi");
const request = require("request-promise");
const exec = require("child_process").execSync;
const round = require("./round.json");
const package = require("./package.json");
const nbFiles = Object.keys(package.config).length;
const buildDir =
process.env.BUILD_DIR || process.env.npm_package_config_buildDir || ".builds";
const solutionDir =
process.env.SOLUTION_DIR || process.env.npm_package_config_solutionDir || "";
const gitTagEnabled =
process.env.TAG_ON_UPLOAD !== "false" ||
process.env.npm_package_config_tagOnUpload !== "false";
const authToken = exec("sh gcloud-auth-token.sh")
.toString()
.trimRight();
// const authToken = process.env.HASH_CODE_JUDGE_AUTH_TOKEN;
// if (authToken) {
// debug("token", shorten(authToken));
// } else {
// console.error(
// "HASH_CODE_JUDGE_AUTH_TOKEN not defined. Set it with your auth token to the Judge system."
// );
// process.exit();
// }
const createUrlUri =
"https://hashcode-judge.appspot.com/api/judge/v1/upload/createUrl";
const submissionsUri =
"https://hashcode-judge.appspot.com/api/judge/v1/submissions";
const authorizationHeader = { Authorization: `Bearer ${authToken}` };
const dataSets = _.range(nbFiles).reduce((dataSets, i) => {
const name = process.env[`npm_package_config_input${i + 1}_name`];
if (!name) return dataSets;
debug(`found data set '${name}' in package.json`);
return Object.assign(dataSets, {
[name]: process.env[`npm_package_config_input${i + 1}_id`]
});
}, {});
const solutionSchema = joi
.object()
.min(2)
.keys(_.mapValues(dataSets, () => joi.string()))
.keys({ sources: joi.string().required() });
function* submitSolution(solution) {
solution = joi.attempt(
solution,
solutionSchema,
"invalid solution parameters"
);
const blobKeys = yield _.mapValues(solution, upload);
const solutionBlobKeys = _.omit(blobKeys, "sources");
const submissions = yield _.mapValues(solutionBlobKeys, function(
blobKey,
dataSetName
) {
debug(`submitting ${dataSetName} (key: ${shorten(blobKey)})`);
return submit(dataSets[dataSetName], blobKey, blobKeys.sources);
});
_.forEach(submissions, (submission, dataSetName) => {
debug(`submitted ${dataSetName} (id: ${submission.id})`);
});
const scoredSubmissions = yield _.mapValues(
submissions,
(submission, dataSetName) => {
debug(`waiting for score on ${dataSetName}`);
return waitForScoring(submission, dataSetName);
}
);
_.forEach(
scoredSubmissions,
({ valid, errorMessage, best, score }, dataSetName) => {
if (!valid) {
debug(`error for ${dataSetName}: ${errorMessage}`);
} else if (best) {
debug(`NEW RECORD for ${dataSetName}: ${score}`);
} else {
debug(`got score for ${dataSetName}: ${score}`);
}
}
);
const overallScore = _(scoredSubmissions)
.map(({ score }) => parseInt(score))
.reduce(_.add);
debug(`got overall score: ${overallScore}`);
return scoredSubmissions;
}
function* upload(filePath) {
const uploadUri = yield createUploadUri();
debug(`uploading ${filePath} to ${shorten(uploadUri)}`);
const formData = { file: fs.createReadStream(filePath) };
const responseBody = yield request({
method: "POST",
uri: uploadUri,
formData,
json: true
});
const blobKey = responseBody.file[0];
debug(`uploaded ${filePath} (key: ${shorten(blobKey)})`);
return blobKey;
}
function* createUploadUri() {
const response = yield request({
method: "GET",
uri: createUrlUri,
headers: authorizationHeader,
json: true
});
return response.value;
}
function* submit(dataSet, submissionBlobKey, sourcesBlobKey) {
const queryParameters = { dataSet, submissionBlobKey, sourcesBlobKey };
return yield request({
method: "POST",
uri: submissionsUri,
headers: authorizationHeader,
qs: queryParameters,
json: true
});
}
function* waitForScoring(submission, dataSetName) {
while (true) {
yield new Promise(resolve => setTimeout(resolve, 1000));
debug(`polling score for ${dataSetName}`);
const { items: submissions } = yield request({
method: "GET",
uri: `${submissionsUri}/${round.id}`,
headers: authorizationHeader,
json: true
});
const scoredSubmission = _.find(submissions, {
id: submission.id,
scored: true
});
if (scoredSubmission) {
return scoredSubmission;
} else {
debug(`no score yet for ${dataSetName}`);
}
}
}
function shorten(str) {
return (
_(str)
.slice(0, 20)
.join("") + "..."
);
}
function createGitTag(submissions) {
const tagScores = _(submissions)
.map(({ score }, dataSetKey) => `${dataSetKey}=${score}`)
.join("_");
const tagName = `${tagScores}_time=${Date.now()}`;
debug(`tagging as '${tagName}'`);
exec(`git tag ${tagName}`, {
encoding: "utf8"
});
}
if (module === require.main) {
if (_.isEmpty(dataSets)) {
console.log(
"data set ids not initialized! open upload.js and fill the dataSets value"
);
process.exit(1);
}
const co = require("co");
const explode = err =>
process.nextTick(() => {
throw err;
});
const solution = Object.assign(
_.mapValues(dataSets, (id, name) =>
path.join(solutionDir, `${name}.out.txt`)
),
{
sources: path.join(buildDir, _.last(fs.readdirSync(buildDir).sort()))
}
);
debug("files to upload", solution);
co(submitSolution(solution))
.catch(explode)
.then(submissions => {
if (gitTagEnabled) {
createGitTag(submissions);
}
});
}