-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GitOrigin-RevId: deb95169db5f62275d7569eba67d433d04bc56fd
- Loading branch information
1 parent
45f8770
commit 1a9f9f0
Showing
7 changed files
with
329 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import { SummaryStats } from "./stats_util"; | ||
import { Comment } from "./types"; | ||
|
||
const TEST_COMMENTS = [ | ||
{ | ||
id: "1", | ||
text: "comment1", | ||
voteTalliesByGroup: { | ||
"0": { | ||
agreeCount: 10, | ||
disagreeCount: 5, | ||
passCount: 0, | ||
totalCount: 15, | ||
}, | ||
"1": { | ||
agreeCount: 5, | ||
disagreeCount: 10, | ||
passCount: 5, | ||
totalCount: 20, | ||
}, | ||
}, | ||
}, | ||
{ | ||
id: "2", | ||
text: "comment2", | ||
voteTalliesByGroup: { | ||
"0": { | ||
agreeCount: 2, | ||
disagreeCount: 5, | ||
passCount: 3, | ||
totalCount: 10, | ||
}, | ||
"1": { | ||
agreeCount: 5, | ||
disagreeCount: 3, | ||
passCount: 2, | ||
totalCount: 10, | ||
}, | ||
}, | ||
}, | ||
]; | ||
|
||
describe("StatsUtilTest", () => { | ||
it("should get the total number of votes from multiple comments", () => { | ||
const summaryStats = new SummaryStats(TEST_COMMENTS); | ||
expect(summaryStats.voteCount).toEqual(55); | ||
}); | ||
|
||
it("SummaryStats should get the total number of comments", () => { | ||
const summaryStats = new SummaryStats(TEST_COMMENTS); | ||
expect(summaryStats.commentCount).toEqual(2); | ||
}); | ||
|
||
it("should count comments by topic", () => { | ||
const comments: Comment[] = [ | ||
{ | ||
id: "1", | ||
text: "comment 1", | ||
topics: [{ name: "Topic A", subtopics: [{ name: "Subtopic A.1" }] }], | ||
}, | ||
{ | ||
id: "2", | ||
text: "comment 2", | ||
topics: [{ name: "Topic A", subtopics: [{ name: "Subtopic A.1" }] }], | ||
}, | ||
{ | ||
id: "3", | ||
text: "comment 3", | ||
topics: [{ name: "Topic A", subtopics: [{ name: "Subtopic A.2" }] }], | ||
}, | ||
]; | ||
|
||
const expectedTopicStats = [ | ||
{ | ||
name: "Topic A", | ||
commentCount: 3, | ||
subtopicStats: [ | ||
{ name: "Subtopic A.1", commentCount: 2 }, | ||
{ name: "Subtopic A.2", commentCount: 1 }, | ||
], | ||
}, | ||
]; | ||
expect(new SummaryStats(comments).getStatsByTopic()).toEqual(expectedTopicStats); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Utils to get statistical information from a deliberation | ||
|
||
import { Comment } from "./types"; | ||
import { groupCommentsBySubtopic } from "./sensemaker_utils"; | ||
|
||
// Statistics to include in the summary. | ||
export class SummaryStats { | ||
comments: Comment[]; | ||
constructor(comments: Comment[]) { | ||
this.comments = comments; | ||
} | ||
|
||
private getCommentVoteCount(comment: Comment): number { | ||
let count = 0; | ||
for (const groupName in comment.voteTalliesByGroup) { | ||
const groupCount = comment.voteTalliesByGroup[groupName].totalCount; | ||
if (groupCount > 0) { | ||
count += groupCount; | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
// The total number of votes in all comments in a deliberation. | ||
get voteCount(): number { | ||
return this.comments.reduce((sum: number, comment: Comment) => { | ||
return sum + this.getCommentVoteCount(comment); | ||
}, 0); | ||
} | ||
|
||
// The total number of comments in a deliberation. | ||
get commentCount(): number { | ||
return this.comments.length; | ||
} | ||
|
||
/** | ||
* Counts the number of comments associated with each topic and subtopic. | ||
* | ||
* @param commentsByTopic A nested map where keys are topic names, values are maps | ||
* where keys are subtopic names, and values are maps where | ||
* keys are comment IDs and values are comment texts. | ||
* @returns An array of `TopicStats` objects. | ||
*/ | ||
getStatsByTopic(): TopicStats[] { | ||
const commentsByTopic = groupCommentsBySubtopic(this.comments); | ||
const topicStats: TopicStats[] = []; | ||
|
||
for (const topicName in commentsByTopic) { | ||
const subtopics = commentsByTopic[topicName]; | ||
const subtopicStats: TopicStats[] = []; | ||
let totalTopicComments = 0; | ||
|
||
for (const subtopicName in subtopics) { | ||
const commentCount = Object.keys(subtopics[subtopicName]).length; | ||
totalTopicComments += commentCount; | ||
subtopicStats.push({ name: subtopicName, commentCount }); | ||
} | ||
|
||
topicStats.push({ | ||
name: topicName, | ||
commentCount: totalTopicComments, | ||
subtopicStats: subtopicStats, | ||
}); | ||
} | ||
|
||
return topicStats; | ||
} | ||
} | ||
|
||
/** | ||
* Represents statistics about a topic and its subtopics. | ||
*/ | ||
export interface TopicStats { | ||
name: string; | ||
commentCount: number; | ||
subtopicStats?: TopicStats[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.