Delete assets/js/dashboard/stats/graph/main-graph-data.ts with huggingface_hub
Browse files
assets/js/dashboard/stats/graph/main-graph-data.ts
DELETED
|
@@ -1,215 +0,0 @@
|
|
| 1 |
-
import { MainGraphResponse, ResultItem } from './fetch-main-graph'
|
| 2 |
-
import { MetricValue } from '../../api'
|
| 3 |
-
|
| 4 |
-
/**
|
| 5 |
-
* Fills gaps in @see MainGraphResponse the series of `results` and `comparisonResults`.
|
| 6 |
-
* The BE doesn't return buckets in the series where the value is 0:
|
| 7 |
-
* these need to filled by the FE to have a consistent plot.
|
| 8 |
-
*
|
| 9 |
-
* The assumption is that the two series each are continuously defined.
|
| 10 |
-
*
|
| 11 |
-
* Extracts the numeric values for the series when they are wrapped.
|
| 12 |
-
*
|
| 13 |
-
*/
|
| 14 |
-
export const remapAndFillData = ({
|
| 15 |
-
data,
|
| 16 |
-
getNumericValue,
|
| 17 |
-
getValue,
|
| 18 |
-
getChange
|
| 19 |
-
}: {
|
| 20 |
-
data: MainGraphResponse
|
| 21 |
-
getNumericValue: (metricValue: MetricValue) => number
|
| 22 |
-
getValue: (item: Pick<ResultItem, 'metrics'>) => MetricValue
|
| 23 |
-
getChange: (value: number, comparisonValue: number) => number
|
| 24 |
-
}): GraphDatum[] => {
|
| 25 |
-
const totalBucketCount = Math.max(
|
| 26 |
-
data.meta.comparison_time_label_result_indices?.length ?? 0,
|
| 27 |
-
data.meta.time_label_result_indices.length
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
const remappedData: GraphDatum[] = new Array(totalBucketCount)
|
| 31 |
-
.fill(null)
|
| 32 |
-
.map((_, index) => {
|
| 33 |
-
const timeLabel = data.meta.time_labels[index] ?? null
|
| 34 |
-
const indexOfResult = data.meta.time_label_result_indices[index] ?? null
|
| 35 |
-
const comparisonTimeLabel =
|
| 36 |
-
(data.meta.comparison_time_labels &&
|
| 37 |
-
data.meta.comparison_time_labels[index]) ??
|
| 38 |
-
null
|
| 39 |
-
const indexOfComparisonResult =
|
| 40 |
-
(data.meta.comparison_time_label_result_indices &&
|
| 41 |
-
data.meta.comparison_time_label_result_indices[index]) ??
|
| 42 |
-
null
|
| 43 |
-
|
| 44 |
-
let main: SeriesValue
|
| 45 |
-
if (typeof timeLabel === 'string') {
|
| 46 |
-
const value =
|
| 47 |
-
indexOfResult !== null
|
| 48 |
-
? getValue(data.results[indexOfResult]!)
|
| 49 |
-
: getValue({ metrics: data.meta.empty_metrics })
|
| 50 |
-
main = {
|
| 51 |
-
isDefined: true,
|
| 52 |
-
timeLabel,
|
| 53 |
-
value,
|
| 54 |
-
numericValue: getNumericValue(value),
|
| 55 |
-
isPartial: (data.meta.partial_time_labels ?? []).includes(timeLabel),
|
| 56 |
-
isCurrent: data.meta.present_index === index
|
| 57 |
-
}
|
| 58 |
-
} else {
|
| 59 |
-
main = { isDefined: false }
|
| 60 |
-
}
|
| 61 |
-
|
| 62 |
-
let comparison: SeriesValue
|
| 63 |
-
if (typeof comparisonTimeLabel === 'string') {
|
| 64 |
-
const value =
|
| 65 |
-
indexOfComparisonResult !== null
|
| 66 |
-
? getValue(data.comparison_results[indexOfComparisonResult]!)
|
| 67 |
-
: getValue({ metrics: data.meta.empty_metrics })
|
| 68 |
-
comparison = {
|
| 69 |
-
isDefined: true,
|
| 70 |
-
timeLabel: comparisonTimeLabel,
|
| 71 |
-
value,
|
| 72 |
-
numericValue: getNumericValue(value),
|
| 73 |
-
isPartial: (data.meta.comparison_partial_time_labels ?? []).includes(
|
| 74 |
-
comparisonTimeLabel
|
| 75 |
-
),
|
| 76 |
-
isCurrent: false
|
| 77 |
-
}
|
| 78 |
-
} else {
|
| 79 |
-
comparison = { isDefined: false }
|
| 80 |
-
}
|
| 81 |
-
|
| 82 |
-
let change = null
|
| 83 |
-
|
| 84 |
-
if (
|
| 85 |
-
change === null &&
|
| 86 |
-
main.isDefined &&
|
| 87 |
-
comparison.isDefined &&
|
| 88 |
-
main.value !== null &&
|
| 89 |
-
comparison.value !== null
|
| 90 |
-
) {
|
| 91 |
-
change = getChange(main.numericValue, comparison.numericValue)
|
| 92 |
-
}
|
| 93 |
-
|
| 94 |
-
return {
|
| 95 |
-
main,
|
| 96 |
-
comparison,
|
| 97 |
-
change
|
| 98 |
-
}
|
| 99 |
-
})
|
| 100 |
-
|
| 101 |
-
return remappedData
|
| 102 |
-
}
|
| 103 |
-
|
| 104 |
-
export const getFirstAndLastTimeLabels = (
|
| 105 |
-
response: Pick<MainGraphResponse, 'meta'>,
|
| 106 |
-
series: MainGraphSeriesName
|
| 107 |
-
): [string | null, string | null] => {
|
| 108 |
-
const labels = {
|
| 109 |
-
[MainGraphSeriesName.main]: response.meta.time_labels,
|
| 110 |
-
[MainGraphSeriesName.comparison]: response.meta.comparison_time_labels
|
| 111 |
-
}[series]
|
| 112 |
-
if (!labels?.length) {
|
| 113 |
-
return [null, null]
|
| 114 |
-
}
|
| 115 |
-
return [labels[0], labels[labels.length - 1]]
|
| 116 |
-
}
|
| 117 |
-
|
| 118 |
-
export const METRICS_WITH_CHANGE_IN_PERCENTAGE_POINTS = [
|
| 119 |
-
'bounce_rate',
|
| 120 |
-
'exit_rate',
|
| 121 |
-
'conversion_rate'
|
| 122 |
-
// 'group_conversion_rate'
|
| 123 |
-
]
|
| 124 |
-
|
| 125 |
-
export const getChangeInPercentagePoints = (
|
| 126 |
-
value: number,
|
| 127 |
-
comparisonValue: number
|
| 128 |
-
): number => {
|
| 129 |
-
return value - comparisonValue
|
| 130 |
-
}
|
| 131 |
-
|
| 132 |
-
export const getRelativeChange = (
|
| 133 |
-
value: number,
|
| 134 |
-
comparisonValue: number
|
| 135 |
-
): number => {
|
| 136 |
-
if (comparisonValue === 0 && value > 0) {
|
| 137 |
-
return 100
|
| 138 |
-
}
|
| 139 |
-
if (comparisonValue === 0 && value === 0) {
|
| 140 |
-
return 0
|
| 141 |
-
}
|
| 142 |
-
|
| 143 |
-
return Math.round(((value - comparisonValue) / comparisonValue) * 100)
|
| 144 |
-
}
|
| 145 |
-
|
| 146 |
-
export const REVENUE_METRICS = ['average_revenue', 'total_revenue']
|
| 147 |
-
|
| 148 |
-
export type LineSegment = {
|
| 149 |
-
startIndexInclusive: number
|
| 150 |
-
stopIndexExclusive: number
|
| 151 |
-
type: 'full' | 'current'
|
| 152 |
-
}
|
| 153 |
-
|
| 154 |
-
/**
|
| 155 |
-
* Creates segments from points of a series.
|
| 156 |
-
*
|
| 157 |
-
* When a point of data is 'current' (only the last point of the series can be),
|
| 158 |
-
* then the line that connects it is dashed. If the 'current' point moves, so
|
| 159 |
-
* does the line connecting it.
|
| 160 |
-
*
|
| 161 |
-
* A full line is drawn only between two or more continuous full periods.
|
| 162 |
-
* No line is drawn from or to gaps in the data.
|
| 163 |
-
*/
|
| 164 |
-
export function getLineSegments(data: SeriesValue[]): LineSegment[] {
|
| 165 |
-
return data.reduce((segments: LineSegment[], curr, i) => {
|
| 166 |
-
if (i === 0) {
|
| 167 |
-
return segments
|
| 168 |
-
}
|
| 169 |
-
const prev = data[i - 1]
|
| 170 |
-
if (!prev.isDefined || !curr.isDefined) {
|
| 171 |
-
return segments
|
| 172 |
-
}
|
| 173 |
-
|
| 174 |
-
const type = curr.isCurrent ? 'current' : 'full'
|
| 175 |
-
|
| 176 |
-
const lastSegment = segments[segments.length - 1]
|
| 177 |
-
|
| 178 |
-
if (lastSegment?.type === type && lastSegment.stopIndexExclusive === i) {
|
| 179 |
-
return [
|
| 180 |
-
...segments.slice(0, -1),
|
| 181 |
-
{ ...lastSegment, stopIndexExclusive: i + 1 }
|
| 182 |
-
]
|
| 183 |
-
}
|
| 184 |
-
|
| 185 |
-
return [
|
| 186 |
-
...segments,
|
| 187 |
-
{ startIndexInclusive: i - 1, stopIndexExclusive: i + 1, type }
|
| 188 |
-
]
|
| 189 |
-
}, [])
|
| 190 |
-
}
|
| 191 |
-
|
| 192 |
-
/**
|
| 193 |
-
* A data point for the graph and tooltip.
|
| 194 |
-
* It's x position is its index in `GraphDatum[]` array.
|
| 195 |
-
* The values for `numericValue`, `comparisonNumericValue` should be plotted on the y axis, when they are defined for the x position.
|
| 196 |
-
*/
|
| 197 |
-
export type GraphDatum = Record<MainGraphSeriesName, SeriesValue> & {
|
| 198 |
-
change?: number | null
|
| 199 |
-
}
|
| 200 |
-
|
| 201 |
-
export enum MainGraphSeriesName {
|
| 202 |
-
main = 'main',
|
| 203 |
-
comparison = 'comparison'
|
| 204 |
-
}
|
| 205 |
-
|
| 206 |
-
type SeriesValue =
|
| 207 |
-
| { isDefined: false }
|
| 208 |
-
| {
|
| 209 |
-
isDefined: true
|
| 210 |
-
numericValue: number
|
| 211 |
-
value: MetricValue
|
| 212 |
-
isPartial: boolean
|
| 213 |
-
isCurrent: boolean
|
| 214 |
-
timeLabel: string
|
| 215 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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