Skip to content
Snippets Groups Projects

Clean up order forms and fix compatibility with modern plotly

Merged David Hoese requested to merge feature-cleanup-order-forms into develop
43 files
+ 3603
2022
Compare changes
  • Side-by-side
  • Inline
Files
43
"use strict";
var updatePlot = require('./update.js');
var loading = require('../ui/loading.js');
var utils = require('./utils.js');
var _ = require('underscore');
@@ -16,17 +16,29 @@ function processData(dataObj) {
}
function plotting(url, var_names, plotsInfo, div, update) {
function plotting(dataCache, url, var_names, plotsInfo, div, update, maxPoints, err_callback) {
//gets information and updates component
$.ajax({
type: 'GET',
url: url,
jsonp: "callback",
dataType: "jsonp",
success: function(dataObj) {
if (dataObj.code != 200) {
console.error("Invalid data API query. Code: {0} | Message: {1}".format(
dataObj.code, dataObj.message));
if (err_callback !== undefined) {
// stop the update timer if specified
err_callback(plotsInfo);
}
return;
}
var graphDiv = document.getElementById(div);
var allData = processData(dataObj['results']);
var existingDates = graphDiv.data[0]['x'];
// var existingDates = graphDiv.data[0]['x'];
var existingDates = dataCache[0]['x'] || [];
var dates = allData['dates'];
dataObj = {
dates: dates
@@ -51,10 +63,11 @@ function plotting(url, var_names, plotsInfo, div, update) {
console.info("No new data downloaded");
return;
} else {
dates = dates.slice(startIndex);
for (var i = 0; i < var_names.length; i++) {
dataObj[var_names[i]] = allData[var_names[i]].slice(startIndex);
}
dates = dates.slice(startIndex);
dataObj.dates = dates;
}
} else {
// replace the entire data array
@@ -63,61 +76,61 @@ function plotting(url, var_names, plotsInfo, div, update) {
}
}
updatePlot.updatePlot(graphDiv, plotsInfo, dataObj, update);
// Update data with
updatePlot.updatePlot(dataCache, graphDiv, plotsInfo, dataObj, update, maxPoints);
}
});
}
function buildRequestUrl(format, interval, epoch, sep, order, time, streams) {
var request_url = METOBS_API_URL+'/api/data.'+format+'?';
request_url += 'interval='+interval;
request_url += '&epoch='+epoch;
request_url +='&sep='+sep || ',';
request_url += '&order='+order;
request_url+=time;
request_url+='&symbols='+streams;
return request_url;
}
//the minimum time period (in milliseconds) that a request must cover in order to use
var intervalLookUp = [
//1 day for 5 minutes
// 1. the minimum time period (in milliseconds) that a request must cover in order to use
// 2. MetObs Data API interval specifier
// 3. reasonable update interval for API given this data interval
const intervalLookUp = [
// 1 day for 5 minutes
[1000*60*60*24, '5m', '-00:10:00'],
//1 month for 1 hour
// 1 month for 1 hour
[1000*60*60*24*30*1, '1h', '-02:00:00'],
//anything else is 1 minute
// anything else is 1 minute
];
const intervalToSeconds = {
'1m': 60,
'5m': 60 * 5,
'1h': 60 * 60,
};
function varNamesForDiv(div) {
function varNamesForDiv(dataCache) {
var varNames = [];
var graphDiv = document.getElementById(div);
_.each(graphDiv.data, function(trace_data) {
_.each(dataCache, function(trace_data) {
Array.prototype.push.apply(varNames, trace_data.var_names);
if ('anc_variables' in trace_data) {
Array.prototype.push.apply(varNames, trace_data.anc_variables);
}
});
return varNames;
return _.uniq(varNames);
}
//poorly ordered function parameters
function requestPlotUpdate(div, plotsInfo, startTime, endTime, interval, update) {
var varNames = varNamesForDiv(div);
function requestPlotUpdate(dataCache, div, plotsInfo, startTime, endTime, interval, update, err_callback) {
var varNames = varNamesForDiv(dataCache);
var stream_string = '';
var dt_in_ms;
var time_parts;
var maxPoints;
var time;
for (var i in varNames) {
stream_string += varNames[i];
if (i < varNames.length - 1) stream_string += ':';
}
var time;
if (!endTime) {
interval = '1m';
// hours, minutes, seconds
var time_parts = _.map(startTime.split(':'), Number);
var dt_in_ms = -1000 * 60 * 60 * time_parts[0] + 1000 * 60 * time_parts[1] + 1000 * time_parts[2];
time_parts = _.map(startTime.split(':'), Number);
dt_in_ms = -1000 * 60 * 60 * time_parts[0] + 1000 * 60 * time_parts[1] + 1000 * time_parts[2];
for (var i in intervalLookUp) {
if (dt_in_ms >= intervalLookUp[i][0]) {
interval = intervalLookUp[i][1];
@@ -129,63 +142,31 @@ function requestPlotUpdate(div, plotsInfo, startTime, endTime, interval, update)
}
}
}
time = '&begin='+startTime;
time = '&begin=' + startTime;
} else {
time='&begin='+startTime+'&end='+endTime;
if(interval == 'auto'){
time = '&begin=' + startTime + '&end=' + endTime;
if (interval == 'auto') {
interval = '1m';
var dt_in_ms = (new Date(endTime)) - (new Date(startTime));
for(var i in intervalLookUp){
dt_in_ms = (new Date(endTime)) - (new Date(startTime));
for (var i in intervalLookUp) {
if (dt_in_ms >= intervalLookUp[i][0])
interval = intervalLookUp[i][1];
}
}
}
var url = buildRequestUrl('json', interval, '', '', 'column', time, stream_string);
plotting(url, varNames, plotsInfo, div, update);
// Maximum number of points (fallback to arbitrary 250)
if (update) {
maxPoints = intervalToSeconds.hasOwnProperty(interval) ? dt_in_ms / 1000 / intervalToSeconds[interval] : 250;
} else {
maxPoints = 0; // delete all previous data
}
var url = utils.buildRequestUrl('json', interval, '', '', 'column', time, stream_string);
plotting(dataCache, url, varNames, plotsInfo, div, update, maxPoints, err_callback);
return interval;
}
/**
* The purpose of this method is to create a url based on user info
*
* @param userInfo - jsn with startdate, start time, end date, end time, and interval
* no returns
*/
function customPlot(userInfo,streams)
{
//get dates
var startDate = userInfo.startDate;
var startParts = startDate.split('/');
startDate = startParts[2] + '-' + startParts[0] + '-' + startParts[1];
var endDate = userInfo.endDate;
var endParts = endDate.split('/');
endDate = endParts[2] + '-' + endParts[0] + '-' + endParts[1];
//get times
var startTime = userInfo.startTime;
var endTime = userInfo.endTime;
//get interval
var interval = userInfo.interval;
//create url
var url = 'http://metobs.ssec.wisc.edu/api/data.json?site=aoss&inst=tower&symbols=air_temp:dewpoint:rel_hum:pressure:wind_speed:wind_direction:accum_precip:solar_flux&begin=';
url += (startDate + 'T' + startTime + '&end=' + endDate + 'T' + endTime + '&interval=');
url += (interval);
//plot data
plotting(url);
//hide gif
setTimeout(loading.loading, 2000);
}
//export functions
module.exports.requestPlotUpdate = requestPlotUpdate;
module.exports.buildRequestUrl = buildRequestUrl;
module.exports.customPlot = customPlot;
module.exports.plotting = plotting;
Loading