From 6827592c1e73737b294e453e33b0440c7021600c Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Tue, 21 Nov 2023 12:38:48 +0000 Subject: [PATCH] Add changes for 0c43f157d0c09837b075ea8f1348daf7b015180a --- .buildinfo | 2 +- _modules/index.html | 13 +- .../analytics_helper.html | 6 +- .../pyprediktormapclient/auth_client.html | 6 +- _modules/pyprediktormapclient/dwh.html | 320 ------------- .../dwh/context/enercast.html | 148 ++++++ .../dwh/context/plant.html | 165 +++++++ .../dwh/context/solcast.html | 150 ++++++ _modules/pyprediktormapclient/dwh/db.html | 383 ++++++++++++++++ _modules/pyprediktormapclient/dwh/dwh.html | 216 +++++++++ _modules/pyprediktormapclient/dwh/idwh.html | 136 ++++++ .../pyprediktormapclient/model_index.html | 6 +- _modules/pyprediktormapclient/opc_ua.html | 6 +- _modules/pyprediktormapclient/shared.html | 6 +- .../pyprediktormapclient.dwh.context.rst.txt | 37 ++ _sources/api/pyprediktormapclient.dwh.rst.txt | 45 ++ _sources/api/pyprediktormapclient.rst.txt | 16 +- _static/documentation_options.js | 2 +- api/modules.html | 34 +- api/pyprediktormapclient.dwh.context.html | 206 +++++++++ api/pyprediktormapclient.dwh.html | 391 ++++++++++++++++ api/pyprediktormapclient.html | 431 +++++++----------- authors.html | 6 +- contributing.html | 6 +- dwh.html | 6 +- genindex.html | 172 +++++-- index.html | 6 +- license.html | 6 +- objects.inv | Bin 1448 -> 1654 bytes py-modindex.html | 43 +- readme.html | 6 +- search.html | 6 +- searchindex.js | 2 +- 33 files changed, 2292 insertions(+), 692 deletions(-) delete mode 100644 _modules/pyprediktormapclient/dwh.html create mode 100644 _modules/pyprediktormapclient/dwh/context/enercast.html create mode 100644 _modules/pyprediktormapclient/dwh/context/plant.html create mode 100644 _modules/pyprediktormapclient/dwh/context/solcast.html create mode 100644 _modules/pyprediktormapclient/dwh/db.html create mode 100644 _modules/pyprediktormapclient/dwh/dwh.html create mode 100644 _modules/pyprediktormapclient/dwh/idwh.html create mode 100644 _sources/api/pyprediktormapclient.dwh.context.rst.txt create mode 100644 _sources/api/pyprediktormapclient.dwh.rst.txt create mode 100644 api/pyprediktormapclient.dwh.context.html create mode 100644 api/pyprediktormapclient.dwh.html diff --git a/.buildinfo b/.buildinfo index a1c12ce..4cca022 100644 --- a/.buildinfo +++ b/.buildinfo @@ -1,4 +1,4 @@ # Sphinx build info version 1 # This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done. -config: a393987fd073ffbfb2537cf938468151 +config: a36fe89aa80c3ded61c7005b168d06a3 tags: 645f666f9bcd5a90fca523b33c5a78b7 diff --git a/_modules/index.html b/_modules/index.html index 046924a..196ef3e 100644 --- a/_modules/index.html +++ b/_modules/index.html @@ -3,7 +3,7 @@ - Overview: module code — pyPrediktorMapClient 0.6.1 documentation + Overview: module code — pyPrediktorMapClient 0.6.2 documentation - - - - - - - - - - - - -
- - -
- -
-
-
- -
-
-
-
- -

Source code for pyprediktormapclient.dwh

