Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

api_service.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 get_list(cls,dialog_id, tenant_id,
  37. page_number, items_per_page, orderby, desc, id):
  38. sessions = cls.model.select().where(cls.model.dialog_id ==dialog_id)
  39. if id:
  40. sessions = sessions.where(cls.model.id == id)
  41. if desc:
  42. sessions = sessions.order_by(cls.model.getter_by(orderby).desc())
  43. else:
  44. sessions = sessions.order_by(cls.model.getter_by(orderby).asc())
  45. sessions = sessions.where(cls.model.user_id == tenant_id)
  46. sessions = sessions.paginate(page_number, items_per_page)
  47. return list(sessions.dicts())
  48. @classmethod
  49. @DB.connection_context()
  50. def append_message(cls, id, conversation):
  51. cls.update_by_id(id, conversation)
  52. return cls.model.update(round=cls.model.round + 1).where(cls.model.id == id).execute()
  53. @classmethod
  54. @DB.connection_context()
  55. def stats(cls, tenant_id, from_date, to_date, source=None):
  56. if len(to_date) == 10:
  57. to_date += " 23:59:59"
  58. return cls.model.select(
  59. cls.model.create_date.truncate("day").alias("dt"),
  60. peewee.fn.COUNT(
  61. cls.model.id).alias("pv"),
  62. peewee.fn.COUNT(
  63. cls.model.user_id.distinct()).alias("uv"),
  64. peewee.fn.SUM(
  65. cls.model.tokens).alias("tokens"),
  66. peewee.fn.SUM(
  67. cls.model.duration).alias("duration"),
  68. peewee.fn.AVG(
  69. cls.model.round).alias("round"),
  70. peewee.fn.SUM(
  71. cls.model.thumb_up).alias("thumb_up")
  72. ).join(Dialog, on=((cls.model.dialog_id == Dialog.id) & (Dialog.tenant_id == tenant_id))).where(
  73. cls.model.create_date >= from_date,
  74. cls.model.create_date <= to_date,
  75. cls.model.source == source
  76. ).group_by(cls.model.create_date.truncate("day")).dicts()