0

I have an api that runs a mysql query and get hourly results for the last 48 and creates an array of objects with those results in it.

api & query:

app.get('/api/countRealTimeServedObject48', function (req, res) {
    newConnection.query('SELECT count(*) AS countRealTimeServedNumber, date_format(created, \'%H:00 - %d/%m/%y\') AS countRealTimeServed48 \
    FROM mimesi_realtime.served_clips \
    WHERE created > NOW() - INTERVAL 48 HOUR \
    GROUP BY date_format(created, \'%H:00 - %d/%m/%y\') \
    ORDER BY created ASC', function (error, results, fields) {
        if (error) throw error;
        res.end(JSON.stringify(results));
    });
});

results:

[{"countRealTimeServedNumber":1,"countRealTimeServed48":"14:00 - 09/07/17"},
{"countRealTimeServedNumber":12,"countRealTimeServed48":"15:00 - 09/07/17"},
{"countRealTimeServedNumber":9,"countRealTimeServed48":"16:00 - 09/07/17"},
{"countRealTimeServedNumber":14,"countRealTimeServed48":"17:00 - 09/07/17"},
{"countRealTimeServedNumber":16,"countRealTimeServed48":"18:00 - 09/07/17"},
{"countRealTimeServedNumber":5,"countRealTimeServed48":"19:00 - 09/07/17"},
{"countRealTimeServedNumber":5,"countRealTimeServed48":"20:00 - 09/07/17"},
{"countRealTimeServedNumber":5,"countRealTimeServed48":"21:00 - 09/07/17"},
{"countRealTimeServedNumber":3,"countRealTimeServed48":"22:00 - 09/07/17"},
{"countRealTimeServedNumber":16,"countRealTimeServed48":"05:00 - 10/07/17"},
{"countRealTimeServedNumber":10,"countRealTimeServed48":"06:00 - 10/07/17"},
{"countRealTimeServedNumber":5,"countRealTimeServed48":"07:00 - 10/07/17"},
{"countRealTimeServedNumber":15,"countRealTimeServed48":"08:00 - 10/07/17"},
{"countRealTimeServedNumber":26,"countRealTimeServed48":"09:00 - 10/07/17"},
{"countRealTimeServedNumber":57,"countRealTimeServed48":"10:00 - 10/07/17"},
{"countRealTimeServedNumber":25,"countRealTimeServed48":"11:00 - 10/07/17"},
{"countRealTimeServedNumber":39,"countRealTimeServed48":"12:00 - 10/07/17"},
{"countRealTimeServedNumber":51,"countRealTimeServed48":"13:00 - 10/07/17"},
{"countRealTimeServedNumber":50,"countRealTimeServed48":"14:00 - 10/07/17"},
{"countRealTimeServedNumber":37,"countRealTimeServed48":"15:00 - 10/07/17"},
{"countRealTimeServedNumber":26,"countRealTimeServed48":"16:00 - 10/07/17"},
{"countRealTimeServedNumber":28,"countRealTimeServed48":"17:00 - 10/07/17"},
{"countRealTimeServedNumber":25,"countRealTimeServed48":"18:00 - 10/07/17"},
{"countRealTimeServedNumber":19,"countRealTimeServed48":"19:00 - 10/07/17"},
{"countRealTimeServedNumber":15,"countRealTimeServed48":"20:00 - 10/07/17"},
{"countRealTimeServedNumber":2,"countRealTimeServed48":"21:00 - 10/07/17"},
{"countRealTimeServedNumber":6,"countRealTimeServed48":"22:00 - 10/07/17"},
{"countRealTimeServedNumber":14,"countRealTimeServed48":"05:00 - 11/07/17"},
{"countRealTimeServedNumber":16,"countRealTimeServed48":"06:00 - 11/07/17"},
{"countRealTimeServedNumber":37,"countRealTimeServed48":"08:00 - 11/07/17"},
{"countRealTimeServedNumber":54,"countRealTimeServed48":"09:00 - 11/07/17"},
{"countRealTimeServedNumber":29,"countRealTimeServed48":"10:00 - 11/07/17"},
{"countRealTimeServedNumber":61,"countRealTimeServed48":"11:00 - 11/07/17"},
{"countRealTimeServedNumber":24,"countRealTimeServed48":"12:00 - 11/07/17"},
{"countRealTimeServedNumber":55,"countRealTimeServed48":"13:00 - 11/07/17"},
{"countRealTimeServedNumber":47,"countRealTimeServed48":"14:00 - 11/07/17"}]