-from pydantic import validate_call
-import pyodbc
-import pandas as pd
-import logging
-from typing import List, Any
-
-logger = logging.getLogger(__name__)
-logger.addHandler(logging.NullHandler())
-
-
-[docs] -class DWH: - """Helper functions to access a PowerView Data Warehouse or other SQL databases. - This class is a wrapper around pyodbc and you can use all pyodbc methods as well - as the provided methods. Look at the pyodbc documentation and use the cursor - attribute to access the pyodbc cursor. - - Args: - url (str): The URL of the sql server - database (str): The name of the database - username (str): The username - password (str): The password - - Attributes: - connection (pyodbc.Connection): The connection object - cursor (pyodbc.Cursor): The cursor object - - Examples: - >>> from pyprediktormapclient.dwh import DWH - >>> dwh = DWH("localhost", "mydatabase", "myusername", "mypassword") - >>> dwh.read("SELECT * FROM mytable") - >>> dwh.write("INSERT INTO mytable VALUES (1, 'test')") - >>> dwh.commit() # Or commit=True in the write method - >>> # You can also use the cursor directly - >>> dwh.cursor.execute("SELECT * FROM mytable") - """ - - @validate_call - def __init__(self, url: str, database: str, username: str, password: str, driver: int = 0) -> None: - """Class initializer - - Args: - url (str): The URL of the sql server - database (str): The name of the database - username (str): The username - password (str): The password - """ - self.url = url - self.database = database - self.username = username - self.password = password - self.driver = None - self.__set_driver(driver) - self.connectionstr = f"DRIVER={self.driver};SERVER={self.url};DATABASE={self.database};UID={self.username};PWD={self.password}" - self.connection = None - self.cursor = None - - self.connect() - - def __enter__(self): - return self - - def __exit__(self, exc_type, exc_val, exc_tb): - if self.connection is not None: - self.disconnect() - - def __set_driver(self, driver: int): - """Sets the driver to use for the connection. Private function for internal use. - - Args: - driver (int): The index of the driver to use. - """ - driverlist = self.list_drivers() - if len(driverlist) < (driver + 1): - raise ValueError(f"Driver index {driver} is out of range. Please use the list_drivers() method to list all available drivers.") - - self.driver = pyodbc.drivers()[driver] - -
-[docs] - def list_drivers(self): - """Lists all available drivers for pyodbc.""" - return pyodbc.drivers()
- - -
-[docs] - def connect(self): - """Establishes a connection to the database.""" - if self.connection: - return - - logging.info("Initiating connection to the database...") - try: - self.connection = pyodbc.connect(self.connectionstr) - self.cursor = self.connection.cursor() - logging.info("Connection successfull...") - except pyodbc.OperationalError as err: - logger.error(f"Operational Error {err.args[0]}: {err.args[1]}") - logger.warning(f"Pyodbc is having issues with the connection. This could be due to the wrong driver being used. Please check your driver with the list_drivers() method and try again.") - raise - except pyodbc.DataError as err: - logger.error(f"Data Error {err.args[0]}: {err.args[1]}") - raise - except pyodbc.IntegrityError as err: - logger.error(f"Integrity Error {err.args[0]}: {err.args[1]}") - raise - except pyodbc.ProgrammingError as err: - logger.error(f"Programming Error {err.args[0]}: {err.args[1]}") - logger.warning(f"There seems to be a problem with your code. Please check your code and try again.") - raise - except pyodbc.NotSupportedError as err: - logger.error(f"Not supported {err.args[0]}: {err.args[1]}") - raise - except pyodbc.DatabaseError as err: - logger.error(f"Database Error {err.args[0]}: {err.args[1]}") - raise - except pyodbc.Error as err: - logger.error(f"Generic Error {err.args[0]}: {err.args[1]}") - raise
- - -
-[docs] - def disconnect(self): - """Closes the connection to the database.""" - if self.connection: - self.connection.close() - self.connection = None - self.cursor = None
- - -
-[docs] - def read(self, sql: str) -> List[Any]: - """Executes a SQL query and returns the results. - - Args: - sql (str): The SQL query to execute. - - Returns: - List[Any]: The results of the query. - """ - self.connect() - self.cursor.execute(sql) - return self.cursor.fetchall()
- - -
-[docs] - def write(self, sql: str, commit: bool = False) -> List[Any]: - """Executes a SQL query and returns the results. - - Args: - sql (str): The SQL query to execute. - commit (bool): Whether to commit the changes to the database. - - Returns: - List[Any]: The results of the query. - """ - self.connect() - self.cursor.execute(sql) - result = self.cursor.fetchall() - if commit: self.commit() - return result
- - -
-[docs] - def executemany(self, sql: str, params: List[Any], commit: bool = False) -> List[Any]: - """Executes a SQL query against all parameters or mappings and returns the results. - - Args: - sql (str): The SQL query to execute. - params (List[Any]): The parameters or mappings to use. - commit (bool): Whether to commit the changes to the database. - - Returns: - List[Any]: The results of the query. - """ - self.connect() - self.cursor.executemany(sql) - result = self.cursor.fetchall() - if commit: self.commit() - return result
- - -
-[docs] - def read_to_dataframe(self, sql: str) -> pd.DataFrame: - """Executes a SQL query and returns the results as a DataFrame. - - Args: - sql (str): The SQL query to execute. - - Returns: - pd.DataFrame: The results of the query. - - """ - self.connect() - return pd.read_sql(sql, self.connection)
- - -
-[docs] - def commit(self): - """Commits any changes to the database.""" - self.connection.commit()
-
- - - -
- -
-
-
- -
- -
-

© Copyright 2022, Prediktor Support.

-
- - Built with Sphinx using a - theme - provided by Read the Docs. - - -
-
-
-
-
- - - - \ No newline at end of file diff --git a/_modules/pyprediktormapclient/dwh/context/enercast.html b/_modules/pyprediktormapclient/dwh/context/enercast.html new file mode 100644 index 0000000..3f367f0 --- /dev/null +++ b/_modules/pyprediktormapclient/dwh/context/enercast.html @@ -0,0 +1,148 @@ + + + + + + pyprediktormapclient.dwh.context.enercast — pyPrediktorMapClient 0.6.2 documentation + + + + + + + + + + + + + + + +
+ + +
+ +
+
+
+
    +
  • + + +
  • +
  • +
+
+
+
+
+ +

Source code for pyprediktormapclient.dwh.context.enercast

+import json
+from pydantic import validate_call
+from typing import List, Dict, Any, Union
+
+from ..idwh import IDWH
+
+
+
+[docs] +class Enercast: + def __init__(self, dwh: IDWH) -> None: + self.dwh = dwh + +
+[docs] + @validate_call + def get_plants_to_update(self) -> List[Any]: + query = "SET NOCOUNT ON; EXEC dwetl.GetEnercastPlantsToUpdate" + return self.dwh.fetch(query)
+ + +
+[docs] + @validate_call + def get_live_meter_data(self, asset_name: str) -> List[Any]: + query = f"SET NOCOUNT ON; EXEC dwetl.GetEnercastLiveMeterData '{asset_name}'" + return self.dwh.fetch(query)
+ + +
+[docs] + @validate_call + def upsert_forecast_data( + self, enercast_forecast_data: Dict, forecast_type_key: Union[int, None] = None + ) -> List[Any]: + enercast_forecast_data_json = json.dumps({"results": enercast_forecast_data}) + query = f"EXEC dwetl.UpsertEnercastForecastData {enercast_forecast_data_json}, {forecast_type_key}" + return self.dwh.execute(query)
+
+ +
+ +
+
+
+ +
+ +
+

© Copyright 2022, Prediktor Support.

+
+ + Built with Sphinx using a + theme + provided by Read the Docs. + + +
+
+
+
+
+ + + + \ No newline at end of file diff --git a/_modules/pyprediktormapclient/dwh/context/plant.html b/_modules/pyprediktormapclient/dwh/context/plant.html new file mode 100644 index 0000000..161531b --- /dev/null +++ b/_modules/pyprediktormapclient/dwh/context/plant.html @@ -0,0 +1,165 @@ + + + + + + pyprediktormapclient.dwh.context.plant — pyPrediktorMapClient 0.6.2 documentation + + + + + + + + + + + + + + + +
+ + +
+ +
+
+
+
    +
  • + + +
  • +
  • +
+
+
+
+
+ +

Source code for pyprediktormapclient.dwh.context.plant

+import json
+from pydantic import validate_call
+from typing import List, Dict, Any
+
+from ..idwh import IDWH
+
+
+
+[docs] +class Plant: + def __init__(self, dwh: IDWH) -> None: + self.dwh = dwh + +
+[docs] + @validate_call + def get_optimal_tracker_angles(self, facility_name: str) -> List[Any]: + query = ( + f"SET NOCOUNT ON; EXEC dwetl.GetOptimalTrackerAngleParameters " + + f"@FacilityName = N'{facility_name}'" + ) + return self.dwh.fetch(query)
+ + +
+[docs] + @validate_call + def upsert_optimal_tracker_angles(self, facility_data: Dict) -> List[Any]: + facility_data_json = json.dumps(facility_data) + facility_data_json.replace("'", '"') + query = f"EXEC dwetl.UpsertOptimalTrackerAngles @json = {facility_data_json}" + return self.dwh.execute(query)
+ + +
+[docs] + @validate_call + def insert_log( + self, + plantname: str, + ext_forecast_type_key: int, + data_type: str, + has_thrown_error: bool = False, + message: str = "", + ) -> List[Any]: + result = "ERROR" if has_thrown_error else "OK" + query = ( + f"EXEC dwetl.InsertExtDataUpdateLog " + + f"@plantname = {plantname}, " + + f"@extkey = {ext_forecast_type_key}, " + + f"@DataType = {data_type}, " + + f"@Message = {message}, " + + f"@Result = {result}" + ) + return self.dwh.execute(query)
+
+ +
+ +
+
+
+ +
+ +
+

