Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 6c5ce9f

Browse files
Use leaderboard api instead of directly interacting with database. Also store the groupId of the contest in the database
1 parent fb8dffc commit 6c5ce9f

22 files changed

+2206
-2944
lines changed

README.md

Lines changed: 70 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
- nodejs https://nodejs.org/en/ (v10)
66
- Kafka (v2)
7-
- Mongodb (v4)
87

98
## Configuration
109

@@ -20,15 +19,25 @@ The following parameters can be set in config files or in env variables:
2019
- KAFKA_CLIENT_CERT_KEY: Kafka connection private key, optional;
2120
if not provided, then SSL connection is not used, direct insecure connection is used;
2221
if provided, it can be either path to private key file or private key content
22+
- KAFKA_GROUP_ID: Kafka group id
2323
- CREATE_DATA_TOPIC: Kafka topic related to creation
2424
- UPDATE_DATA_TOPIC: Kafka topic related to update
2525
- DELETE_DATA_TOPIC: Kafka topic related to deletion
26-
- GROUP_IDS: List of Group IDs which should be used for filtering
26+
- LEADERBOARD_API_URL: Leaderboard API URL
2727
- SUBMISSION_API_URL: Submission API URL
28-
- CHALLENGE_API_URL: Challenge API URL
29-
- MEMBER_API_URL: Member API URL
30-
- All variables starting with prefix `AUTH0` corresponds to Auth0 related credentials
31-
- MONGODB_URL: Mongo DB URL
28+
- AUTH0_URL: Auth0 URL, used to get TC M2M token
29+
- AUTH0_AUDIENCE: Auth0 audience, used to get TC M2M token
30+
- TOKEN_CACHE_TIME: Auth0 token cache time, used to get TC M2M token
31+
- AUTH0_CLIENT_ID: Auth0 client id, used to get TC M2M token
32+
- AUTH0_CLIENT_SECRET: Auth0 client secret, used to get TC M2M token
33+
- AUTH0_PROXY_SERVER_URL: Proxy Auth0 URL, used to get TC M2M token
34+
35+
Also note that there is a `/health` endpoint that checks for the health of the app. This sets up an expressjs server and listens on the environment variable `PORT`. It's not part of the configuration file and needs to be passed as an environment variable
36+
37+
Configuration for the tests is at `config/test.js`, only add such new configurations different from `config/default.js`
38+
- WAIT_TIME: wait time used in test, default is 1000 or one second
39+
- LEADERBOARD_API_URL: Leaderboard API URL used in testing
40+
- SUBMISSION_API_URL: Submission API URL used in testing
3241

3342
## Local Kafka setup
3443

@@ -47,7 +56,7 @@ The following parameters can be set in config files or in env variables:
4756
```bash
4857
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic submission.notification.create
4958
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic submission.notification.update
50-
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic notifications.autopilot.events
59+
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic submission.notification.delete
5160
```
5261

5362
- verify that the topics are created:
@@ -58,7 +67,7 @@ The following parameters can be set in config files or in env variables:
5867
- In the console, write some message, one message per line:
5968

