-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdata.ts
More file actions
204 lines (193 loc) · 5.48 KB
/
Copy pathdata.ts
File metadata and controls
204 lines (193 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import * as d3 from 'd3'
import type { TopData } from '.'
export function useMaxStreak(data: Ref<{
date: Date
duration: number
}[]>) {
return computed(() => {
if (data.value === null) {
return 0
}
let streak = 0
let maxStreak = 0
for (let i = 0; i < data.value.length; i++) {
const d = data.value[i]
if (d.duration === 0) {
maxStreak = Math.max(maxStreak, streak)
streak = 0
continue
}
streak++
}
return Math.max(maxStreak, streak)
})
}
export function useCurrentStreak(data: Ref<{
date: Date
duration: number
}[]>) {
return computed(() => {
if (data.value === null) {
return 0
}
let streak = 0
for (let i = 0; i < data.value.length; i++) {
const d = data.value[i]
if (d.duration === 0) {
break
}
streak++
}
return streak
})
}
export function useTodayMinutes(data: Ref<{
data: {
duration: number
time: string
}[]
} | null>) {
return computed(() => {
if (!data.value) {
return 0
}
const today = new Date()
const todayString = today.toISOString().slice(0, 10)
const todayData = data.value.data.find(d => d.time.slice(0, 10) === todayString)
return todayData?.duration ?? 0
})
}
export function useTotalMinutes(data: Ref<{
data: {
duration: number
time: string
}[]
} | null>) {
return computed(() => {
if (!data) {
return 0
}
return data.value?.data.reduce((acc, cur) => acc + cur.duration, 0) ?? 0
})
}
// Function to transform platform data
export function transformTopPlatformData(data: TopData[]) {
return data.map((d) => {
if (d.field.toLocaleLowerCase().includes('mac')) {
d.icon = 'i-mdi-apple'
}
else if (d.field.toLocaleLowerCase().includes('window')) {
d.icon = 'i-mdi-microsoft-windows'
}
else if (d.field.toLocaleLowerCase().includes('linux')) {
d.icon = 'i-codicon-terminal-linux'
}
else {
d.icon = 'i-mdi-desktop-classic'
}
return d
})
}
export function transformTopLanguageData(data: TopData[]) {
return data.map((d) => {
d.icon = iconMap.get(d.field) ?? 'i-material-symbols-question-mark-rounded'
// d.field = getLanguageName(d.field)
return d
})
}
export function useMergedFilters(filters: FilterItem[]) {
return computed(() => {
const map = new Map<string, string>()
filters.forEach((f) => {
if (map.has(f.key)) {
const value = map.get(f.key)
if (value) {
map.set(f.key, `${value},${f.value}`)
}
}
else {
map.set(f.key, f.value)
}
})
const res: FilterItem[] = []
map.forEach((value, key) => {
res.push({ key, value })
})
return res
})
}
export async function useAllTopData(hasData: MaybeRef<boolean>, days: MaybeRef<number>, filters: FilterItem[]) {
const mergedFilters = useMergedFilters(filters)
let languageTopData
let projectTopData
let platformTopData
if (unref(hasData)) {
const limit = 5
const durationInMinutes = unref(days) * 1440
try {
[languageTopData, projectTopData, platformTopData] = await Promise.all([
fetchTop('language', durationInMinutes, limit, mergedFilters, { transform: transformTopLanguageData }),
fetchTop('project', durationInMinutes, limit, mergedFilters),
fetchTop('platform', durationInMinutes, limit, mergedFilters, { transform: transformTopPlatformData }),
])
}
catch (error) {
// Handle errors here
console.error(error)
}
}
return [languageTopData, projectTopData, platformTopData]
}
export function useProcessedData(data: Ref<{
data: {
duration: number
time: string
by?: string | undefined
}[]
} | null>) {
const t = useI18N()
return computed(() => {
const differentBy = new Set<string>()
data.value?.data.forEach((d) => {
differentBy.add(d.by ?? t.value.plot.label.unknown)
})
// get sum duration for each by
const sumDurationBy = new Map<string, number>()
data.value?.data.forEach((d) => {
const duration = sumDurationBy.get(d.by ?? t.value.plot.label.unknown) ?? 0
sumDurationBy.set(d.by ?? t.value.plot.label.unknown, duration + d.duration)
})
// get top by
const sortedBy = Array.from(sumDurationBy.entries()).sort((a, b) => b[1] - a[1])
const topBy = sortedBy.slice(0, 5).map(d => d[0])
const dataWithOther = data.value?.data.map((d) => {
d = { ...d }
if (!topBy.includes(d.by ?? t.value.plot.label.unknown)) {
d.by = t.value.plot.label.other
}
return d
}) ?? []
const minDateString = d3.min(data.value?.data ?? [], d => d.time)
const minDateDate = minDateString ? new Date(minDateString) : new Date()
const dateRange = d3.utcDay.range(minDateDate, new Date())
const dataMap = new Map<string, number>()
dateRange.forEach((d) => {
topBy.forEach((by) => {
const key: string = [d.toISOString().slice(0, 10), by].join(',')
dataMap.set(key, 0)
})
})
dataWithOther.forEach((d) => {
const date = new Date(d.time)
const key = [date.toISOString().slice(0, 10), d.by ?? t.value.plot.label.unknown].join(',')
dataMap.set(key, d.duration)
})
const res = Array.from(dataMap.entries()).map(([keyRaw, data]) => {
const key = keyRaw.split(',')
return { date: new Date(key[0]), duration: data, by: key[1] }
})
.sort((a, b) => a.by.localeCompare(b.by))
.sort((a, b) => a.date.getTime() - b.date.getTime())
return res
})
}