© Copyright 2022, Prediktor Support.

+
+ + Built with Sphinx using a + theme + provided by Read the Docs. + + +
+
+
+
+
+ + + + \ No newline at end of file diff --git a/_modules/pyprediktormapclient/dwh/context/solcast.html b/_modules/pyprediktormapclient/dwh/context/solcast.html new file mode 100644 index 0000000..5a37320 --- /dev/null +++ b/_modules/pyprediktormapclient/dwh/context/solcast.html @@ -0,0 +1,150 @@ + + + + + + pyprediktormapclient.dwh.context.solcast — pyPrediktorMapClient 0.6.2 documentation + + + + + + + + + + + + + + + +
+ + +
+ +
+
+
+
    +
  • + + +
  • +
  • +
+
+
+
+
+ +

Source code for pyprediktormapclient.dwh.context.solcast

+import json
+from pydantic import validate_call
+from typing import List, Dict, Any, Union
+
+from ..idwh import IDWH
+
+
+
+[docs] +class Solcast: + def __init__(self, dwh: IDWH) -> None: + self.dwh = dwh + +
+[docs] + @validate_call + def get_plants_to_update(self) -> List[Any]: + query = "SET NOCOUNT ON; EXEC dwetl.GetSolcastPlantsToUpdate" + return self.dwh.fetch(query)
+ + +
+[docs] + @validate_call + def upsert_forecast_data( + self, + plantname: str, + solcast_forecast_data: Dict, + forecast_type_key: Union[int, None] = None, + ) -> List[Any]: + solcast_forecast_data_json = json.dumps( + { + "results": { + "plantname": plantname, + "values": solcast_forecast_data["forecasts"], + } + } + ) + query = f"EXEC dwetl.UpsertSolcastForecastData {solcast_forecast_data_json}, {forecast_type_key}" + return self.dwh.execute(query)
+
+ +
+ +
+
+
+ +
+ +
+

© Copyright 2022, Prediktor Support.

+
+ + Built with Sphinx using a + theme + provided by Read the Docs. + + +
+
+
+
+
+ + + + \ No newline at end of file diff --git a/_modules/pyprediktormapclient/dwh/db.html b/_modules/pyprediktormapclient/dwh/db.html new file mode 100644 index 0000000..18a7ef9 --- /dev/null +++ b/_modules/pyprediktormapclient/dwh/db.html @@ -0,0 +1,383 @@ + + + + + + pyprediktormapclient.dwh.db — pyPrediktorMapClient 0.6.2 documentation + + + + + + + + + + + + + + + +
+ + +
+ +
+
+
+ +
+
+
+
+ +

Source code for pyprediktormapclient.dwh.db

