+: querys_sqlite flag -q [opcs.]

opcs: --top `n`
      -p <pais>
      --detalle-pais <pais>
      --pais-desde <unix_epoch-date>
This commit is contained in:
jp.av.dev 2022-09-19 04:07:28 -03:00
parent 154882ef0a
commit e7aef5a404
3 changed files with 201 additions and 0 deletions

147
consultas/querys_sqlite.py Executable file
View File

@ -0,0 +1,147 @@
import sqlite3
import os
from rich.console import Console
from rich.table import Table
from rich import box
selfpath = os.path.abspath(os.path.dirname(__file__))
console = Console()
conn = sqlite3.connect(f'{selfpath}/../ipinfo.db')
c = conn.cursor()
# Detalle Visitas por pais
def vistas_pais_detalle(pais):
pais = pais
consulta = f"""
SELECT DATE(fecha, 'unixepoch') AS fecha_local, ip, metodo, cod_html, consulta
FROM visita WHERE ip IN (SELECT `ip` FROM `registro` WHERE `pais` = '{pais}');
"""
c.execute(consulta)
resp = c.fetchall()
return resp
def pt_visita_pais_detalle(pais):
respuesta = vistas_pais_detalle(pais)
tbl_v = Table(
title=f"[bold][blue]Detalle visitas pais: [/blue][yellow]{pais}[/yellow][/bold]",
box = box.ROUNDED,
show_lines = False,
row_styles=["dim", ""],
border_style="dark_magenta")
tbl_v.add_column("Fecha", justify="center", style="bright_yellow")
tbl_v.add_column("IP", justify="left", style="bright_cyan")
tbl_v.add_column("Metodo", justify="left", style="bright_green")
tbl_v.add_column("Respuesta", justify="left", style="bright_blue")
tbl_v.add_column("Consulta", justify="left", style="yellow")
for item in respuesta:
tbl_v.add_row(str(item[0]), str(item[1]), str(item[2]), str(item[3]), str(item[4]))
console.print(tbl_v)
# Formato fecha -- Convertir fecha 'unixepoch' a 'localtime'
def unix_to_local_date():
consulta = """
SELECT DATE(fecha, 'unixepoch') AS fecha_local FROM visita;
"""
c.execute(consulta)
return c.fetchall()
# Select cod 200 -- SELECT all from registro where ip=(SELECT ip from visita where cod_html=200);
def select_cod(codigo):
consulta = f"""
SELECT geoloc FROM registro WHERE ip IN
(SELECT ip FROM visita WHERE cod_html = {codigo});
"""
c.execute(consulta)
resp = c.fetchall()
return resp
# Select fecha mayor que (ej. 1661226920)
def sel_pais_desde(pais, unix_e):
consulta = f"""
SELECT DATE(fecha, 'unixepoch') AS fecha_local, ip, metodo, cod_html, consulta
FROM visita WHERE ip IN (SELECT `ip` FROM `registro` WHERE `pais` = '{pais}') and fecha > {unix_e};
"""
c.execute(consulta)
resp = c.fetchall()
return resp
def pt_sel_pais_fecha(pais, unix_e):
respuesta = sel_pais_desde(pais, unix_e)
tbl_v = Table(
title=f"[bold][blue]Visitas {pais}, desde {unix_e}[/blue][/bold]",
box = box.ROUNDED,
show_lines = False,
row_styles=["dim", ""],
border_style="dark_magenta")
tbl_v.add_column("Fecha", justify="center", style="bright_yellow")
tbl_v.add_column("IP", justify="left", style="bright_cyan")
tbl_v.add_column("Metodo", justify="left", style="bright_green")
tbl_v.add_column("Respuesta", justify="left", style="bright_blue")
tbl_v.add_column("Consulta", justify="left", style="yellow")
for item in respuesta:
tbl_v.add_row(str(item[0]), str(item[1]), str(item[2]), str(item[3]), str(item[4]))
console.print(tbl_v)
# Top 50 paises
def top_paises(top):
consulta = f"""
SELECT pais,
count(pais) AS Ocurrencias
FROM registro
GROUP BY pais
ORDER BY count(*) DESC
LIMIT {top};
"""
c.execute(consulta)
resp = c.fetchall()
return resp
def pt_top_paises(top):
respuesta = top_paises(top)
tbl_v = Table(
title=f"[bold][blue]Vistas Top {top}[/blue][/bold]",
box = box.ROUNDED,
show_lines = False,
row_styles=["dim", ""],
border_style="dark_magenta")
tbl_v.add_column("País", justify="center", style="bright_yellow")
tbl_v.add_column("Visitas", justify="left", style="bright_cyan")
for item in respuesta:
tbl_v.add_row(str(item[0]), str(item[1]))
console.print(tbl_v)
# respuesta HTML para pais=?
def cod_html_pais(pais):
consulta = f"""
SELECT cod_html,
count(cod_html) AS Ocurrencias
FROM visita
WHERE ip IN (SELECT `ip` FROM `registro` WHERE `pais` = '{pais}')
GROUP BY cod_html
ORDER BY count(*) DESC;
"""
c.execute(consulta)
resp = c.fetchall()
return resp
def pt_html_cod_pais(pais):
respuesta = cod_html_pais(pais)
tbl_v = Table(
title=f"[bold][blue]Códigos html: [/blue][green]{pais}[/bold][/green]",
box = box.ROUNDED,
show_lines = False,
row_styles=["dim", ""],
border_style="dark_magenta")
tbl_v.add_column("Código", justify="center", style="bright_yellow")
tbl_v.add_column("Conteo", justify="left", style="bright_cyan")
for item in respuesta:
tbl_v.add_row(str(item[0]), str(item[1]))
console.print(tbl_v)

