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 ( )
0 commit comments