Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from chalicelib.api.members import members_api
from chalicelib.api.events import events_api
from chalicelib.api.accountability import accountability_api
from chalicelib.api.monitoring import monitoring_api

app = Chalice(app_name="zap")
app.register_blueprint(announcements_api)
Expand All @@ -16,6 +17,7 @@
app.register_blueprint(members_api)
app.register_blueprint(events_api)
app.register_blueprint(accountability_api)
app.register_blueprint(monitoring_api)


@app.route("/")
Expand Down
12 changes: 12 additions & 0 deletions chalicelib/api/monitoring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from chalice import Blueprint
from chalicelib.services.MonitoringService import monitoring_service
from chalicelib.decorators import auth
from chalicelib.models.roles import Roles

monitoring_api = Blueprint(__name__)


@monitoring_api.route("/monitoring/visited-pages", methods=["GET"], cors=True)
@auth(monitoring_api, roles=[Roles.ADMIN])
def get_top_10_visited_pages():
return monitoring_service.get_top_10_visited_pages()
27 changes: 27 additions & 0 deletions chalicelib/modules/aws_ssm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import boto3


class AWS_SSM:
def __init__(self):
self.client = boto3.client("ssm")

def get_parameter_value(self, name: str, with_decryption: bool = True):
"""
Retrieves the value of a parameter from the AWS Systems Manager (SSM) Parameter Store.

Args:
name (str): The name of the parameter to retrieve.
with_decryption (bool, optional): Whether to decrypt the parameter value if it is encrypted. Defaults to True.

Returns:
str: The value of the parameter.

Raises:
KeyError: If the parameter is not found in the Parameter Store.

"""
return self.client.get_parameter(Name=name, WithDecryption=with_decryption)[
"Parameter"
]["Value"]

aws_ssm = AWS_SSM()
106 changes: 106 additions & 0 deletions chalicelib/services/MonitoringService.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import requests
from chalicelib.modules.aws_ssm import aws_ssm
import uuid


class MonitoringService:

def __init__(self):
try:
self.api_key = aws_ssm.get_parameter_value("/Vault/POSTHOG_API_KEY")
self.project_id = aws_ssm.get_parameter_value("/Vault/POSTHOG_PROJECT_ID")

except Exception as e:
print(e)

def get_top_10_visited_pages(self):
"""
Retrieves the top 10 visited pages from the PostHog API.

Returns:
dict: A dictionary containing the top 10 visited pages, with each page represented as a dictionary
with the keys "name" and "value". The "name" key corresponds to the URL of the page, and the "value" key
corresponds to the number of unique sessions for that page.

Raises:
requests.exceptions.RequestException: If there is an error making the API request.
"""
# Make a POST request to the PostHog API to retrieve the top 10 visited pages
query_response = requests.post(
f"https://us.posthog.com/api/projects/{self.project_id}/query/",
headers={"Authorization": f"Bearer {self.api_key}"},
json={
# The query to be executed
"query": {
"kind": "TrendsQuery",
"properties": {
# Filter the events to only include those that correspond to page views
"type": "AND",
"values": [
{
"type": "AND",
"values": [
{
"key": "$current_url",
"type": "event",
"value": "?",
"operator": "not_icontains",
}
],
}
],
},
# Filter out test accounts when retrieving the data
"filterTestAccounts": True,
# Only retrieve data from the past 30 days
"dateRange": {"date_from": "-30d"},
# The events to retrieve
"series": [
{
"kind": "EventsNode",
"event": "$pageview",
# The name of the event
"name": "$pageview",
# How to aggregate the data
"math": "unique_session",
}
],
# The time interval to group the data by
"interval": "day",
# How to group the data
"breakdownFilter": {
"breakdown_type": "event",
"breakdown": "$current_url",
},
# How to display the data
"trendsFilter": {"display": "ActionsBarValue"},
},
# A unique identifier for the query
"client_query_id": str(uuid.uuid4()),
# Whether to refresh the cache
"refresh": True,
# Whether to execute the query asynchronously
"async": False,
},
)

# Parse the response as JSON
query_data = query_response.json()

# Sort the results by the number of unique sessions in descending order
top_website_pages_result = sorted(
[
# For each event, create a dictionary with the page URL and the number of unique sessions
{"name": data["label"], "value": data["aggregated_value"]}
for data in query_data["results"]
# Only include events that correspond to a page URL
if data["label"].startswith("https")
],
key=lambda x: -x["value"],
)[:10]

# Return the top 10 visited pages
return {"topWebsitePages": top_website_pages_result}


monitoring_service = MonitoringService()