-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtech.ts
More file actions
202 lines (186 loc) · 5.78 KB
/
Copy pathtech.ts
File metadata and controls
202 lines (186 loc) · 5.78 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
import { internalMutation, internalQuery } from '~/convex/_generated/server'
import { v } from 'convex/values'
import { authAction, authQuery, getGithubData } from '~/convex/utils'
import { internal } from '~/convex/_generated/api'
import type { UnwrapConvex } from '~/convex/types'
import dayjs from 'dayjs'
import { getManyFrom, getOneFrom } from 'convex-helpers/server/relationships'
export const findTechByBlock = authQuery({
args: { blockId: v.id('blocks') },
handler: async ({ db }, { blockId }) => {
return getManyFrom(db, 'tech', 'by_blockId', blockId)
}
})
export const findTechByMultipleBlocks = authQuery({
args: { blockIds: v.array(v.id('blocks')) },
handler: async ({ db }, { blockIds }) => {
const allTech = await Promise.all(
blockIds.map((blockId) => getManyFrom(db, 'tech', 'by_blockId', blockId))
)
return allTech.flat()
}
})
export const getTechByName = internalQuery({
args: { name: v.string() },
handler: async ({ db }, { name }) => {
return await getOneFrom(db, 'tech', 'by_name', name)
}
})
export const getTechGithubData = internalQuery({
args: { name: v.string() },
handler: async ({ db }, { name }) => {
return await getOneFrom(db, 'techGithubInfo', 'by_techName', name)
}
})
export const updateTechGithubData = internalMutation({
args: { id: v.id('techGithubInfo'), techName: v.string(), data: v.any() },
handler: async ({ db }, { id, techName, data }) => {
return await db.replace(id, { data, techName, lastUpdated: Date.now() })
}
})
export const insertTechGithubData = internalMutation({
args: { techName: v.string(), data: v.any() },
handler: async ({ db }, { techName, data }) => {
return await db.insert('techGithubInfo', {
data,
techName,
lastUpdated: Date.now()
})
}
})
export const getTechData = authAction({
args: { techName: v.string() },
handler: async ({ runQuery, runMutation, runAction }, { techName }) => {
const tech = (await runQuery(internal.tech.getTechByName, {
name: techName
})) as UnwrapConvex<typeof getTechByName>
if (!tech) {
return null
}
try {
if (tech.githubUrl) {
const githubData = (await runQuery(internal.tech.getTechGithubData, {
name: techName
})) as UnwrapConvex<typeof getTechGithubData>
// if we have GitHub data, and it's less than 6 hours old, return it
if (
githubData &&
githubData.lastUpdated > Date.now() - 1000 * 60 * 60 * 6
) {
return {
...tech,
repoData: githubData.data
}
}
const githubDataJson = await getGithubData(tech.githubUrl)
// save the data to the db
if (githubData) {
await runMutation(internal.tech.updateTechGithubData, {
id: githubData._id,
techName,
data: githubDataJson
})
} else {
await runMutation(internal.tech.insertTechGithubData, {
techName,
data: githubDataJson
})
}
return {
...tech,
repoData: githubDataJson
}
}
return tech
} catch (err) {
console.error(err)
return tech
}
}
})
// get tech useage percentage from all stacks
export const getTechUsage = authQuery({
args: { techName: v.string() },
handler: async ({ db }, { techName }) => {
const stacks = await db.query('stacks').collect()
// filter stacks that use this tech
const techs = stacks.filter((stack) =>
stack.stackBlocks.some((block) => block.data.tech?.name === techName)
)
// find which project types use this tech
const projectTypes = techs.flatMap((stack) => stack.projectTypes)
// calculate percentage from overall project types for each type
const projectTypeCount = Object.entries(
projectTypes.reduce(
(acc, type) => {
acc[type] = (acc[type] || 0) + 1
return acc
},
{} as Record<string, number>
)
)
.sort((a, b) => b[1] - a[1])
.map(([type, count]) => [
type,
Math.round((count / projectTypes.length) * 100)
])
return {
count: techs.length,
p: Math.round((techs.length / stacks.length) * 100),
projectTypeCount
}
}
})
export const getTechUseagePerDay = authQuery({
args: { techName: v.string(), daysBack: v.number() },
handler: async ({ db }, { techName, daysBack }) => {
// count tech useage per month for daysBack
const daysAgo = dayjs().subtract(daysBack, 'day').unix()
const stacks = await db
.query('stacks')
.filter((q) => q.gte(q.field('_creationTime'), daysAgo))
.collect()
// filter stacks that use this tech
const techs = stacks.filter((stack) =>
stack.stackBlocks.some((block) => block.data.tech?.name === techName)
)
// count tech useage per day
const useagePerDay = techs.reduce(
(acc, stack) => {
const day = dayjs(stack._creationTime).format('DD/MM/YYYY')
acc[day] = (acc[day] || 0) + 1
return acc
},
{} as Record<string, number>
)
// return as array of days and array of useage
return {
labels: Object.keys(useagePerDay),
data: Object.values(useagePerDay)
}
}
})
export const internalInsertTech = internalMutation({
args: {
name: v.string(),
icon: v.string(),
githubUrl: v.optional(v.string()),
websiteUrl: v.optional(v.string()),
description: v.optional(v.string()),
blockId: v.id('blocks'),
tags: v.optional(v.array(v.string()))
},
handler: async ({ db }, args) => {
const { name, icon, githubUrl, websiteUrl, description, blockId, tags } =
args
return await db.insert('tech', {
name,
icon,
githubUrl,
websiteUrl,
description,
blockId,
tags
})
}
})