Ejemplo n.º 1
0
def time_series_in_cdp():
    global TEST_TS_1_ID, TEST_TS_2_ID
    client = CogniteClient()

    try:
        ts_list = [TimeSeries(name=TEST_TS_1_NAME)]
        client.time_series.post_time_series(ts_list)
        log.warning("Posted sdk test time series 1")
        client.datapoints.post_datapoints(
            name=TEST_TS_1_NAME,
            datapoints=[Datapoint(timestamp=i, value=i) for i in range(TEST_TS_START, TEST_TS_END, int(3.6e6))],
        )
        log.warning("Posted datapoints to sdk test time series 1")
        TEST_TS_1_ID = client.time_series.get_time_series(prefix=TEST_TS_1_NAME).to_json()[0]["id"]
    except APIError as e:
        log.warning("Posting test time series 1 failed with code {}".format(e.code))

    try:
        ts_list = [TimeSeries(name=TEST_TS_2_NAME)]
        client.time_series.post_time_series(ts_list)
        log.warning("Posted sdk test time series 2")
        client.datapoints.post_datapoints(
            name=TEST_TS_2_NAME,
            datapoints=[Datapoint(timestamp=i, value=i) for i in range(TEST_TS_START, TEST_TS_END, int(3.6e6))],
        )
        log.warning("Posted datapoints to sdk test time series 2")
        TEST_TS_2_ID = client.time_series.get_time_series(prefix=TEST_TS_2_NAME).to_json()[0]["id"]
    except APIError as e:
        log.warning("Posting test time series 2 failed with code {}".format(e.code))

    TEST_TS_1_ID = client.time_series.get_time_series(prefix=TEST_TS_1_NAME).to_json()[0]["id"]
    TEST_TS_2_ID = client.time_series.get_time_series(prefix=TEST_TS_2_NAME).to_json()[0]["id"]
    yield TEST_TS_1_ID, TEST_TS_2_ID
Ejemplo n.º 2
0
def post_data(df):
    prefix = "".join(
        random.choice(string.ascii_uppercase + string.digits)
        for _ in range(5))
    ts_names = {
        "x": "tutorial_{}_x".format(prefix),
        "y": "tutorial_{}_y".format(prefix)
    }
    df.rename(columns=ts_names, inplace=True)

    client = CogniteClient()
    client.time_series.post_time_series(
        [TimeSeries(name=name) for name in df.columns if name != "timestamp"])
    client.time_series.post_time_series(
        [TimeSeries(name="tutorial_{}_mean_x_y".format(prefix))])

    created_time_series = []
    while len(created_time_series) != 3:
        created_time_series = client.time_series.get_time_series(
            prefix="tutorial_" + prefix)
        sleep(0.5)

    client.datapoints.post_datapoints_frame(df)

    ts_ids = {ts.name.split("_", 2)[-1]: ts.id for ts in created_time_series}
    return ts_ids
def post_data(df):
    time_series_to_post = [TimeSeries(name=name) for name in df.columns if name != "timestamp"]
    # Create a time series for the prediction output as well
    time_series_to_post.append(TimeSeries(name="{}_predicted_prod_rate".format(prefix)))

    client.time_series.post_time_series(time_series_to_post)

    created_time_series = []
    while len(created_time_series) != 5:
        created_time_series = client.time_series.get_time_series(prefix=prefix)
        sleep(0.5)

    client.datapoints.post_datapoints_frame(df)

    ts_dict = {"_".join(ts.name.split("_")[1:]): ts.id for ts in created_time_series}
    print(ts_dict)
def new_ts_id():
    name = "test_ts_{}".format(randint(1, 2 ** 53 - 1))
    stable_time_series.post_time_series([TimeSeries(name)])

    res = stable_time_series.get_time_series(prefix=name)
    while len(res) == 0:
        res = stable_time_series.get_time_series(prefix=name)
        sleep(0.5)
    yield res[0].id
Ejemplo n.º 5
0
    def test_post_datapoints_frame(self):
        data = pd.DataFrame()
        data["timestamp"] = [int(1537208777557 + 1000 * i) for i in range(0, 100)]
        X = data["timestamp"].values.astype(float)
        data["X"] = X ** 2
        data["Y"] = 1.0 / (1 + X)

        for name in data.drop(["timestamp"], axis=1).columns:
            ts = TimeSeries(name=name, description="To be deleted")
            try:
                client.time_series.post_time_series([ts])
            except:
                pass

        res = client.datapoints.post_datapoints_frame(data)
        assert res is None
Ejemplo n.º 6
0
def update_cdp_timeseries(client,
                          name,
                          metadata,
                          asset_name=None,
                          asset_id=None):
    """Insert timeseries metadata into CDP."""
    vargs = {"is_string": False}
    if asset_id:
        vargs["asset_id"] = asset_id
    if "Unit" in metadata and metadata["Unit"]:
        vargs["unit"] = metadata["Unit"]
    if "Type" in metadata:
        vargs["description"] = metadata["Type"]
    if asset_name:
        metadata["assetName"] = asset_name
    client.time_series.post_time_series(
        [TimeSeries(name=name, metadata=metadata, **vargs)])
Ejemplo n.º 7
0
def update_cdp_timeseries(client,
                          name,
                          metadata,
                          asset_name=None,
                          asset_id=None):
    """Insert timeseries metadata into CDP."""
    vargs = {"is_string": False}
    if asset_id:
        vargs["asset_id"] = asset_id
    if "unit_string" in metadata and metadata["unit_string"]:
        vargs["unit"] = metadata["unit_string"]
    if "NI_CM_Reason" in metadata:
        vargs["description"] = metadata["NI_CM_Reason"]
    if asset_name:
        metadata["assetName"] = asset_name
    metadata.update(
        {k: str(v)
         for k, v in metadata.items() if isinstance(v, date)})
    client.time_series.post_time_series(
        [TimeSeries(name=name, metadata=metadata, **vargs)])
Ejemplo n.º 8
0
 def test_update_timeseries(self):
     tso = TimeSeries(TS_NAME, unit="celsius")
     res = timeseries.update_time_series([tso])
     assert res is None
Ejemplo n.º 9
0
 def test_post_timeseries(self):
     tso = TimeSeries(TS_NAME)
     res = timeseries.post_time_series([tso])
     assert res is None
Ejemplo n.º 10
0
def datapoints_fixture():
    tso = TimeSeries(TS_NAME)
    client.time_series.post_time_series([tso])
    yield
    client.time_series.delete_time_series(TS_NAME)
Ejemplo n.º 11
0
def new_ts_id():
    name = "test_ts_{}".format(randint(1, 2 ** 53 - 1))
    stable_time_series.post_time_series([TimeSeries(name)])
    yield stable_time_series.get_time_series(prefix=name).to_json()[0]["id"]