@mulingai-npm/redis 2.0.0 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/enums/redis-database.d.ts +1 -2
- package/dist/enums/redis-database.js +1 -2
- package/dist/managers/mulingstream-chunk-manager.js +9 -3
- package/dist/managers/mulingstream-listener-manager.d.ts +5 -0
- package/dist/managers/mulingstream-listener-manager.js +26 -0
- package/dist/redis-client.d.ts +1 -0
- package/dist/redis-client.js +6 -0
- package/package.json +1 -1
|
@@ -4,6 +4,5 @@ exports.RedisDatabase = void 0;
|
|
|
4
4
|
var RedisDatabase;
|
|
5
5
|
(function (RedisDatabase) {
|
|
6
6
|
RedisDatabase[RedisDatabase["MULINGSTREAM_LISTENER"] = 0] = "MULINGSTREAM_LISTENER";
|
|
7
|
-
RedisDatabase[RedisDatabase["
|
|
8
|
-
RedisDatabase[RedisDatabase["MULINGSTREAM_CHUNK"] = 2] = "MULINGSTREAM_CHUNK";
|
|
7
|
+
RedisDatabase[RedisDatabase["MULINGSTREAM_CHUNK"] = 1] = "MULINGSTREAM_CHUNK";
|
|
9
8
|
})(RedisDatabase = exports.RedisDatabase || (exports.RedisDatabase = {}));
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MulingstreamChunkManager = void 0;
|
|
4
4
|
const EXPIRATION = 12 * 60 * 60; // 12 hours in seconds
|
|
5
|
+
const ROOM_ARRAY_LENGTH = 5; // keep only the last 5 elements
|
|
5
6
|
class MulingstreamChunkManager {
|
|
6
7
|
constructor(redisClient) {
|
|
7
8
|
this.redisClient = redisClient;
|
|
@@ -13,9 +14,8 @@ class MulingstreamChunkManager {
|
|
|
13
14
|
*/
|
|
14
15
|
async initRoom(roomId) {
|
|
15
16
|
// Check if the key already exists (JSON.GET)
|
|
16
|
-
const
|
|
17
|
-
if (
|
|
18
|
-
// Already exists
|
|
17
|
+
const isRoomExisting = await this.redisClient.jsonGet(`[${roomId}]`, '.');
|
|
18
|
+
if (isRoomExisting !== null) {
|
|
19
19
|
return false;
|
|
20
20
|
}
|
|
21
21
|
// Create an empty array at the root
|
|
@@ -58,6 +58,12 @@ class MulingstreamChunkManager {
|
|
|
58
58
|
*/
|
|
59
59
|
async addMulingstreamChunk(params) {
|
|
60
60
|
const { roomId, chunkNumber, language, start, end, duration, isFirst, isLast, theme, sttProviders, targetLanguages } = params;
|
|
61
|
+
// ensure we only keep the last 5 elements
|
|
62
|
+
const currentLength = await this.redisClient.jsonArrPop(`[${roomId}]`, '.', 0);
|
|
63
|
+
if (typeof currentLength === 'number' && currentLength >= ROOM_ARRAY_LENGTH) {
|
|
64
|
+
// remove the oldest (front of the array)
|
|
65
|
+
await this.redisClient.jsonArrPop(`[${roomId}]`, '.', 0);
|
|
66
|
+
}
|
|
61
67
|
// Build the chunk
|
|
62
68
|
const audioChunk = {
|
|
63
69
|
start,
|
|
@@ -21,4 +21,9 @@ export declare class MulingstreamListenerManager {
|
|
|
21
21
|
updateNameLanguage(listenerIdOrToken: string, name: string, language: string): Promise<boolean>;
|
|
22
22
|
updateSocketId(listenerIdOrToken: string, socketId: string): Promise<boolean>;
|
|
23
23
|
getTargetSocketIdsByRoomLanguage(roomId: string, language: string): Promise<string[]>;
|
|
24
|
+
/**
|
|
25
|
+
* Returns an array of unique languages for the given room, from highest frequency to lowest.
|
|
26
|
+
* If a listener has no language (empty string / undefined), it is ignored.
|
|
27
|
+
*/
|
|
28
|
+
getUniqueLanguagesByRoom(roomId: string): Promise<string[]>;
|
|
24
29
|
}
|
|
@@ -139,5 +139,31 @@ class MulingstreamListenerManager {
|
|
|
139
139
|
});
|
|
140
140
|
return filteredListeners.map((listener) => listener.socketId);
|
|
141
141
|
}
|
|
142
|
+
/**
|
|
143
|
+
* Returns an array of unique languages for the given room, from highest frequency to lowest.
|
|
144
|
+
* If a listener has no language (empty string / undefined), it is ignored.
|
|
145
|
+
*/
|
|
146
|
+
async getUniqueLanguagesByRoom(roomId) {
|
|
147
|
+
// 1) Fetch all listeners for the room.
|
|
148
|
+
// (This includes inactive ones, but feel free to filter out isActive === false if you only want active ones.)
|
|
149
|
+
const listeners = await this.getListenersByRoom(roomId);
|
|
150
|
+
// if I wanted to get only active listeners
|
|
151
|
+
// const listeners = (await this.getListenersByRoom(roomId)).filter(l => l.isActive);
|
|
152
|
+
// 2) Count how many times each language appears.
|
|
153
|
+
const languageCountMap = {};
|
|
154
|
+
for (const listener of listeners) {
|
|
155
|
+
// skip blank/unset language
|
|
156
|
+
if (!listener.language || !listener.language.trim()) {
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
// increment count
|
|
160
|
+
const lang = listener.language;
|
|
161
|
+
languageCountMap[lang] = (languageCountMap[lang] || 0) + 1;
|
|
162
|
+
}
|
|
163
|
+
// 3) Sort by frequency descending: highest count first
|
|
164
|
+
const sortedEntries = Object.entries(languageCountMap).sort(([, countA], [, countB]) => countB - countA);
|
|
165
|
+
// 4) Map back to just the language strings in order
|
|
166
|
+
return sortedEntries.map(([language]) => language);
|
|
167
|
+
}
|
|
142
168
|
}
|
|
143
169
|
exports.MulingstreamListenerManager = MulingstreamListenerManager;
|
package/dist/redis-client.d.ts
CHANGED
|
@@ -23,4 +23,5 @@ export declare class RedisClient {
|
|
|
23
23
|
jsonGet<T>(key: string, path: string): Promise<T | null>;
|
|
24
24
|
jsonDel(key: string, path: string): Promise<number>;
|
|
25
25
|
jsonArrAppend<T>(key: string, path: string, ...values: T[]): Promise<number>;
|
|
26
|
+
jsonArrPop(key: string, path: string, index?: number): Promise<any>;
|
|
26
27
|
}
|
package/dist/redis-client.js
CHANGED
|
@@ -81,5 +81,11 @@ class RedisClient {
|
|
|
81
81
|
const result = await this.client.call('JSON.ARRAPPEND', key, path, ...stringifiedValues);
|
|
82
82
|
return Number(result);
|
|
83
83
|
}
|
|
84
|
+
async jsonArrPop(key, path, index) {
|
|
85
|
+
if (index !== undefined) {
|
|
86
|
+
return this.client.call('JSON.ARRPOP', key, path, index);
|
|
87
|
+
}
|
|
88
|
+
return this.client.call('JSON.ARRPOP', key, path);
|
|
89
|
+
}
|
|
84
90
|
}
|
|
85
91
|
exports.RedisClient = RedisClient;
|