Skip to main content

Library for Piscada Foresight; Access knowledge-graph and timeseries data.

Project description

Piscada Foresight

Access knowledge-graph and timeseries data.

Overview

This library provides access to the knowledge-graph and timeseries data in the Foresight platform. It implements a Transport using HTTPX to be used with the GQL GraphQL client. It provides the following modules:

  • data module: Read timeseries values as Pandas DataFrames or Series

    • get_value(): Get latest value before a given time
    • get_values(): Get values between start and end time
    • get_all_values(): Extract all values from a graph query response
  • domains module: Access domain definitions and trait hierarchies

    • Domain class: Contains traits and relationships for a domain
      • get_trait_by_id(): Gets a trait by its ID
    • get_domains(): Retrieve list of all available domains
    • get_trait_by_id(): Get a trait from any domain by its ID string
    • get_parent_traits(): Get all parent traits for a given trait
  • http module: OAuth2 authenticated transport

    • ForesightHTTPXTransport class: HTTPX transport with OAuth2 authentication
      • connect(): Establish authenticated connection

Installation

pip install piscada-foresight

You will need access to a Piscada Foresight instance. The library uses the OAuth2 Authorization Code Flow with Proof Key for Code Exchange (PKCE) to act on behalf of your user. After an initial interactive login, the library persists the session state in $HOME/.<client_id>_state and will use that to authenticate non-interactive the next time.

Usage

Local usage

When building the QueryManager singleton, the transport (redirection) is built with the ForesightHTTPXTransport class. This class is a subclass of the HTTPXTransport class from the gql library. The ForesightHTTPXTransport class THAT is responsible for handling the OAuth2 authentication and token refreshing.

There are 3 different transport mechanisms available (interactive, non-interactive and production_ready:

  • Interactive (local development) : Specify the domain in the QueryManager creation. Specify the client_id in the QueryManager creation. (default is "foresight-lib-py")
from datetime import datetime, timedelta, timezone

from piscada_foresight.data import get_values
from piscada_foresight.queries_templates.query_manager import QueryManager

domain = "foresight.piscada.cloud"
query_manager = QueryManager(domain=domain) #client_id default is "foresight-lib-py" or query_manager = QueryManager(domain=domain, client_id="<client_id>")

# Retrieve timeseries values for two specific entites:
get_values(
  query_manager,
  entity_ids=["ENTITY_ID", "ENTITY_ID2"],
  start=datetime.now(tz=timezone.utc) - timedelta(hours=8),
)

# Retrieve aggregated timeseries values for two specific entites:
get_values(
  query_manager,
  entity_ids=["ENTITY_ID", "ENTITY_ID2"],
  start=datetime.now(tz=timezone.utc) - timedelta(hours=8),
  interval="1h",
  aggregation_functions=["min", "max", "avg", "count", "last"],
)
  • Non-interactive (client_secrets are passed directly): Specify the client_id and the client_secret in the QueryManager creation. QueryManager(domain=domain, client_id="foresight-lib-py", client_secret="")
from datetime import datetime, timedelta, timezone

from piscada_foresight.data import get_values
from piscada_foresight.queries_templates.query_manager import QueryManager

domain = "foresight.piscada.cloud"
query_manager = QueryManager(domain=domain, client_id="<client_id>", client_secret="<client_secret>")
  • Production : to handle authorization, catch the headers from the request and pass them to the query_manager transport.
from piscada_foresight.queries_templates.query_manager import QueryManager
from fastapi import Request
from starlette.datastructures import Headers
...

domain = "foresight.piscada.cloud"
#build your query manager object
query_manager = QueryManager(domain=domain)

#Catcht the request
@count_and_duration_app.api_route("/graphql", methods=["GET", "POST"])
async def graphql_server(request: Request):
    # Get the headers       
    headers: Headers = request.headers
    dict_headers = dict(headers)
    
    #sort/filter the headers that you need to keep 
    keys_to_keep = ["x-auth-request-email",
                    "X-Auth-Request-Email",
                    "X-Auth-Request-User",
                    "x-auth-request-user",
                    "X-Auth-Request-Groups",
                    "x-auth-request-groups",
                    "X-Forwarded-Host",
                    "x-forwarded-host",
                    ] 
    filtered_headers = {key: dict_headers[key] for key in keys_to_keep if key in dict_headers}
    query_manager.update_transport(headers=filtered_headers)

Note: Not all interval values are accepted (e.g. 30m work but 45m won't work)

Contributing

Contributions are welcome! You can contact us at foresight@piscada.com.

Support

If you have any questions, issues, or suggestions, please contact us at foresight@piscada.com.

Copyright

© Piscada AS 2024

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

piscada_foresight-0.6.3.tar.gz (128.2 kB view details)

Uploaded Source

Built Distribution

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

piscada_foresight-0.6.3-py3-none-any.whl (20.2 kB view details)

Uploaded Python 3

File details

Details for the file piscada_foresight-0.6.3.tar.gz.

File metadata

  • Download URL: piscada_foresight-0.6.3.tar.gz
  • Upload date:
  • Size: 128.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.4.30

File hashes

Hashes for piscada_foresight-0.6.3.tar.gz
Algorithm Hash digest
SHA256 f2b73c48d49cef3daa8e53a1a903a5f780173c4686b5c46e6a4509d5e71eda72
MD5 a0ea6f9c6e2645667e868a9877161c82
BLAKE2b-256 59ca6281b1b75a60d6b4daa2e7d8eba0ffb40526ac1be0da85c5211015ae7ce7

See more details on using hashes here.

File details

Details for the file piscada_foresight-0.6.3-py3-none-any.whl.

File metadata

File hashes

Hashes for piscada_foresight-0.6.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a195d4a855ebbf53b4c0ae03c41392d7ec8f330cb4f260873bcd0a8ebb5666a6
MD5 c90487e0c829c64c51d75476d767d8dd
BLAKE2b-256 5a391810e4ce4e87f5299ba3ebed6be4b2486d70a844d714f1ea006e49b9f965

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