stats4sd / aec_portfolio

A proof of concept for the AEC Consortium Project Management / Assessment System
GNU General Public License v3.0
0 stars 0 forks source link

Get One Day Exchange Rates Data #209

Closed dan-tang-ssd closed 1 year ago

dan-tang-ssd commented 1 year ago

This PR is submitted to fix #124.

Freecurrency API provides exchange rate data up to yesterday, consider time difference and time zones, reserve more buffer to get data of the date before yesterday instead of yesterday.

Command: php artisan app:get-daily-exchange-rates

The schedule job will be executed at 00:21 AM everyday.


I intended to modify GetHistoricExchangeRates.php to support this feature. But I realised that this class cannot have another constructor method with different parameters. Finally I copy it as GetOneDayExchangeRates.php.

dan-tang-ssd commented 1 year ago

I have revised program to get yesterday exchange rate data every day at 6:00 AM.

Suppose we should have 1089 (i.e. 33 x 33) exchange_rates records everyday. image

I tried several days but the exchange data retrieved is incomplete... image

It looks like we only have exchange rate data for the first few base currencies... image

dan-tang-ssd commented 1 year ago

The command add jobs to queue normally... image

Telescope Jobs tab looks normal... image

dave-mills commented 1 year ago

The reason it's only retrieving the first few currencies is because of the rate limiter on the API:

CleanShot 2023-08-21 at 10 10 56

The middleware added to the class (and defined in AppServiceProvider was meant to handle that, by only allowing 5 jobs to complete per minute. I think I messed that up by not specifying a "by" key on the limiter, so it was limiting each individual job to 5 attempts per minute).

I don't quite understand why sometimes it showed all the jobs as "processed" in Telescope, as-in on your version, and sometimes it showed the first few jobs as processed and others as failed.

But, with a few minor tweaks, I think this is now working as intended:

Changes

To test locally:

(It doesn't matter if it's not exactly 1089 - occasionally, the API doesn't seems to have entries for certain combos. I have added a small extra into the ExchangeRateController so that if the rate for a specific date is not found, it will have 1 attempt at retriving the rate from the day before. I think this will be enough to cover most cases of missing data).

@dan-tang-ssd - can you double check this works ok locally? Then I think it's good to go.

dan-tang-ssd commented 1 year ago

Thank you Dave for the enhancement. It works well in my local env.

There is one minor item, in order to put back jobs on the queue, we need to change config item "QUEUE_CONNECTION" from "sync" to "database" in .env file

# QUEUE_CONNECTION=sync
QUEUE_CONNECTION=database

I will update .env file when we have deployment in staging env.

dan-tang-ssd commented 1 year ago

Deployment is completed in staging env.

.env file in staging env is updated for config item "QUEUE_CONNECTION", change from "sync" to "database".

I found that there is no queue created in Forge, I have just created a queue name "default" with connection "database". image

I run command to get one day exchange rate data, it works well. Suppose the command will be triggered at 6:00 AM tomorrow by schedule job. I would expect exchange rate data for 2023-08-21 will be existed in database tomorrow morning.

dan-tang-ssd commented 1 year ago

I did a checking in backend database, there is no exchange rate data for 2023-08-21. Not sure whether setup for schedule job is completed in staging env. I will check and feedback.

image

dan-tang-ssd commented 1 year ago

Checked that scheduler has not been created for staging env before. I have just created a scheduler for staging env. Suppose the schedule job for getting exchange rate data will be executed at 6:00 AM tomorrow. It is expected to have 2023-08-22 exchange rate data when I do checking tomorrow morning.

I have just manually run the command for getting exchange rate data for 2023-08-21. It manages to complete within a few minutes, exchange rate data for 2023-08-21 is successfully retrieved.

image

dan-tang-ssd commented 1 year ago

I did a checking in backend database this morning, there is no exchange rate data for 2023-08-22. image

@dave-mills , I have created a queue on 21 Aug and a scheduler on 22 Aug in staging env. Is there anything I missed in setup...?

dave-mills commented 1 year ago

Looking at the Forge scheduler output, I see it says something about not recognising the value "Sunday".

CleanShot 2023-08-23 at 09 58 28@2x

Turns out when specifying a day of the week we should use number values instead of the strings. I've sent a pull request to fix this. #218

dan-tang-ssd commented 1 year ago

We have deployed a fix for schedule job on 2023-08-23. I did a checking on scheduler yesterday, no error message. I suppose the scheduler is working properly.

We had exchange rate data for 2023-08-20 and 2023-08-21. The scheduler should run schedule job on 2023-08-24 6:00 AM to get exchange rate data for 2023-08-23.

Just did a checking in front end with positive result. Different exchange rate data showed for 2023-08-20, 2023-08-21 and 2023-08-23.

I would consider this PR can be closed now.


Screen shots:

image

image

image