6069
```bash
61-
{ "topic":"submission.notification.create", "originator":"submission-api", "timestamp":"2018-08-06T15:46:05.575Z", "mime-type":"application/json", "payload":{ "resource":"review", "id": "d34d4180-65aa-42ec-a945-5fd21dec0502", "score": 92.0, "typeId": "c56a4180-65aa-42ec-a945-5fd21dec0501", "reviewerId": "c23a4180-65aa-42ec-a945-5fd21dec0503", "scoreCardId": "b25a4180-65aa-42ec-a945-5fd21dec0503", "submissionId": "fad49103-37ac-4a04-8294-c840483178a5", "created": "2018-05-20T07:00:30.123Z", "updated": "2018-06-01T07:36:28.178Z", "createdBy": "admin", "updatedBy": "admin" } }
70+
{ "topic":"submission.notification.create", "originator":"submission-api", "timestamp":"2018-08-06T15:46:05.575Z", "mime-type":"application/json", "payload":{ "resource":"reviewSummation", "id": "d24d4180-65aa-42ec-a945-5fd21dec0508", "submissionId": "a34e1158-2c27-4d38-b079-5e5cca1bdcf7", "aggregateScore": 88, "scoreCardId": "b25a4180-65aa-42ec-a945-5fd21dec0503", "isPassing": true, "created": "2018-05-20T07:00:30.123Z", "updated": "2018-06-01T07:36:28.178Z", "createdBy": "admin", "updatedBy": "admin" } }
6271
```
6372
6473
- optionally, use another terminal, go to same directory, start a consumer to view the messages:
@@ -97,14 +106,43 @@ npm start
97106
- git add .
98107
- git commit -m init
99108
- heroku create
100-
- heroku config:set KAFKA_URL=... AUTH0_URL=...
109+
- heroku config:set KAFKA_URL=... AUTH0_URL=...
101110
- git push heroku master
102111
103112
## Verification
104113
105-
1. Ensure that Kafka is up and running and the topics `submission.notification.create, submission.notification.update and notifications.autopilot.events` are created in Kafka
114+
1. Ensure that Kafka is up and running and the topics `submission.notification.create, submission.notification.update and submission.notification.delete` are created in Kafka
115+
116+
2. Refer `README.md` in `leaderboard-api` to start leaderboard api, all operations are under `leaderboard-api` project root folder
117+
118+
- Start mock app and it will listen on 3001 PORT.
119+
120+
```bash
121+
npm run mock-api
122+
```
123+
124+
- Ensure you have start MongoDB and properly configure `MONGODB_URL`. Run the following commands to clear and insert test data, step up environment variables and start the app(it will listen on 3002 PORT!).
125+
126+
```bash
127+
npm run init-db
128+
npm run test-data
129+
export CHALLENGE_API_URL=http://localhost:3001/challenges
130+
export MEMBER_API_URL=http://localhost:3001/users
131+
export PORT=3002
132+
npm start
133+
```
106134
107-
2. Ensure that MONGODB_URL configured is correct and ensure that Submission API URL is pointing to `https://api.topcoder-dev.com/v5/submissions` (To verify based on the data present in Dev)
135+
3. Run the following command to start processor app(Under this project's root folder)
136+
```bash
137+
export AUTH0_URL=https://topcoder-dev.auth0.com/oauth/token
138+
export AUTH0_AUDIENCE=https://m2m.topcoder-dev.com/
139+
export TOKEN_CACHE_TIME=90
140+
export AUTH0_CLIENT_ID=8QovDh27SrDu1XSs68m21A1NBP8isvOt
141+
export AUTH0_CLIENT_SECRET=3QVxxu20QnagdH-McWhVz0WfsQzA1F8taDdGDI4XphgpEYZPcMTF4lX3aeOIeCzh
142+
export LEADERBOARD_API_URL=http://localhost:3002/v5/leaderboard
143+
export SUBMISSION_API_URL=http://localhost:3001/submissions
144+
npm start
145+
```
108146
109147
3. Attach to the topic `submission.notification.create` using Kafka console producer
110148
@@ -115,47 +153,50 @@ bin/kafka-console-producer.sh --broker-list localhost:9092 --topic submission.no
115153
4. Write the following message to the Console
116154
117155
```bash
118-
{ "topic":"submission.notification.create", "originator":"submission-api", "timestamp":"2018-08-06T15:46:05.575Z", "mime-type":"application/json", "payload":{ "resource":"reviewSummation", "id": "d24d4180-65aa-42ec-a945-5fd21dec0507", "aggregateScore": 87.5, "isPassing": true, "scoreCardId": "b25a4180-65aa-42ec-a945-5fd21dec0503", "submissionId": "fad49103-37ac-4a04-8294-c840483178a5", "created": "2018-05-20T07:00:30.123Z", "updated": "2018-06-01T07:36:28.178Z", "createdBy": "admin", "updatedBy": "admin" } }
156+
{ "topic":"submission.notification.create", "originator":"submission-api", "timestamp":"2018-08-06T15:46:05.575Z", "mime-type":"application/json", "payload":{ "resource":"reviewSummation", "id": "d24d4180-65aa-42ec-a945-5fd21dec0508", "submissionId": "a34e1158-2c27-4d38-b079-5e5cca1bdcf7", "aggregateScore": 90, "scoreCardId": "b25a4180-65aa-42ec-a945-5fd21dec0503", "isPassing": true, "metadata": { "assertions": { "pending": 0, "failed": 1, "total": 10}, "tests": { "total": 10 } }, "created": "2018-05-20T07:00:30.123Z", "updated": "2018-06-01T07:36:28.178Z", "createdBy": "admin", "updatedBy": "admin" } }
119157
```
120158
121-
5. You could see in the console that message will be processed
159+
5. You could see in the console that message will be processed, and find the following message: `Record with Challenge ID # 30051825 and Member ID # 8547899 does not exists in database. Creating the record`. Also check the leaderboard-api console for more information(Console in step 2)
122160
123-
6. Data in the Database could be verified directly or by using the `/leaderboard` end point
161+
6. Attach to the topic `submission.notification.update` using Kafka console producer
124162
125-
7. Open Postman and import the environment, collection in `docs` directory
126-
127-
8. Trigger the `leaderboard` end point to the verify the data present in Mongo DB
163+
```bash
164+
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic submission.notification.update
165+
```
128166
129-
9. Attach to the topic `submission.notifcation.update` using Kafka console producer and write the below message
167+
7. Write the following message to the Console
130168
131169
```bash
132-
{ "topic":"submission.notification.update", "originator":"submission-api", "timestamp":"2018-08-06T15:46:05.575Z", "mime-type":"application/json", "payload":{ "resource":"reviewSummation", "id": "d24d4180-65aa-42ec-a945-5fd21dec0507", "aggregateScore": 85.83 } }
170+
{ "topic":"submission.notification.update", "originator":"submission-api", "timestamp":"2018-08-06T15:46:05.575Z", "mime-type":"application/json", "payload":{ "resource":"reviewSummation", "id": "d24d4180-65aa-42ec-a945-5fd21dec0508", "submissionId": "a34e1158-2c27-4d38-b079-5e5cca1bdcf7", "aggregateScore": 80, "scoreCardId": "b25a4180-65aa-42ec-a945-5fd21dec0503", "isPassing": true, "metadata": { "assertions": { "pending": 0, "failed": 1, "total": 5}, "tests": { "total": 5 } }, "created": "2018-05-20T07:00:30.123Z", "updated": "2018-06-01T07:36:28.178Z", "createdBy": "admin", "updatedBy": "admin" } }
133171
```
134172
135-
10. Validate the data using Postman
173+
8. You could see in the console that message will be processed, and find the following message: `Record with Challenge ID # 30051825 and Member ID # 8547899 exists in database. Updating the score`. Also check the leaderboard-api console for more information(Console in step 2)
136174
137-
11. Attach to the topic `submission.notifcation.delete` using Kafka console producer and write the below message
175+
9. Attach to the topic `submission.notification.delete` using Kafka console producer
138176
139177
```bash
140-
{ "topic":"submission.notification.delete", "originator":"submission-api", "timestamp":"2018-08-06T15:46:05.575Z", "mime-type":"application/json", "payload":{ "resource":"reviewSummation", "id": "d24d4180-65aa-42ec-a945-5fd21dec0507" } }
178+
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic submission.notification.delete
141179
```
142180
143-
12. Validate the data using Postman
181+
10. Write the following message to the Console. Also check the leaderboard-api console for more information(Console in step 2)
144182
145-
## Running unit tests and coverage
183+
```bash
184+
{ "topic":"submission.notification.delete", "originator":"submission-api", "timestamp":"2018-08-06T15:46:05.575Z", "mime-type":"application/json", "payload":{ "resource":"reviewSummation", "id": "d24d4180-65aa-42ec-a945-5fd21dec0508" } }
185+
```
146186
147-
To run tests, following Environment variables need to be set up
187+
11. You could see in the console that message will be processed
148188
149-
- TEST_MONGODB_URL MongoDB URL pointing to Test DB instance
189+
### Tests
190+
Note: you need to stop the processor app before execute test.
150191
151-
To run unit tests alone
192+
- Run the following command to execute unit test and generate coverage report
152193
153194
```bash
154195
npm run test
155196
```
156197
157-
To run unit tests with coverage report
198+
- Run the following command to execute e2e test and generate coverage report
158199
159200
```bash
160-
npm run cov
201+
npm run e2e
161202
```

config/default.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,20 @@ module.exports = {
1212
KAFKA_CLIENT_CERT: process.env.KAFKA_CLIENT_CERT,
1313
KAFKA_CLIENT_CERT_KEY: process.env.KAFKA_CLIENT_CERT_KEY,
1414

15+
KAFKA_GROUP_ID: process.env.KAFKA_GROUP_ID || 'tc-leaderboard-processor',
16+
1517
// Kafka topics related to Creation, Update and Delete
1618
CREATE_DATA_TOPIC: process.env.CREATE_DATA_TOPIC || 'submission.notification.create',
1719
UPDATE_DATA_TOPIC: process.env.UPDATE_DATA_TOPIC || 'submission.notification.update',
1820
DELETE_DATA_TOPIC: process.env.DELETE_DATA_TOPIC || 'submission.notification.delete',
1921

20-
GROUP_IDS: process.env.GROUP_IDS || '202343,20000000', // Comma separated string of Group IDs
21-
22+
LEADERBOARD_API_URL: process.env.LEADERBOARD_API_URL || 'https://api.topcoder-dev.com/v5/leaderboard',
2223
SUBMISSION_API_URL: process.env.SUBMISSION_API_URL || 'https://api.topcoder-dev.com/v5/submissions',
23-
REVIEW_SUMMATION_API_URL: process.env.REVIEW_SUMMATION_API_URL || 'https://api.topcoder-dev.com/v5/reviewSummations',
24-
CHALLENGE_API_URL: process.env.CHALLENGE_API_URL || 'https://api.topcoder-dev.com/v3/challenges',
25-
MEMBER_API_URL: process.env.MEMBER_API_URL || 'https://api.topcoder-dev.com/v3/users',
2624

27-
AUTH0_URL: process.env.AUTH0_URL, // Auth0 credentials for Leaderboard Service
25+
AUTH0_URL: process.env.AUTH0_URL,
2826
AUTH0_AUDIENCE: process.env.AUTH0_AUDIENCE,
2927
TOKEN_CACHE_TIME: process.env.TOKEN_CACHE_TIME,
3028
AUTH0_CLIENT_ID: process.env.AUTH0_CLIENT_ID,
3129
AUTH0_CLIENT_SECRET: process.env.AUTH0_CLIENT_SECRET,
32-
AUTH0_PROXY_SERVER_URL: process.env.AUTH0_PROXY_SERVER_URL,
33-
MONGODB_URL: process.env.MONGODB_URL || 'mongodb://localhost:27017/leaderboardDB'
30+
AUTH0_PROXY_SERVER_URL: process.env.AUTH0_PROXY_SERVER_URL
3431
}

config/test.js

100755100644
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/**
2-
* Configuration file to be used while running tests
2+
* The configuration file.
33
*/
44

55
module.exports = {
6-
MONGODB_URL: process.env.TEST_MONGODB_URL || 'mongodb://localhost:27017/leaderboardTestDB'
6+
LEADERBOARD_API_URL: 'https://api.topcoder-dev.com/v5/leaderboard',
7+
SUBMISSION_API_URL: 'https://api.topcoder-dev.com/v5/submissions',
8+
WAIT_TIME: 1000
79
}

0 commit comments

Comments
 (0)