File size: 2,680 Bytes
518c3a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# -*- 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