MD数组:如果找不到值,则添加其他数组

时间:2016-02-22 11:27:17

标签: javascript mysql arrays google-visualization

我目前正在尝试使用Google表格根据从我的MYSQL数据库提取的信息生成报告。

问题是有5个标题值:['Call Disposition', 'Answered', 'Busy', 'No Answer','Failed']

但问题是从数据库中提取的数据几乎会有4个值中的3个,这将定义我的Google图表标题,原因是如果"失败"找到并且只有一个值我从Google图表中收到以下错误:Uncaught Error: Row 0 has 4 columns, but must have 5

为了清楚地了解数据库,根据呼叫发生的情况自动插入值,而不是所有呼叫都会失败。

只有少数实例会使值失败,所以我正在尝试开发一种方法,如果找不到特定数字,那么添加额外的数组。

例如,这就是它当前基于从数据库中拉出来并推入MD阵列的方式:

[ [ 'ANSWERED', '477', 728 ],
  [ 'BUSY', '477', 48 ],
  [ 'NO ANSWER', '477', 277 ],
  [ 'ANSWERED', '488', 953 ],
  [ 'BUSY', '488', 9 ],
  [ 'FAILED', '488', 1 ],
  [ 'NO ANSWER', '488', 126 ] ]

因此,如果说没有数字没有失败处理的数字,请添加它,但是如果没有找到每个数字,则将值设置为0:

[ [ 'ANSWERED', '477', 728 ],
  [ 'BUSY', '477', 48 ],
  [ 'FAILED', '477', 0 ],
  [ 'NO ANSWER', '477', 277 ],
  [ 'ANSWERED', '488', 953 ],
  [ 'BUSY', '488', 9 ],
  [ 'FAILED', '488', 1 ],
  [ 'NO ANSWER', '488', 126 ] ]

我现在的代码如下:

    function SQLReportAgentCalls() {
    valueArr = [];
    connection2.query({
      sql : 'select disposition, src, COUNT(*) from cdr WHERE src="477" or src="488" GROUP BY src, disposition',
      timeout : 40000
    }, function (error, results, rows, fields) {
        console.log("-----------REPORTS------------------------");
        for(i in results){
                var dipo = results[i]['disposition'];
                var src = results[i]['src'];
                var count = results[i]['COUNT(*)'];

                if(dipo === "FAILED" && count){
                var chanar = new Array(dipo,src,count);
                valueArr.push(chanar);
                }

                else if(src && count === null && dipo !== "FAILED" ){
                var chanar = new Array("FAILED",src,0);
                valueArr.push(chanar);
                }

                else{
                var chanar = new Array(dipo,src,count);
                valueArr.push(chanar);
                }
        }
        console.log(valueArr);
        console.log("---------------------------------------------------");
        SQLReport();
    });
  }

生成报告的客户端代码:

//This generates the report(Google Chart) based on the information that has been pulled from the CDR table server side.
    socket.on("SQL", function (valueArr) {
        google.charts.load('current', {
            packages : ['corechart']
        });
        google.charts.setOnLoadCallback(drawMaterial);
        var data = valueArr;
        function x(data) {
            var r = [],
            temp;
            valueArr.forEach(function (a, i) {
                if (!(i % 3)) {
                    temp = [a[1]];
                    r.push(temp);
                }
                temp.push(a[2]);
            });
            return r;
        }
        function drawMaterial() {

            result = [['Call Disposition', 'Answered', 'Busy', 'No Answer', 'Failed']].concat(x(valueArr));
            console.log(result);
            var options = {
                title : 'Call Disposition',
                hAxis : {
                    title : 'Agents',
                    minValue : 0,
                },
                vAxis : {
                    title : 'Disposition Number'
                },
                isStacked : true
            };
            var chartdata = new google.visualization.arrayToDataTable(result);
            var material = new google.visualization.ColumnChart(document.getElementById('chart'));
            material.draw(chartdata, options);
        }
    });

这可能吗?我不确定Google Charts是否提供了自动添加值的选项(如果它们不存在),这似乎是我能想到解决问题的唯一方法。

1 个答案:

答案 0 :(得分:0)

如果数组需要类似于此......

['Call Disposition', 'Answered', 'Busy', 'No Answer','Failed']

将SQL更改为类似的东西可能会更容易......

select
  a.src,
  (select count(*) from cdr where src = a.src and disposition = 'ANSWERED'),
  (select count(*) from cdr where src = a.src and disposition = 'BUSY'),
  (select count(*) from cdr where src = a.src and disposition = 'NO ANSWER'),
  (select count(*) from cdr where src = a.src and disposition = 'FAILED')
from
  cdr a
where
  a.src in ("477", "488")
group by
  a.src