+import pyodbc
+import logging
+import pandas as pd
+from typing import List, Any
+from pydantic import validate_call
+
+logger = logging.getLogger(__name__)
+logger.addHandler(logging.NullHandler())
+
+
+
+[docs] +class Db: + """Access a PowerView Data Warehouse or other SQL databases. + + Args: + url (str): The URL of the sql server + database (str): The name of the database + username (str): The username + password (str): The password + + Attributes: + connection (pyodbc.Connection): The connection object + cursor (pyodbc.Cursor): The cursor object + """ + + @validate_call + def __init__( + self, url: str, database: str, username: str, password: str, driver: int = 0 + ) -> None: + """Class initializer. + + Args: + url (str): The URL of the sql server + database (str): The name of the database + username (str): The username + password (str): The password + """ + self.__set_driver(driver) + + self.url = url + self.cursor = None + self.database = database + self.username = username + self.password = password + self.connection = None + self.connection_string = ( + f"UID={self.username};" + + f"PWD={self.password};" + + f"DRIVER={self.driver};" + + f"SERVER={self.url};" + + f"DATABASE={self.database};" + ) + self.connection_attempts = 3 + + self.__connect() + + def __enter__(self): + return self + + @validate_call + def __exit__(self, exc_type, exc_val, exc_tb): + if self.connection is not None: + self.__disconnect() + + """ + Public + """ + +
+[docs] + @validate_call + def fetch(self, query: str, to_dataframe: bool = False) -> List[Any]: + """Execute the SQL query to get results from DWH and return the data. + + Use that method for getting data. That means that if you use SELECT or + you'd like to call a stored procedure that returns one or more sets + of data, that is the correct method to use. + + Use that method to GET. + + Args: + query (str): The SQL query to execute. + to_dataframe (bool): If True, return the results as a list + of DataFrames. + + Returns: + List[Any]: The results of the query. If DWH returns multiple + data sets, this method is going to return a list + of result sets (lists). If DWH returns a single data set, + the method is going to return a list representing the single + result set. + + If to_dataframe is True, the data inside each data set + is going to be in DataFrame format. + """ + self.__connect() + self.cursor.execute(query) + + data_sets = [] + while True: + data_set = [] + + columns = [col[0] for col in self.cursor.description] + for row in self.cursor.fetchall(): + data_set.append( + {name: row[index] for index, name in enumerate(columns)} + ) + + data_sets.append(pd.DataFrame(data_set) if to_dataframe else data_set) + + if not self.cursor.nextset(): + break + + return data_sets if len(data_sets) > 1 else data_sets[0]
+ + +
+[docs] + @validate_call + def execute(self, query: str, commit: bool = True) -> List[Any]: + """Execute the SQL query and return the results. + + For instance, if we create a new record in DWH by calling + a stored procedure returning the id of the inserted element or + in our query we use `SELECT SCOPE_IDENTITY() AS LastInsertedId;`, + the DWH is going to return data after executing our write request. + + Please note that here we expect a single result set. Therefore DWH + is obligated to return only one data set and also we're obligated to + construct our query according to this requirement. + + Use that method to CREATE, UPDATE, DELETE or execute business logic. + To NOT use for GET. + + Args: + query (str): The SQL query to execute. + commit (bool): If True, commit the changes to the database. + + Returns: + List[Any]: The results of the query. + """ + self.__connect() + self.cursor.execute(query) + + result = self.cursor.fetchall() + if commit: + self.__commit() + + return result
+ + + """ + Private - Driver + """ + + @validate_call + def __set_driver(self, driver_index: int) -> None: + """Sets the driver to use for the connection to the database. + + Args: + driver (int): The index of the driver to use. + """ + if self.__get_number_of_available_pyodbc_drivers() < (driver_index + 1): + raise ValueError( + f"Driver index {driver_index} is out of range. Please use " + + f"the __get_list_of_available_pyodbc_drivers() method " + + f"to list all available drivers." + ) + + self.driver = self.__get_list_of_available_pyodbc_drivers()[driver_index] + + @validate_call + def __get_number_of_available_pyodbc_drivers(self) -> int: + return len(self.__get_list_of_available_pyodbc_drivers()) + + @validate_call + def __get_list_of_available_pyodbc_drivers(self) -> List[Any]: + return pyodbc.drivers() + + """ + Private - Connector & Disconnector + """ + + @validate_call + def __connect(self) -> None: + """Establishes a connection to the database.""" + if self.connection: + return + + logging.info("Initiating connection to the database...") + + attempt = 0 + while attempt < self.connection_attempts: + try: + self.connection = pyodbc.connect(self.connection_string) + self.cursor = self.connection.cursor() + logging.info("Connection successfull!") + break + + # Exceptions once thrown there is no point attempting + except pyodbc.DataError as err: + logger.error(f"Data Error {err.args[0]}: {err.args[1]}") + raise + except pyodbc.IntegrityError as err: + logger.error(f"Integrity Error {err.args[0]}: {err.args[1]}") + raise + except pyodbc.ProgrammingError as err: + logger.error(f"Programming Error {err.args[0]}: {err.args[1]}") + logger.warning( + f"There seems to be a problem with your code. Please " + + f"check your code and try again." + ) + raise + except pyodbc.NotSupportedError as err: + logger.error(f"Not supported {err.args[0]}: {err.args[1]}") + raise + + # Exceptions when thrown we can continue attempting + except pyodbc.OperationalError as err: + logger.error(f"Operational Error {err.args[0]}: {err.args[1]}") + logger.warning( + f"Pyodbc is having issues with the connection. This " + + f"could be due to the wrong driver being used. Please " + + f"check your driver with " + + f"the __get_list_of_available_pyodbc_drivers() method " + + f"and try again." + ) + + attempt += 1 + if self.__are_connection_attempts_reached(attempt): + raise + except pyodbc.DatabaseError as err: + logger.error(f"Database Error {err.args[0]}: {err.args[1]}") + + attempt += 1 + if self.__are_connection_attempts_reached(attempt): + raise + except pyodbc.Error as err: + logger.error(f"Generic Error {err.args[0]}: {err.args[1]}") + + attempt += 1 + if self.__are_connection_attempts_reached(attempt): + raise + + @validate_call + def __are_connection_attempts_reached(self, attempt) -> bool: + if attempt != self.connection_attempts: + logger.warning("Retrying connection...") + return False + + logger.error( + f"Failed to connect to the DataWarehouse after " + + f"{self.connection_attempts} attempts." + ) + return True + + @validate_call + def __disconnect(self) -> None: + """Closes the connection to the database.""" + if self.connection: + self.connection.close() + + self.cursor = None + self.connection = None + + """ + Private - Low level database operations + """ + + @validate_call + def __commit(self) -> None: + """Commits any changes to the database.""" + self.connection.commit()
+ +
+ +
+
+
+ +
+ +
+

