You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

langfuse_service.py 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #
  2. # Copyright 2025 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, TenantLangfuse
  19. from api.db.services.common_service import CommonService
  20. from api.utils import current_timestamp, datetime_format
  21. class TenantLangfuseService(CommonService):
  22. """
  23. All methods that modify the status should be enclosed within a DB.atomic() context to ensure atomicity
  24. and maintain data integrity in case of errors during execution.
  25. """
  26. model = TenantLangfuse
  27. @classmethod
  28. @DB.connection_context()
  29. def filter_by_tenant(cls, tenant_id):
  30. fields = [cls.model.host, cls.model.secret_key, cls.model.public_key]
  31. try:
  32. keys = cls.model.select(*fields).where(cls.model.tenant_id == tenant_id).first()
  33. return keys
  34. except peewee.DoesNotExist:
  35. return None
  36. @classmethod
  37. def update_by_tenant(cls, tenant_id, langfuse_keys):
  38. fields = ["tenant_id", "host", "secret_key", "public_key"]
  39. return cls.model.update(**langfuse_keys).fields(*fields).where(cls.model.tenant_id == tenant_id).execute()
  40. @classmethod
  41. def save(cls, **kwargs):
  42. kwargs["create_time"] = current_timestamp()
  43. kwargs["create_date"] = datetime_format(datetime.now())
  44. kwargs["update_time"] = current_timestamp()
  45. kwargs["update_date"] = datetime_format(datetime.now())
  46. obj = cls.model.create(**kwargs)
  47. return obj