Skip to content

Commit

Permalink
feat: retrieve availability and save as metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
vncsna committed Feb 14, 2024
1 parent 847c652 commit e8f6e2c
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/deploy-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ jobs:
value: "${{ secrets.STRIPE_TEST_SECRET_KEY }}"
- name: "DJSTRIPE_WEBHOOK_SECRET"
value: "${{ secrets.DJSTRIPE_WEBHOOK_SECRET }}"
- name: "BETTERSTACK_BEARER_TOKEN"
value: "${{ secrets.BETTERSTACK_BEARER_TOKEN }}"
- name: "DISCORD_BACKEND_WEBHOOK_URL"
value: "${{ secrets.DISCORD_BACKEND_WEBHOOK_URL }}"
envFrom:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/deploy-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ jobs:
value: "${{ secrets.STRIPE_TEST_SECRET_KEY }}"
- name: "DJSTRIPE_WEBHOOK_SECRET"
value: "${{ secrets.DJSTRIPE_WEBHOOK_SECRET }}"
- name: "BETTERSTACK_BEARER_TOKEN"
value: "${{ secrets.BETTERSTACK_BEARER_TOKEN }}"
- name: "DISCORD_BACKEND_WEBHOOK_URL"
value: "${{ secrets.DISCORD_BACKEND_WEBHOOK_URL }}"
envFrom:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/deploy-staging.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ jobs:
value: "${{ secrets.STRIPE_TEST_SECRET_KEY }}"
- name: "DJSTRIPE_WEBHOOK_SECRET"
value: "${{ secrets.DJSTRIPE_WEBHOOK_SECRET }}"
- name: "BETTERSTACK_BEARER_TOKEN"
value: "${{ secrets.BETTERSTACK_BEARER_TOKEN }}"
- name: "DISCORD_BACKEND_WEBHOOK_URL"
value: "${{ secrets.DISCORD_BACKEND_WEBHOOK_URL }}"
envFrom:
Expand Down
3 changes: 3 additions & 0 deletions bd_api/apps/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@


class Metadata(BaseModel):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

id = models.UUIDField(primary_key=True, default=uuid4)
key = models.JSONField(default=dict, blank=False, null=False)
value = models.JSONField(default=dict, blank=False, null=False)
36 changes: 36 additions & 0 deletions bd_api/apps/core/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
from datetime import datetime, timedelta

from huey import crontab
from huey.contrib.djhuey import periodic_task

from bd_api.apps.core.models import Metadata
from bd_api.custom.client import BetterStackClient
from bd_api.utils import production_task


@periodic_task(crontab(day="1", hour="3", minute="0"))
@production_task
def get_monitor_availability():
"""Get availability metric and save as metadata"""

def get_period():
"""Get last month period as tuple of strings"""

today = datetime.today()
since = datetime(today.year, today.month - 1, 1)
until = datetime(today.year, today.month, 1) - timedelta(days=1)
return since.strftime("%Y-%m-%d"), until.strftime("%Y-%m-%d")

since, until = get_period()
client = BetterStackClient()
for monitor in client.get_monitors():
Metadata.objects.create(
key={
"since": since,
"until": until,
"monitor_id": monitor["id"],
"monitor_url": monitor["attributes"]["url"],
},
value=client.get_monitor_summary(monitor["id"], since, until),
)
24 changes: 23 additions & 1 deletion bd_api/custom/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from google.cloud.storage import Client as GCSClient
from google.oauth2.service_account import Credentials
from loguru import logger
from requests import post
from requests import Session, post

from bd_api.custom.model import BaseModel

Expand Down Expand Up @@ -72,3 +72,25 @@ def send(self):
"""Send message if it has body text"""
if self._message.count("\n"):
send_discord_message(self._message)


class BetterStackClient:
def __init__(self) -> None:
self.session = Session()
self.session.headers.update(
{"Authorization": f"Bearer {settings.BETTERSTACK_BEARER_TOKEN}"}
)
self.url = "https://uptime.betterstack.com"

def get_monitors(self):
response = self.session.get(f"{self.url}/api/v2/monitors")
response.raise_for_status()
return response.json()

def get_monitor_summary(self, monitor_id: str, since: str, until: str):
response = self.session.get(
f"{self.url}/api/v2/{monitor_id}/sla",
params={"from": since, "to": until},
)
response.raise_for_status()
return response.json()
3 changes: 3 additions & 0 deletions bd_api/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,6 @@

# Discord
DISCORD_BACKEND_WEBHOOK_URL = getenv("DISCORD_BACKEND_WEBHOOK_URL")

# BetterStack
BETTERSTACK_BEARER_TOKEN = getenv("BETTERSTACK_BEARER_TOKEN")

0 comments on commit e8f6e2c

Please sign in to comment.