© Copyright 2022, Prediktor Support.

+
+ + Built with Sphinx using a + theme + provided by Read the Docs. + + +
+
+
+
+
+ + + + \ No newline at end of file diff --git a/_modules/pyprediktormapclient/dwh/dwh.html b/_modules/pyprediktormapclient/dwh/dwh.html new file mode 100644 index 0000000..a9269f9 --- /dev/null +++ b/_modules/pyprediktormapclient/dwh/dwh.html @@ -0,0 +1,216 @@ + + + + + + pyprediktormapclient.dwh.dwh — pyPrediktorMapClient 0.6.2 documentation + + + + + + + + + + + + + + + +
+ + +
+ +
+
+
+ +
+
+
+
+ +

Source code for pyprediktormapclient.dwh.dwh

+import pkgutil
+import logging
+import importlib
+from typing import Dict
+from pydantic import validate_call
+
+from .db import Db
+from . import context
+from .idwh import IDWH
+
+logger = logging.getLogger(__name__)
+logger.addHandler(logging.NullHandler())
+
+
+
+[docs] +class DWH(Db, IDWH): + """Helper functions to access a PowerView Data Warehouse or other + SQL databases. This class is a wrapper around pyodbc and you can use + all pyodbc methods as well as the provided methods. Look at the pyodbc + documentation and use the cursor attribute to access the pyodbc cursor. + + Args: + url (str): The URL of the sql server + database (str): The name of the database + username (str): The username + password (str): The password + + Attributes: + connection (pyodbc.Connection): The connection object + cursor (pyodbc.Cursor): The cursor object + + Examples - of low level usage: + >>> from pyprediktormapclient.dwh import DWH + >>> + >>> dwh = DWH("localhost", "mydatabase", "myusername", "mypassword") + >>> + >>> dwh.fetch("SELECT * FROM mytable") + >>> + >>> dwh.execute("INSERT INTO mytable VALUES (1, 'test')") + + Examples - of high level usage: + >>> from pyprediktormapclient.dwh import DWH + >>> + >>> dwh = DWH("localhost", "mydatabase", "myusername", "mypassword") + >>> + >>> database_version = dwh.version() + >>> + >>> enercast_plants = dwh.enercast.get_plants_to_update() + """ + + @validate_call + def __init__( + self, url: str, database: str, username: str, password: str, driver: int = 0 + ) -> None: + super().__init__(url, database, username, password, driver) + self.__initialize_context_services() + + """ + Public + """ + +
+[docs] + @validate_call + def version(self) -> Dict: + """Get the DWH version. + + Returns: + Dict: A dictionary with the following keys (or similar): + DWHVersion, + UpdateDate, + ImplementedDate, + Comment, + MajorVersionNo, + MinorVersionNo, + InterimVersionNo + """ + query = "SET NOCOUNT ON; EXEC [dbo].[GetVersion]" + results = self.fetch(query) + return results[0] if len(results) > 0 else {}
+ + + """ + Private + """ + + @validate_call + def __initialize_context_services(self) -> None: + """ + Initialise all services defined in `context` folder. These are methods + used to directly call certain stored procedures. For instance, class + Enercast contains calls to stored procedures directly related to + Enercast. + """ + package = context + prefix = package.__name__ + "." + for _, modname, ispkg in pkgutil.iter_modules(package.__path__, prefix): + if not ispkg: + module = importlib.import_module(modname) + + for attribute_name in dir(module): + attribute = getattr(module, attribute_name) + + if isinstance(attribute, type) and attribute is not IDWH: + service_name = modname.split(".")[-1] + setattr(self, service_name, attribute(self))
+ +
+ +
+
+
+ +
+ +
+

© Copyright 2022, Prediktor Support.

