|
1 | 1 | import type { ColDefaultValue, ColSettings, FictionDbTableSettings } from '@fiction/core'
|
2 |
| -import type { TimeLineInterval } from '../types.js' |
3 | 2 | import type { FictionEvent } from '../typesTracking.js'
|
4 |
| -import type { BaseChartData, ClickHouseDatatype, FictionClickHouse } from './index.js' |
5 |
| -import { Col, dayjs, FictionDbTable } from '@fiction/core' |
| 3 | +import type { ClickHouseDatatype, FictionClickHouse } from './index.js' |
| 4 | +import { Col, FictionDbTable } from '@fiction/core' |
6 | 5 | import { z } from 'zod'
|
7 | 6 |
|
8 | 7 | type ValueCallback = (params: {
|
@@ -95,72 +94,72 @@ export class FictionAnalyticsTable extends FictionDbTable {
|
95 | 94 | }
|
96 | 95 | }
|
97 | 96 |
|
98 |
| -export function fillData<T extends BaseChartData>(args: { |
99 |
| - timeZone: string |
100 |
| - timeStartAt: dayjs.Dayjs |
101 |
| - timeEndAt: dayjs.Dayjs |
102 |
| - interval: TimeLineInterval |
103 |
| - withRollup?: boolean |
104 |
| - data: T[] |
105 |
| -}): T[] { |
106 |
| - const { timeStartAt, timeEndAt, timeZone, interval, data = [], withRollup } = args |
107 |
| - |
108 |
| - const newData: { date: string, [key: string]: any }[] |
109 |
| - = withRollup && data[0] ? [data[0]] : [{} as T] |
110 |
| - |
111 |
| - // clickhouse returns different timezone handling for weeks/months/years vs days/hours |
112 |
| - // appropriate timezone is returned for < weeks but always utc otherwise |
113 |
| - let loopTime: dayjs.Dayjs |
114 |
| - let finishTime: dayjs.Dayjs |
115 |
| - if (interval === 'week' || interval === 'month') { |
116 |
| - loopTime = timeStartAt.utc().startOf(interval) |
117 |
| - finishTime = timeEndAt.utc().endOf(interval) |
118 |
| - } |
119 |
| - else { |
120 |
| - loopTime = timeStartAt.clone().tz(timeZone) |
121 |
| - finishTime = timeEndAt.clone().tz(timeZone) |
122 |
| - } |
123 |
| - |
124 |
| - const duration = Math.abs(finishTime.diff(loopTime, 'day')) |
125 |
| - |
126 |
| - const sample = data[0] ?? {} |
127 |
| - // create default object from sample set to zeros |
128 |
| - const defaultObjectIfMissing = Object.fromEntries( |
129 |
| - Object.entries(sample) |
130 |
| - .map(([k, v]) => { |
131 |
| - return ((typeof v === 'string' && /^-?\d+$/.test(v)) || typeof v === 'number') ? [k, 0] : undefined |
132 |
| - }) |
133 |
| - .filter(Boolean) as [string, number][], |
134 |
| - ) |
135 |
| - |
136 |
| - while ( |
137 |
| - loopTime.isBefore(finishTime, interval) |
138 |
| - || loopTime.isSame(finishTime, interval) |
139 |
| - ) { |
140 |
| - const date = loopTime.toISOString() |
141 |
| - const displayDate = loopTime.tz(timeZone) |
142 |
| - |
143 |
| - const now = dayjs() |
144 |
| - const found = data.find(_ => _.date === date) || defaultObjectIfMissing |
145 |
| - |
146 |
| - const dateFormat |
147 |
| - = duration < 3 ? 'ha' : duration > 180 ? 'MMM D, YYYY' : 'MMM D' |
148 |
| - |
149 |
| - const d: BaseChartData = { |
150 |
| - ...found, |
151 |
| - date, |
152 |
| - label: displayDate.format(dateFormat), |
153 |
| - tense: displayDate.isSame(now, interval) |
154 |
| - ? 'present' |
155 |
| - : displayDate.isAfter(now, interval) |
156 |
| - ? 'future' |
157 |
| - : 'past', |
158 |
| - } |
159 |
| - |
160 |
| - newData.push(d) |
161 |
| - |
162 |
| - loopTime = loopTime.add(1, interval) |
163 |
| - } |
164 |
| - |
165 |
| - return newData as T[] |
166 |
| -} |
| 97 | +// export function fillData<T extends BaseChartData>(args: { |
| 98 | +// timeZone: string |
| 99 | +// timeStartAt: dayjs.Dayjs |
| 100 | +// timeEndAt: dayjs.Dayjs |
| 101 | +// interval: TimeLineInterval |
| 102 | +// withRollup?: boolean |
| 103 | +// data: T[] |
| 104 | +// }): T[] { |
| 105 | +// const { timeStartAt, timeEndAt, timeZone, interval, data = [], withRollup } = args |
| 106 | + |
| 107 | +// const newData: { date: string, [key: string]: any }[] |
| 108 | +// = withRollup && data[0] ? [data[0]] : [{} as T] |
| 109 | + |
| 110 | +// // clickhouse returns different timezone handling for weeks/months/years vs days/hours |
| 111 | +// // appropriate timezone is returned for < weeks but always utc otherwise |
| 112 | +// let loopTime: dayjs.Dayjs |
| 113 | +// let finishTime: dayjs.Dayjs |
| 114 | +// if (interval === 'week' || interval === 'month') { |
| 115 | +// loopTime = timeStartAt.utc().startOf(interval) |
| 116 | +// finishTime = timeEndAt.utc().endOf(interval) |
| 117 | +// } |
| 118 | +// else { |
| 119 | +// loopTime = timeStartAt.clone().tz(timeZone) |
| 120 | +// finishTime = timeEndAt.clone().tz(timeZone) |
| 121 | +// } |
| 122 | + |
| 123 | +// const duration = Math.abs(finishTime.diff(loopTime, 'day')) |
| 124 | + |
| 125 | +// const sample = data[0] ?? {} |
| 126 | +// // create default object from sample set to zeros |
| 127 | +// const defaultObjectIfMissing = Object.fromEntries( |
| 128 | +// Object.entries(sample) |
| 129 | +// .map(([k, v]) => { |
| 130 | +// return ((typeof v === 'string' && /^-?\d+$/.test(v)) || typeof v === 'number') ? [k, 0] : undefined |
| 131 | +// }) |
| 132 | +// .filter(Boolean) as [string, number][], |
| 133 | +// ) |
| 134 | + |
| 135 | +// while ( |
| 136 | +// loopTime.isBefore(finishTime, interval) |
| 137 | +// || loopTime.isSame(finishTime, interval) |
| 138 | +// ) { |
| 139 | +// const date = loopTime.toISOString() |
| 140 | +// const displayDate = loopTime.tz(timeZone) |
| 141 | + |
| 142 | +// const now = dayjs() |
| 143 | +// const found = data.find(_ => _.date === date) || defaultObjectIfMissing |
| 144 | + |
| 145 | +// const dateFormat |
| 146 | +// = duration < 3 ? 'ha' : duration > 180 ? 'MMM D, YYYY' : 'MMM D' |
| 147 | + |
| 148 | +// const d: BaseChartData = { |
| 149 | +// ...found, |
| 150 | +// date, |
| 151 | +// label: displayDate.format(dateFormat), |
| 152 | +// tense: displayDate.isSame(now, interval) |
| 153 | +// ? 'present' |
| 154 | +// : displayDate.isAfter(now, interval) |
| 155 | +// ? 'future' |
| 156 | +// : 'past', |
| 157 | +// } |
| 158 | + |
| 159 | +// newData.push(d) |
| 160 | + |
| 161 | +// loopTime = loopTime.add(1, interval) |
| 162 | +// } |
| 163 | + |
| 164 | +// return newData as T[] |
| 165 | +// } |
0 commit comments