From 36cfccb103e8919e02e81efc55ad4f1e4a0c0c14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ho=C3=A0ng=20Rio?= Date: Thu, 5 Dec 2024 15:57:26 +0700 Subject: [PATCH] [skip ci] change ci fetch-depth to 10 instead of all with 0 --- .github/workflows/deploy_with_git.yml | 2 +- web_viewer/__init__.py | 30 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/.github/workflows/deploy_with_git.yml b/.github/workflows/deploy_with_git.yml index c99a49c..0226405 100644 --- a/.github/workflows/deploy_with_git.yml +++ b/.github/workflows/deploy_with_git.yml @@ -19,7 +19,7 @@ jobs: steps: - uses: actions/checkout@v3 with: - fetch-depth: 0 + fetch-depth: 10 - name: Create deploy key file run: | mkdir ${{env.SSH_KEY_FILE_DIR}} diff --git a/web_viewer/__init__.py b/web_viewer/__init__.py index 9a921af..b380661 100644 --- a/web_viewer/__init__.py +++ b/web_viewer/__init__.py @@ -115,6 +115,34 @@ def daily_chart(_: web.Request): res = web.json_response(daily_chart, headers=VITE_CORS_HEADER) return res +def monthly_chart(_: web.Request): + if "DB_NAME" not in config: + return web.json_response([], headers=VITE_CORS_HEADER) + global db_connection + if db_connection is None: + db_connection = sqlite3.connect(config["DB_NAME"]) + cursor = db_connection.cursor() + now = datetime.now() + monthly_chart = cursor.execute( + "SELECT id, id, month || '/' || year as month, SUM(pv), SUM(battery_charged), SUM(battery_discharged), SUM(grid_import), SUM(grid_export), SUM(consumption) FROM daily_chart WHERE year = ? GROUP BY month", + (now.year,) + ).fetchall() + res = web.json_response(monthly_chart, headers=VITE_CORS_HEADER) + return res + +def yearly_chart(_: web.Request): + if "DB_NAME" not in config: + return web.json_response([], headers=VITE_CORS_HEADER) + global db_connection + if db_connection is None: + db_connection = sqlite3.connect(config["DB_NAME"]) + cursor = db_connection.cursor() + yearly_chart = cursor.execute( + "SELECT id, id, year, SUM(pv), SUM(battery_charged), SUM(battery_discharged), SUM(grid_import), SUM(grid_export), SUM(consumption) FROM daily_chart GROUP BY year" + ).fetchall() + res = web.json_response(yearly_chart, headers=VITE_CORS_HEADER) + return res + def create_runner(): app = web.Application() @@ -124,6 +152,8 @@ def create_runner(): web.get("/state", state), web.get("/hourly-chart", hourly_chart), web.get("/daily-chart", daily_chart), + web.get("/monthly-chart", monthly_chart), + web.get("/yearly-chart", yearly_chart), web.get("/total", total), web.static("/", path.join(path.dirname(__file__), "public")) ])