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.

Export CSV
#6
I made some tests, and I notice it works fine when export_all = true, when I put it false then happens what I wrote upside.
So I think is just the filtering function not properly working.

here is the script:


Code:
--**************************************************************************--
--** Email trendlog data as CSV attachment created by Erwin van der Zwart **--
--************ For HL from FW 1.5 and SL from FW 1.2 with NGINX ************--
--**************************************************************************--
--*************************** Start of parameters **************************--
-- Version 1.1 *************************************************************--



--Set export mode (selected trend(s) or all trends)
export_all = false

--Set trend names if not all trends need to be exported (only used when export_all = false)
trendnames = {
    "Potenza Attiva PT",
      "Forno",
      "Microonde",
      "Piano Lavoro",
    "Prese Stireria",
    "Lavastoviglie",
    "Lavatrice",
    "Asciugatrice",
    "Lavastoviglie 2",
      "Carico 9",

}

--**************************************************************************--
--**************************** End of parameters ***************************--
--**************************************************************************--
--****************** DON'T CHANGE ANYTHING UNDER THIS LINE *****************--
--**************************************************************************--

require('trends')

-- Get all trend names from DB
trends_table = db:getall('SELECT name FROM trends ORDER BY name DESC')

-- Check if all trends or selection neeed to be exported
if export_all == false then
  tmp = {}
  for _, t in ipairs(trends_table) do
    for _, name in ipairs(trendnames) do
      if name == t.name then
        tmp[ #tmp + 1 ] = t
      end
    end
  end
  trends_table = tmp
end

-- Check if the is at least 1 trend to be exported
if #trends_table < 1 then
  log("Nessun Trend disponibile, impossibile esportare")
  return
end

-- csv buffer
buffer = {}

-- Add to buffer
table.insert(buffer, '"Questo file contiene i dati in formato CSV di ' .. #trends_table .. ' trend(s), creato automaticamente in data: ' .. os.date("%d-%m-%y alle %H:%M") .. '"')
-- Add empty line
table.insert(buffer, '""')

-- Loop through trends_table
for _, trend_names in ipairs(trends_table) do
  -- Grab trend name from DB table
  trend_name = trend_names.name
 
if string.sub(trend_name, -1) ~= '_' then
   
  -- Add to buffer
  table.insert(buffer, '"START TREND N.' .. _ .. '     <<< ' .. trend_name .. ' >>>"')
  -- Add empty line
  table.insert(buffer, '""')
 
  -- Get current timestamp
timestamp = os.time()
enddate = os.date('*t', timestamp)
  startdate =  os.date('*t', (timestamp - 60*60*24)) -- 1 day

dates = {}
dates['start'] = startdate
dates['end'] = enddate
 
 
  -- Set resolution to dayly data
  resolution = 60*15 -- 15 min
 
  -- Get last month for data by each days in this month
  trenddatamonth = trends.fetch(trend_name, dates, resolution)
 
  -- Get last month total avarage data
  trenddatamonthavg = trends.fetchone(trend_name, dates, resolution)

  -- Add header
  table.insert(buffer, '"Data","Ora","Valore Medio Giornaliero"')
  for _, row in ipairs(trenddatamonth) do
    stamp = dates['start'].year .. '-' .. dates['start'].month .. '-' .. string.format("%02d", _)
        local y, m, d = stamp:match("(%d+)%-(%d+)%-(%d+)")
        local t = { year = y, month = m, day = d}
   
    n15 = tonumber (string.format("%02d", _))*60*15
   
    modulo = n15*15

   
    hours1 = string.format("%02.f", math.floor(n15/3600));
    mins1 = string.format("%02.f", math.floor(n15/60 - (hours1*60)));

    Ora = hours1..":"..mins1
   
   
   
    -- format csv row
   csv = string.format('%q,%q,%q', string.format("%02d", dates['start'].day) .. "-" .. string.format("%02d", dates['start'].month) .. "-" .. dates['start'].year, Ora, row)
    -- add to buffer
    table.insert(buffer, csv)
  end
  -- Add empty linestring.format("%02d", _)
  table.insert(buffer, '""')
  -- Add header
  table.insert(buffer, '"END TREND ' .. _ .. '     <<< ' .. trend_name .. ' >>>"')
  -- Add empty line
  table.insert(buffer, '""')
  end
end

--Create table to include mail settings
local settings = {
    from = from,
    rcpt = to,
    user = user,
    password = password,
    server = 'smtp.gmail.com',
    port = 465,
    secure = 'sslv23',
}

--Create attachment inside FTP server
src = 'Trend Export '.. os.date('%Y-%m-%d') .. '.csv'
dst = '/home/ftp/' .. src
io.writefile(dst, buffer)


-- read csv report file

data1 = io.readfile(dst)

-- send file as report.csv with text/csv mime type

soggetto = 'XXXXXX: invio TRENDS giornalieri'
testo = 'Si allega file CSV.'


res, err = mailattach('giuseppe.dimaria@komponext.it', soggetto, testo, src, data1, 'text/csv')
log(res, err)
--Delete created backup file from ftp folder inside HL
os.remove(dst)
Reply


Messages In This Thread
Export CSV - by gdimaria - 25.11.2020, 12:06
RE: Export CSV - by admin - 25.11.2020, 13:15
RE: Export CSV - by gdimaria - 26.11.2020, 17:27
RE: Export CSV - by gdimaria - 26.11.2020, 14:31
RE: Export CSV - by admin - 27.11.2020, 08:20
RE: Export CSV - by gdimaria - 27.11.2020, 08:55
RE: Export CSV - by Domoticatorino - 11.12.2020, 15:13

Forum Jump: