Skip to content

Commit 2c281ec

Browse files
committed
Merge branch 'CORE-140' into CORE-40
2 parents e34065c + 0bd5aaa commit 2c281ec

File tree

7 files changed

+234
-65
lines changed

7 files changed

+234
-65
lines changed

config/default.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,6 @@ module.exports = {
128128
INTERNAL_CACHE_TTL: process.env.INTERNAL_CACHE_TTL || 1800,
129129
GRPC_CHALLENGE_SERVER_HOST: process.env.GRPC_DOMAIN_CHALLENGE_SERVER_HOST || "localhost",
130130
GRPC_CHALLENGE_SERVER_PORT: process.env.GRPC_DOMAIN_CHALLENGE_SERVER_PORT || 8888,
131+
GRPC_ACL_SERVER_HOST: process.env.GRPC_ACL_SERVER_HOST || "localhost",
132+
GRPC_ACL_SERVER_PORT: process.env.GRPC_ACL_SERVER_PORT || 8889,
131133
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"@grpc/grpc-js": "^1.8.12",
4444
"@opensearch-project/opensearch": "^2.2.0",
4545
"@topcoder-framework/domain-challenge": "^0.24.1",
46+
"@topcoder-framework/domain-acl": "^0.24.0",
4647
"@topcoder-framework/lib-common": "^0.24.1",
4748
"aws-sdk": "^2.1145.0",
4849
"axios": "^0.19.0",

src/common/srm-helper.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
const _ = require("lodash");
2+
const moment = require("moment");
3+
4+
const SRMScheduleKeyMappings = _.reduce(
5+
[
6+
"roundId",
7+
"name",
8+
"shortName",
9+
"contestName",
10+
"roundType",
11+
"status",
12+
"registrationStartTime",
13+
"registrationEndTime",
14+
"codingStartTime",
15+
"codingEndTime",
16+
"intermissionStartTime",
17+
"intermissionEndTime",
18+
"challengeStartTime",
19+
"challengeEndTime",
20+
"systestStartTime",
21+
"systestEndTime",
22+
],
23+
(acc, field) => ({ ...acc, [_.toLower(field)]: field }),
24+
{}
25+
);
26+
27+
/**
28+
* Get schedule query
29+
* @param {Object} filter the query filter
30+
* @param {Array<String>} filter.statuses the statues
31+
* @param {Date} filter.registrationStartTimeAfter the start of the registration time
32+
* @param {Date} filter.registrationStartTimeBefore the end of the registration time
33+
*/
34+
function getSRMScheduleQuery(filter) {
35+
const statuses = _.join(
36+
_.map(filter.statuses, (s) => `'${_.toUpper(s)}'`),
37+
","
38+
);
39+
const registrationTimeFilter = `reg.start_time >= '${moment(
40+
filter.registrationStartTimeAfter
41+
).format("yyyy-MM-DD HH:mm:ss")}'${
42+
filter.registrationStartTimeBefore
43+
? ` AND reg.start_time <= '${moment(filter.registrationStartTimeBefore).format(
44+
"yyyy-MM-DD HH:mm:ss"
45+
)}'`
46+
: ""
47+
}`;
48+
49+
const query = `SELECT
50+
FIRST 50
51+
r.round_id AS roundId
52+
, r.name AS name
53+
, r.short_name AS shortName
54+
, c.name AS contestName
55+
, rt.round_type_desc AS roundType
56+
, r.status AS status
57+
, reg.start_time AS registrationStartTime
58+
, reg.end_time AS registrationEndTime
59+
, coding.start_time AS codingStartTime
60+
, coding.end_time AS codingEndTime
61+
, intermission.start_time AS intermissionStartTime
62+
, intermission.end_time AS intermissionEndTime
63+
, challenge.start_time AS challengeStartTime
64+
, challenge.end_time AS challengeEndTime
65+
, systest.start_time AS systestStartTime
66+
, systest.end_time AS systestEndTime
67+
FROM
68+
informixoltp:contest AS c
69+
INNER JOIN informixoltp:round AS r ON r.contest_id = c.contest_id
70+
INNER JOIN informixoltp:round_type_lu AS rt ON rt.round_type_id = r.round_type_id
71+
LEFT JOIN informixoltp:round_segment AS reg ON reg.round_id = r.round_id AND reg.segment_id = 1
72+
LEFT JOIN informixoltp:round_segment AS coding ON coding.round_id = r.round_id AND coding.segment_id = 2
73+
LEFT JOIN informixoltp:round_segment AS intermission ON intermission.round_id = r.round_id AND intermission.segment_id = 3
74+
LEFT JOIN informixoltp:round_segment AS challenge ON challenge.round_id = r.round_id AND challenge.segment_id = 4
75+
LEFT JOIN informixoltp:round_segment AS systest ON systest.round_id = r.round_id AND systest.segment_id = 5
76+
WHERE
77+
r.round_type_id in (1,2,10) AND
78+
UPPER(r.status) in (${statuses}) AND
79+
${registrationTimeFilter}
80+
ORDER BY
81+
reg.start_time DESC`;
82+
return query;
83+
}
84+
85+
function convertSRMScheduleQueryOutput(queryOutput) {
86+
return transformDatabaseResponse(queryOutput, SRMScheduleKeyMappings);
87+
}
88+
89+
function transformDatabaseResponse(databaseResponse, keyMappings) {
90+
const transformedData = [];
91+
92+
if (databaseResponse && databaseResponse.rows && Array.isArray(databaseResponse.rows)) {
93+
databaseResponse.rows.forEach((row) => {
94+
const record = {};
95+
if (row.fields && Array.isArray(row.fields)) {
96+
row.fields.forEach((field) => {
97+
const lowercaseKey = field.key.toLowerCase();
98+
const mappedKey = keyMappings[lowercaseKey] || lowercaseKey;
99+
record[mappedKey] = field.value;
100+
});
101+
}
102+
transformedData.push(record);
103+
});
104+
}
105+
return transformedData;
106+
}
107+
108+
module.exports = {
109+
getSRMScheduleQuery,
110+
convertSRMScheduleQueryOutput,
111+
};

src/controllers/ChallengeController.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@ async function advancePhase(req, res) {
126126
res.send(await service.advancePhase(req, req.authUser, req.params.challengeId, req.body));
127127
}
128128

129+
/**
130+
* Get SRM Schedule
131+
* @param {Object} req the request
132+
* @param {Object} res the response
133+
*/
134+
async function getSRMSchedule(req, res) {
135+
const result = await service.getSRMSchedule(req, req.query);
136+
res.send(result);
137+
}
138+
129139
module.exports = {
130140
searchChallenges,
131141
createChallenge,
@@ -135,4 +145,5 @@ module.exports = {
135145
getChallengeStatistics,
136146
sendNotifications,
137147
advancePhase,
148+
getSRMSchedule,
138149
};

src/routes.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ module.exports = {
4040
method: "createRequest",
4141
},
4242
},
43+
"/challenges/srms/schedule": {
44+
get: {
45+
controller: "ChallengeController",
46+
method: "getSRMSchedule",
47+
},
48+
},
4349
"/challenges/health": {
4450
get: {
4551
controller: "HealthController",

src/services/ChallengeService.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const esClient = helper.getESClient();
3333

3434
const PhaseAdvancer = require("../phase-management/PhaseAdvancer");
3535
const { ChallengeDomain } = require("@topcoder-framework/domain-challenge");
36+
const { QueryDomain } = require("@topcoder-framework/domain-acl");
3637

3738
const { hasAdminRole } = require("../common/role-helper");
3839
const {
@@ -44,6 +45,7 @@ const {
4445
} = require("../common/challenge-helper");
4546
const deepEqual = require("deep-equal");
4647
const { getM2MToken } = require("../common/m2m-helper");
48+
const { getSRMScheduleQuery, convertSRMScheduleQueryOutput } = require("../common/srm-helper");
4749

4850
const challengeDomain = new ChallengeDomain(
4951
GRPC_CHALLENGE_SERVER_HOST,
@@ -65,6 +67,9 @@ const challengeDomain = new ChallengeDomain(
6567
}),
6668
}
6769
);
70+
71+
const aclQueryDomain = new QueryDomain(config.GRPC_ACL_SERVER_HOST, config.GRPC_ACL_SERVER_PORT);
72+
6873
const phaseAdvancer = new PhaseAdvancer(challengeDomain);
6974

7075
/**
@@ -2475,6 +2480,27 @@ async function indexChallengeAndPostToKafka(updatedChallenge, track, type) {
24752480
});
24762481
}
24772482

2483+
/**
2484+
* Get SRM Schedule
2485+
* @param {Object} criteria the criteria
2486+
*/
2487+
async function getSRMSchedule(criteria = {}) {
2488+
if (!criteria.statuses) {
2489+
criteria.statuses = ["A", "F", "P"];
2490+
}
2491+
const sql = getSRMScheduleQuery(criteria);
2492+
const result = await aclQueryDomain.rawQuery({ sql });
2493+
return convertSRMScheduleQueryOutput(result);
2494+
}
2495+
2496+
getSRMSchedule.schema = {
2497+
criteria: Joi.object().keys({
2498+
registrationStartTimeAfter: Joi.date().default(new Date()),
2499+
registrationStartTimeBefore: Joi.date(),
2500+
statuses: Joi.array().items(Joi.string().valid(["A", "F", "P"])),
2501+
}),
2502+
};
2503+
24782504
module.exports = {
24792505
searchChallenges,
24802506
createChallenge,
@@ -2484,6 +2510,7 @@ module.exports = {
24842510
getChallengeStatistics,
24852511
sendNotifications,
24862512
advancePhase,
2513+
getSRMSchedule,
24872514
};
24882515

24892516
logger.buildService(module.exports);

0 commit comments

Comments
 (0)