Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gas cost type error #26

Merged
merged 1 commit into from
Apr 16, 2023

Conversation

kwridan
Copy link
Contributor

@kwridan kwridan commented Feb 1, 2023

  • In cases where an IHD is configured for electricity meter only without a gas meter, the gas cost calculations would error
...
in <lambda>
    (js['gasmeter']['energy']['import']['day'] * js['gasmeter']['energy']['import']['price']['unitrate']), 2)
TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType'
  • This was due to the gas values being null
  • We now default to 0 for any missing values

Test Plan:

  • Manually run the calculation function on the following json sample
{
    "gasmeter": {
        "timestamp": "1970-01-01T00:00:00Z",
        "energy": {
            "import": {
                "cumulative": null,
                "day": null,
                "week": null,
                "month": null,
                "units": "kWh",
                "cumulativevol": null,
                "cumulativevolunits": "",
                "dayvol": null,
                "weekvol": null,
                "monthvol": null,
                "dayweekmonthvolunits": "",
                "mprn": "read pending",
                "supplier": "---",
                "price": {
                    "unitrate": null,
                    "standingcharge": null
                }
            }
        }
    }
}

- In cases where an IHD is configured for electricity meter only without a gas meter, the gas cost calculations would error

```
...
in <lambda>
    (js['gasmeter']['energy']['import']['day'] * js['gasmeter']['energy']['import']['price']['unitrate']), 2)
TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType'
```

- This was due to the gas values being `null`
- We now default to `0` for any missing values

Test Plan:

- Manually run the calculation function on the following json sample

```json
{
    "gasmeter": {
        "timestamp": "1970-01-01T00:00:00Z",
        "energy": {
            "import": {
                "cumulative": null,
                "day": null,
                "week": null,
                "month": null,
                "units": "kWh",
                "cumulativevol": null,
                "cumulativevolunits": "",
                "dayvol": null,
                "weekvol": null,
                "monthvol": null,
                "dayweekmonthvolunits": "",
                "mprn": "read pending",
                "supplier": "---",
                "price": {
                    "unitrate": null,
                    "standingcharge": null
                }
            }
        }
    }
}
```
Comment on lines +256 to +257
"func": lambda js : round((js['gasmeter']['energy']['import']['price']['standingcharge'] or 0)+ \
((js['gasmeter']['energy']['import']['day'] or 0) * (js['gasmeter']['energy']['import']['price']['unitrate'] or 0)), 2),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would creating a standalone function to do the calculation and type checking be a bit neater perhaps?

Something along the lines of:

"func": lambda js : calculate_cost(
      js['gasmeter']['energy']['import']['price']['standingcharge'],
      js['gasmeter']['energy']['import']['day'],
      js['gasmeter']['energy']['import']['price']['unitrate']
    )

and

def calculate_cost(standing_charge, day_import, unit_rate):
  return round((standing_charge or 0) + ((day_import or 0) * (unit_rate or 0)), 2)

This can also be re-used for the electricity costs too which will probably suffer the same issue when the situation is reversed (have a gas meter, but no electricity meter).

@megakid megakid self-requested a review April 16, 2023 08:13
@megakid megakid merged commit eccb06e into megakid:main Apr 16, 2023
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.

2 participants