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: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
- name: set up Docker builder
uses: docker/setup-buildx-action@v1
- name: Log in to the Cntainer registry
uses: docker/login-action@v1
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
Expand Down
6 changes: 3 additions & 3 deletions generate_word/generate_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from datetime import date, timedelta


def create_report(counter: int, avg_load: float, max_load: float):
def create_report(counter: int, avg_load: float, max_load: float, site_name: str):
template_word = DocxTemplate(join(get_settings().template_dir, 'report.docx'))
template_word.render(
{
Expand All @@ -16,5 +16,5 @@ def create_report(counter: int, avg_load: float, max_load: float):
}
)
current_date = (date.today() - timedelta(days=1)).strftime('%d-%m-%Y')
template_word.save(join(get_settings().static_dir, f'{current_date}.docx'))
return join(get_settings().static_dir, f'{current_date}.docx')
template_word.save(join(get_settings().static_dir, f'{current_date}-{site_name}.docx'))
return join(get_settings().static_dir, f'{current_date}-{site_name}.docx')
16 changes: 10 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
from database import get_session, engine
from models import \
Traffic, \
Site
Site, \
Email
from settings import SECRET_KEY

app = FastAPI()
Expand All @@ -43,7 +44,8 @@ async def calculate(identification: str,
if traffic:
await session.execute(update(Traffic).
where(Traffic.id == traffic[0].id).
values(counter=Traffic.counter+1,
values(id=traffic[0].id,
counter=Traffic.counter+1,
average_load=Traffic.counter * 0.0184,
maximum_load=Traffic.counter * 0.0305,
))
Expand All @@ -65,7 +67,7 @@ async def verify_token_access(token_access: str):
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail='У вас нет доступа к запрашиваемой странице')


@app.post('/identification_site/')
@app.get('/identification_site/')
async def form_send(request: Request):
return templates.TemplateResponse('post_identification.html', {'request': request})

Expand All @@ -74,13 +76,15 @@ async def form_send(request: Request):
response_model=Site)
async def generate_secret_key(website_url: str = Form(...),
secret_key: str = Form(...),
list_email: str = Form(...),
session: AsyncSession = Depends(get_session)):
print('username', website_url)
print('password', secret_key)
verify_site = (await session.execute(select(Site).where(Site.site_name == website_url))).first()
if verify_site is None:
email_id = (await session.execute(insert(Email).values(name=list_email))).inserted_primary_key[0]
await session.commit()
site_id = (await session.execute(insert(Site).values(site_name=website_url,
identification=secret_key))).inserted_primary_key[0]
identification=secret_key,
email_id=email_id))).inserted_primary_key[0]
await session.commit()
return JSONResponse(content=jsonable_encoder((await session.execute(select(Site).
where(Site.id == site_id))).first()))
Expand Down
2 changes: 1 addition & 1 deletion migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata

from models import Traffic
from models import Traffic, Site, Email

target_metadata = SQLModel.metadata

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Add columnls
"""Add models

Revision ID: 65a007288939
Revision ID: 53b75e1e6be5
Revises:
Create Date: 2022-05-18 16:44:06.441247
Create Date: 2022-05-20 19:56:07.821485

"""
from alembic import op
Expand All @@ -11,26 +11,35 @@


