Commit 6de75526 authored by sebastien letort's avatar sebastien letort

Again, little code simplification, PerStatePlot more similar to PerUserPlot.

parent 03f83da6
......@@ -39,6 +39,15 @@ function build_list( l_data, key, fn_map )
.map( x => fn_map(x) );
}
function build_default_dict( l_keys, default_value=0 )
{
let d_defaults = {};
for( let i=0; i<l_keys.length; ++i )
{ let key = l_keys[i]; d_defaults[key] = default_value; }
return d_defaults;
}
// ===========================================
class Metrics
{
......@@ -145,9 +154,7 @@ class PerUserPlot extends Metrics
// turn [ {'time_period':x, key:y, 'n':z }, ...]
// to { y: [{ x: z },{ x: z },...], ... }
// d_default is the default dictionnary {any_key: default_val}
let d_defaults = {};
for( let i=0; i<l_periods.length; ++i )
{ let tp = l_periods[i]; d_defaults[tp] = 0; }
let d_defaults = build_default_dict( l_periods, 0 );
let d_data = {};
for( let i=0; i<l_data.length; ++i )
......@@ -209,29 +216,27 @@ class PerStatePlot extends Metrics
_setChartjsData( l_data )
{
function __dictionnarize( l_data, key, d_defaults )
function __dictionnarize( l_data, key, l_periods, l_states )
{
// turn [ {'time_period':x, key:y, 'n':z }, ...]
// to { x: { y: z }, ... }
// to { y: { x: z }, ... }
// d_defaults is the default dictionnary {any_key: default_val}
let d_defaults = build_default_dict( l_periods, 0 );
//~ let ld_new = []
let d_keys = {};
let d_data = {};
for( let i=0; i<l_states.length; ++i )
{
d_data[l_states[i]] = JSON.parse(JSON.stringify(d_defaults)); // new object.
}
for( let i=0; i<l_data.length; ++i )
{
let tp = fmt_date( l_data[i]["time_period"] );
let val = l_data[i][key];
let n = l_data[i]["n"];
if( !d_data[tp] )
if( d_defaults )
d_data[tp] = JSON.parse(JSON.stringify(d_defaults)); // new object.
else
// the [val] notation as key means 'val' will be evaluated.
d_data[tp] = { [val]: null };
d_data[tp][val] = n;
d_keys[val] = 1;
d_data[val][tp] = n;
}
return d_data;
......@@ -245,35 +250,28 @@ class PerStatePlot extends Metrics
"ABORTED": '#FF8000',
"TIMEOUT": '#DDAA22'
};
let d_defaults = {
// I did not succeed in building it looping on d_labels !
"NONE" : 0,
"SUCCESS": 0,
"ERROR" : 0,
"ABORTED": 0,
"TIMEOUT": 0
};
let l_periods = build_list( l_data, 'time_period', fmt_date );
let l_states = [ "NONE", "SUCCESS", "ERROR", "ABORTED", "TIMEOUT" ]; // to ensure the order.
let d_data = __dictionnarize( l_data, 'result', d_defaults );
let d_data = __dictionnarize( l_data, 'result', l_periods, l_states );
// now let's turn this dict to chartjs_data
let chartjs_data = { labels: l_periods, datasets: [] };
let l_labels = [ "NONE", "SUCCESS", "ERROR", "ABORTED", "TIMEOUT" ]; // to ensure the order.
for( let i=0; i<l_labels.length; ++i )
for( let i=0; i<l_states.length; ++i )
{
let status = l_labels[i];
let status = l_states[i];
chartjs_data.datasets[i] =
{
data : l_periods.map( x => d_data[x][status] ),
data : l_periods.map( x => d_data[status][x] ),
label: status.toLowerCase(),
fill : false,
backgroundColor: d_colors[status],
borderColor: d_colors[status],
borderWidth: 1,
borderColor : d_colors[status],
borderWidth : 1,
fill : false,
};
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment