ralberth / MMM-MysqlQuery

MagicMirror plug-in module that displays a table of MySQL SELECT results
MIT License
8 stars 3 forks source link

precision: 0; not working? #17

Closed Coernel82 closed 1 year ago

Coernel82 commented 1 year ago

Hi, I tried to get rid after the digits after the comma, so instead of 89,32 just 89 - this seems to fail. I also tried precision: 5; which correctly resulted in 89,32000

Can you confirm that there is a bug?

ralberth commented 1 year ago

Hi! What did you try to get rid of the digits after the comma? For example, do you have the table definition, or the section of your config.js with the MMM-MysqlQuery set up? I'm wondering if you used precision: 0 and are seeing trouble.

As a stop-gap, you could consider logic in your SQL SELECT that prepares the data the way you want it to display, such as "select format(num_col, 0) from mytablename".

Coernel82 commented 1 year ago

Yes, precision: 0 is causing the trouble:

{
            module: 'MMM-MysqlQuery',
            position: "bottom_center",
            config: {
                connection: {
                    host: "localhost",
                    port: 3306,
                    user: "zisterne",
                    password: "zisterne",
                    database: "RASPBERRY_PI",
                },
                query: `select level from zisterne order by id desc limit 1`,
                intervalSeconds: 15 * 60,
                emptyMessage: "Kein Füllstand",
                columns: [
                    { name: "level", title: " ", precision: 0,  cssClass: "zisterne", suffix: "%"},
                ]
            }
        },

Thank you for the sql hack but I am not sure how to implement. I also found SELECT ROUND(x,y) So would it be select round(level,0) from zisterne order by id desc limit 1 ?

ralberth commented 1 year ago

For now, yes you can use

query: `select format(level, 0) as level from zisterne order by id desc limit 1`,

as a temporary solution.


I tried precision: 0 with different data-types and values, and I can confirm it's a bug!

Here's some developer junk:

Setting a precision in config.js just causes the number toFixed() function to be called. This behaves like you think it would:

❯ node
Welcome to Node.js v16.13.0.
Type ".help" for more information.
> let a = 123.456;
undefined
> a.toFixed(0)
'123'
> a.toFixed(1)
'123.5'
> a.toFixed(2)
'123.46'

The code in question from MMM-MysqlQuery: https://github.com/ralberth/MMM-MysqlQuery/blob/master/MMM-MysqlQuery.js#L172-L174

When I tried it out, all values for precision worked as expected, but using 0 didn't. 0 just skips this "if". This is a crappy JavaScript thing: zero is "falsy", meaning it fails an IF statement. The fix is to change if (cellConf.precision) to if (cellConf.precision == null). I'll put in the change and test it soon.

Coernel82 commented 1 year ago

Thank you for the explanation - I understand that and indeed that is crappy. A little anecdote from our German tax program ELSTER: In the past more ore less randomly the tax forms wanted to have a 0 for empty vaules and in other form fields just blank space and no 0 . You had to figure that out by trial and error.

ralberth commented 1 year ago

Fixed in commit https://github.com/ralberth/MMM-MysqlQuery/commit/c1e79c5b0033fdac25923fffbb0f24fc06f49569

Please do a git pull in your modules/MMM-MysqlQuery to pick-up the change, and restart your MagicMirror server. Let me know how it goes and if you're happy, we'll resolve this ticket.

Coernel82 commented 1 year ago

Sorry, I missed to replay. I can confirm that it is fixed. Thank you for your great efforts!