Spaces:
Sleeping
Sleeping
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}") | |