PortalLVAM / logs_portal.py
bullm's picture
new test
518c3a9
raw
history blame
2.68 kB
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 16 16:29:25 2021
@author: bullm
"""
import streamlit as st
from datetime import datetime
from datetime import timedelta
import pandas as pd
import os
import boto3
import json
import io
import xlsxwriter
def save_s3(key, secret_key, bucket, df, path):
with io.BytesIO() as output:
with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
df.to_excel(writer, 'sheet_name')
data = output.getvalue()
s3 = boto3.resource('s3', aws_access_key_id = key, aws_secret_access_key= secret_key)
s3.Bucket(bucket).put_object(Key=path, Body=data)
def read_excel_s3(key, secret_key, bucket, path):
s3_client = boto3.client('s3', aws_access_key_id = key, aws_secret_access_key= secret_key)
response = s3_client.get_object(Bucket=bucket, Key=path)
data = response["Body"].read()
df = pd.read_excel(io.BytesIO(data))
return df
def log(func):
"""
Log portal permite almacenar las visitas que tienen tanto
las vistas como las subvistas, esta funcion se utiliza como decorador
"""
def visita_vista(*args, **kwargs):
key ='AKIARYMZ4J2YQDB66VX4'
secret_key = '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'
bucket='portallvam'
path ='Logs.xlsx'
analista = st.session_state.key
fecha = datetime.today()
data = read_excel_s3(key, secret_key, bucket, 'Logs.xlsx')[['Analista', 'Fecha', 'Vista',
'Subvista']]
vista = st.session_state['Funcion']
subvista = st.session_state['Subvista']
last_view = data.iloc[-1]
last_analista = last_view["Analista"]
last_vista = last_view["Vista"]
last_subvista = last_view["Subvista"]
last_fecha = last_view["Fecha"]
delta_t = fecha - last_fecha
if analista == last_analista and vista == last_vista:
if last_subvista == subvista and delta_t < timedelta(minutes=10):
pass
else:
data = data.append({"Analista": analista, "Fecha": fecha,
"Vista": vista, "Subvista": subvista},
ignore_index=True)
save_s3(key, secret_key, bucket, data, path)
else:
data = data.append({"Analista": analista, "Fecha": fecha,
"Vista": vista, "Subvista": subvista},
ignore_index=True)
save_s3(key, secret_key, bucket, data, path)
# data.to_excel('Data/Logs.xlsx', engine='openpyxl')
func(*args, **kwargs)
return visita_vista