Get and write total register attempts by date

This commit is contained in:
spla 2022-01-18 18:50:12 +01:00
pare c614ff3eb1
commit e929ea2526
S'han modificat 2 arxius amb 83 adicions i 0 eliminacions

Veure arxiu

@ -157,6 +157,11 @@ if __name__ == '__main__':
sql = "create table "+table+" (created_at timestamptz, ip inet PRIMARY KEY)" sql = "create table "+table+" (created_at timestamptz, ip inet PRIMARY KEY)"
create_table(db, db_user, table, sql) create_table(db, db_user, table, sql)
table = "totals"
sql = "create table "+table+" (datetime timestamptz PRIMARY KEY, registers int)"
create_table(db, db_user, table, sql)
############################################################ ############################################################

Veure arxiu

@ -11,6 +11,80 @@ from langdetect import detect
import requests import requests
import pdb import pdb
def write_totals(spamcheck_datetime_lst, spamcheck_registers_lst):
insert_sql = 'INSERT INTO totals(datetime, registers) VALUES(%s,%s) ON CONFLICT (datetime) DO UPDATE SET (datetime, registers) = (EXCLUDED.datetime, EXCLUDED.registers)'
i = 0
while i < len(spamcheck_datetime_lst):
conn = None
try:
conn = psycopg2.connect(database = spamcheck_db, user = spamcheck_db_user, password = "", host = "/var/run/postgresql", port = "5432")
cur = conn.cursor()
cur.execute(insert_sql, (spamcheck_datetime_lst[i], spamcheck_registers_lst[i]))
conn.commit()
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
i += 1
def get_totals():
spamcheck_datetime_lst = []
spamcheck_registers_lst = []
select_sql = 'select date(created_at), count(ip) as registers from spamcheck group by date(created_at) order by date(created_at)'
conn = None
try:
conn = psycopg2.connect(database = spamcheck_db, user = spamcheck_db_user, password = "", host = "/var/run/postgresql", port = "5432")
cur = conn.cursor()
cur.execute(select_sql)
rows = cur.fetchall()
for row in rows:
spamcheck_datetime_lst.append(row[0])
spamcheck_registers_lst.append(row[1])
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
return (spamcheck_datetime_lst, spamcheck_registers_lst)
def check_ip(ip): def check_ip(ip):
is_tor_exit_node = 'f' is_tor_exit_node = 'f'
@ -296,3 +370,7 @@ if __name__ == '__main__':
if conn is not None: if conn is not None:
conn.close() conn.close()
spamcheck_datetime_lst, spamcheck_registers_lst = get_totals()
write_totals(spamcheck_datetime_lst, spamcheck_registers_lst)