Skip to content

Replace duplicated raw export size calculations with a shared estimator - #1686

Open
stockingstocker wants to merge 8 commits into
OpenEnergyDashboard:developmentfrom
stockingstocker:issue-1684
Open

Replace duplicated raw export size calculations with a shared estimator#1686
stockingstocker wants to merge 8 commits into
OpenEnergyDashboard:developmentfrom
stockingstocker:issue-1684

Conversation

@stockingstocker

Copy link
Copy Markdown
Contributor

Description

This PR moves the raw export file size estimation logic into a shared module used by both the client and server. The client uses the estimate to warn users and determine whether an export should proceed, while the server uses the same estimate to enforce the configured file size limit.

Previously, exportThunk.ts and readings.js each contained their own copy of the file size estimation formula:

count * 0.082 / 1000

Keeping separate copies creates a risk that the client and server could use different values or formulas if one is updated without the other.

Now, the client and server import estimateRawExportSizeMB() instead of calculating the estimate independently. Unit tests have also been added undersrc/server/test/util to verify the estimator returns the expected values for:

  • zero readings
  • a known reading count with an expected estimated size
  • a non-round reading count to verify the calculation does not depend on values that simplify cleanly

Fixes #1684

Type of change

(Check the ones that apply by placing an "x" instead of the space in the [ ] so it becomes [x])

  • Note merging this changes the database configuration.
  • This change requires a documentation update

Checklist

(Note what you have done by placing an "x" instead of the space in the [ ] so it becomes [x]. It is hoped you do all of them.)

  • I have followed the OED pull request ideas
  • I have removed text in ( ) from the issue request
  • You acknowledge that every person contributing to this work has signed the OED Contributing License Agreement and each author is listed in the Description section.

Limitations

No limitations.

@huss huss left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stockingstocker Thank you for another contribution. Review and testing found it works well. I have made a few comments to consider. Please let me know if anything is not clear or you have thoughts/questions.

* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

const chai = require('chai');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this works, many files get the chai, mocha & expect from common file to keep them consistent across files. I think that would be better. Having said that, OED probably should open an issue to do that across all tests to be consistent, assuming it causes no problem. Would you like to do that or should I? This should also get rid of the blank line between items as OED prefers.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching that. I was working from habit and forgot about OED’s common test file.. For the issue, I would be happy to open it after I take a closer look at the existing test files.


mocha.describe('Raw Export File Size Estimator', () => {
mocha.it('returns zero for zero readings', () => {
expect(estimateRawExportSizeMB(0)).to.be.closeTo(0, 0.001);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a specific reason you use 0.001 tolerance on these tests? In readings (see src/server/util/readingsUtils.js) it has a DELTA that is stricter. Stricter is better when it works. I would have thought this would be basic quasi-random numerical variation in the final digit given what is being tested. So, I tried 1e-15 and that worked fine. Maybe that should be used and set to a const in the file..

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't notice the delta value in readingsUtils.js. I now see that I gave the tests too much wiggle room. I will make the change


const ESTIMATED_KB_PER_RAW_READING = 0.082;
const KB_PER_MB = 1000;
/**

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a blank line before the JSDco.

Comment thread src/common/RawExportFileSize.js Outdated
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

const ESTIMATED_KB_PER_RAW_READING = 0.082;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have two thoughts. First, given the estimate is in MB, would it be easier to make this const be in MB and avoid the KB_PER_MB conversion? I might make it 8.2e-5 so it is easier to understand. Second, I'm wondering why this is exported. I could not find uses outside this file except in the d.ts file but then that usage does not seem to be used anywhere else. I'm uncertain this const would ever be used anywhere else in OED and encapsulating it to the function may make sense. Thus, I was wondering about removing the export, placing the const inside the function and removing it from the t.ds file. It worked fine for me.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the first point, I was keeping the structure of the formula the same as it originally was in the client but you're right that changing the estimate to be in MB makes a lot more sense now that its in a function. For the second point I was being safe just in case the values may be needed elsewhere but I realize now that encapsulating it for just the function is more reasonable.

@huss

huss commented Aug 4, 2026

Copy link
Copy Markdown
Member

I saw the update. I'm sorry that it is going to take a little while for me to get to this. Let me know if the delay becomes an issue for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Share raw export file size estimator between client and server

2 participants