Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #387

Merged
merged 12 commits into from
Jun 6, 2024
Merged

Dev #387

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: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3
FROM python:3.12

ARG requirements=/code/requirements/prod.txt
ENV DJANGO_SETTINGS_MODULE="kmd_ntx_stats.settings.prod"
Expand Down
5 changes: 5 additions & 0 deletions alerts/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM python:3

RUN mkdir /alerts
WORKDIR /alerts
COPY ./ /alerts/
53 changes: 53 additions & 0 deletions alerts/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python3
import time
import discord
import asyncio
from config import ALERT_INTERVAL, DISCORD_CHANNEL, DISCORD_TOKEN
from models import NotaryMonitor

class MyClient(discord.Client):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.sauron = NotaryMonitor()

async def setup_hook(self) -> None:
# create the background task and run it in the background
self.bg_task = self.loop.create_task(self.process_alerts())

async def on_ready(self):
print(f'Logged in as {self.user} (ID: {self.user.id})')
print('------')

async def process_alerts(self):
await self.wait_until_ready()
print('Logged in as')
print(self.user.name)
print(self.user.id)
print('------')
channel = self.get_channel(DISCORD_CHANNEL)
while not self.is_closed():

try:
loop = 0
while True:
msg = self.sauron.alert_slow_miners()
if msg != "":
# await self.channel.send(msg)
print(msg)
break

if loop > 12:
await channel.send(f"<@448777271701143562> Mining endpoint not responding!\n")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await channel.send(f"<@448777271701143562> Mining endpoint not responding!\n")
await channel.send("<@448777271701143562> Mining endpoint not responding!\n")

f-string is unnecessary here. This can just be a string. More.

break
time.sleep(60)
loop += 1

except Exception as e:
await channel.send(f"<@448777271701143562> Mining endpoint not responding!\n{e}")
await asyncio.sleep(ALERT_INTERVAL)



if __name__ == '__main__':
client = MyClient(intents=discord.Intents.default())
client.run(DISCORD_TOKEN)
13 changes: 13 additions & 0 deletions alerts/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os
from dotenv import load_dotenv
load_dotenv()

ALERT_INTERVAL = 60*60*8 # 8 hours
try:
DISCORD_CHANNEL = int(os.getenv('DISCORD_CHANNEL'))
except:
print("You need to add 'DISCORD_CHANNEL' to .env file")

DISCORD_TOKEN = os.getenv('DISCORD_TOKEN')
if DISCORD_TOKEN is None:
print("You need to add 'DISCORD_TOKEN' to .env file")
66 changes: 66 additions & 0 deletions alerts/contacts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
NN_DISCORD_IDS = {
"colmapol_EU": "",
"alien_NA": "412323938782150658",
"alien_EU": "412323938782150658",
"alien_SH": "412323938782150658",
"alienx_NA": "412323938782150658",
"alright_EU": "405011811511828481",
"alright_DEV": "405011811511828481",
"artem.pikulin_AR": "457121878465708043",
"batman_AR": "407288940148555786",
"blackice2_AR": "345544724167524352",
"blackice_AR": "345544724167524352",
"blackice_EU": "345544724167524352",
"blackice_NA": "345544724167524352",
"blackice_DEV": "345544724167524352",
"ca333_EU": "375074477756645386",
"caglarkaya_EU": "689469673343156323",
"mcrypt_SH": "458507065178980353",
"chmex2_SH": "420119978138664961",
"chmex_AR": "420119978138664961",
"chmex_EU": "420119978138664961",
"chmex_NA": "420119978138664961",
"chmex_SH": "420119978138664961",
"cipi_AR": "419964976397156352",
"cipi_EU": "419964976397156352",
"cipi_NA": "419964976397156352",
"computergenie2_NA": "474206298427097099",
"computergenie_EU": "474206298427097099",
"computergenie_NA": "474206298427097099",
"dimxy_AR": "507209916449292303",
"dimxy_DEV": "507209916449292303",
"dragonhound_AR": "448777271701143562",
"dragonhound_EU": "448777271701143562",
"dragonhound_NA": "448777271701143562",
"dragonhound_DEV": "448777271701143562",
"emmaccen_DEV": "933467505886904370",
"fediakash_AR": "803217186792800326",
"gcharang_AR": "423176312354635779",
"gcharang_SH": "423176312354635779",
"gcharang_DEV": "423176312354635779",
"kmdude_SH": "829743507433979904",
"marmara_AR": "409990217047474176",
"marmara_EU": "409990217047474176",
"nodeone2_NA": "474206298427097099",
"nodeone_NA": "474206298427097099",
"ozkanonur_NA": "147806685015179267",
"pbca26_NA": "403229823834521616",
"pbca26_SH": "403229823834521616",
"phit_SH": "352577127494713345",
"ptyx_SH": "303794669945618442",
"shamardy_SH": "729091988565131295",
"sheeba2_SH": "288941564263268353",
"sheeba_SH": "288941564263268353",
"smdmitry2_AR": "486144655369437184",
"smdmitry_AR": "486144655369437184",
"smdmitry_EU": "486144655369437184",
"smdmitry_SH": "486144655369437184",
"strob_SH": "278565047113089025",
"tonyl_AR": "272003866906722306",
"tonyl_DEV": "272003866906722306",
"van_EU": "",
"webworker01_EU": "300741339279130624",
"webworker01_NA": "300741339279130624",
"who-biz_NA": "402649850241482752",
"yurri-khi_DEV": "668742126992883714"
}
40 changes: 40 additions & 0 deletions alerts/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import time
import datetime
import requests
from contacts import NN_DISCORD_IDS

class NotaryMonitor():
def __init__(self):
pass

def get_last_mined(self):
try:
r = requests.get("https://stats.kmd.io/api/mining/notary_last_mined_table/")
return r.json()["results"]
except Exception as e:
print(f"Error: {e}")
print(f"Response: {r.content}")
return {}

def alert_slow_miners(self):
msg = ""
data = self.get_last_mined()
now = int(time.time())
threshold = now - 4 * 60 * 60 # 4 hours
slow_miners = {}
[
slow_miners.update({
i["name"]: datetime.timedelta(seconds=now - i["blocktime"])
})
for i in data
if i["blocktime"] < threshold
]

if len(slow_miners) > 1:
msg = "**The following Notaries have not mined a block recently...**\n"
for notary in slow_miners:
if NN_DISCORD_IDS[notary] == "":
msg += f"{notary} (last mined {slow_miners[notary]} ago)\n"
else:
msg += f"{NN_DISCORD_IDS[notary]} ({notary} last mined {slow_miners[notary]} ago)\n"
return msg
1 change: 1 addition & 0 deletions alerts/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
discord.py==2.3.2
Loading
Loading