Posts: 292
Threads: 79
Joined: May 2017
Reputation:
0
Hi,
I am using your script to export csv and send trends via mail.
If I set export_all = true it sends all trends, of course... but if I set export_all = false it ignore my namescripts list and export n.25 trends instead of the 9 I selected.
What is wrong? Thanks
Peppe
Attached Files
Posts: 8097
Threads: 43
Joined: Jun 2015
Reputation:
471
I'm not sure that filtering function is correct, try replacing it with this:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
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
Posts: 292
Threads: 79
Joined: May 2017
Reputation:
0
uhmm..... now it seems ok, but I give a look to hh:mm in the report, I see they become over 24 hours and the day remain the same.
What I really need it to send a monthly mail to the customer with hir trendslog attached.
is it possible to select a monthly report like that by script?
Posts: 292
Threads: 79
Joined: May 2017
Reputation:
0
(25.11.2020, 13:15) admin Wrote: I'm not sure that filtering function is correct, try replacing it with this:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
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
there is something wrong here:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
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
csv =
string.format (
'%q,%q,%q' ,
string.format (
"%02d" ,
dates [
'start' ].
day ) ..
"-" ..
string.format (
"%02d" ,
dates [
'start' ].
month ) ..
"-" ..
dates [
'start' ].
year ,
Ora ,
row )
table.insert (
buffer ,
csv )
end
Because time are over the 24 hours:
Posts: 8097
Threads: 43
Joined: Jun 2015
Reputation:
471
The script assumes that trend resolution is fixed to 15 minutes but it can be different. This is fixable but the whole script has to fully rewritten.
Posts: 292
Threads: 79
Joined: May 2017
Reputation:
0
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
export_all =
false
trendnames = {
"Potenza Attiva PT" ,
"Forno" ,
"Microonde" ,
"Piano Lavoro" ,
"Prese Stireria" ,
"Lavastoviglie" ,
"Lavatrice" ,
"Asciugatrice" ,
"Lavastoviglie 2" ,
"Carico 9" ,
}
require (
'trends' )
trends_table =
db :
getall (
'SELECT name FROM trends ORDER BY name DESC' )
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
if #
trends_table <
1 then
log (
"Nessun Trend disponibile, impossibile esportare" )
return
end
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" ) ..
'"' )
table.insert (
buffer ,
'""' )
for _ ,
trend_names in ipairs (
trends_table )
do
trend_name =
trend_names.name
if string.sub (
trend_name , -
1 ) ~=
'_' then
table.insert (
buffer ,
'"START TREND N.' ..
_ ..
' <<< ' ..
trend_name ..
' >>>"' )
table.insert (
buffer ,
'""' )
timestamp =
os.time ()
enddate =
os.date (
'*t' ,
timestamp )
startdate =
os.date (
'*t' , (
timestamp -
60 *
60 *
24 ))
dates = {}
dates [
'start' ] =
startdate
dates [
'end' ] =
enddate
resolution =
60 *
15
trenddatamonth =
trends.fetch (
trend_name ,
dates ,
resolution )
trenddatamonthavg =
trends.fetchone (
trend_name ,
dates ,
resolution )
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
csv =
string.format (
'%q,%q,%q' ,
string.format (
"%02d" ,
dates [
'start' ].
day ) ..
"-" ..
string.format (
"%02d" ,
dates [
'start' ].
month ) ..
"-" ..
dates [
'start' ].
year ,
Ora ,
row )
table.insert (
buffer ,
csv )
end
table.insert (
buffer ,
'""' )
table.insert (
buffer ,
'"END TREND ' ..
_ ..
' <<< ' ..
trend_name ..
' >>>"' )
table.insert (
buffer ,
'""' )
end
end
local settings = {
from =
from ,
rcpt =
to ,
user =
user ,
password =
password ,
server =
'smtp.gmail.com' ,
port =
465 ,
secure =
'sslv23' ,
}
src =
'Trend Export ' ..
os.date (
'%Y-%m-%d' ) ..
'.csv'
dst =
'/home/ftp/' ..
src
io.writefile (
dst ,
buffer )
data1 =
io.readfile (
dst )
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 )
os.remove (
dst )
Posts: 410
Threads: 93
Joined: Jul 2016
Reputation:
3
Hi,
set a user library with this following code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
function trend_export (
name ,
dates ,
mode ,
timestampType )
require (
'trends' )
function detectTimeShift (
t0 ,
t1 )
t0_isdst ,
t1_isdst =
os.date (
'*t' ,
t0 ).
isdst ,
os.date (
'*t' ,
t1 ).
isdst
if (
t0_isdst ~=
t1_isdst )
then
if (
t1_isdst )
then
return 1
else
return 2
end
else
return false
end
end
if type (
dates [
'start' ]) ==
"string" then
if dates [
'start' ] ==
"now" then dates [
'start' ] =
os.date (
"*t" ,
os.time ())
elseif dates [
'start' ] ==
"yesterday" then dates [
'start' ] =
os.date (
"*t" ,
os.time ()-
24 *
3600 )
elseif dates [
'start' ] ==
"week_ago" then dates [
'start' ] =
os.date (
"*t" ,
os.time ()-
7 *
24 *
3600 )
elseif dates [
'start' ] ==
"month_ago" then dates [
'start' ] =
os.date (
"*t" ,
os.time ()-
30 *
24 *
3600 )
elseif dates [
'start' ] ==
"year_ago" then dates [
'start' ] =
os.date (
"*t" ,
os.time ()-
365 *
24 *
3600 )
end
end
if type (
dates [
'end' ]) ==
"string" then
if dates [
'end' ] ==
"now" then dates [
'end' ] =
os.date (
"*t" ,
os.time ())
elseif dates [
'end' ] ==
"yesterday" then dates [
'end' ] =
os.date (
"*t" ,
os.time ()-
24 *
3600 )
elseif dates [
'end' ] ==
"week_ago" then dates [
'end' ] =
os.date (
"*t" ,
os.time ()-
7 *
24 *
3600 )
elseif dates [
'end' ] ==
"month_ago" then dates [
'end' ] =
os.date (
"*t" ,
os.time ()-
30 *
24 *
3600 )
elseif dates [
'end' ] ==
"year_ago" then dates [
'end' ] =
os.date (
"*t" ,
os.time ()-
365 *
24 *
3600 )
end
end
starttime =
os.time (
dates [
'start' ])
endtime =
os.time (
dates [
'end' ])
if starttime >
endtime then
_ =
dates [
'start' ]
dates [
'start' ] =
dates [
'end' ]
dates [
'end' ] =
_
end
resolution =
db :
getlist (
"SELECT resolution*60 FROM trends WHERE name='" ..
name ..
"'" )[
1 ]
if resolution then
if (
os.date (
"%H" ,
endtime )*
3600 +
os.date (
"%M" ,
endtime )*
60 ) >=
resolution then
dates [
'end' ].
day =
dates [
'end' ].
day +
1
raw_data =
trends.fetch (
name ,
dates ,
resolution )
else
raw_data =
trends.fetch (
name ,
dates ,
resolution )
end
start =
1
data = {}
start_timestamp =
os.time (
dates [
'start' ]) +
resolution
offset =
0
skip =
0
k =
1
while k <= #
raw_data do
v =
raw_data [
k ]
if k ==
1 then last_timestamp =
start_timestamp else last_timestamp =
current_timestamp end
current_timestamp =
start_timestamp + (
k-1 ) *
resolution +
offset
timeShiftStatus =
detectTimeShift (
last_timestamp ,
current_timestamp )
if timeShiftStatus then
if timeShiftStatus ==
1 then
skip =
6
offset =
offset -
3600
elseif timeShiftStatus ==
2 then
offset =
offset +
3600
end
end
if current_timestamp >=
starttime and current_timestamp <=
endtime then
if timestampType ==
'unixepoch' then data [
start ] = {
current_timestamp ,
v }
else data [
start ] = {
os.date (
'%Y.%m.%d %H:%M:%S' ,
current_timestamp ),
v }
end
start =
start +
1
end
k =
k +
1 +
skip
skip =
0
end
if mode ==
'csv' then
for k ,
v in ipairs (
data )
do
data [
k ] =
table.concat (
data [
k ],
"," )
end
csv =
"timestamp, value\r\n" ..
table.concat (
data ,
"\r\n" )
return name ,
csv
elseif mode ==
'table' then
return name ,
data
end
else
alert (
'There is no trend with such a name' )
end
end
then in a event script or scheduled script this following:
Quote: require('user.trend_pro')
-- Export data
name = 'trend name' -- trend name to export
dates = {}
dates['start'] = "month_ago"
dates['end'] = "now"
name, csv = trend_export(name, dates, 'csv')
-- Send an email with data
attachement = { {filename=name .. '.csv', filedata=csv, mimetype='text/csv'} }
mailattach('mail_address@mail.com', 'Report from trend ' .. name, 'CSV file attached', attachement)