balena-io / balena-sdk

The SDK to make balena powered JavaScript applications.
https://www.balena.io/
Apache License 2.0
145 stars 46 forks source link

Device history model does not return data #1388

Closed fisehara closed 11 months ago

fisehara commented 11 months ago

Expected Behavior

Retrieve device history data for a device that the user has admin access on the containing application. Device history entry needs to be younger than 24h hours to make sure that the history lies in the free plan of 24h history.

Actual Behavior

SDK calls return empty arrays

Steps to Reproduce the Problem

Run script with UUID="<uuid>" APITOKEN="<apiTokenFromDashboard>" node test.js

const { getSdk } = require('balena-sdk');

const APITOKEN = process.env.APITOKEN
const balena = getSdk({
    apiUrl: "https://api.balena-cloud.com/"
});

const headers = { "Authorization": "Bearer " + APITOKEN }
const deviceUUID = process.env.UUID

async function runnerSdk() {
    await balena.auth.loginWithToken(APITOKEN);
    const device = await balena.models.device.get(deviceUUID);
    const sdk_hist_uuid = await balena.models.device.history.getAllByDevice(device.uuid)
    console.log(`sdk_hist_uuid:${JSON.stringify(sdk_hist_uuid, null, 2)}`);
    const sdk_hist_id = await balena.models.device.history.getAllByDevice(device.id)
    console.log(`sdk_hist_id:${JSON.stringify(sdk_hist_id, null, 2)}`);
}

async function runnerFetch() {
    const fetchHeaders = [
        ["Authorization", "Bearer " + APITOKEN],
    ];
    await balena.auth.loginWithToken(APITOKEN);
    const device = await balena.models.device.get(deviceUUID);
    let res = await fetch(
        `https://api.balena-cloud.com/v6/device_history?$filter=uuid eq '${device.uuid}'`,
        { headers },
    )
    if (res.ok) {
        const data = await res.json();
        console.log(`urlHistoryUuid:${JSON.stringify(data, null, 2)}`);
    }
    res = await fetch(
        `https://api.balena-cloud.com/v6/device_history?$filter=tracks__device eq ${device.id}`,
        { headers },
    )
    if (res.ok) {
        const data = await res.json();
        console.log(`urlHistoryId:${JSON.stringify(data, null, 2)}`);
    }
}

async function runner() {
    await runnerSdk();
    await runnerFetch();
}

runner();

Specifications

fisehara commented 11 months ago

While preparing a python sdk snippet to fetch device_history data, I discovered that, we have intentionally added range filters on the SDK / client) side to not fetch all data by default. That made me think it's not working. Please excuse the noise.