24.08.2023, 07:18
(05.08.2023, 08:59)mellotron Wrote: hi please i need your help, i use this lua api code to get json data and save it on csv file but when many user send request i have the lines that get mixed up. please how to fixe it.hi is it possible to use more than one storage lis on wiser??
Code:<?
require('apps')
require('socket.url')
action = socket.url.unescape( getvar('action') or 'none' )
storagename = socket.url.unescape( getvar('storagename') or 'none')
format = socket.url.unescape( getvar('format') or 'string' )
storagevalue = socket.url.unescape( getvar('value') or '{}')
-- Modification pour gérer le nom de fichier et le répertoire
filename = socket.url.unescape( getvar('storagename') or 'none')
folder = '/home/ftp/Rapport_SatCl/Jour/'
if action == 'storageset' then
if storagename ~= 'none' then
if format == 'json' then
storagevalue = json.decode(storagevalue)
end
-- Création et enregistrement du fichier CSV
if filename ~= 'none' then
local ip = ngx.var.remote_addr
local filepath = folder..storagename..".csv"
local file = io.open(filepath, "a")
-- Ajout des entêtes de colonnes
if file:seek('end') == 0 then
file:write("datetime;agenceName;name;caisse;cheque;gestion_de_compte;autre_service;tres_satisfait;satisfait;un_peu_satifait;pas_satisfait;pas_du_tout_satisfait;pas_du_tout;pas_vraiment;plutot_bien;bien;tres_bien;oui;non;commentaire;telephone\n")
end
for i, row in ipairs(storagevalue) do
file:write( row['datetime'] ..";"..row['agenceName']..";"..row['name']..";" .. row['caisse'] .. ";" .. row['cheque'] .. ";" .. row['gestion_de_compte'] .. ";" ..row['autres_services'] .. ";" .. row['tres_satisfait'] .. ";" .. row['satisfait'] .. ";" .. row['un_peu_satifait'] .. ";" .. row['pas_satisfait'] .. ";" .. row['pas_du_tout_satisfait'] .. ";" ..row['pas_du_tout']..";" ..row['pas_vraiment'] .. ";" .. row['plutot_bien'] .. ";" .. row['bien'] .. ";" .. row['tres_bien'] ..";" .. row['oui'] .. ";" .. row['non'] .. ";" .. row['commentaire'] .. ";" .. row['telephone'] .. "\n")
end
file:close()
print('file saved as: ' .. filepath)
else
print('no filename specified')
end
end
?>