Charts that don't work all the time

Hello :wave:

Feature(s) impacted

Dashboard, chart

Observed behavior

In our dashboard in the Production environment we have several charts, but there are times when the charts are no longer displayed.
For example, here:

But sometimes when I refresh it works and other charts don’t work anymore.
Also, in the Staging and Preprod environments, all charts work.

Expected behavior

Before there were no display problems, would you have idea for this problem ?

Context

  • Project name: Nostrum Care v2
  • Team name: Operation
  • Environment name: Production
  • Agent (forest package) name & version: “forest-express-sequelize”: “^8.0.0”
  • Database type: Postgres

Thank you in advance

Hello @jacques_liao,

Do you have any error logs on the browser or on the agent side while encountering this kind of issue ?

Hello @nbouliol,


then I don’t get an error log.

Hello @jacques_liao

On which kind of charts (simple / query / api) are you seeing the cors issue ?

Hello @nbouliol
On charts api

Thank you

Before there were no display problems

Did you make recent changes ?

No, we haven’t made any recent changes

And I have this error in console

POST https://prd......?timezone=Europe%2FParis net::ERR_FAILED 504 (Gateway Time-out)

Could this problem be due to our servers?

Hello, do you have any news ?

Hello @jacques_liao

Sorry for the delay

Yes, I think your server returning 504 error might be the issue

Nicolas

Hello @nbouliol
We check on our side.

Hello @nbouliol,

We checked on our side and there were no problems in our servers. Could this be linked to our Cors configuration?

I added an allowedHeaders :

allowedHeaders: ['Forest-Context-Url', 'Authorization', 'X-Requested-With', 'Content-Type'],
const corsConfig = {
  origin: allowedOrigins,
  allowedHeaders: ['Forest-Context-Url','Authorization', 'X-Requested-With', 'Content-Type'],
  maxAge: 86400, // NOTICE: 1 day
  credentials: true,
};

But it doesn’t work

Hello,
do you have any news?

Hello @jacques_liao

I am trying to reproduce your issue, I’m coming back to you when I found what is happening

Can you please share with me the code of one of the failing charts ?

Yes

router.post('/subscription/nv/amountByYears/byWeeks', async (req, res) => {
    try {
        const contracts = await nvContracts.findAll();
        const beneficiariesList = await beneficiaries.findAll();
        let contractAmountsTotal = 0;
        let countNvBeneficiaries = 0;
        let values = [];
        const dateNow = moment().format();
        const startDate = moment("22/06/2023", 'DD/MM/YYYY').format();
        let date = startDate;
        let dateGraph;
        let allContractNvAmountDivideByNvBeneficiaries = 0;
        // add 7 days in startDate while startDate is before or same date now
        while (moment(date).isSameOrBefore(dateNow)) {
            date = moment(date).add(7, 'days');
            values.push({ label: date, values: { value: 0 } });
        }

        for (const valueIndex in values) {
            // check if actual date is before or same in date
            if (moment(values[valueIndex].label).isSameOrBefore(dateNow)) {
                dateGraph = values[valueIndex].label
                while (moment(dateGraph).isSameOrBefore(dateNow)) {
                    for (const contract of contracts) {
                        const createdAtMoment = moment(contract.createdAt, 'DD/MM/YYYY').format();
                        // check if actual beneficiary contract is before or same in date
                        if (moment(createdAtMoment).isSameOrBefore(dateGraph, "days")) {
                            // recover amounts from all nv contracts
                            if (contract.amount) {
                                // add up all the amounts then total * 12
                                contractAmountsTotal += contract.amount * 12;
                            }
                            for (const beneficiary of beneficiariesList) {
                                // ccount beneficiaries nv
                                if (beneficiary.fkUserIdKey === contract.fkUserId) {
                                    countNvBeneficiaries += 1;
                                }
                            }
                        }
                    }
                    // All nv contract amount divide by nv beneficiaries
                    allContractNvAmountDivideByNvBeneficiaries += contractAmountsTotal / (countNvBeneficiaries - 1);
                    dateGraph = moment(dateGraph).add(1, 'days').format();
                }
                let index = values.findIndex(obj => moment(obj.label).format('YYYY-MM-DD') === moment(values[valueIndex].label).format('YYYY-MM-DD'));
                if (index != undefined) {
                    values[index].values.value += allContractNvAmountDivideByNvBeneficiaries;
                }
                contractAmountsTotal = 0;
                countNvBeneficiaries = 0;
                allContractNvAmountDivideByNvBeneficiaries = 0;
            } else {
                allContractNvAmountDivideByNvBeneficiaries = 0;
                let index = values.findIndex(obj => moment(obj.label).format('YYYY-MM-DD') === moment(values[valueIndex].label).format('YYYY-MM-DD'));
                if (index != undefined) {
                    values[index].values.value += allContractNvAmountDivideByNvBeneficiaries;
                }
            }
        }
        let json = new Liana.StatSerializer({
            value: values
        }).perform();
        return res.send(json);
    } catch (error) {
        console.error(`Error while count nv beneficiaries by weeks, error : ${error}`);
        throw error;
    }
});

Hello @jacques_liao

I think your charts don’t render because of the gateway timeout, the CORS is not the issue here

For performance reasons, you should avoid using findAll on your collection and iterating over all your datas.

Maybe you should consider getting the contracts and beneficiaries after the startDate

  const startDate = moment("22/06/2023", 'DD/MM/YYYY').format(); 
  const contracts = await nvContracts.findAll({where: {[Op.gte]}: startDate});
  const beneficiariesList = await beneficiaries.findAll({where: {[Op.gte]}: startDate});

Hello @nbouliol
Yes, it’s for performance reasons because of the for and while loops.
Thanks for the help, I’ll try to optimize on our side. :+1:

1 Like