Skip to content

Commit 9d1efab

Browse files
author
rajs64
committed
Complete SupremeTA_Bot
0 parents  commit 9d1efab

8 files changed

+608
-0
lines changed

.env

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
AUTH_TOKEN=""
2+
PROJECT_ID=
3+
PAGE_SIZE=1000
4+
VIEW_ID=
5+
FILTER='[{"filter":"filter:tasks:data.verdict","operator":"contains","value":"batch","type":"Unknown"},{"filter":"filter:tasks:updated_by","operator":"contains","value":1950,"type":"List"}]'

LICENSE

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# License
2+
3+
This work is licensed under the Creative Commons Attribution-NonCommercial 4.0 International License.
4+
To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/4.0/.
5+
6+
You are free to:
7+
- Share — copy and redistribute the material in any medium or format
8+
- Adapt — remix, transform, and build upon the material
9+
10+
Under the following terms:
11+
- Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made.
12+
- NonCommercial — You may not use the material for commercial purposes.
13+
14+
For commercial use, please contact me at: [email protected].

app.js

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
const { assignTasksToDeveloper, fetchFilterData } = require('./toloka-apis')
2+
const fs = require('fs');
3+
const winston = require('winston');
4+
5+
const Project_name = 'Project P'
6+
7+
// Configure winston to log to a file
8+
const logger = winston.createLogger({
9+
level: 'info',
10+
format: winston.format.combine(
11+
winston.format.timestamp(),
12+
winston.format.printf(({ timestamp, level, message }) => {
13+
return `${timestamp} [${level}]: ${message}`;
14+
})
15+
),
16+
transports: [
17+
new winston.transports.File({ filename: 'log/task_assign.log' })
18+
]
19+
});
20+
21+
// Function to read JSON file
22+
function readJsonFile(filePath) {
23+
return new Promise((resolve, reject) => {
24+
fs.readFile(filePath, 'utf8', (err, data) => {
25+
if (err) {
26+
reject(err);
27+
} else {
28+
resolve(JSON.parse(data));
29+
}
30+
});
31+
});
32+
}
33+
34+
const assignTasks = async () => {
35+
try {
36+
const payload = await readJsonFile('data/task_data.json');
37+
38+
// Calculate the sum of values in the payload
39+
const sum = Object.values(payload).reduce((total, value) => total + Number(value), 0);
40+
console.log(sum);
41+
logger.info(`Total Task assigning ${sum}:`);
42+
43+
// Fetch the result of filter annotation
44+
const result = await fetchFilterData();
45+
console.log(result);
46+
47+
if (!result || !result.tasks) {
48+
logger.error('Invalid result format from fetchFilterData.');
49+
throw new Error("Invalid result format from fetchFilterData.");
50+
}
51+
tasks = result.tasks;
52+
53+
if (sum <= Number(result.total)) {
54+
const user_details = await readJsonFile('data/users_id.json'); // Ensure this function is awaited
55+
56+
// Convert payload object to an array of [email, value] pairs
57+
const entries = Object.entries(payload);
58+
59+
let taskIndex = 0;
60+
console.log("Beginning the task assignment process...");
61+
logger.info('Beginning the task assignment process...');
62+
// Iterate over the entries array
63+
for (let [email, value] of entries) {
64+
const user = user_details.find(user => user.email === email);
65+
if (user && Number(value) > 0) {
66+
let data = [];
67+
// Collect task IDs for the current user
68+
for (let i = 0; i < Number(value); i++) {
69+
if (tasks[taskIndex] && tasks[taskIndex].id) {
70+
data.push(tasks[taskIndex].id);
71+
taskIndex++;
72+
} else {
73+
console.warn("No more tasks available.");
74+
logger.warn("No more tasks available.");
75+
break; // Stop if no more tasks are available
76+
}
77+
}
78+
79+
// Convert data array to JSON string
80+
const eachTasks = JSON.stringify(data);
81+
// Perform the task assignment
82+
let assignmentResult = await assignTasksToDeveloper(user.id, eachTasks);
83+
// let assignmentResult = 200
84+
if (assignmentResult === 200) {
85+
console.log(`Successfully assigned ${value} tasks of ${Project_name} to ${user.email}: ${eachTasks}`)
86+
// Log successful task assignment
87+
logger.info(`Successfully assigned ${value} tasks of \`Project P\` to ${user.email}: ${eachTasks}`);
88+
} else {
89+
console.log(`Failed to assigned tasks to ${user.email}: ${eachTasks}`)
90+
// Log failed task assignment
91+
logger.error(`Failed to assigned tasks to ${user.email}: ${eachTasks}`)
92+
}
93+
} else {
94+
console.log(`Zero tasks assigned to ${user.email}: [null]`);
95+
logger.info(`Zero tasks assigned to ${user.email}: [null]`);
96+
}
97+
}
98+
99+
// Return a success message or result if needed
100+
console.log("All tasks have been successfully assigned.");
101+
logger.info('All tasks have been successfully assigned.');
102+
} else {
103+
console.log("Total Annotations Zero task is less than assigned tasks. Stopping the assignment.");
104+
logger.error('Total Annotations Zero task is less than assigned tasks. Stopping the assignment.');
105+
}
106+
} catch (error) {
107+
// Handle any errors that might occur
108+
console.error("An error occurred:", error);
109+
logger.error('Error occurs while assigning tasks. Stopping the assignment.');
110+
}
111+
}
112+
113+
assignTasks()

data/task_data.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
3+
4+
5+
6+
7+
}

data/users_id.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{ "id": 2009, "email": "[email protected]" },
3+
{ "id": 2101, "email": "[email protected]" },
4+
{ "id": 1300, "email": "[email protected]" },
5+
{ "id": 2100, "email": "[email protected]" },
6+
{ "id": 3600, "email": "[email protected]" }
7+
]

0 commit comments

Comments
 (0)