File size: 3,238 Bytes
d8535a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
from google.cloud import bigquery
import functions_framework
from queries import queries
from google.oauth2 import service_account
import json
import requests
import streamlit as st
from app import send_message_via_webhook, Webhook_urls
from html_templates import logo, BigQuery_upload_title, table_id_placeholder, uploader, button_styles, tooltip_message_bq, recon_title, channel_member_title, duplicate_count, tooltip_message_recon
from utils import check_duplicates, upload_to_bigquery, authenticate_bigquery, load_gcp_credentials
import os
from queries import queries

# Display logo
st.markdown(logo, unsafe_allow_html=True)
st.logo("alerter_4.jpeg")

# Display page title
st.markdown(recon_title, unsafe_allow_html=True)
st.write("")
st.markdown(tooltip_message_recon, unsafe_allow_html = True)
st.write("")

# Dropdown for channels/members selection
webhook_url = list(Webhook_urls.keys())
st.markdown(channel_member_title, unsafe_allow_html=True)
st.write('')


# Multiselect to select channels
selection = st.multiselect("Select channels", webhook_url, label_visibility="hidden", help = ' Select the channel member(s) where you want to Slack the duplicate counts')

# Load GCP credentials
gcp_credentials = load_gcp_credentials()

if gcp_credentials:
    # Authenticate BigQuery client with the loaded credentials
    if selection:
        try:
            bigquery_creds = authenticate_bigquery()
            client = bigquery.Client(credentials=bigquery_creds)
            st.success("BigQuery client authenticated successfully")
        except Exception as e:
            st.error(f"Error authenticating BigQuery client: {e}")
            st.stop()  # Stop execution if BigQuery authentication fails

        # Check for duplicates in the queries
        results = check_duplicates(client)

        if results:
            st.write("")
            st.write("")
            
            # Display the duplicate count
            st.markdown(duplicate_count, unsafe_allow_html=True)

            # Prepare the HTML message to display results in the Streamlit UI
            message_html = ""
            for query_name, count in results.items():
                message_html += f"""
                <div class="result-box">
                    <div class="result-title">{query_name}</div>
                    <div>{count} records</div>
                </div>
                """

            # Display the results in the Streamlit UI
            st.markdown(message_html, unsafe_allow_html=True)

            # Prepare the plain-text message for Slack
            message_text = ""
            for query_name, count in results.items():
                message_text += f"*{query_name}*\n{count} records\n\n"

            # If no duplicates were found, update the message
            if not results:
                message_text = "No duplicates found in the queries."

            # Send the results to the selected Slack channels
            for channel in selection:
                webhook_url = Webhook_urls.get(channel)
                if webhook_url:
                    send_message_via_webhook(message_text, webhook_url)
                else:
                    st.error(f"Webhook URL not found for channel: {channel}")