-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: ♻️ Continued
Indicator
Refactor and Creation of `Indicato…
…rResult`
- Loading branch information
Showing
6 changed files
with
129 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
"""Pydantic model that stores the Indicator results.""" | ||
|
||
from datetime import datetime | ||
from typing import Optional | ||
|
||
from pydantic import BaseModel | ||
|
||
from foresight.utils.database import TimeScaleService | ||
|
||
|
||
class IndicatorResult(BaseModel): | ||
"""Model representing a result for a given component and its value. | ||
Args: | ||
""" | ||
|
||
component_name: str | ||
time: Optional[datetime] = datetime.now() | ||
value: str | ||
|
||
@staticmethod | ||
def create_table(table_name: str = "indicator_results") -> str: | ||
"""Create a table in the data store if it does not exist. | ||
Args: | ||
table_name (str): The name of the table to create. | ||
Returns: | ||
str: The name of the table created. | ||
""" | ||
TimeScaleService().create_table( | ||
query=f""" | ||
CREATE TABLE IF NOT EXISTS {table_name} ( | ||
component_name VARCHAR(255) NOT NULL, | ||
time TIMESTAMPTZ NOT NULL, | ||
value TEXT NOT NULL, | ||
PRIMARY KEY (component_name, time) | ||
)""", | ||
table_name=table_name, | ||
column_name="time", | ||
) | ||
|
||
return table_name | ||
|
||
def insert(self, table_name: str = "indicator_results"): | ||
"""Insert indicator results into the database.""" | ||
TimeScaleService().execute( | ||
query=f"""INSERT INTO {table_name} (component_name, time, value) | ||
VALUES (%s, %s, %s)""", | ||
params=(self.component_name, self.time, self.value), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
5fc6705
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Coverage