Files

99 lines
3.8 KiB
Python
Executable File

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""ar_heal.py — поднять каналы ar-* (api-route) в iishko: status, abilities, model_limits, квоты юзеров.
usage: python3 ar_heal.py [--apply]
"""
import json
import sqlite3
import sys
import time
DB = "/opt/grok-gateway/new-api/one-api.db"
POOL = "/root/ar_keys.jsonl"
BASE = "https://www.api-route.com"
MODELS = ["gpt-6.1-sol", "gpt-6-sol", "gpt-5.6-sol", "gpt-6-astra"]
GROUPS = ["default", "premium", "t1", "t2", "t3", "t4"]
TAG = "ar"
PRIO = 660
MAP = '{"401":429,"402":429,"403":429}'
APPLY = "--apply" in sys.argv
keys, seen = [], set()
for l in open(POOL, encoding="utf-8"):
l = l.strip()
if not l:
continue
try:
k = json.loads(l).get("key")
except Exception:
continue
if k and k not in seen:
seen.add(k)
keys.append(k)
con = sqlite3.connect(DB, timeout=300)
con.execute("pragma busy_timeout=300000")
cur = con.cursor()
now = int(time.time())
cids = []
for k in keys:
name = "ar-" + k[-6:]
row = cur.execute("select id from channels where name=?", (name,)).fetchone()
if row:
cid = row[0]
if APPLY:
cur.execute(
"""update channels set status=1, auto_ban=0, priority=?, models=?, base_url=?,
"group"=?, key=?, status_code_mapping=?, tag=? where id=?""",
(PRIO, ",".join(MODELS), BASE, ",".join(GROUPS), k, MAP, TAG, cid),
)
elif APPLY:
cur.execute(
"""insert into channels (type,key,status,name,weight,created_time,test_time,base_url,other,
balance,balance_updated_time,models,"group",used_quota,priority,auto_ban,tag,channel_info,
status_code_mapping)
values (1,?,1,?,1,?,?,?,'',0,?,?,?,0,?,0,?,'{}',?)""",
(k, name, now, now, BASE, now, ",".join(MODELS), ",".join(GROUPS), PRIO, TAG, MAP),
)
cid = cur.lastrowid
else:
cid = row[0]
cids.append(cid)
if APPLY:
for cid in cids:
for m in MODELS:
for g in GROUPS:
cur.execute(
"""insert into abilities ("group",model,channel_id,enabled,priority,weight,tag)
values (?,?,?,1,?,1,?)
on conflict("group",model,channel_id) do update set enabled=1,priority=?,weight=1""",
(g, m, cid, PRIO, TAG, PRIO),
)
# квоты юзеров: quota считается как quota - used_quota
u = cur.execute("select count(*) from users where quota - used_quota < 2000000000").fetchone()[0]
cur.execute("update users set quota = used_quota + 20000000000 where quota - used_quota < 2000000000")
# model_limits: дописать модели всем живым токенам
patched = 0
for tid, ml in cur.execute("select id, coalesce(model_limits,'') from tokens where status=1").fetchall():
have = [x for x in ml.split(",") if x]
need = [m for m in MODELS if m not in have]
if need:
cur.execute("update tokens set model_limits=? where id=?", (",".join(have + need), tid))
patched += 1
con.commit()
print("users_quota_fixed:", u, "| tokens_patched:", patched)
alive = cur.execute(
"""select count(distinct c.id) from channels c join abilities a on a.channel_id=c.id
where c.tag='ar' and a.model='gpt-6.1-sol' and a.enabled=1 and c.status=1"""
).fetchone()[0]
statuses = cur.execute("select status, count(*) from channels where name like 'ar-%' group by 1").fetchall()
abil = cur.execute(
"""select a.model, a.enabled, count(*) from abilities a
join channels c on c.id=a.channel_id where c.tag='ar' group by 1,2"""
).fetchall()
con.close()
print("keys:", len(keys), "| channels alive:", alive, "| statuses:", statuses)
print("abilities:", abil)