Package: hectiq-console
Hectiq ConsoleOverviewThe Hectiq Console Python Client is a comprehensive toolkit designed to interact with the Hectiq Console, a platform for managing various resources and incidents. This client provides functionalities to authenticate, manage resources, create incidents, handle files, and track metrics within the Hectiq environment.This service is for Hectiq's client only.OverviewMiddleware for APIInstallationStarletteFastAPISend metricsSend annotationsFunctional for workersFeaturesInstallationAuthenticationCommand Line InterfaceCredential StorageManaging ResourcesCreating IncidentsFile HandlingMetricsAnnotationsTiming Code ExecutionContactMiddleware for APIInstallationpipinstall--upgrade'hectiq-console[starlette]'StarletteBelow is an example how to use the middleware for Starlette application.fromstarlette.applicationsimportStarlettefromstarlette.middlewareimportMiddlewarefromhectiq_console.starletteimportHectiqConsoleStarletteMiddlewaremiddleware=[Middleware(HectiqConsoleStarletteMiddleware,resource="hectiq-test")]app=Starlette(middleware=middleware)FastAPIBelow is an example how to use the middleware for FastAPI. It shares the same Middleware as Starlette.importtimeimportrandomfromfastapiimportFastAPI,Requestfromhectiq_console.starletteimportHectiqConsoleStarletteMiddleware,store_metricsapp=FastAPI(title="Demo application")app.add_middleware(HectiqConsoleStarletteMiddleware,resource="hectiq-demo",include_paths=["/predict"])@app.get("/")asyncdefroot():return{"message":"🚨 This route is not monitored by the hectiq console."}@app.get("/predict")asyncdefroot(request:Request):# Store a random numberreturn{"message":"✅ This route is monitored by the hectiq console."}Send metricsBy default, the middleware stores the latency and counts of the monitored requests. You may add other metrics using thestore_metricsin a request handler.@app.get("/predict")asyncdefroot(request:Request):store_metrics(request=request,key=metrics_key,value=metrics_value)You can send as many metrics in the same request as you want. However, if you use the same key in the same request, the previous value is overwritten by the new one.Do not forget to create the metrics definition in the console beforehand. Otherwise, you'll get an error at handshake.Send annotationsAnnotations are useful to track predictions in your application. For example, you may want to track the result of a model. Use the methodstore_annotation. You can send as many annotations as you want in the same request.@app.get("/predict")asyncdefroot(request:Request):store_annotation(request=request,inputs={"y":[0,1,2,3,4],"x":[0,1,2,3,4]},outputs={"y_true":[5,6,7,8],"y_pred":[5,6,7,8]},label="high-accuracy",metadata={"model":"demo-model","accuracy":0.89})Functional for workersFeaturesAuthentication: Securely authenticate with the Hectiq Console using API keys.Resource Management: Set and retrieve resources within the Hectiq Console.Incident Management: Create incidents with detailed descriptions and associated files.File Handling: Add files to resources and upload them to the Hectiq Console.Metrics Tracking: Add and manage metrics for resources.Annotation Downloading: Download annotations related to resources.Code Timing: Utilize a context manager to time code execution and add metrics to a resource.InstallationTo use this client, ensure you have Python installed on your system. Install the package via pip:pipinstallhectiq-consoleAuthenticationCommand Line InterfaceSome requests require authentification. Run the CLI tool to authenticate:hectiq-consoleauthenticateYou'll be prompted to enter your email and password. The CLI tool will automatically generate and store a new API key for you. These steps are described below:Login: Enter your email and password.API Key Alias: Set an alias for your new API key. The default is your hostname.Select Organization: Choose from the list of organizations associated with your account.API Key Generation: Automatically generates and stores a new API key.API Keys are associated with an organization. If you have access to multiple organizations, you will be prompted to select an organization. If you only have access to one organization, the CLI tool will automatically select it for you.Once you have a valid API key in the credentials file (or environment variable), you can use the client to make authenticated requests. If you have multiple organizations in your credentials file, you can switch organization using theset_organizationmethod.importhectiq_console.functionalashchc.set_resource("demo-resource")hc.set_organization("hectiq-ai")# Required if you have multiple organizationsis_logged=hc.authenticate()Credential StorageThe API key is stored locally in the~/.hectiq-console/credentials.tomlfile. This file is created if it does not exist. If you need to change the default location, you can set theHECTIQ_CONSOLE_CREDENTIALS_FILEenvironment variable to the path of your choice.You can also set theHECTIQ_CONSOLE_API_KEYenvironment variable to your API key. This variable is used by the client to authenticate requests. If the environment variable is set, the client will not use the credentials file.Managing ResourcesSet your resource using theset_resourcemethod. Place this method at the beginning of your script. It uses a ContextVar to store the resource. Therefore, you can use it in a multi-threaded environment. If you set the resource, it will be used for all requests. Otherwise, you'll need to specify the resource in each request.importhectiq_console.functionalashchc.set_resource("resource_id")Creating IncidentsCreate incidents related to a resource:importhectiq_console.functionalashchc.create_incident(title="Incident Title",description="Detailed description")You can also add files to the incident:importhectiq_console.functionalashchc.create_incident(title="Incident Title",description="Detailed description",filenames=["path/to/your/file"])File HandlingAdd and upload files to a resource:importhectiq_console.functionalashchc.add_file("path/to/your/file")MetricsAdd metrics to a resource:importhectiq_console.functionalashchc.add_metrics(name="metric_name",value=123)AnnotationsDownload annotations related to a resource:importhectiq_console.functionalashcannotation=hc.download_annotation("annotation_id")Timing Code ExecutionTime a block of code execution:importhectiq_console.functionalashcwithhc.timer_context("timer_name"):# Your code herepassThe timer name is used as the metric name. The timer context manager automatically adds the metric to the resource.ContactFor support or inquiries, please contact the project maintainer at [info@hectiq.ai].