Skip to main content

This module helps to connect to our data warehouse

Project description

License codecov for extractors

This project helps to connect to our data warehouse.

Requirements

  • Python 3.8.x or later.

Quick start

Synchronize tags

Synchronize tags from PI server to Data warehouse on period [01/01/2022 05:00:00 - 31/01/2022 05:00:00] can be easily achieved by the code below:

from datetime import datetime

from edv_dwh_connector.pg_dwh import PgDwh
from edv_dwh_connector.pi_web_api_client import PiWebAPIClient
from edv_dwh_connector.pi.rest.rest_sync_pi_tags import RestSyncPITags
from edv_dwh_connector.utils.periods import HourIntervals
from edv_dwh_connector.pi.sync_pi_data import SyncPIDataRestToDwh
from edv_dwh_connector.pi.db.pg_pi_tag import PgPITags

# We should firstly declare the Data warehouse pool connection.
dwh = PgDwh.from_connection(
    name="dwh_db_name", host="dwh_server_name_or_ip",
    user="dwh_user", password="dwh_password", port=5432
)
# Next, we should declare the PI server REST API client.
client = PiWebAPIClient(
    base_url="base/url/of/pi/server", username="admin_user",
    password="admin_password", session_timeout=2.5
)
# Finally, we can synchronize tags measures.
# The code below will automatically synchronize tags (create new tags),
# split period provided into hour intervals and synchronize PI measures
# on these intervals.
SyncPIDataRestToDwh(
    tags=RestSyncPITags(
        server_id="F1DSmN2338899MpX8PREOtdbEZ56sypOOOKRZLVNSVi1QSS1ISTGt", # Fake server ID
        client=client,
        codes=['AI162003_SCLD', 'AI162007_SCLD', 'AI162014_SCLD'],
        target=PgPITags(dwh)
    ),
    periods=HourIntervals(
        datetime(2022, 1, 1, 5, 0, 0), datetime(2022, 1, 31, 5, 0, 0)
    ),
    client=client, dwh=dwh
).synchronize()

To store data to a CSV file in fact_pi_measure format, you could do this:

SyncPIDataRestToCSVDwhLike(
    tags=RestSyncPITags(
        server_id="F1DSmN2338899MpX8PREOtdbEZ56sypOOOKRZLVNSVi1QSS1ISTGt", # Fake server ID
        client=client,
        codes=['AI162003_SCLD', 'AI162007_SCLD', 'AI162014_SCLD'],
        target=PgPITags(dwh)
    ),
    periods=HourIntervals(
        datetime(2022, 1, 1, 5, 0, 0), datetime(2022, 1, 31, 5, 0, 0)
    ),
    client=client,
    file="path/of/csv/file/where/to/store"
).synchronize()

after importing SyncPIDataRestToCSVDwhLike from edv_dwh_connector.pi.sync_pi_data. It is very useful when you want to recover data of DWH table fact_pi_measure on a long period.

N.B. You could also fetch on day intervals by using class DayIntervals instead of HourIntervals. But, HourIntervals could be faster than DayIntervals depending on the size of data to be imported.

Synchronize partially a CSV file for a tag

To synchronize a CSV file with the latest data from interpolated data from DWH, we do like this:

dwh = ...
tag = "AI56222_SCDL"
CsvWithLatestPIMeasuresDf(
    path="my/path/data.csv",
    tag=tag,
    origin=PgMinuteInterpolatedPIMeasuresDf(tag, dwh)
).frame(
    datetime(2022, 1, 1, 5, 0, 0), datetime(2022, 1, 31, 5, 0, 0)
)

If data.csv contains data for period [01/01/2022 - 24/01/2022], this instruction will only add to the CSV file data of the last week.

Read tags and measures from DWH

Read tags

To get all tags, just do this:

tags = PgCachedPITags(dwh).items()

Read measures

To read measures of a tag on a period, just do this:

tag = ... # get PI tag here
measures = PgCachedPIMeasures(tag, dwh).items(
    datetime(2022, 1, 1, 5, 0, 0), datetime(2022, 1, 31, 5, 0, 0)
)
# or use only tag code
measures = PgCachedPIMeasures("AI162014_SCLD", dwh).items(
    datetime(2022, 1, 1, 5, 0, 0), datetime(2022, 1, 31, 5, 0, 0)
)
# or use a data frame (by tag or tag code)
dt = PgPIMeasuresDf(tag, dwh).frame(
    datetime(2022, 1, 1, 5, 0, 0), datetime(2022, 1, 31, 5, 0, 0)
)

Synchronize blend proposals

We easily synchronize by this code below:

SyncBlendProposals(
    src=ExcelBlendProposals(
        file="path/of/excel/file/name",
        start_date=date.fromisoformat("2022-10-18"),
        end_date=date.fromisoformat("2022-10-20")
    ),
    target=PgBlendProposals(dwh)
).synchronize()

Read blend proposals from DWH

blends = PgBlendProposals(dwh).items(
    date(2022, 10, 18), datetime(2022, 10, 20)
)

Development environment

It is recommended to start by creating a virtual environment. You could do it by following commands:

python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

N.B. We activate an environment on Windows by executing:

.venv\Scripts\activate.bat

How to contribute

Please read contributing rules.

Fork repository, make changes, send us a pull request. We will review your changes and apply them to the master branch shortly, provided they don't violate our quality standards. To avoid frustration, before sending us your pull request please run these commands:

sh pyqulice.sh # Linux
pyqulice.bat # Windows
pytest tests/unit/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

edv-dwh-connector-0.14.1.tar.gz (36.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

edv_dwh_connector-0.14.1-py3-none-any.whl (90.0 kB view details)

Uploaded Python 3

File details

Details for the file edv-dwh-connector-0.14.1.tar.gz.

File metadata

  • Download URL: edv-dwh-connector-0.14.1.tar.gz
  • Upload date:
  • Size: 36.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for edv-dwh-connector-0.14.1.tar.gz
Algorithm Hash digest
SHA256 0d594cf5d5c4635806f63697311967f2ab4673648afa7cdbdbe4d138add3eee5
MD5 267429abf8a8ad80cc16ba47330353ce
BLAKE2b-256 9328fb2c1e3f15603d80630db380fe25dd472762a958de4ef40a575db1fe47e4

See more details on using hashes here.

File details

Details for the file edv_dwh_connector-0.14.1-py3-none-any.whl.

File metadata

File hashes

Hashes for edv_dwh_connector-0.14.1-py3-none-any.whl
Algorithm Hash digest
SHA256 39fff138221b1d37a2d9746a6a989ae34e8696cddd88f3bb62b81fded5160063
MD5 c9b8c0f65013630217557edd02bd8bcd
BLAKE2b-256 a7c65ac9ae370ba8fed4dba6529e4145cb6f4e4f0ea06cca0ec736d71067632b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page