Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

api_service.py 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #
  2. # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from datetime import datetime
  17. import peewee
  18. from api.db.db_models import DB, API4Conversation, APIToken, Dialog
  19. from api.db.services.common_service import CommonService
  20. from api.utils import current_timestamp, datetime_format
  21. class APITokenService(CommonService):
  22. model = APIToken
  23. @classmethod
  24. @DB.connection_context()
  25. def used(cls, token):
  26. return cls.model.update({
  27. "update_time": current_timestamp(),
  28. "update_date": datetime_format(datetime.now()),
  29. }).where(
  30. cls.model.token == token
  31. )
  32. class API4ConversationService(CommonService):
  33. model = API4Conversation
  34. @classmethod
  35. @DB.connection_context()
  36. def append_message(cls, id, conversation):
  37. cls.update_by_id(id, conversation)
  38. return cls.model.update(round=cls.model.round + 1).where(cls.model.id==id).execute()
  39. @classmethod
  40. @DB.connection_context()
  41. def stats(cls, tenant_id, from_date, to_date, source=None):
  42. if len(to_date) == 10: to_date += " 23:59:59"
  43. return cls.model.select(
  44. cls.model.create_date.truncate("day").alias("dt"),
  45. peewee.fn.COUNT(
  46. cls.model.id).alias("pv"),
  47. peewee.fn.COUNT(
  48. cls.model.user_id.distinct()).alias("uv"),
  49. peewee.fn.SUM(
  50. cls.model.tokens).alias("tokens"),
  51. peewee.fn.SUM(
  52. cls.model.duration).alias("duration"),
  53. peewee.fn.AVG(
  54. cls.model.round).alias("round"),
  55. peewee.fn.SUM(
  56. cls.model.thumb_up).alias("thumb_up")
  57. ).join(Dialog, on=(cls.model.dialog_id == Dialog.id & Dialog.tenant_id == tenant_id)).where(
  58. cls.model.create_date >= from_date,
  59. cls.model.create_date <= to_date,
  60. cls.model.source == source
  61. ).group_by(cls.model.create_date.truncate("day")).dicts()