From 3cfc77b261cce27faf0236f03bd752905485dd62 Mon Sep 17 00:00:00 2001
From: Michal Zander <mzander@syncad.com>
Date: Thu, 27 Feb 2025 13:45:44 +0000
Subject: [PATCH] Openapi rewrite

---
 .gitignore                    |   2 +-
 endpoints/endpoint_schema.sql | 138 ++++++++++++++++++++++++++++++++++
 rewrite_rules.conf            |   6 ++
 scripts/openapi_rewrite.sh    |   5 +-
 4 files changed, 149 insertions(+), 2 deletions(-)

diff --git a/.gitignore b/.gitignore
index 119fff0..16e1c9a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,7 +7,7 @@ tests/performance/.~lock.log.jtl#
 tests/account_balances/accounts_dump.json
 db/__pycache__
 server/__pycache__
-btracker_sync.log
+btracker_sync.*
 shellcheck-*.*
 **/test-results.xml
 **/test-results.jtl
diff --git a/endpoints/endpoint_schema.sql b/endpoints/endpoint_schema.sql
index 9788828..a906559 100644
--- a/endpoints/endpoint_schema.sql
+++ b/endpoints/endpoint_schema.sql
@@ -66,6 +66,14 @@ declare
   ],
   "components": {
     "schemas": {
+      "btracker_endpoints.granularity": {
+        "type": "string",
+        "enum": [
+          "daily",
+          "monthly",
+          "yearly"
+        ]
+      },
       "btracker_endpoints.nai_type": {
         "type": "string",
         "enum": [
@@ -363,6 +371,136 @@ declare
           }
         }
       }
+    },
+    "/accounts/{account-name}/aggregated-history": {
+      "get": {
+        "tags": [
+          "Accounts"
+        ],
+        "summary": "Historical balance change",
+        "description": "History of change of `coin-type` balance in given block range\n\nSQL example\n* `SELECT * FROM btracker_endpoints.get_balance_aggregation(''blocktrades'', ''VESTS'');`\n\nREST call example\n* `GET ''https://%1$s/balance-api/accounts/blocktrades/aggregated-history?coin-type=VESTS''`\n",
+        "operationId": "btracker_endpoints.get_balance_aggregation",
+        "parameters": [
+          {
+            "in": "path",
+            "name": "account-name",
+            "required": true,
+            "schema": {
+              "type": "string"
+            },
+            "description": "Name of the account"
+          },
+          {
+            "in": "query",
+            "name": "coin-type",
+            "required": true,
+            "schema": {
+              "$ref": "#/components/schemas/btracker_endpoints.nai_type"
+            },
+            "description": "Coin types:\n\n* HBD\n\n* HIVE\n\n* VESTS\n"
+          },
+          {
+            "in": "query",
+            "name": "granularity",
+            "required": false,
+            "schema": {
+              "$ref": "#/components/schemas/btracker_endpoints.granularity",
+              "default": "yearly"
+            },
+            "description": "granularity types:\n\n* daily\n\n* monthly\n\n* yearly\n"
+          },
+          {
+            "in": "query",
+            "name": "direction",
+            "required": false,
+            "schema": {
+              "$ref": "#/components/schemas/btracker_endpoints.sort_direction",
+              "default": "desc"
+            },
+            "description": "Sort order:\n\n * `asc` - Ascending, from oldest to newest \n\n * `desc` - Descending, from newest to oldest \n"
+          },
+          {
+            "in": "query",
+            "name": "from-block",
+            "required": false,
+            "schema": {
+              "type": "string",
+              "default": null
+            },
+            "description": "Lower limit of the block range, can be represented either by a block-number (integer) or a timestamp (in the format YYYY-MM-DD HH:MI:SS).\n\nThe provided `timestamp` will be converted to a `block-num` by finding the first block \nwhere the block''s `created_at` is more than or equal to the given `timestamp` (i.e. `block''s created_at >= timestamp`).\n\nThe function will interpret and convert the input based on its format, example input:\n\n* `2016-09-15 19:47:21`\n\n* `5000000`\n"
+          },
+          {
+            "in": "query",
+            "name": "to-block",
+            "required": false,
+            "schema": {
+              "type": "string",
+              "default": null
+            },
+            "description": "Similar to the from-block parameter, can either be a block-number (integer) or a timestamp (formatted as YYYY-MM-DD HH:MI:SS). \n\nThe provided `timestamp` will be converted to a `block-num` by finding the first block \nwhere the block''s `created_at` is less than or equal to the given `timestamp` (i.e. `block''s created_at <= timestamp`).\n\nThe function will convert the value depending on its format, example input:\n\n* `2016-09-15 19:47:21`\n\n* `5000000`\n"
+          }
+        ],
+        "responses": {
+          "200": {
+            "description": "Balance change\n",
+            "content": {
+              "application/json": {
+                "schema": {
+                  "type": "string",
+                  "x-sql-datatype": "JSON"
+                },
+                "example": [
+                  {
+                    "date": "2016-12-31T23:59:59",
+                    "balance": "8172549681941451",
+                    "block": 4999992
+                  }
+                ]
+              }
+            }
+          },
+          "404": {
+            "description": "No such account in the database"
+          }
+        }
+      }
+    },
+    "/accounts/{account-name}/delegations": {
+      "get": {
+        "tags": [
+          "Accounts"
+        ],
+        "summary": "Historical balance change",
+        "description": "History of change of `coin-type` balance in given block range\n\nSQL example\n* `SELECT * FROM btracker_endpoints.get_balance_delegations(''blocktrades'');`\n\nREST call example\n* `GET ''https://%1$s/balance-api/accounts/blocktrades/delegations''`\n",
+        "operationId": "btracker_endpoints.get_balance_delegations",
+        "parameters": [
+          {
+            "in": "path",
+            "name": "account-name",
+            "required": true,
+            "schema": {
+              "type": "string"
+            },
+            "description": "Name of the account"
+          }
+        ],
+        "responses": {
+          "200": {
+            "description": "Balance change\n",
+            "content": {
+              "application/json": {
+                "schema": {
+                  "type": "string",
+                  "x-sql-datatype": "JSON"
+                }
+              }
+            }
+          },
+          "404": {
+            "description": "No such account in the database"
+          }
+        }
+      }
     }
   }
 }
