Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

api_service.py 3.2KB

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