forked from CodebuffAI/codebuff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount-edit-file.ts
More file actions
102 lines (94 loc) · 2.8 KB
/
Copy pathcount-edit-file.ts
File metadata and controls
102 lines (94 loc) · 2.8 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
import db from 'common/db'
import * as schema from 'common/db/schema'
import { gt, and, isNotNull } from 'drizzle-orm'
import { Message } from 'common/actions'
/**
* Count the number of times the given substring occurs in a string.
* @param text The text to search.
* @param substring The substring to count.
* @returns The number of occurrences.
*/
function countOccurrences(text: string, substring: string): number {
const regex = new RegExp(
substring.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'),
'g'
)
const matches = text.match(regex)
return matches ? matches.length : 0
}
/**
* Parse a message response which could be a string or Message content array
*/
function parseResponse(response: unknown): string {
if (typeof response === 'string') {
return response
}
if (Array.isArray(response)) {
return response
.map((content) => {
if (typeof content === 'string') return content
if ('text' in content) return content.text
return ''
})
.join('\n')
}
return JSON.stringify(response)
}
async function main() {
try {
// Define the timestamp for one day ago
const oneDayAgo = new Date(Date.now() - 24 * 60 * 60 * 1000)
// Query the message table for messages with finished_at greater than one day ago
const messages = await db
.select({
id: schema.message.id,
response: schema.message.response,
finished_at: schema.message.finished_at,
})
.from(schema.message)
.where(
and(
gt(schema.message.finished_at, oneDayAgo),
isNotNull(schema.message.finished_at),
isNotNull(schema.message.response)
)
)
let totalEditFileCount = 0
let messagesWithEdits = 0
for (const msg of messages) {
try {
const responseText = parseResponse(msg.response)
const count = countOccurrences(responseText, '</edit_fil' + 'e>')
if (count > 0) {
messagesWithEdits++
}
totalEditFileCount += count
} catch (error) {
console.error(`Error processing message ${msg.id}:`, error)
}
}
console.log('Summary:')
console.log('=========')
console.log('Total messages processed:', messages.length)
console.log('Messages containing edits:', messagesWithEdits)
console.log('Total edit file blocks:', totalEditFileCount)
console.log(
'Average edits per message with edits:',
messagesWithEdits > 0
? (totalEditFileCount / messagesWithEdits).toFixed(2)
: 0
)
} catch (error) {
console.error('Error processing messages:', error)
process.exit(1)
} finally {
// await db.end()
}
}
// Only run if this is the main module
if (require.main === module) {
main().catch((error) => {
console.error('Fatal error:', error)
process.exit(1)
})
}