This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

chartist to display nice pie...
#26
Custom JS to fetch trend data from trends.lp:
Code:
function getdate(offset) {
  var date = new Date();
 
  date.setDate(date.getDate() + offset);
 
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();
 
  return { year, month, day };
}

function getdata() {
  var trends = [
    1,
    'Temperature',
    'Humidity',
   ];

  var data = JSON.stringify({
    trends: trends,
    dates: {
      start: getdate(0),
      end: getdate(1),
    },
    // resolution: 86400,
    // timestamps: true,
  });
 
  $.post('/user/trends.lp', data, function(resp) {
    console.log(resp);
  });
}

Both trend ID and name can be specified. Set resolution to 86400 to fetch daily data. Set timestamps to true to fetch both data and timestamps.


trends.lp
Code:
<?

require('json')
require('trends')

trends.NaN = json.null

ngx.header.content_type = 'application/json'

local req = ngx.req.get_body_data()
req = json.pdecode(req)

local res = {}
local dates = req.dates
local resolution = tonumber(req.resolution)
local timestamps = toboolean(req.timestamps)

for i, idname in ipairs(req.trends) do
  local id = tonumber(idname)

  if id then
    res[ i ] = trends.fetchrangebyid(id, dates, resolution, timestamps)
  else
    res[ i ] = trends.fetch(idname, dates, resolution, timestamps)
  end
end

json.write(res)

Note that there is no input data validation so the script can end with 500 Internal Server Error if the input data is incorrect.
Reply


Messages In This Thread
chartist to display nice pie... - by domotiqa - 05.01.2022, 08:56
RE: chartist to display nice pie... - by jmir - 14.02.2022, 11:32
RE: chartist to display nice pie... - by jmir - 14.02.2022, 13:34
RE: chartist to display nice pie... - by jmir - 15.02.2022, 11:15
RE: chartist to display nice pie... - by Acla - 12.06.2023, 05:05
RE: chartist to display nice pie... - by JRP - 08.09.2022, 16:18
RE: chartist to display nice pie... - by Acla - 12.06.2023, 15:44
RE: chartist to display nice pie... - by jmir - 25.03.2024, 14:49
RE: chartist to display nice pie... - by admin - 27.03.2024, 08:22

Forum Jump: