Ë
    ¼vëjá  ã                  óú   — d Z ddlmZ ddlZddlZddlmZ ddlmZ dZ	dZ
dZd	ed	ez  dez  d
œZ ej                  dej                  «      Z ed¬«       G d„ d«      «       Zdd„Zddd„Zdd„Zdd„Zdd„Zdd„Zy)zÒWhat the organisation's plan allows in storage: one figure for vectors, one for cloud objects, for every
product together. Read by code off the platform's own records; a plan that names no figure sets no limit.é    )ÚannotationsN)Ú	dataclass)ÚOptionalÚVector_DB_SpaceÚCloud_Storagei   i   )ÚKBÚMBÚGBÚTBz9^((?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?)\s*(KB|MB|GB|TB)?$T)Úfrozenc                  ó6   — e Zd ZU ded<   ded<   ded<   ded<   y)	ÚStoragePlanÚstrÚorganisation_idzOptional[str]Úplan_idúOptional[int]Úvector_bytesÚcloud_bytesN)Ú__name__Ú
__module__Ú__qualname__Ú__annotations__© ó    ú[/var/www/html/strategist-ai/venv/lib/python3.12/site-packages/galaxiq_utils/storage/plan.pyr   r      s   … àÓØÓØÓØÔr   r   c                óÀ   — t        | �| nd«      j                  dd«      j                  «       }|j                  «       r t	        |«      dkD  rt	        |«      t
        z  S dS )zNA plan column in MB as bytes; 'unlimited', blank or unreadable means no limit.NÚ ú,r   )r   ÚreplaceÚstripÚisdigitÚintr	   )ÚvalueÚtexts     r   Ú_mbr%      sN   € ä˜Ð)‰u¨rÓ2×:Ñ:¸3ÀÓC×IÑIÓK€DØ!Ÿ\™\œ^´°D³	¸A²Œ3ˆt‹9”r‰>ÐGÀ4ÐGr   c                ó`  — t        | �| nd«      j                  «       }t        j                  |«      }|syt	        |j                  d«      j                  dd«      «      }t        j                  |j                  d«      xs |xs dj                  «       «      }|�|dk  ryt        ||z  «      S )a  A storage figure as bytes, the way the platform writes it: the unit inside the value ("2GB", "512 MB",
    "1.5 TB") wins; else the feature's own allowanceUnit; else GB, the platform's default for a bare storage cap.
    'unlimited', blank or unreadable means no limit.Nr   é   r   é   r
   r   )r   r    ÚUNIT_IN_VALUEÚmatchÚfloatÚgroupr   ÚUNITSÚgetÚupperr"   )r#   Úunitr$   ÚmÚamountÚscales         r   Ú_storage_bytesr4      s™   € ô ˜Ð)‰u¨rÓ2×8Ñ8Ó:€DÜ×Ñ˜DÓ!€AÙØÜ�1—7‘7˜1“:×%Ñ% c¨2Ó.Ó/€FÜ�I‰I�q—w‘w˜q“zÒ1 TÒ1¨T×8Ñ8Ó:Ó;€EØ€}˜ !šØÜˆv˜‰~ÓÐr   c           	     ó(  — t        | t        «      rt        j                  | «      n| xs g } | D ��cg c]f  }t        |t        «      sŒ|j                  d«      xs g D ];  }t        |t        «      r)|j                  d«      |k(  r|j                  d«      dur|‘Œ= Œh }}}|D ]2  }t        |j                  d«      |j                  d«      «      }|€Œ0|c S  |D ]2  }t        |j                  d«      |j                  d«      «      }|€Œ0|c S  yc c}}w )	zàThe plan's figure for `key`, as bytes, the way AI tokens are read: the add-on total (includedAllowanceTotal)
    wins when the backend wrote one; otherwise the first feature carrying the key. None when the plan says nothing.ÚfeaturesÚ
featureKeyÚenabledFÚincludedAllowanceTotalÚallowanceUnitNÚcappingValue)Ú
isinstancer   ÚjsonÚloadsÚdictr.   r4   )ÚproductsÚkeyÚpÚfr6   ÚtotalÚgivens          r   Úfeature_allowancerF   .   s  € ô (2°(¼CÔ'@Œt�z‰z˜(Ô#ÀxÂ~ÐSU€HØ#÷ i�a¤z°!´TÕ':ÀQÇUÁUÈ:ÓEVÒE\ÐZ\ò i¸qÜ˜a¤Ô&¨1¯5©5°Ó+>À#Ò+EÈ!Ï%É%ÐPYÓJZÐbgÑJgò ð i�ð i€Hñ iàò ˆÜ˜qŸu™uÐ%=Ó>ÀÇÁÀoÓ@VÓWˆØÑØŠLðð ò ˆÜ˜qŸu™u ^Ó4°a·e±e¸OÓ6LÓMˆØÑØŠLðð ùóis   ±DÁADc                ó"   — t        | t        «      S ©N)rF   Ú
VECTOR_KEY)r@   s    r   Úvector_allowancerJ   ?   s   € Ü˜X¤zÓ2Ð2r   c                ó@   — t        | t        «      }|�|S t        |«      S )zŒThe Cloud_Storage feature when the plan carries one (the add-on path, same as tokens); until then the
    plan's storageAllocationMB column.)rF   Ú	CLOUD_KEYr%   )r@   Ú	column_mbÚfrom_features      r   Úcloud_allowancerO   C   s$   € ô % X¬yÓ9€LØ'Ð3ˆ<ÐG¼¸Y»ÐGr   c           	     óü   — | j                  dt        |«      j                  d«      f«      j                  «       }|sy|d   \  }}}}t	        t        |«      |rt        |«      ndt        |«      t        ||«      «      S )zfThe organisation behind this tenant and what its newest live plan allows. None without a subscription.aõ  SELECT o.id AS org, p.id AS plan, p."productFeatures" AS features, p."storageAllocationMB" AS cloud_mb FROM "Organisations" o JOIN "Subscriptions" s ON s."organisationId" = o.id LEFT JOIN "Plans" p ON p.id = s."planId" WHERE o."tenantId"::text = %s   AND (s."isActive" = TRUE OR lower(coalesce(s.status, '')) IN ('active', 'trial', 'trialing')        OR (s."trialStart" IS NOT NULL AND s."trialEnd" IS NOT NULL AND now() BETWEEN s."trialStart" AND s."trialEnd")) ORDER BY s."createdAt" DESC NULLS LASTÚorg_Nr   )Úexecuter   ÚremoveprefixÚfetchallr   rJ   rO   )ÚconnÚ	tenant_idÚrowsÚorgÚplanr6   Úcloud_mbs          r   Úread_storage_planr[   J   s   € à�<‰<ð	1ô 
ˆY‹×	$Ñ	$ VÓ	,Ð.ó	0÷ 19±³
ð 	ñ ØØ$(¨¡GÑ!€Cˆˆx˜Ü”s˜3“x©d¤ T¤¸Ô>NÈxÓ>XÔZiÐjrÐt|ÓZ}Ó~Ð~r   )Úreturnr   rH   )rA   r   r\   r   )rV   r   r\   zOptional[StoragePlan])Ú__doc__Ú
__future__r   r=   ÚreÚdataclassesr   Útypingr   rI   rL   r	   r-   ÚcompileÚIr)   r   r%   r4   rF   rJ   rO   r[   r   r   r   ú<module>rd      s™   ðñmå "ã Û 	Ý !Ý à€
Ø€	Ø€Ø˜2 T¨B¡Y°kÀBÑ6FÑG€Ø�—
‘
ÐWÐY[×Y]ÑY]Ó^€ñ �$Ô÷ð ó ðóHôóó"3óHôr   