What I need to do is to loop through the array and check where there are missing hours and, where there are missing hours, add the countRealTimeServed48 with "hour:00 - date/month/year" and the countRealTimeServedNumber = 0;

How could I do it with javascript after the JSON.stringify()?

EDIT part of the code I am using in my app:

app.get('/api/countRealTimeServedObject48', function (req, res) {
newConnection.query('SELECT count(*) AS countRealTimeServedNumber, date_format(created, \'%H:00 - %d/%m/%y\') AS countRealTimeServed48 \
FROM mimesi_realtime.served_clips \
WHERE created > NOW() - INTERVAL 48 HOUR \
GROUP BY date_format(created, \'%H:00 - %d/%m/%y\') \
ORDER BY created ASC', function (error, results, fields) {
    if (error) throw error;
    const moment = require('moment');
    const result = JSON.stringify(results); //I believe this might be the problem
    const DATE_FORMAT = 'HH:mm - DD/MM/YY';

    const startDate = moment();

    const dateForIndex = (date, index) =>
    date.clone().add(index, 'hour').format(DATE_FORMAT);

    const dates = Array(48)
    .fill()
    .map((val, index) => dateForIndex(startDate, index))
    .map(
        date =>
        result.find(el => el.countRealTimeServed48 === date) || {
            countRealTimeServedNumber: 0,
            countRealTimeServed48: date
        }
    );
    res.end(dates);
});

});

4
  • need to do it before you stringify or do it in browser. Please provide a minimal reproducible example. The query is virtually meaningless , it's the resultant data that we can't see Commented Jul 11, 2017 at 13:21
  • It's up now. Sorry I didn't link the result picture correctly before Commented Jul 11, 2017 at 13:25
  • Don't post as image...nobody can copy that from image and it is much easier to read as formatted text Commented Jul 11, 2017 at 13:43
  • I added it as code, is that alright? Commented Jul 11, 2017 at 13:53

1 Answer 1

1

This should do the trick:

'use strict';

const moment = require('moment');

