Compare commits

..

1 Commits

Author SHA1 Message Date
a0209b612e tukaeva_alfiya_lab_3 is ready 2024-10-11 01:01:25 +04:00
19 changed files with 396 additions and 198 deletions

View File

@@ -7,6 +7,3 @@
/dozorova_alena_lab_2/ConsoleApp2/.vs
/dozorova_alena_lab_2/ConsoleApp2/bin
/dozorova_alena_lab_2/ConsoleApp2/obj
/dozorova_alena_lab_6/ConsoleApp1/.vs
/dozorova_alena_lab_6/ConsoleApp1/bin
/dozorova_alena_lab_6/ConsoleApp1/obj

View File

@@ -1,10 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -1,25 +0,0 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.10.35004.147
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp1", "ConsoleApp1.csproj", "{29269567-7466-4C99-BEEF-F5766BDDFB24}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{29269567-7466-4C99-BEEF-F5766BDDFB24}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{29269567-7466-4C99-BEEF-F5766BDDFB24}.Debug|Any CPU.Build.0 = Debug|Any CPU
{29269567-7466-4C99-BEEF-F5766BDDFB24}.Release|Any CPU.ActiveCfg = Release|Any CPU
{29269567-7466-4C99-BEEF-F5766BDDFB24}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EDED6E1D-0A86-43F9-94EA-6ADCC1FA1B42}
EndGlobalSection
EndGlobal

View File

@@ -1,55 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
public static class Extention
{
public static int[,] CreateMatrixWithoutColumn(this int[,] matrix, int column)
{
var result = new int[matrix.GetLength(0), matrix.GetLength(1) - 1];
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1) - 1; j++)
{
result[i, j] = j < column ? matrix[i, j] : matrix[i, j + 1];
}
}
return result;
}
public static int[,] CreateMatrixWithoutRow(this int[,] matrix, int row)
{
var result = new int[matrix.GetLength(0) - 1, matrix.GetLength(1)];
for (int i = 0; i < matrix.GetLength(0) - 1; i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
result[i, j] = i < row ? matrix[i, j] : matrix[i + 1, j];
}
}
return result;
}
public static double CalculateDeterminant(this int[,] matrix)
{
if (matrix.GetLength(0) == 2)
{
return matrix[0, 0] * matrix[1, 1] - matrix[0, 1] * matrix[1, 0];
}
double result = 0;
for (var j = 0; j < matrix.GetLength(0); j++)
{
result += (j % 2 == 1 ? 1 : -1) * matrix[1, j] *
matrix.CreateMatrixWithoutColumn(j).CreateMatrixWithoutRow(1).CalculateDeterminant();
}
//Console.WriteLine("Ко мне пришли с размером " + matrix.GetLength(0));
return result;
}
}
}

View File

@@ -1,87 +0,0 @@

using ConsoleApp1;
using System.Data.Common;
using System.Diagnostics;
internal class Program
{
private static void Main(string[] args)
{
var value = new int[3] {100, 300, 500 };
foreach(var i in value)
{
var a = CreateMatrix(i, i);
var b = CreateMatrix(i, i);
List<long> times = new() {};
Console.WriteLine("Для пяти потоков: ");
for (int j = 1; j <= 10; j++)
{
var sw = new Stopwatch();
sw.Start();
Calculate(a, j);
sw.Stop();
times.Add(sw.ElapsedTicks);
}
Console.WriteLine("Количество тиков для вычисления матрицы стороной "+i+": "+string.Join("\t", times));
Console.WriteLine("Для десяти потоков: ");
for (int j = 1; j <= 10; j++)
{
var sw = new Stopwatch();
sw.Start();
Calculate(a, j);
sw.Stop();
times.Add(sw.ElapsedTicks);
}
Console.WriteLine("Количество тиков для вычисления матрицы стороной " + i + ": " + string.Join("\t", times));
}
}
private static int[,] CreateMatrix(int x, int y)
{
var rnd = new Random();
var res = new int[y, x];
for (int i = 0; i < y; i++)
{
for (int j = 0; j < x; j++)
{
res[i, j] = rnd.Next(0, 100);
}
}
return res;
}
private static double Calculate(int[,] matrix, int maxTask)
{
double res = 0;
var semaphore = new SemaphoreSlim(maxTask, maxTask);
for (var j = 0; j < matrix.GetLength(0) - 1; j++)
{
_ = Task.Run(() =>
{
try
{
semaphore.Wait();
res += (j % 2 == 1 ? 1 : -1) * matrix[1, j] *
matrix.CreateMatrixWithoutColumn(j).
CreateMatrixWithoutRow(1).CalculateDeterminant();
}
finally { semaphore.Release(); }
});
}
semaphore.Wait(maxTask);
return res;
}
}