View File

@ -11,6 +11,7 @@ import sql_alch
from rich import box from rich import box
from rich.console import Console from rich.console import Console
from rich.table import Table from rich.table import Table
import consultas.querys_sqlite as querys
selfpath = os.path.abspath(os.path.dirname(__file__)) selfpath = os.path.abspath(os.path.dirname(__file__))
ownip = requests.get('https://ifconfig.me/').text ownip = requests.get('https://ifconfig.me/').text
@ -218,6 +219,23 @@ def main():
case '-M': case '-M':
console.print('[bold yellow]Generando mapa de visitas[/bold yellow]') console.print('[bold yellow]Generando mapa de visitas[/bold yellow]')
sql_alch.mapsgen() sql_alch.mapsgen()
case '-q':
match sys.argv[2]:
case '-p':
pais = sys.argv[3]
querys.pt_html_cod_pais(pais.upper())
case '--top':
top = sys.argv[3]
querys.pt_top_paises(top)
case '--pais-desde':
pais = sys.argv[3]
desde = sys.argv[4]
querys.pt_sel_pais_fecha(pais.upper(), desde)
case '--detalle-pais':
pais = sys.argv[3]
querys.pt_visita_pais_detalle(pais.upper())
case _:
console.print(f'[red] query desconocida [bold]{sys.argv[2]}[/bold][/red]')
case _: case _:
ip = sys.argv[1] ip = sys.argv[1]
print_ipinfo(ip, False) print_ipinfo(ip, False)

View File