+
+ + Built with Sphinx using a + theme + provided by Read the Docs. + + +
+
+
+
+
+ + + + \ No newline at end of file diff --git a/_modules/pyprediktormapclient/dwh/idwh.html b/_modules/pyprediktormapclient/dwh/idwh.html new file mode 100644 index 0000000..4e32b01 --- /dev/null +++ b/_modules/pyprediktormapclient/dwh/idwh.html @@ -0,0 +1,136 @@ + + + + + + pyprediktormapclient.dwh.idwh — pyPrediktorMapClient 0.6.2 documentation + + + + + + + + + + + + + + + +
+ + +
+ +
+
+
+
    +
  • + + +
  • +
  • +
+
+
+
+
+ +

Source code for pyprediktormapclient.dwh.idwh

+from typing import Dict, List, Any
+from abc import ABC, abstractmethod
+
+
+
+[docs] +class IDWH(ABC): +
+[docs] + @abstractmethod + def version(self) -> Dict: + pass
+ + +
+[docs] + @abstractmethod + def fetch(self, query: str, to_dataframe: bool = False) -> List[Any]: + pass
+ + +
+[docs] + @abstractmethod + def execute(self, query: str, commit: bool = True) -> List[Any]: + pass
+
+ +
+ +
+
+
+ +
+ +
+

© Copyright 2022, Prediktor Support.

+
+ + Built with Sphinx using a + theme + provided by Read the Docs. + + +
+
+
+
+
+ + + + \ No newline at end of file diff --git a/_modules/pyprediktormapclient/model_index.html b/_modules/pyprediktormapclient/model_index.html index f390e48..a3f9181 100644 --- a/_modules/pyprediktormapclient/model_index.html +++ b/_modules/pyprediktormapclient/model_index.html @@ -3,7 +3,7 @@ - pyprediktormapclient.model_index — pyPrediktorMapClient 0.6.1 documentation + pyprediktormapclient.model_index — pyPrediktorMapClient 0.6.2 documentation + + + + + + + + + + + + + +
+ + +
+ +
+
+
+ +
+
+
+
+ +
+

pyprediktormapclient.dwh.context package

+
+

Submodules

+
+
+

pyprediktormapclient.dwh.context.enercast module

+
+
+class pyprediktormapclient.dwh.context.enercast.Enercast(dwh: IDWH)[source]
+

Bases: object

+
+
+get_live_meter_data[source]
+
+ +
+
+get_plants_to_update[source]
+
+ +
+
+upsert_forecast_data[source]
+
+ +
+ +
+
+

pyprediktormapclient.dwh.context.plant module

+
+
+class pyprediktormapclient.dwh.context.plant.Plant(dwh: IDWH)[source]
+

Bases: object

+
+
+get_optimal_tracker_angles[source]
+
+ +
+
+insert_log[source]
+
+ +
+
+upsert_optimal_tracker_angles[source]
+
+ +
+ +
+
+

pyprediktormapclient.dwh.context.solcast module

+
+
+class pyprediktormapclient.dwh.context.solcast.Solcast(dwh: IDWH)[source]
+

Bases: object

+
+
+get_plants_to_update[source]
+
+ +
+
+upsert_forecast_data[source]
+
+ +
+ +
+
+

Module contents

+
+
+ + +
+
+ +
+
+
+
+ + + + \ No newline at end of file diff --git a/api/pyprediktormapclient.dwh.html b/api/pyprediktormapclient.dwh.html new file mode 100644 index 0000000..d24cf96 --- /dev/null +++ b/api/pyprediktormapclient.dwh.html @@ -0,0 +1,391 @@ + + + + + + + pyprediktormapclient.dwh package — pyPrediktorMapClient 0.6.2 documentation + + + + + + + + + + + + + + + + + +
+ + +
+ +
+
+
+ +
+
+
+
+ +
+

pyprediktormapclient.dwh package

+
+

Subpackages

+ +
+
+

Submodules

+
+
+

pyprediktormapclient.dwh.db module

+
+
+class pyprediktormapclient.dwh.db.Db(url: str, database: str, username: str, password: str, driver: int = 0)[source]
+

Bases: object

+

Access a PowerView Data Warehouse or other SQL databases.

+
+
Parameters:
+
    +
  • url (str) – The URL of the sql server

  • +
  • database (str) – The name of the database

  • +
  • username (str) – The username

  • +
  • password (str) – The password

  • +
+
+
+
+
+connection
+

The connection object

+
+
Type:
+

pyodbc.Connection

+
+
+
+ +
+
+cursor
+

The cursor object

+
+
Type:
+

pyodbc.Cursor

