forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapps_count.py
More file actions
105 lines (82 loc) · 3.11 KB
/
Copy pathapps_count.py
File metadata and controls
105 lines (82 loc) · 3.11 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
103
104
105
import json
import os
import threading
from collections import defaultdict
from dotenv import load_dotenv
from models.app import UsageHistoryType
load_dotenv('../../.env')
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = '../../' + os.getenv('GOOGLE_APPLICATION_CREDENTIALS', '')
import firebase_admin
firebase_admin.initialize_app() # type: ignore[reportUnknownMemberType] # firebase_admin untyped
from typing import Dict, List
from database.apps import record_app_usage
from database.redis_db import get_enabled_apps, set_app_installs_count
from database._client import get_users_uid
import database.conversations as conversations_db
import database.chat as chat_db
def single(uid: str, data: Dict[str, int]) -> List[str]:
pids = get_enabled_apps(uid)
for pid in pids:
data[pid] += 1
return pids
def execute() -> None:
uids = get_users_uid()
data: Dict[str, int] = defaultdict(int)
threads: List[threading.Thread] = []
for uid in uids:
threads.append(
threading.Thread(
target=single,
args=(
uid,
data,
),
)
)
count = 20
chunks = [threads[i : i + count] for i in range(0, len(threads), count)]
for chunk in chunks:
[thread.start() for thread in chunk]
[thread.join() for thread in chunk]
print(json.dumps(data, indent=2))
for pid, count in data.items():
set_app_installs_count(pid, count)
def count_memory_prompt_plugins_trigger() -> None:
uids = get_users_uid()
def single(uid: str) -> None:
memories = conversations_db.get_conversations(uid, limit=1000)
print('user', uid, 'conversations', len(memories))
for memory in memories:
triggered = memory.get('plugins_results', [])
created_at = memory.get('created_at')
if triggered:
print('memory', memory['id'], 'triggered', len(triggered), 'plugins')
for trigger in triggered:
record_app_usage(
uid,
trigger['plugin_id'],
UsageHistoryType.memory_created_prompt,
conversation_id=memory['id'],
timestamp=created_at,
)
messages = chat_db.get_messages(uid, limit=1000)
print('user', uid, 'messages', len(messages))
for message in messages:
if pid := message.get('plugin_id'):
record_app_usage(
uid,
pid,
UsageHistoryType.chat_message_sent,
message_id=message['id'],
timestamp=message['created_at'],
)
threads: List[threading.Thread] = []
for uid in uids:
threads.append(threading.Thread(target=single, args=(uid,)))
count = 20
chunks = [threads[i : i + count] for i in range(0, len(threads), count)]
for chunk in chunks:
[thread.start() for thread in chunk]
[thread.join() for thread in chunk]
if __name__ == '__main__':
count_memory_prompt_plugins_trigger()