@ -10863,6 +10863,42 @@
<g clip-path="url(#page)" transform="translate(1024, 0)"> <g clip-path="url(#page)" transform="translate(1024, 0)">
<path d="M 289.1259733333333 502.9950508822771 l -3.4641016151377544 -6.0 a 4 4 0 1 1 6.928203230275509 0 Z" fill="#b20101" opacity="1.0" stroke="#ffffff" stroke-width="1"/> <path d="M 289.1259733333333 502.9950508822771 l -3.4641016151377544 -6.0 a 4 4 0 1 1 6.928203230275509 0 Z" fill="#b20101" opacity="1.0" stroke="#ffffff" stroke-width="1"/>
</g> </g>
<g clip-path="url(#page)" transform="translate(-1024, 0)">
<path d="M 421.4705777777778 277.28170863802444 l -3.4641016151377544 -6.0 a 4 4 0 1 1 6.928203230275509 0 Z" fill="#b20101" opacity="1.0" stroke="#ffffff" stroke-width="1"/>
</g>
<g clip-path="url(#page)" transform="translate(0, 0)">
<path d="M 421.4705777777778 277.28170863802444 l -3.4641016151377544 -6.0 a 4 4 0 1 1 6.928203230275509 0 Z" fill="#b20101" opacity="1.0" stroke="#ffffff" stroke-width="1"/>
</g>
<g clip-path="url(#page)" transform="translate(1024, 0)">
<path d="M 421.4705777777778 277.28170863802444 l -3.4641016151377544 -6.0 a 4 4 0 1 1 6.928203230275509 0 Z" fill="#b20101" opacity="1.0" stroke="#ffffff" stroke-width="1"/>
</g>
<g clip-path="url(#page)" transform="translate(-1024, 0)">
<path d="M 111.85507555555557 291.29044977172686 l -3.4641016151377544 -6.0 a 4 4 0 1 1 6.928203230275509 0 Z" fill="#b20101" opacity="1.0" stroke="#ffffff" stroke-width="1"/>
</g>
<g clip-path="url(#page)" transform="translate(0, 0)">
<path d="M 111.85507555555557 291.29044977172686 l -3.4641016151377544 -6.0 a 4 4 0 1 1 6.928203230275509 0 Z" fill="#b20101" opacity="1.0" stroke="#ffffff" stroke-width="1"/>
</g>
<g clip-path="url(#page)" transform="translate(1024, 0)">
<path d="M 111.85507555555557 291.29044977172686 l -3.4641016151377544 -6.0 a 4 4 0 1 1 6.928203230275509 0 Z" fill="#b20101" opacity="1.0" stroke="#ffffff" stroke-width="1"/>
</g>
<g clip-path="url(#page)" transform="translate(-1024, 0)">
<path d="M 206.90816000000007 359.2368038984731 l -3.4641016151377544 -6.0 a 4 4 0 1 1 6.928203230275509 0 Z" fill="#b20101" opacity="1.0" stroke="#ffffff" stroke-width="1"/>
</g>
<g clip-path="url(#page)" transform="translate(0, 0)">
<path d="M 206.90816000000007 359.2368038984731 l -3.4641016151377544 -6.0 a 4 4 0 1 1 6.928203230275509 0 Z" fill="#b20101" opacity="1.0" stroke="#ffffff" stroke-width="1"/>
</g>
<g clip-path="url(#page)" transform="translate(1024, 0)">
<path d="M 206.90816000000007 359.2368038984731 l -3.4641016151377544 -6.0 a 4 4 0 1 1 6.928203230275509 0 Z" fill="#b20101" opacity="1.0" stroke="#ffffff" stroke-width="1"/>
</g>
<g clip-path="url(#page)" transform="translate(-1024, 0)">
<path d="M 472.3255466666667 306.96768142637353 l -3.4641016151377544 -6.0 a 4 4 0 1 1 6.928203230275509 0 Z" fill="#b20101" opacity="1.0" stroke="#ffffff" stroke-width="1"/>
</g>
<g clip-path="url(#page)" transform="translate(0, 0)">
<path d="M 472.3255466666667 306.96768142637353 l -3.4641016151377544 -6.0 a 4 4 0 1 1 6.928203230275509 0 Z" fill="#b20101" opacity="1.0" stroke="#ffffff" stroke-width="1"/>
</g>
<g clip-path="url(#page)" transform="translate(1024, 0)">
<path d="M 472.3255466666667 306.96768142637353 l -3.4641016151377544 -6.0 a 4 4 0 1 1 6.928203230275509 0 Z" fill="#b20101" opacity="1.0" stroke="#ffffff" stroke-width="1"/>
</g>
<g clip-path="url(#page)" transform="translate(-1024, 0)"> <g clip-path="url(#page)" transform="translate(-1024, 0)">
<path d="M 797.48224 326.9801587634675 l -4.330127018922193 -7.5 a 5 5 0 1 1 8.660254037844386 0 Z" fill="#00ff29" opacity="1.0" stroke="#000000" stroke-width="1"/> <path d="M 797.48224 326.9801587634675 l -4.330127018922193 -7.5 a 5 5 0 1 1 8.660254037844386 0 Z" fill="#00ff29" opacity="1.0" stroke="#000000" stroke-width="1"/>
</g> </g>

Before

Width:  |  Height:  |  Size: 1.1 MiB

After

Width:  |  Height:  |  Size: 1.1 MiB