# revision identifiers, used by Alembic.
revision = '65a007288939'
revision = '53b75e1e6be5'
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('site',
sa.Column('identification', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
sa.Column('site_name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
op.create_table('email',
sa.Column('name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
sa.Column('id', sa.Integer(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_table('site',
sa.Column('identification', sa.String(), nullable=False),
sa.Column('site_name', sa.String(), nullable=False),
sa.Column('email_id', sa.Integer(), nullable=False),
sa.Column('id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['email_id'], ['email.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('identification'),
sa.UniqueConstraint('site_name')
)
op.create_table('traffic',
sa.Column('counter', sa.Integer(), nullable=True),
sa.Column('average_load', sa.Float(), nullable=True),
sa.Column('maximum_load', sa.Float(), nullable=True),
sa.Column('average_load', sa.Float(), nullable=False),
sa.Column('maximum_load', sa.Float(), nullable=False),
sa.Column('create_at', sa.Date(), nullable=False),
sa.Column('site_id', sa.Integer(), nullable=True),
sa.Column('site_id', sa.Integer(), nullable=False),
sa.Column('id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['site_id'], ['site.id'], ),
sa.PrimaryKeyConstraint('id')
Expand All @@ -42,4 +51,5 @@ def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('traffic')
op.drop_table('site')
op.drop_table('email')
# ### end Alembic commands ###
29 changes: 16 additions & 13 deletions models.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
from typing import Optional
from sqlmodel import SQLModel, Field
from datetime import date
from sqlalchemy import Date, Column
from sqlalchemy import Date, Column, String


class TrafficBase(SQLModel):
counter: int = Field(default=1, title='Количество запросов в день')
average_load: Optional[float] = Field(title='Средняя нагрузка на сеть за день')
maximum_load: Optional[float] = Field(title='Максимальная нагрузка на сеть за день')
create_at: date = Field(title='Дата')
site_id: int = Field(default=None, foreign_key='site.id')
average_load: Optional[float] = Field(default=0, nullable=False, title='Средняя нагрузка на сеть за день')
maximum_load: Optional[float] = Field(default=0, nullable=False, title='Максимальная нагрузка на сеть за день')
create_at: date = Field(nullable=False, title='Дата')
site_id: int = Field(default=None, foreign_key='site.id', nullable=False)


class Traffic(TrafficBase, table=True):
id: int = Field(default=None, primary_key=True)


class TrafficCreate(TrafficBase):
pass


class SiteBase(SQLModel):
# identification: str = Field(sa_column=Column('identification', unique=True), title='Индентификатор сайта')
# site_name: str = Field(sa_column=Column('site_name', unique=True), title='URL сайта')
identification: str = Field(title='Индентификатор сайта')
site_name: str = Field(title='URL сайта')
identification: str = Field(sa_column=Column('identification', String, unique=True, nullable=False), title='Индентификатор сайта')
site_name: str = Field(sa_column=Column('site_name', String, unique=True, nullable=False), title='URL сайта')
email_id: int = Field(default=None, foreign_key='email.id', nullable=False)


class Site(SiteBase, table=True):
id: int = Field(default=None, primary_key=True)


class EmailBase(SQLModel):
name: str = Field(nullable=False, title='Список email')


class Email(EmailBase, table=True):
id: int = Field(default=None, primary_key=True)
4 changes: 2 additions & 2 deletions send_message/send_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@


async def send_file(login: str, password: str, sender: str, receivers: str,
attachment_path: str, smtp_server: str, port: int):
attachment_path: str, smtp_server: str, port: int, site_name: str):
current_date = (date.today() - timedelta(days=1)).strftime('%d-%m-%Y')
message = MIMEMultipart()
message['Subject'] = 'Отчет о состоянии IT-инфраструктуры и результатах ' \
'мониторинга инцидентов в области кибербезопасности.'
f'мониторинга инцидентов в области кибербезопасности сайта {site_name}.'
message['From'] = sender
message['To'] = receivers

Expand Down
47 changes: 27 additions & 20 deletions task.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,40 @@
from generate_word.generate_file import create_report
from send_message.send_email import send_file
from database import engine
from models import Traffic
from models import Traffic, Site, Email
from settings import CONFIG_EMAIL, NOTIFICATION_SEND_TIME
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import sessionmaker


async def send_message():
async with engine.connect() as session:
get_record = (await session.execute(
select(Traffic).
where(Traffic.create_at == date.today() - timedelta(days=1)))).first()
path_report = create_report(get_record.counter,
round(get_record.average_load, 2),
round(get_record.maximum_load, 2)
)
await send_file(CONFIG_EMAIL['MAIL_FROM'],
CONFIG_EMAIL['MAIL_PASSWORD'],
CONFIG_EMAIL['MAIL_FROM'],
CONFIG_EMAIL['MAIL_TO'],
path_report,
CONFIG_EMAIL['MAIL_SERVER'],
CONFIG_EMAIL['MAIL_PORT']
)
remove(path_report)
sleep(1)
async with sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)() as session:
get_records = (await session.execute(select(Traffic).
where(Traffic.create_at == date.today() - timedelta(days=1)))).all()
for get_record in get_records:
site = (await session.execute(select(Site).
where(Site.id == get_record[0].site_id))).first()
email_to = (await session.execute(select(Email).
where(Email.id == site[0].email_id))).first()[0].name
path_report = create_report(get_record[0].counter,
round(get_record[0].average_load, 2),
round(get_record[0].maximum_load, 2),
site[0].site_name)
await send_file(CONFIG_EMAIL['MAIL_FROM'],
CONFIG_EMAIL['MAIL_PASSWORD'],
CONFIG_EMAIL['MAIL_FROM'],
email_to,
path_report,
CONFIG_EMAIL['MAIL_SERVER'],
CONFIG_EMAIL['MAIL_PORT'],
site[0].site_name
)
remove(path_report)
sleep(1)


schedule.every().day.at(f'{NOTIFICATION_SEND_TIME}').do(send_message)
#schedule.every(10).seconds.do(send_message)
#schedule.every(20).seconds.do(send_message)

while True:
get_event_loop().run_until_complete(schedule.run_pending())
Expand Down
10 changes: 7 additions & 3 deletions templates/post_identification.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<title>Добавление сайта</title>
</head>
<body>
<div class="container">
<form action="/identification/" enctype="multipart/form-data" method="post">
<label>Enter website url</label>
<label>Введите URL сайта</label>
<br>
<input name="website_url" type="website_url" >
<br>
<label>Enter secret key</label>
<label>Введите email(s) (через запятую) на который будут приходить отчеты</label>
<br>
<input name="list_email" type="list_email" >
<br>
<label>Введите секретный ключ</label>
<br>
<input name="secret_key" type="secret_key" >
<br><br>
Expand Down
2 changes: 1 addition & 1 deletion templates/statistics.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<title>Сатистика сайта</title>
</head>
<body>
<table style="border:1px solid black;margin-left:auto;margin-right:auto;text-align:center">
Expand Down