View File

@@ -1,18 +0,0 @@
# Лабораторная работа 6
В рамках данной работы мы изучаем выигрыш при распаралелливании процесса вычисления определителя матрицы
## Описание
Для вычисления определителя мы используем следующую формулу:
![alt text](image.png)
где ![alt text](image-1.png) - определитель матрицы, полученной из исходной вырезанием 1 строки и j столбца.
## Запуск
По опыту прошлой лабораторной работы, в консольном приложении был реализован алгоритм вычисление детерминанта и запущено сравнение затраченного времени (в тиках) для 5 и 10 потоков.
## Результаты
Результаты:
<br/>
![Результат](image-2.png)
<br/>
Как мы видим, подтверждаются выводы прошлой лабораторной работы: для небольших матриц выигрыш несущественнен из-за затраты времени на работу с потоком, а для больших эта разница в скорости уже существенна
## Видеодемонстрация
Видеодемонстрация по [адресу](https://drive.google.com/file/d/1LFRNbTAOzg21Du60Ehy9u6HOp9D3sKPm/view?usp=sharing)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 556 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 67 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.3 KiB

2
tukaeva_alfiya_lab_3/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/.venv
/.idea

View File

@@ -0,0 +1,52 @@
# Лабораторная работа №3 - REST API, Gateway и синхронный обмен между микросервисами
## Задание
* Создать 2 микросервиса, реализующих CRUD на связанных сущностях.
* Реализовать механизм синхронного обмена сообщениями между микросервисами.
* Реализовать шлюз на основе прозрачного прокси-сервера nginx.
## Предметная область:
В качестве предметной области использовался пример 19-го варианта. Имеются 2 сущности: одна сущность - книга в библиотеке, вторая - тематика.
Поля тематики: УИД (уникальный идентификатор) тематики, номер, название. Поля книги: УИД книги, Автор, Название, Год издания, УИД тематики, к которой принадлежит книга.
## Запуск ЛР:
Введем в терминале команду:
```
docker compose up --build
```
Эта команда сначала выполнит сборку, а затем запустит контейнеры.
Также нужно убедиться в том,что 80 порт свободен и тогда вся система запустится.
Запускаем Postman и запускаем следующие запросы:
![](lab_3.1.png "")
Для каждой сущности были реализованы CRUD-операции.
Theme сервис:
GET /themes — получить все темы
POST /themes — создать новую тему
GET /themes/{id} — получить тему по ID
GET /themes/with-books/{themeId} — получить все книги, которые относятся к themeId
PUT /themes/{id} — обновить тему по ID
DELETE /themes/{id} — удалить тему по ID
Book сервис:
GET /books — получить все книги
POST /books — создать новую книгу
GET /books/{id} — получить книгу по ID
PUT /books/{id} — обновить книгу по ID
DELETE /books/{id} — удалить книгу по ID
# Видео
https://vk.com/video230744264_456239105?list=ln-xiFZDAJoFyAsG9Dg2l

View File

@@ -0,0 +1,16 @@
FROM python:latest
# Устанавливаю рабочую директорию внутри контейнера
WORKDIR /app
# Копирую файл requirements.txt в контейнер
COPY requirements.txt .
# Устанавливаю зависимости
RUN pip install --no-cache-dir -r requirements.txt
# Копирую все файлы в контейнер
COPY book_service/book_service.py .
# Команда для запуска Python-скрипта
CMD ["python", "book_service.py"]

View File

@@ -0,0 +1,134 @@
from flask import Flask, jsonify, request
import requests
from uuid import uuid4
import uuid
class Book:
def __init__(self, title, year, author, uuid_: uuid, theme_id: uuid):
if uuid_ is None:
self.uuid_: uuid = uuid4()
else:
self.uuid_: uuid = uuid.UUID(uuid_)
self.title: str = title
self.year: int = year
self.author: str = author
self.theme_id: uuid = uuid.UUID(theme_id)
def to_dict(self):
return {
'title': self.title,
'year': self.year,
'author': self.author,
'theme_id': self.theme_id,
'uuid': self.uuid_
}
def to_dict_for_themes(self):
return {
'title': self.title,
'year': self.year,
'author': self.author,
'uuid': self.uuid_
}
app = Flask(__name__)
books: list[Book] = [
Book(title='Mac', year=1977, author= 'Jane', uuid_='89fa1e7a-7e88-445e-a4d8-6d4497ea8f19',
theme_id='997aa4c5-ebb2-4794-ba81-e742f9f1fa30'),
Book(title='Portrait', year=1989, uuid_='0351ee11-f11b-4d83-b2c8-1075b0c357dc', author= 'Mark',
theme_id='694827e4-0f93-45a5-8f75-bad7ef2d21fe'),
Book(title='Chamomile', uuid_='dfc17619-7690-47aa-ae8e-6a5068f8ddec', year=1977, author= 'Laura',
theme_id='997aa4c5-ebb2-4794-ba81-e742f9f1fa30')
]
themes_url = 'http://theme_service:20001/'
def list_jsonify():
return jsonify([book.to_dict() for book in books])
# получить список книг
@app.route('/', methods=['GET'])
def get_all():
return list_jsonify(), 200
# получение списка книг по идентификатору темы (для theme_service)
@app.route('/by-theme/<uuid:theme_uuid>', methods=['GET'])
def get_by_theme_id(theme_uuid):
return [book.to_dict_for_themes() for book in books if book.theme_id == theme_uuid], 200
# получение книги по идентификатору
@app.route('/<uuid:uuid_>', methods=['GET'])
def get_one(uuid_):
for book in books:
if book.uuid_ == uuid_:
return book.to_dict(), 200
return 'Книга с таким uuid не была найдена', 404
# создание новой книги
@app.route('/', methods=['POST'])
def create():
data = request.json
title = data.get('title', None)
year = data.get('year', None)
author = data.get('author', None)
theme_id = data.get('theme_id', None)
checking = requests.get(themes_url + f'/check/{theme_id}')
print(checking)
if checking.status_code == 200:
new_book = Book(title, year,author, None, theme_id)
books.append(new_book)
return get_one(new_book.uuid_)
if checking.status_code == 404:
return 'Тема с таким uuid не существует', 404
return 'Неизвестная ошибка', 500
# изменение книги по идентификатору
@app.route('/<uuid:uuid_>', methods=['PUT'])
def update_by_id(uuid_):
data = request.json
new_title = data.get('title', None)
new_year = data.get('year', None)
new_author = data.get('author', None)
for book in books:
print(book.uuid_)
if book.uuid_ == uuid_:
if new_title is not None:
book.title = new_title
if new_year is not None:
book.year = new_year
if new_author is not None:
book.author= new_author
return get_one(book.uuid_)
return 'Книга с таким uuid не была найдена', 404
# удаление книги по идентификатору
@app.route('/<uuid:uuid_>', methods=['DELETE'])
def delete(uuid_):
for book in books:
if book.uuid_ == uuid_:
books.remove(book)
return 'Книга успешно удалена', 200
return 'Книга с таким uuid не была найдена', 404
if __name__ == '__main__':
app.run(host='0.0.0.0', port=20002, debug=True)

View File

@@ -0,0 +1,29 @@
services:
book_service:
container_name: book_service
build:
context: .
dockerfile: ./book_service/Dockerfile
expose:
- 20001
theme_service:
container_name: theme_service
build:
context: .
dockerfile: ./theme_service/Dockerfile
expose:
- 20002
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
depends_on:
- book_service
- theme_service

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

View File

@@ -0,0 +1,26 @@
events { worker_connections 1024; }
http {
server {
listen 80;
listen [::]:80;
server_name localhost;
location /theme_service/ {
proxy_pass http://theme_service:20001/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /book_service/ {
proxy_pass http://book_service:20002/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}

View File

@@ -0,0 +1,3 @@
Flask==3.0.3
requests==2.32.3

View File

@@ -0,0 +1,16 @@
FROM python:latest
# Устанавливаю рабочую директорию внутри контейнера
WORKDIR /app
# Копирую файл requirements.txt в контейнер
COPY requirements.txt .
# Устанавливаю зависимости
RUN pip install --no-cache-dir -r requirements.txt
# Копирую все файлы в контейнер
COPY theme_service/theme_service.py .
# Команда для запуска Python-скрипта
CMD ["python", "theme_service.py"]

View File

@@ -0,0 +1,118 @@
from flask import Flask, jsonify, request
from uuid import uuid4
import uuid
import requests
class Theme:
def __init__(self, number, name, uuid_: uuid):
if uuid_ is None:
self.uuid_: uuid = uuid4()
else:
self.uuid_: uuid = uuid.UUID(uuid_)
self.number: str = number
self.name: str = name
def to_dict(self):
return {
"uuid": self.uuid_,
"number": self.number,
"name": self.name
}
def to_dict_with_books(self, books: list):
return {
"uuid": self.uuid_,
"number": self.number,
"name": self.name,
"books": books
}
app = Flask(__name__)
themes: list[Theme] = [
Theme(name='Flowers', number='1', uuid_='997aa4c5-ebb2-4794-ba81-e742f9f1fa30'),
Theme(name='Art', number='2', uuid_='694827e4-0f93-45a5-8f75-bad7ef2d21fe')
]
books_url = 'http://book_service:20002/'
def list_jsonify():
return jsonify([theme.to_dict() for theme in themes])
# получение списка всех тем
@app.route('/', methods=['GET'])
def get_all():
return list_jsonify(), 200
# получение темы по идентификатору
@app.route('/<uuid:uuid_>', methods=['GET'])
def get_one(uuid_):
for theme in themes:
if theme.uuid_ == uuid_:
return theme.to_dict(), 200
return 'Тема с таким uuid не была найдена', 404
# получение темы со списком книг по ней
@app.route('/with-books/<uuid:uuid_>', methods=['GET'])
def get_one_with_books(uuid_):
for theme in themes:
if theme.uuid_ == uuid_:
response = requests.get(books_url + f'by-theme/{uuid_}')
print(response.json())
return theme.to_dict_with_books(response.json()), 200
return 'Тема с таким uuid не была найдена', 404
# создание темы
@app.route('/', methods=['POST'])
def create():
data = request.json
number = data.get('number', None)
name = data.get('name', None)
if name is None or number is None:
return 'Недостаточно полей для создания новой темы', 404
new_theme = Theme(number, name, None)
themes.append(new_theme)
return get_one(new_theme.uuid_)
# изменение темы по идентификатору
@app.route('/<uuid:uuid_>', methods=['PUT'])
def update_by_id(uuid_):
data = request.json
new_number = data.get('number', None)
new_name = data.get('name', None)
for theme in themes:
if theme.uuid_ == uuid_:
if new_number is not None:
theme.number = new_number
if new_name is not None:
theme.name = new_name
return get_one(theme.uuid_)
return 'Тема с таким uuid не была найдена', 404
# удаление темы по идентификатору
@app.route('/<uuid:uuid_>', methods=['DELETE'])
def delete(uuid_):
for theme in themes:
if theme.uuid_ == uuid_:
themes.remove(theme)
return 'Тема успешно удалена', 200
return 'Тема с таким uuid не была найдена', 404
if __name__ == '__main__':
app.run(host='0.0.0.0', port=20001, debug=True)