-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(DOCSP-33419) [ANALYTICS] Operational Views (#192)
- Loading branch information
Showing
4 changed files
with
193 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { MongoClient, ObjectId } from "mongodb"; | ||
import { assertEnvVars } from "chat-core"; | ||
import { Message } from "chat-server"; | ||
|
||
import "dotenv/config"; | ||
|
||
const { MONGODB_DATABASE_NAME, MONGODB_CONNECTION_URI } = assertEnvVars({ | ||
MONGODB_DATABASE_NAME: "", | ||
MONGODB_CONNECTION_URI: "", | ||
}); | ||
|
||
type Rating = true | false | null; | ||
|
||
export type MessageByRating = { | ||
_id: Rating; | ||
rating: Rating; | ||
count: number; | ||
uniqueIps: string[]; | ||
conversationIds: ObjectId[]; | ||
messages: (Message & { | ||
conversationId: ObjectId; | ||
messageId: ObjectId; | ||
index: number; | ||
})[]; | ||
}; | ||
|
||
(async function main() { | ||
const client = await MongoClient.connect(MONGODB_CONNECTION_URI); | ||
try { | ||
const db = client.db(MONGODB_DATABASE_NAME); | ||
await db.createCollection<MessageByRating>("messages_by_rating", { | ||
viewOn: "conversations", | ||
pipeline: [ | ||
{ | ||
$match: { | ||
messages: { | ||
$elemMatch: { | ||
role: "user", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
$unwind: { | ||
path: "$messages", | ||
includeArrayIndex: "messageIndex", | ||
}, | ||
}, | ||
{ | ||
$match: { | ||
"messages.role": "assistant", | ||
}, | ||
}, | ||
{ | ||
$group: { | ||
_id: "$messages.rating", | ||
count: { | ||
$sum: 1, | ||
}, | ||
uniqueIps: { | ||
$addToSet: "$ipAddress", | ||
}, | ||
conversationIds: { | ||
$addToSet: "$_id", | ||
}, | ||
messages: { | ||
$push: { | ||
$mergeObjects: [ | ||
"$messages", | ||
{ | ||
conversationId: "$_id", | ||
messageId: "$messages.id", | ||
index: "$messageIndex", | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
$addFields: { | ||
rating: "$_id", | ||
}, | ||
}, | ||
], | ||
}); | ||
} finally { | ||
await client.close(); | ||
} | ||
})(); |
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,101 @@ | ||
import { MongoClient, ObjectId } from "mongodb"; | ||
import { assertEnvVars } from "chat-core"; | ||
import { Message } from "chat-server"; | ||
|
||
import "dotenv/config"; | ||
|
||
const { MONGODB_DATABASE_NAME, MONGODB_CONNECTION_URI } = assertEnvVars({ | ||
MONGODB_DATABASE_NAME: "", | ||
MONGODB_CONNECTION_URI: "", | ||
}); | ||
|
||
const NUM_REFERENCES = 250; | ||
|
||
export type TopReference = { | ||
_id: string; | ||
count: number; | ||
conversationIds: ObjectId[]; | ||
messages: (Message & { | ||
conversationId: ObjectId; | ||
messageId: ObjectId; | ||
index: number; | ||
})[]; | ||
}; | ||
|
||
(async function main() { | ||
const client = await MongoClient.connect(MONGODB_CONNECTION_URI); | ||
try { | ||
const db = client.db(MONGODB_DATABASE_NAME); | ||
await db.createCollection<TopReference>( | ||
`top_${NUM_REFERENCES}_references`, | ||
{ | ||
viewOn: "conversations", | ||
pipeline: [ | ||
{ | ||
$match: { | ||
"messages.role": "user", | ||
}, | ||
}, | ||
{ | ||
$unwind: { | ||
path: "$messages", | ||
includeArrayIndex: "messageIndex", | ||
}, | ||
}, | ||
{ | ||
$match: { | ||
"messages.role": "assistant", | ||
"messages.references.0": { | ||
$exists: true, | ||
}, | ||
}, | ||
}, | ||
{ | ||
$addFields: { | ||
references: "$messages.references", | ||
}, | ||
}, | ||
{ | ||
$unwind: { | ||
path: "$references", | ||
includeArrayIndex: "referenceIndex", | ||
}, | ||
}, | ||
{ | ||
$group: { | ||
_id: "$references.title", | ||
count: { | ||
$sum: 1, | ||
}, | ||
conversationIds: { | ||
$addToSet: "$_id", | ||
}, | ||
messages: { | ||
$push: { | ||
$mergeObjects: [ | ||
"$messages", | ||
{ | ||
conversationId: "$_id", | ||
messageId: "$messages.id", | ||
index: "$messageIndex", | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
$sort: { | ||
count: -1, | ||
}, | ||
}, | ||
{ | ||
$limit: NUM_REFERENCES, | ||
}, | ||
], | ||
} | ||
); | ||
} finally { | ||
await client.close(); | ||
} | ||
})(); |
This file was deleted.
Oops, something went wrong.