+
+
+
+ +
+
+execute[source]
+

Execute the SQL query and return the results.

+

For instance, if we create a new record in DWH by calling +a stored procedure returning the id of the inserted element or +in our query we use SELECT SCOPE_IDENTITY() AS LastInsertedId;, +the DWH is going to return data after executing our write request.

+

Please note that here we expect a single result set. Therefore DWH +is obligated to return only one data set and also we’re obligated to +construct our query according to this requirement.

+

Use that method to CREATE, UPDATE, DELETE or execute business logic. +To NOT use for GET.

+
+
Parameters:
+
    +
  • query (str) – The SQL query to execute.

  • +
  • commit (bool) – If True, commit the changes to the database.

  • +
+
+
Returns:
+

The results of the query.

+
+
Return type:
+

List[Any]

+
+
+
+ +
+
+fetch[source]
+

Execute the SQL query to get results from DWH and return the data.

+

Use that method for getting data. That means that if you use SELECT or +you’d like to call a stored procedure that returns one or more sets +of data, that is the correct method to use.

+

Use that method to GET.

+
+
Parameters:
+
    +
  • query (str) – The SQL query to execute.

  • +
  • to_dataframe (bool) – If True, return the results as a list +of DataFrames.

  • +
+
+
Returns:
+

+
The results of the query. If DWH returns multiple

data sets, this method is going to return a list +of result sets (lists). If DWH returns a single data set, +the method is going to return a list representing the single +result set.

+

If to_dataframe is True, the data inside each data set +is going to be in DataFrame format.

+
+
+

+
+
Return type:
+

List[Any]

+
+
+
+ +
+ +
+
+

pyprediktormapclient.dwh.dwh module

+
+
+class pyprediktormapclient.dwh.dwh.DWH(url: str, database: str, username: str, password: str, driver: int = 0)[source]
+

Bases: Db, IDWH

+

Helper functions to access a PowerView Data Warehouse or other +SQL databases. This class is a wrapper around pyodbc and you can use +all pyodbc methods as well as the provided methods. Look at the pyodbc +documentation and use the cursor attribute to access the pyodbc cursor.

+
+
Parameters:
+
    +
  • url (str) – The URL of the sql server

  • +
  • database (str) – The name of the database

  • +
  • username (str) – The username

  • +
  • password (str) – The password

  • +
+
+
+
+
+connection
+

The connection object

+
+
Type:
+

pyodbc.Connection

+
+
+
+ +
+
+cursor
+

The cursor object

+
+
Type:
+

pyodbc.Cursor

+
+
+
+ +
+
Examples - of low level usage:
>>> from pyprediktormapclient.dwh import DWH
+>>>
+>>> dwh = DWH("localhost", "mydatabase", "myusername", "mypassword")
+>>>
+>>> dwh.fetch("SELECT * FROM mytable")
+>>>
+>>> dwh.execute("INSERT INTO mytable VALUES (1, 'test')")
+
+
+
+
Examples - of high level usage:
>>> from pyprediktormapclient.dwh import DWH
+>>>
+>>> dwh = DWH("localhost", "mydatabase", "myusername", "mypassword")
+>>>
+>>> database_version = dwh.version()
+>>>
+>>> enercast_plants = dwh.enercast.get_plants_to_update()
+
+
+
+
+
+
+version[source]
+

Get the DWH version.

+

Returns: +Dict: A dictionary with the following keys (or similar):

+
+

DWHVersion, +UpdateDate, +ImplementedDate, +Comment, +MajorVersionNo, +MinorVersionNo, +InterimVersionNo

+
+
+ +
+ +
+
+

pyprediktormapclient.dwh.idwh module

+
+
+class pyprediktormapclient.dwh.idwh.IDWH[source]
+

Bases: ABC

+
+
+abstract execute(query: str, commit: bool = True) List[Any][source]
+
+ +
+
+abstract fetch(query: str, to_dataframe: bool = False) List[Any][source]
+
+ +
+
+abstract version() Dict[source]
+
+ +
+ +
+
+

Module contents

+
+
+ + +
+
+ +
+
+
+
+ + + + \ No newline at end of file diff --git a/api/pyprediktormapclient.html b/api/pyprediktormapclient.html index 0cebf59..67292e1 100644 --- a/api/pyprediktormapclient.html +++ b/api/pyprediktormapclient.html @@ -4,7 +4,7 @@ - pyprediktormapclient package — pyPrediktorMapClient 0.6.1 documentation + pyprediktormapclient package — pyPrediktorMapClient 0.6.2 documentation