diff --git a/rewrite_rules.conf b/rewrite_rules.conf
index ff134bf..2657745 100644
--- a/rewrite_rules.conf
+++ b/rewrite_rules.conf
@@ -1,3 +1,9 @@
+rewrite ^/accounts/([^/]+)/delegations /rpc/get_balance_delegations?account-name=$1 break;
+# endpoint for get /accounts/{account-name}/delegations
+
+rewrite ^/accounts/([^/]+)/aggregated-history /rpc/get_balance_aggregation?account-name=$1 break;
+# endpoint for get /accounts/{account-name}/aggregated-history
+
 rewrite ^/accounts/([^/]+)/balance-history /rpc/get_balance_history?account-name=$1 break;
 # endpoint for get /accounts/{account-name}/balance-history
 
diff --git a/scripts/openapi_rewrite.sh b/scripts/openapi_rewrite.sh
index a0108b5..399762a 100755
--- a/scripts/openapi_rewrite.sh
+++ b/scripts/openapi_rewrite.sh
@@ -15,10 +15,13 @@ temp_output_file=$(mktemp)
 OUTPUT="$SCRIPTDIR/output"
 ENDPOINTS_IN_ORDER="
 ../$endpoints/endpoint_schema.sql
+../$endpoints/types/granularity.sql
 ../$endpoints/types/coin_type.sql
 ../$endpoints/types/sort_direction.sql
 ../$endpoints/account-balances/get_account_balances.sql
-../$endpoints/account-balances/get_balance_history.sql"
+../$endpoints/account-balances/get_balance_history.sql
+../$endpoints/account-balances/get_history_aggregation.sql
+../$endpoints/account-balances/get_account_delegations.sql"
 
 # Function to reverse the lines
 reverse_lines() {
-- 
GitLab