import { HistorySource } from './history.types'; export const RAW_HISTORY_SOURCE: HistorySource = { name: 'raw', table: 'history_points', timeColumn: 'time', intervalSeconds: 0, }; export const AGGREGATE_HISTORY_SOURCES: HistorySource[] = [ { name: '1m', table: 'history_1m', timeColumn: 'bucket', intervalSeconds: 60 }, { name: '5m', table: 'history_5m', timeColumn: 'bucket', intervalSeconds: 300 }, { name: '1h', table: 'history_1h', timeColumn: 'bucket', intervalSeconds: 3600 }, { name: '1d', table: 'history_1d', timeColumn: 'bucket', intervalSeconds: 86400 }, ]; // Подбирает самый дешевый слой данных под диапазон и целевое число точек на графике. export function chooseHistorySource( from: Date, to: Date, targetPoints: number, ): HistorySource { const durationSeconds = Math.max((to.getTime() - from.getTime()) / 1000, 1); const desiredSeconds = durationSeconds / targetPoints; if (desiredSeconds < 60) { return RAW_HISTORY_SOURCE; } if (desiredSeconds < 300) { return AGGREGATE_HISTORY_SOURCES[0]; } if (desiredSeconds < 3600) { return AGGREGATE_HISTORY_SOURCES[1]; } if (desiredSeconds < 86400) { return AGGREGATE_HISTORY_SOURCES[2]; } return AGGREGATE_HISTORY_SOURCES[3]; }