ioBroker.history icon indicating copy to clipboard operation
ioBroker.history copied to clipboard

sendto(getHistory) not returning the oldest value

Open klein0r opened this issue 9 months ago • 2 comments

Describe the bug
A clear and concise description of what the bug is.

To Reproduce

Check the following script. It writes a number given of records into the database and tries to find the oldest timestamp afterwards (with value 0).

It's working if less that 961 records have been generated (in this case). Otherwise it returns the value "23".

const objId = '0_userdata.0.test_history';
const startTs = new Date(2024, 0, 1, 0, 0, 0); // 1704063600000

const stepSize = 60 * 60 * 1000; // 1 hour
const generateRows = 960; // CHANGE THIS to 961 (or higher)

function getTestData() {
    const testData = [];
    let ts = startTs.getTime();

    for (let i = 0; i < generateRows; i++) {
        ts = startTs.getTime() + (stepSize * i);
        testData.push({ ts, val: i, ack: true, from: 'system.adapter.javascript.0' });
    }

    console.log(`Generated test data: ${testData.length} items, from ${formatDate(startTs.getTime(), 'DD.MM.YYYY hh:mm:ss.sss')} to ${formatDate(ts, 'DD.MM.YYYY hh:mm:ss.sss')}`);

    return { testData, endTs: ts };
}

createState(objId, { type: 'number', read: true, write: true }, async () => {
    await sendToAsync('history.0', 'enableHistory', {
        id: objId,
        options: {
            enabled: true,
            aliasId: '',
            debounceTime: 0,
            blockTime: 0,
            changesOnly: true,
            changesRelogInterval: 0,
            changesMinDelta: 0,
            ignoreBelowNumber: '',
            disableSkippedValueLogging: false,
            retention: 31536000,
            customRetentionDuration: 365,
            maxLength: 960,
            enableDebugLogs: true,
            debounce: 0,
        }
    });
    const enabledDps = await sendToAsync('history.0', 'getEnabledDPs', {});

    console.log(`Activated logging for ${Object.keys(enabledDps).join(', ')}`);

    if (Object.keys(enabledDps).includes(objId)) {
        // Delete previous data
        const deleteAllResult = await sendToAsync('history.0', 'deleteAll', [{ id: objId }]);
        console.log(`Deleted all saved values: ${deleteAllResult?.success}`);

        // Save new test data
        const { testData, endTs } = getTestData();
        const storeStateResult = await sendToAsync('history.0', 'storeState', {
            id: objId,
            state: testData,
        });
        console.log(`Stored new values: ${storeStateResult?.success}`);

        const getHistoryResult = await sendToAsync('history.0', 'getHistory', {
            id: objId,
            options: {
                start: startTs.getTime(),
                end: endTs,
                aggregate: 'none',
                limit: 1000,
                returnNewestEntries: false,
                ignoreNull: true,
                addId: true,
                from: true,
                ack: true,
                q: true,
            }
        });
        console.log(`History result: ${JSON.stringify(getHistoryResult?.result[0])}`);

        const getOldestResult = await sendToAsync('history.0', 'getHistory', {
            id: objId,
            options: {
                start: 1,
                end: endTs,
                aggregate: 'none',
                limit: 1,
                returnNewestEntries: false,
                ignoreNull: true,
            }
        });
        console.log(`Oldest record result: ${JSON.stringify(getOldestResult?.result[0])}`);
    } else {
        console.error(`Logging not activated for ${objId}`);
    }
});

Versions:

  • Adapter version: 3.0.1
  • JS-Controller version: 5.0.19
  • Node version: 20.12.2
  • Operating system: Debian 12

klein0r avatar May 14 '24 17:05 klein0r