diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b4433d9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.env +.vscode/ +__pycache__/ +poetry.lock \ No newline at end of file diff --git a/front/app.vue b/front/app.vue index 9edd131..4f4e428 100644 --- a/front/app.vue +++ b/front/app.vue @@ -3,10 +3,10 @@ const links = [[ { label: 'Состояние вендотеков', icon: 'i-heroicons-credit-card', - to: '/wtk', + to: '/vtk', }, - { label: 'Офис', to: '/wtk/office' }, - { label: 'Парк', to: '/wtk/park' }, + { label: 'Офис', to: '/vtk/office' }, + { label: 'Парк', to: '/vtk/park' }, ], [ { diff --git a/front/nuxt.config.ts b/front/nuxt.config.ts index 005ed49..a06ebf1 100644 --- a/front/nuxt.config.ts +++ b/front/nuxt.config.ts @@ -2,7 +2,13 @@ export default defineNuxtConfig({ devtools: { enabled: true }, modules: ["@nuxt/ui"], + ssr: false, devServer: { port: 3010 - } + }, + runtimeConfig: { + public: { + apiBase: '', // can be overridden by NUXT_PUBLIC_API_BASE environment variable + } + }, }) \ No newline at end of file diff --git a/front/pages/vtk/[slug].vue b/front/pages/vtk/[slug].vue new file mode 100644 index 0000000..9f97303 --- /dev/null +++ b/front/pages/vtk/[slug].vue @@ -0,0 +1,34 @@ + + \ No newline at end of file diff --git a/front/pages/wtk/index.vue b/front/pages/vtk/index.vue similarity index 100% rename from front/pages/wtk/index.vue rename to front/pages/vtk/index.vue diff --git a/front/pages/wtk/[slug].vue b/front/pages/wtk/[slug].vue deleted file mode 100644 index 31eaea5..0000000 --- a/front/pages/wtk/[slug].vue +++ /dev/null @@ -1,3 +0,0 @@ - \ No newline at end of file diff --git a/helpers.py b/helpers.py new file mode 100644 index 0000000..19b0c78 --- /dev/null +++ b/helpers.py @@ -0,0 +1,8 @@ +def flatten_dict(dd, separator='_', prefix=''): + res = {} + for key, value in dd.items(): + if isinstance(value, dict): + res.update(flatten_dict(value, separator, prefix + key + separator)) + else: + res[prefix + key] = value + return res \ No newline at end of file diff --git a/main.py b/main.py index d1872e1..a6e53a6 100644 --- a/main.py +++ b/main.py @@ -1,26 +1,99 @@ -import requests +import json import os -from fastapi import FastAPI +import pprint +import requests +from dotenv import load_dotenv +from fastapi import FastAPI, HTTPException from fastapi.staticfiles import StaticFiles from fastapi.middleware.cors import CORSMiddleware import logging +from helpers import flatten_dict + +load_dotenv() logging.basicConfig( level=logging.INFO, format="%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(funcName)s %(message)s", ) logger = logging.getLogger(__name__) + origins = [ "http://localhost", + "http://localhost:3010", "http://localhost:4173", "http://localhost:5173", "http://localhost:8000", ] +vtk = json.loads(os.getenv("VTK_KEYS")) +vtk_org = {} api_app = FastAPI(title="api app") + +@api_app.get("/") +def test(): + return {"app": "service monitoring api"} + + +@api_app.get("/vtk/{item_id}") +def get_vtk(item_id): + if not item_id in vtk: + raise HTTPException(status_code=404, detail="Item not found") + try: + if not item_id in vtk_org: + res = requests.get( + "https://my.vendotek.com/api/v1/org", + headers={"Authorization": f"Bearer {vtk[item_id]}"}, + ) + data = res.json() + org_id = data[0]["name"] + vtk_org[item_id] = org_id + + res_unit = requests.get( + f"https://my.vendotek.com/api/v1/org/{vtk_org[item_id]}/unit", + headers={"Authorization": f"Bearer {vtk[item_id]}"}, + ) + data_unit = res_unit.json() + result = [ + { + "sn": d["sn"], + "last_online": d["last_seen_at"], + "name": ", ".join( + list(filter(None, [d["location_name"], d["address"], d["city"]])) + ), + "ip": list( + filter( + lambda item: item["name"] == "IP address", + list( + filter( + lambda item: item["id"] == "LAN", + list( + filter( + lambda item: item["id"] == "Communications", + d["modules"], + ) + )[0]["modules"], + ) + )[0]["details"], + ) + )[0]["value"], + } + for d in data_unit + ] + return {"status": "success", "data": result} + except Exception as e: + logger.error(e) + raise HTTPException(status_code=500, detail=str(e)) + + app = FastAPI(title="main app") +app.exception_handler(404) + + +async def return_error(): + return {"error": "custom error"} + app.add_middleware( CORSMiddleware, diff --git a/pyproject.toml b/pyproject.toml index 503a0e8..7931e03 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,6 +10,7 @@ python = "^3.10" fastapi = "^0.111.0" uvicorn = "^0.29.0" requests = "^2.31.0" +python-dotenv = "^1.0.1" [tool.poetry.group.dev.dependencies] taskipy = "^1.0.0" @@ -20,4 +21,4 @@ build-backend = "poetry.core.masonry.api" [tool.taskipy.tasks] build = "cd front && npm install && npm run generate" -start = "uvicorn main:app --reload --host 0.0.0.0" \ No newline at end of file +start = "uvicorn main:app --reload --host 0.0.0.0 --port 8000" \ No newline at end of file