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.

lua api
#1
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.
Code:
1234567891011121314151617181920212223242526272829303132333435363738394041
<? 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 ?>
Reply
#2
You can create a scheduled script to write data to CSV and use storage lists for storing temporary entries. This will solve the issue with writing in parallel.

This code adds an entry to the end of the list named mylist. Note that the value can only be a string, but not a Lua table.
Code:
123
key = 'mylist' value = 'some string value' storage.exec('rpush', key, value)

This code reads all entries from the list named mylist. All read entries are removed. New entries entries that can be added when the script is already running are not affected.
Code:
1234567891011
key = 'mylist' values = storage.exec('lrange', key, 0, -1) if type(values) == 'table' then   for _, value in ipairs(values) do     -- do something with value   end   -- remove read entries from the list   storage.exec('ltrim', key, #values, -1) end
Reply
#3
(07.08.2023, 08:22)admin Wrote: You can create a scheduled script to write data to CSV and use storage lists for storing temporary entries. This will solve the issue with writing in parallel.

This code adds an entry to the end of the list named mylist. Note that the value can only be a string, but not a Lua table.
Code:
123
key = 'mylist' value = 'some string value' storage.exec('rpush', key, value)

This code reads all entries from the list named mylist. All read entries are removed. New entries entries that can be added when the script is already running are not affected.
Code:
1234567891011
key = 'mylist' values = storage.exec('lrange', key, 0, -1) if type(values) == 'table' then   for _, value in ipairs(values) do     -- do something with value   end   -- remove read entries from the list   storage.exec('ltrim', key, #values, -1) end

Thanks
Reply
#4
(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.
Code:
1234567891011121314151617181920212223242526272829303132333435363738394041
<? 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 ?>
hi is it possible to use more than one storage lis on wiser??
Reply
#5
Use a different storage key name.
Reply
#6
(24.08.2023, 08:13)admin Wrote: Use a different storage key name.

ok thanks
Reply


Forum Jump: