var hours = 24;
var granularity = '2m';
var end = 0;
var solcapdata = "https://bastart.spoton.cz/data/solcap_monitor?range=24h&granularity=2m&end=0h";
var cpumemdata = "https://bastart.spoton.cz/data/cpumem_monitor?range=24h&granularity=2m&end=0h";

solcap = new Dygraph(
// containing div
document.getElementById("solcap"),
// CSV or path to a CSV file.
solcapdata
,{
    //labels: ['time','Solar','Capacitor'],
    axes : {
            x : {
                drawGrid: true,
                drawAxis : true
            },
            y : {
                drawGrid: false,
                drawAxis : true,
                valueRange: [0,4.6]
            },
            y2 : {
                drawGrid: false,
                drawAxis: true,
                valueRange: [0,8.6],
                independentTicks: true
            }
    },
    rollPeriod: 5,
    interactionModel: {},
    connectSeparatedPoints: true,
    series:{
        'Solar': {
            axis: 'y',
            color: '#888844',
            fillGraph: true,
            fillAlpha: 0.4
        },
        'Capacitor': {
            axis: 'y2',
            color: '#884444'
        }

    },
        ylabel: '<span style="color:#888844;">Solar Irradiance</span>',
        y2label: '<span style="color:#884444;">Capacitor [V]</span>',
        labelsDiv: 'solcap_labels',
        legend: 'always'
    }
);


cpumem = new Dygraph(
// containing div
document.getElementById("cpumem"),
// CSV or path to a CSV file.
cpumemdata
,{
    //labels: ['time','cpu','mem','disk'],
    axes : {
            x : {
                drawGrid: true,
                drawAxis : true
            },
            y : {
                drawGrid: false,
                drawAxis : true,
                valueRange: [0,100]
            },
            y2 : {
                drawGrid: false,
                drawAxis: true,
                valueRange: [0,100],
                independentTicks: true
            }
    },
    rollPeriod: 5,
    interactionModel: {},
    connectSeparatedPoints: true,
    series:{
        'Cpu': {
            axis: 'y',
            color: '#888844',
            fillGraph: true,
            fillAlpha: 0.4
        },
        'Mem': {
            axis: 'y2',
            color: '#884444'
        },
        'Disk': {
            axis: 'y2',
            color: '#448844'
        }

    },
        ylabel: '<span style="color:#888844;">CPU [%]</span>',
        y2label: '<span style="color:#884444;">MEM / DISK [%]</span>',
        labelsDiv: 'cpumem_labels',
        legend: 'always'
    }
);

function refreshGraph(source){
  graphdata = "https://bastart.spoton.cz/data/" + source + "?range=" + hours + "h&granularity=" + granularity + "&end=" + end + "h";
  if(source == 'solcap_monitor'){
      solcap.updateOptions({'file': graphdata});
  }
  if(source == 'cpumem_monitor'){
      cpumem.updateOptions({'file': graphdata});
  }
  //document.getElementById('xxx').innerHTML = source;
  //power.updateOptions({'file': graphdata});
}

function setHours(hours_to_set, target){
  hours = hours_to_set;
  switch(hours){
      case '1':
        granularity = '10s';
        break;
      case '6':
        granularity = '2m';
        break;
      case '12':
        granularity = '2m';
        break;
      case '24':
        granularity = '5m';
        break;
      case '168':
        granularity = '10m';
        break;
      case '720':
        granularity = '1h';
        break;
      case '4320':
        granularity = '6h';
        break;
      case '8640':
        granularity = '1d';
        break;
      default:
        granularity = '10m';
  }
  end = 0;
  //document.getElementById('xxx').innerHTML = target;
  refreshGraph(target);
}

function setBack(target){
    // range=1h -> range=2h&end=1h
    disp_range = hours*1 - end*1;
    hours = hours*1 + disp_range;
    end = end*1 + disp_range;
    //document.getElementById('xxx').innerHTML = graphdata;
    refreshGraph(target);
}

function setForth(target){
    disp_range = hours*1 - end*1;
    hours = hours*1 - disp_range;
    if(hours < disp_range){
        hours = disp_range;
    }
    end = end*1 - disp_range;
    if(end < 0){
        end = 0;
    }
    //document.getElementById('xxx').innerHTML = graphdata;
    refreshGraph(target);
}
