@xuda.io/account_module 1.2.2156 → 1.2.2158
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/index.mjs +93 -1
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -3381,6 +3381,98 @@ export const save_cache_hit = async function (_id) {
|
|
|
3381
3381
|
///////////////////////////////
|
|
3382
3382
|
|
|
3383
3383
|
export const get_account_ai_usage = async function (req, job_id, headers) {
|
|
3384
|
+
const d = new Date();
|
|
3385
|
+
const curr_month = Number(String(d.getMonth() + 1).padStart(2, '0')); // months are 0-based
|
|
3386
|
+
const curr_year = d.getFullYear();
|
|
3387
|
+
|
|
3388
|
+
const { uid, year_from = curr_year, month_from = curr_month, day_from = 0, year_to = curr_year, month_to = curr_month, day_to = 99 } = req;
|
|
3389
|
+
try {
|
|
3390
|
+
const app_id = await get_account_default_project_id(uid);
|
|
3391
|
+
|
|
3392
|
+
const timestamp_from = new Date(Number(year_from) || year, month - 1, day).getTime();
|
|
3393
|
+
|
|
3394
|
+
const ai_usage = await db_module.get_couch_view('xuda_usage', 'ai_usage', {
|
|
3395
|
+
startkey: [uid, year_from || curr_year, month_from || curr_month, day_from || 0],
|
|
3396
|
+
endkey: [uid, year_to || curr_year, month_to || curr_month, day_to || 99],
|
|
3397
|
+
reduce: true,
|
|
3398
|
+
group_level: 999,
|
|
3399
|
+
});
|
|
3400
|
+
// console.log('ai_usage', ai_usage.rows);
|
|
3401
|
+
debugger;
|
|
3402
|
+
let total_usage = 0;
|
|
3403
|
+
let profile = {};
|
|
3404
|
+
|
|
3405
|
+
for (const row of ai_usage.data.rows) {
|
|
3406
|
+
const user = row.key[0];
|
|
3407
|
+
const year = row.key[1];
|
|
3408
|
+
const month = row.key[2];
|
|
3409
|
+
const day = row.key[3];
|
|
3410
|
+
const account_profile_id = row.key[4];
|
|
3411
|
+
const uid = row.key[5];
|
|
3412
|
+
const model = row.key[6];
|
|
3413
|
+
const input_tokens = row.value[0];
|
|
3414
|
+
const output_tokens = row.value[1];
|
|
3415
|
+
const input_credits = row.value[2];
|
|
3416
|
+
const output_credits = row.value[3];
|
|
3417
|
+
if (!profile[account_profile_id]) {
|
|
3418
|
+
profile[account_profile_id] = {};
|
|
3419
|
+
}
|
|
3420
|
+
if (!profile[account_profile_id][uid]) {
|
|
3421
|
+
profile[account_profile_id][uid] = 0;
|
|
3422
|
+
}
|
|
3423
|
+
|
|
3424
|
+
profile[account_profile_id][uid] += input_credits + output_credits;
|
|
3425
|
+
total_usage += input_credits + output_credits;
|
|
3426
|
+
}
|
|
3427
|
+
|
|
3428
|
+
const ai_credits = await db_module.get_couch_view('xuda_billing', 'ai_credits', {
|
|
3429
|
+
startkey: [uid, ''],
|
|
3430
|
+
endkey: [uid, 'zzz'],
|
|
3431
|
+
reduce: true,
|
|
3432
|
+
group_level: 999,
|
|
3433
|
+
});
|
|
3434
|
+
|
|
3435
|
+
let credits_obj = {},
|
|
3436
|
+
total_credits = 0;
|
|
3437
|
+
for (const row of ai_credits.data.rows) {
|
|
3438
|
+
const user = row.key[0];
|
|
3439
|
+
const source = row.key[1];
|
|
3440
|
+
|
|
3441
|
+
const credits = row.value;
|
|
3442
|
+
|
|
3443
|
+
total_credits += credits;
|
|
3444
|
+
|
|
3445
|
+
if (!credits_obj[source]) {
|
|
3446
|
+
credits_obj[source] = 0;
|
|
3447
|
+
}
|
|
3448
|
+
credits_obj[source] += credits;
|
|
3449
|
+
}
|
|
3450
|
+
|
|
3451
|
+
let data = { credits: { ...credits_obj, total: total_credits }, usage: { profile, projects: [], total: total_usage } };
|
|
3452
|
+
|
|
3453
|
+
if (job_id) {
|
|
3454
|
+
data.profile_info = {};
|
|
3455
|
+
data.user_info = {};
|
|
3456
|
+
for (const [key, val] of Object.entries(data?.usage?.profile || {})) {
|
|
3457
|
+
try {
|
|
3458
|
+
data.profile_info[key] = (await db_module.get_app_couch_doc_native(app_id, key))?.profile_name;
|
|
3459
|
+
|
|
3460
|
+
for (const [user_id, val2] of Object.entries(val || {})) {
|
|
3461
|
+
try {
|
|
3462
|
+
data.user_info[user_id] = (await db_module.get_couch_doc_native('xuda_accounts', user_id))?.account_info?.full_name;
|
|
3463
|
+
} catch (error) {}
|
|
3464
|
+
}
|
|
3465
|
+
} catch (error) {}
|
|
3466
|
+
}
|
|
3467
|
+
}
|
|
3468
|
+
|
|
3469
|
+
return { code: 55, data };
|
|
3470
|
+
} catch (err) {
|
|
3471
|
+
return { code: -55, data: err.message };
|
|
3472
|
+
}
|
|
3473
|
+
};
|
|
3474
|
+
|
|
3475
|
+
export const get_account_ai_usage_old = async function (req, job_id, headers) {
|
|
3384
3476
|
const { uid, year_from, month_from, day_from, year_to, month_to, day_to } = req;
|
|
3385
3477
|
try {
|
|
3386
3478
|
const app_id = await get_account_default_project_id(uid);
|
|
@@ -3396,7 +3488,7 @@ export const get_account_ai_usage = async function (req, job_id, headers) {
|
|
|
3396
3488
|
group_level: 999,
|
|
3397
3489
|
});
|
|
3398
3490
|
// console.log('ai_usage', ai_usage.rows);
|
|
3399
|
-
|
|
3491
|
+
debugger;
|
|
3400
3492
|
let total_usage = 0;
|
|
3401
3493
|
let profile = {};
|
|
3402
3494
|
|