-
Notifications
You must be signed in to change notification settings - Fork 21
/
writeSummaryToCSV.js
61 lines (50 loc) · 2.08 KB
/
writeSummaryToCSV.js
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
const path = require('path')
const { escapeComment, stripHTML, writeHeader, appendRow, postStatistics, toPacificTimeString } = require('./util') // Adjust the path as necessary
const topicSummary = (topic) => {
const posts = topic.replies.flat()
const topicCreatedAt = new Date(topic.topicCreatedAt)
const postSummary = postStatistics(posts, topicCreatedAt)
return postSummary
}
// Function to write the summary to CSV
const writeSummaryToCSV = (courseId, data) => {
console.log(`Writing summary of discussion data for course: ${courseId}`)
const csvPath = path.join(__dirname, `output/${courseId}-discussion-summary.csv`)
const headers = [
'topic_id',
'topic_title',
//'topic_message',
'topic_author_id',
'topic_author_name',
'topic_created_at',
'topic_posted_at',
'number_of_posts',
'median_posts_word_count',
'average_time_to_post_hours',
'first_reply_timestamp',
'average_time_to_post_from_first_reply_hours',
'average_posts_per_author'
]
// Write the headers to the CSV file
writeHeader(csvPath, Array.from(headers))
data.forEach(discussion => {
const summary = topicSummary(discussion)
const topicDetails = {
topic_id: discussion.topicId,
topic_title: stripHTML(escapeComment(discussion.topicTitle)),
//topic_message: stripHTML(escapeComment(discussion.topicMessage)),
topic_author_id: discussion.topicAuthorId,
topic_author_name: escapeComment(discussion.topicAuthorName),
topic_created_at: toPacificTimeString(discussion.topicCreatedAt),
topic_posted_at: toPacificTimeString(discussion.topicPostedAt),
number_of_posts: summary.numberOfPosts,
median_posts_word_count: summary.medianWordCount,
average_time_to_post_hours: summary.averageTimeDiff,
first_reply_timestamp: toPacificTimeString(summary.firstReplyTimestamp),
average_time_to_post_from_first_reply_hours: summary.averageTimeToPostFromFirst,
average_posts_per_author: summary.averagePostsPerAuthor
}
appendRow(csvPath, Object.values(topicDetails))
})
}
module.exports = writeSummaryToCSV