0

I have a dashboard that is displayed on the main screen when the user logs on.

Initially I have two graphics, one of lines and one of bars. The problem is that the graphical queries are a bit heavy and take about 30 seconds to load.

I already did optimization of the queries and they improved a little, the problem is that when wanting to access other options of the system does not leave me, because I have to wait until the graphs are loaded.

It is difficult for the user to wait until the graphs load, I would like to know if there is a way that other system options can be accessed, regardless of the graphs being loaded.

The code I have is the following:

/*
* You get the urls that the user has access to in the dashboard. This is executed * when the user is on the main screen.
*/
  $.getJSON(getBaseUri() + 'dashboard/index', function(data) {

    var datas  = data['return'];

    if(datas == ""){
      $productivities.removeClass('chartID');
      $productions.removeClass('chartID');
      $combox.addClass('hidden');
    }

    for (var i in datas) {
      receiveData({
        div       : datas[i].div,
        title     : datas[i].privilege,
        sign      : datas[i].sign,
        iconClass : datas[i].class,
        id        : datas[i].id,
        label     : datas[i].label,
        xaxis     : datas[i].xaxis,
        yaxis     : datas[i].yaxis,
        background: datas[i].background,
        url       : datas[i].route,
        type      : datas[i].type
      });
    }
  });

/*
   * The function receiveData () receives the parameters of the dashboard that the user has access and brings the data of each url consulted, and according to the type of graph the function is executed.
   */
  function receiveData(param) {

    $.ajax({
      url: param.url,
      method: 'GET',
      dataType: 'json',
      success: function(data){
        var datas = data['return'];

        if(param.type === "LineChart"){
          lineChart({
            data : datas,
            div  : param.div,
            title: param.title,
            url  : param.url
          });
          $loaderProduction.addClass("hidden");
        }

        if (param.type === "BarChart") {
          barCharts({
            data      : datas,
            div       : param.div,
            title     : param.title,
            url       : param.url,
            label     : param.label,
            xaxis     : param.xaxis,
            yaxis     : param.yaxis,
            background: param.background
          });
          $loaderProductivity.addClass('hidden');
        }

        if (param.type === "Indicator") {
          indicatorsChart({
            data  : datas,
            div   : param.div,
            title : param.title,
            icon  : param.sign,
            class : param.iconClass,
            idDash: param.id
          });
        }

        if (param.type === "Sowing") {
          sowingIndicator({
            data  : datas,
            div   : param.div,
            title : param.title,
            idDash: param.id
          });
        }
      },
      error: function(error){
        console.log(error);
      }
    });
  }

Basically these are the functions I'm using to display dashboard graphs.

I think that this case I need to use WebWorkers, but I don´t know how to use it

Fabian Sierra
  • 766
  • 1
  • 8
  • 22
  • How many data points are in this chart? – jonmrich Feb 07 '17 at 01:53
  • There are two graphs with 52 points in every graph @jonmrich – Fabian Sierra Feb 07 '17 at 01:55
  • That should execute in milliseconds for charts.js. I'd guess that your data is being returned to AJAX very slowly. Can you check this? – jonmrich Feb 07 '17 at 01:56
  • I know that the queries are a bit heavy, in this question I asked for help to improve one of the queries (http://stackoverflow.com/questions/42069953/change-query-sql) @jonmrich – Fabian Sierra Feb 07 '17 at 01:59
  • Based on that query, I don't think charts.js is remotely the problem. You might just need to add a loading icon that disappears when the data is ready. That would at least tell the user that something is coming. – jonmrich Feb 07 '17 at 02:15
  • @jonmrich I already added a loading icon, but the user sometimes doesn´t wait, for that reason I would like to know if there is a way to solve them – Fabian Sierra Feb 07 '17 at 02:17
  • Not unless you fix your SQL query. Charts.js can't be optimized any more since you're only talking about a very small number of data points. – jonmrich Feb 07 '17 at 02:18
  • Yes, but the problem is the ajax query that does not allow to make more options in the page @jonmrich :( – Fabian Sierra Feb 07 '17 at 02:20

2 Answers2

0

Right now you're making an AJAX call for every iteration of your for...in loop. Depending on the number of iterations this could mean a huge overhead, although it is best to reduce the number of round trips to the server in all cases.

You could try to modify both the front- and backend logic to combine multiple datasets into one request instead of one dataset per request and multiple requests.

Other than this there are no obvious bottlenecks in your code, so if the slowness persists the problem is most likely with your backend code.

ppajer
  • 3,045
  • 1
  • 15
  • 22
  • I already optimized the code, but i think that the problem is the AJAX, I do not know if there is a way for the user to access the system menu options regardless of which graphics are loading! @ppajer – Fabian Sierra Feb 07 '17 at 12:44
0

Of course you got some options:

  • go through a Single Page Application, i.e. make your app in the way that, when user navigate other application options, the execution remains always in the same page. It may be heavy, exspecially if your application is already structured. Angular (or AngularJs), as other frontend frameworks, do this for you in an automated way.

  • let execute your loading js in an iframe, old and dirty but it works. When finished send a notification message in the main page,

Anyway you should optimize your code too, reducing number of calls from n (one for each element in datas) to 1.

Massimo Petrus
  • 1,881
  • 2
  • 13
  • 26