22import logging
33import os
44
5- from refiner .models .schema import Schema
5+ from refiner .models .offchain_schema import OffChainSchema
6+ from refiner .models .output import Output
67from refiner .transformer .user_transformer import UserTransformer
78from refiner .config import settings
8- from refiner .utils .ipfs import upload_json_to_ipfs
9+ from refiner .utils .encrypt import encrypt_file
10+ from refiner .utils .ipfs import upload_file_to_ipfs , upload_json_to_ipfs
911
1012class Refiner :
1113 def __init__ (self ):
1214 self .db_path = os .path .join (settings .OUTPUT_DIR , 'db.libsql' )
1315
14- def transform (self ) -> None :
16+ def transform (self ) -> Output :
1517 """Transform all input files into the database."""
1618 logging .info ("Starting data transformation" )
19+ output = Output ()
1720
1821 # Iterate through files and transform data
1922 for input_filename in os .listdir (settings .INPUT_DIR ):
@@ -28,20 +31,28 @@ def transform(self) -> None:
2831 transformer .process (input_data )
2932 logging .info (f"Transformed { input_filename } " )
3033
31- # Create a schema file
34+ # Create a schema based on the SQLAlchemy schema
35+ schema = OffChainSchema (
36+ name = settings .SCHEMA_NAME ,
37+ version = settings .SCHEMA_VERSION ,
38+ description = settings .SCHEMA_DESCRIPTION ,
39+ dialect = settings .SCHEMA_DIALECT ,
40+ schema = transformer .get_schema ()
41+ )
42+ output .schema = schema
43+
44+ # Upload the schema to IPFS
3245 schema_file = os .path .join (settings .OUTPUT_DIR , 'schema.json' )
3346 with open (schema_file , 'w' ) as f :
34- schema = Schema (
35- name = settings .SCHEMA_NAME ,
36- version = settings .SCHEMA_VERSION ,
37- description = settings .SCHEMA_DESCRIPTION ,
38- dialect = settings .SCHEMA_DIALECT ,
39- schema = transformer .get_schema ()
40- )
4147 json .dump (schema .model_dump (), f , indent = 4 )
42- logging .info (f"Schema saved to { schema_file } " )
43- # ipfs_hash = upload_json_to_ipfs(schema.model_dump())
44- # logging.info(f"Schema uploaded to IPFS with hash: {ipfs_hash}")
48+ schema_ipfs_hash = upload_json_to_ipfs (schema .model_dump ())
49+ logging .info (f"Schema uploaded to IPFS with hash: { schema_ipfs_hash } " )
50+
51+ # Encrypt and upload the database to IPFS
52+ encrypted_path = encrypt_file (settings .REFINEMENT_ENCRYPTION_KEY , self .db_path )
53+ ipfs_hash = upload_file_to_ipfs (encrypted_path )
54+ output .refinement_url = f"https://ipfs.vana.org/ipfs/{ ipfs_hash } "
4555 continue
4656
4757 logging .info ("Data transformation completed successfully" )
58+ return output
0 commit comments