const result = [
  { countRealTimeServedNumber: 1, countRealTimeServed48: '14:00 - 09/07/17' },
  { countRealTimeServedNumber: 12, countRealTimeServed48: '15:00 - 09/07/17' },
  { countRealTimeServedNumber: 9, countRealTimeServed48: '16:00 - 09/07/17' },
  { countRealTimeServedNumber: 14, countRealTimeServed48: '17:00 - 09/07/17' },
  { countRealTimeServedNumber: 16, countRealTimeServed48: '18:00 - 09/07/17' },
  { countRealTimeServedNumber: 5, countRealTimeServed48: '19:00 - 09/07/17' },
  { countRealTimeServedNumber: 5, countRealTimeServed48: '20:00 - 09/07/17' },
  { countRealTimeServedNumber: 5, countRealTimeServed48: '21:00 - 09/07/17' },
  { countRealTimeServedNumber: 3, countRealTimeServed48: '22:00 - 09/07/17' },
  { countRealTimeServedNumber: 16, countRealTimeServed48: '05:00 - 10/07/17' },
  { countRealTimeServedNumber: 10, countRealTimeServed48: '06:00 - 10/07/17' },
  { countRealTimeServedNumber: 5, countRealTimeServed48: '07:00 - 10/07/17' },
  { countRealTimeServedNumber: 15, countRealTimeServed48: '08:00 - 10/07/17' },
  { countRealTimeServedNumber: 26, countRealTimeServed48: '09:00 - 10/07/17' },
  { countRealTimeServedNumber: 57, countRealTimeServed48: '10:00 - 10/07/17' },
  { countRealTimeServedNumber: 25, countRealTimeServed48: '11:00 - 10/07/17' },
  { countRealTimeServedNumber: 39, countRealTimeServed48: '12:00 - 10/07/17' },
  { countRealTimeServedNumber: 51, countRealTimeServed48: '13:00 - 10/07/17' },
  { countRealTimeServedNumber: 50, countRealTimeServed48: '14:00 - 10/07/17' },
  { countRealTimeServedNumber: 37, countRealTimeServed48: '15:00 - 10/07/17' },
  { countRealTimeServedNumber: 26, countRealTimeServed48: '16:00 - 10/07/17' },
  { countRealTimeServedNumber: 28, countRealTimeServed48: '17:00 - 10/07/17' },
  { countRealTimeServedNumber: 25, countRealTimeServed48: '18:00 - 10/07/17' },
  { countRealTimeServedNumber: 19, countRealTimeServed48: '19:00 - 10/07/17' },
  { countRealTimeServedNumber: 15, countRealTimeServed48: '20:00 - 10/07/17' },
  { countRealTimeServedNumber: 2, countRealTimeServed48: '21:00 - 10/07/17' },
  { countRealTimeServedNumber: 6, countRealTimeServed48: '22:00 - 10/07/17' },
  { countRealTimeServedNumber: 14, countRealTimeServed48: '05:00 - 11/07/17' },
  { countRealTimeServedNumber: 16, countRealTimeServed48: '06:00 - 11/07/17' },
  { countRealTimeServedNumber: 37, countRealTimeServed48: '08:00 - 11/07/17' },
  { countRealTimeServedNumber: 54, countRealTimeServed48: '09:00 - 11/07/17' },
  { countRealTimeServedNumber: 29, countRealTimeServed48: '10:00 - 11/07/17' },
  { countRealTimeServedNumber: 61, countRealTimeServed48: '11:00 - 11/07/17' },
  { countRealTimeServedNumber: 24, countRealTimeServed48: '12:00 - 11/07/17' },
  { countRealTimeServedNumber: 55, countRealTimeServed48: '13:00 - 11/07/17' },
  { countRealTimeServedNumber: 47, countRealTimeServed48: '14:00 - 11/07/17' }
];

const DATE_FORMAT = 'HH:mm - DD/MM/YY';

const startDate = moment('2017-07-09 14:00:00'); // To make it work with the provided sample data set. You'll probably want just moment() in your code.

const dateForIndex = (date, index) =>
  date.clone().add(index, 'hour').format(DATE_FORMAT);

const dates = Array(48)
  .fill()
  .map((val, index) => dateForIndex(startDate, index))
  .map(
    date =>
      result.find(el => el.countRealTimeServed48 === date) || {
        countRealTimeServedNumber: 0,
        countRealTimeServed48: date
      }
  );

console.log(dates);

I suggest replacing the database's NOW() function with the startDate calculated in your application to keep things in sync.

Sign up to request clarification or add additional context in comments.

5 Comments

The OP does not appear to be using the moment library.
That we don't know yet! ;-) But I think it's unavoidable to do some sort of date calculation and I highly advise against reinventing those...
if I plug it into my app it says 'result.find is not a function'. btw, is fine, I am using moment.js
If you look at the edit you see what I am trying to do in my app
Exactly! stringify() converts your Array to a String which has no find() method. If you're using Express, you don't need to stringify the result at all. Just use res.json() to send it as JSON to the browser. I guess that's what you're trying to do, isn't it? See this example (untested): pastebin.com/2duebtaU

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.