feat: add back fetch route
This commit is contained in:
		
							parent
							
								
									1d3728b48d
								
							
						
					
					
						commit
						fb00495840
					
				
					 3 changed files with 54 additions and 1 deletions
				
			
		
							
								
								
									
										46
									
								
								app/controllers/internals_controller.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								app/controllers/internals_controller.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,46 @@ | ||||||
|  | import type { HttpContext } from '@adonisjs/core/http' | ||||||
|  | import redis from '@adonisjs/redis/services/main' | ||||||
|  | import { DateTime } from 'luxon' | ||||||
|  | 
 | ||||||
|  | export default class InternalsController { | ||||||
|  |   // POST /internals/back-fetch
 | ||||||
|  |   async backFetch({ response, request }: HttpContext) { | ||||||
|  |     const className = request.input('className') | ||||||
|  |     if (!className) { | ||||||
|  |       return response.badRequest({ message: 'className is required' }) | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // Start a new thread to avoid blocking the event loop
 | ||||||
|  |     response.send({ | ||||||
|  |       success: true, | ||||||
|  |       message: `Fetching colles for class ${className}...`, | ||||||
|  |     }) | ||||||
|  |     setImmediate(async () => { | ||||||
|  |       await queue(className) | ||||||
|  |       console.log(`Colles fetching for class ${className} completed.`) | ||||||
|  |     }) | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | async function queue(className: string) { | ||||||
|  |   // 1er Septembre 2019 début de BJColle
 | ||||||
|  |   const startDate = DateTime.fromISO('2019-09-01T00:00:00') | ||||||
|  |   const endDate = DateTime.now() | ||||||
|  |   let date = endDate | ||||||
|  | 
 | ||||||
|  |   // Loop through all days from startDate to endDate
 | ||||||
|  |   while (date >= startDate) { | ||||||
|  |     await redis.publish( | ||||||
|  |       'jobs_queue', | ||||||
|  |       JSON.stringify({ | ||||||
|  |         type: 1, // Fetch day colles
 | ||||||
|  |         // Format DD/MM/YYYY
 | ||||||
|  |         date: date.toFormat('dd/MM/yyyy'), | ||||||
|  |         class_name: className, | ||||||
|  |       }) | ||||||
|  |     ) | ||||||
|  |     date = date.minus({ days: 1 }) | ||||||
|  |     // Wait for 1 second to avoid overwhelming the queue
 | ||||||
|  |     await new Promise((resolve) => setTimeout(resolve, 1000)) | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | @ -97,7 +97,6 @@ export class GradeService { | ||||||
|         periodStartDate |         periodStartDate | ||||||
|       ) |       ) | ||||||
| 
 | 
 | ||||||
|       console.log(this.reduce(subjectAverages), subjectAverages) |  | ||||||
|       results.push({ |       results.push({ | ||||||
|         period: periodStartDate.toFormat('MMM'), |         period: periodStartDate.toFormat('MMM'), | ||||||
|         average: periodAverage, |         average: periodAverage, | ||||||
|  |  | ||||||
|  | @ -62,3 +62,11 @@ router | ||||||
| 
 | 
 | ||||||
| const GradesController = () => import('#controllers/grades_controller') | const GradesController = () => import('#controllers/grades_controller') | ||||||
| router.get('/grades', [GradesController, 'index']).use(middleware.auth()) | router.get('/grades', [GradesController, 'index']).use(middleware.auth()) | ||||||
|  | 
 | ||||||
|  | const InternalsController = () => import('#controllers/internals_controller') | ||||||
|  | router | ||||||
|  |   .group(() => { | ||||||
|  |     router.post('/back-fetch', [InternalsController, 'backFetch']) | ||||||
|  |   }) | ||||||
|  |   .prefix('/internals') | ||||||
|  | // TODO: Token authentication
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 Nathan Lamy
						Nathan Lamy