Time Series Controller

Module containing the Time Series Controller class for interact with the Real World Data API.

class genomcore.controllers.time_series.TimeSeriesController(*args, **kwargs)

Bases: BaseController

Time Series Controller class to interact with the time series API. Time series API docs: https://rwd-api.genomcore.net/api/docs

create_time_series(body: List[dict], chunksize: int | None = 100000, retries: int | None = 0, chunk_details: bool | None = False, measure_time: bool | None = False) List[dict]

Create time-series observations.

Parameters:
  • body (List[dict]) – List of python dictionaries representing the time-series observations.

  • chunksize (int) – Size of each chunk for batch processing. Defaults to 100000 as the max for a single chunk.

  • max_retries (int) – Maximum number of retries for failed requests. Defaults to 3.

  • chunk_details (bool) – If True, prints the first and last observation of each chunk. Defaults to False.

  • timer (bool) – If True, prints the time taken for each chunk upload. Defaults to False.

Warning

The method only checks if the batch value is None or an empty string. It is the user responsibility to ensure that the batch value is unique for each group of observations.

Note

The body should have the following list of dictionaries structure:

[
    {
        "meta": {
            "userId": int,
            "source": str,
            "metric": str,
            "externalId": str,
            "batch": str
        },
        "point": {
            "start": str (ISO 8601 datetime),
            "end": str (ISO 8601 datetime),
            "value": int
        }
    },
    ...
]
Returns:

A JSON response from requests.

Example:

from genomcore.client import Genomcore

api = Genomcore(token="A_VALID_TOKEN", refresh_token="A_VALID_REFRESH_TOKEN")
api.time_series.create_time_series(body = body, chunksize = 10000, max_retries = 3)
query_time_series(page: int | None = 0, pageSize: int | None = 10000, body: List[dict] = None, measure_time: bool | None = False)

Get time-series observations by providing MongoDB queries.

Parameters:
  • page (int, Optional) – Page number to query. Defaults to first page (page 0).

  • pageSize (int, Optional) – Number of entries per page. Defaults to 10.000.

  • body (List[dict]) – Payload of the request containing MongoDB queries.

Returns:

List[dict] object from the query.

Example:

from genomcore.client import Genomcore

api = Genomcore(token="A_VALID_TOKEN", refresh_token="A_VALID_REFRESH_TOKEN")

# A valid body query in MongoDB format
# This example queries the time-series observations of userId "1" in the batch "TestBatch"
# between the dates 2024-04-01 and 2024-04-08 and sorts them by timestamp in descending order.
body = {
    "filter": {
        "meta.userId": {"$in": [1]},
        "meta.batch": {"$in": ["TestBatch"]}
    },
    "sort": {
        "timestamp": -1
    },
    "beforeDate": "2024-04-01",
    "afterDate": "2024-04-08"
}
api.time_series.query_time_series(page = 2, pageSize = 500, body = body)
delete_time_series(body: List[dict] = None, measure_time: bool | None = False) List[dict]

Delete time-series observations.

Parameters:
  • body (dict) – List of A dictionary with the following keys to specify the elements to delete: - ‘userId’: User ID associated with the observation. - ‘batch’: Unique value identifying the group of observations. - ‘metric’: Metric of the observations to delete.

  • timer (bool) – If True, prints the time taken for delete the specified datapoints. Defaults to False.

Returns:

A JSON response with the ‘deletedCount’ number of the deleted datapoints.

Warning

If the connection is closed before receiving the response, it could be due to a large deletion that takes longer than usual (more than 60 seconds). The server might close the connection but still process the request successfully. Please verify if the data points were deleted correctly.

Example:

from genomcore.client import Genomcore

api = Genomcore(token="A_VALID_TOKEN", refresh_token="A_VALID_REFRESH_TOKEN")

# A valid body with the delete JSON structure
# This example deletes the time-series observations of userId "10"
# containing the batch "ChunkTest" and the metric "STEPS".
body = {
        'userId': 10,
        'batch': "ChunkTest",
        'metric': "STEPS"

}
api.time_series.delete_time_series(body = body)
__annotations__ = {}
__firstlineno__ = 15
__static_attributes__ = ('_requester',)