Inspect SQL History and Profiles

Use the AI Studio SDK to paginate through workspace SQL execution history, obtain a statement ID, and read execution details and profiles for that statement.

Task flow

First, read execution history pages through the workspace SQL service to obtain a target statement ID; then query execution status, error details, and the execution profile for that statement ID.

Workspace SQL Service
         │
         ▼
Query SQL execution history (offset + limit)
         │
         ▼
    Statement ID
  ├── Read execution details (statement status and error text)
  └── Read execution profile (profile availability and message)

Prerequisites

  1. Complete the endpoint, personal access token, and workspace setup in Use the SDK.

  2. Statement ID (when reading details and a profile): obtain one from the SQL execution history step below, or record the statement ID returned in Run SQL Queries.

Query SQL execution history

The example below reads up to 20 recent SQL execution records starting from offset 0, printing the total count along with each record’s statement ID, database name, status, and error message. To read subsequent pages, advance the offset by the number of returned records and stop when an empty list is returned.

import os

import moi_product_sdk as sdk

client = sdk.new_with_personal_access_token(
    os.environ["PRODUCT_API_BASE_URL"],
    os.environ["PRODUCT_API_KEY"],
)
workspace = client.workspace(os.environ["WORKSPACE_ID"])
service = workspace.sql()

page = service.query_history(
    sdk.with_query_history_offset(0),
    sdk.with_query_history_limit(20),
)
print("Total:", page.total, "Offset:", page.offset, "Limit:", page.limit)
for record in page.query_list:
    print(record.statement_id, record.database, record.status, record.error)
package main

import (
	"context"
	"fmt"
	"os"

	sdk "github.com/matrixorigin/matrixflow/sdk/go-sdk"
)

func main() {
	ctx := context.Background()
	client, err := sdk.NewWithPersonalAccessToken(
		os.Getenv("PRODUCT_API_BASE_URL"),
		os.Getenv("PRODUCT_API_KEY"),
	)
	if err != nil {
		panic(err)
	}
	workspace, err := client.Workspace(os.Getenv("WORKSPACE_ID"))
	if err != nil {
		panic(err)
	}
	service := workspace.SQL()

	page, err := service.QueryHistory(
		ctx,
		sdk.WithQueryHistoryOffset(0),
		sdk.WithQueryHistoryLimit(20),
	)
	if err != nil {
		panic(err)
	}
	fmt.Println("Total:", page.GetTotal(), "Offset:", page.GetOffset(), "Limit:", page.GetLimit())
	for _, record := range page.GetQueryList() {
		fmt.Println(record.GetStatementId(), record.GetDatabase(), record.GetStatus(), record.GetError())
	}
}

Read execution details and profile

Select a target statement ID from the history list and reuse the workspace SQL service to fetch its execution details and profile. The detail response reports statement status and error text, while the profile response indicates whether profiling succeeded and returns the profile payload.

if not page.query_list:
    raise SystemExit("No SQL execution records found in this workspace")

statement_id = page.query_list[0].statement_id
detail = service.query_detail(statement_id)
if detail is not None:
    print("Details:", detail.statement_id, detail.status, detail.error)

profile = service.query_profile(statement_id)
print("Profile:", profile.statement_id, profile.profile.success, profile.profile.message)
if len(page.GetQueryList()) == 0 {
	fmt.Println("No SQL execution records found in this workspace")
	return
}

statementID := page.GetQueryList()[0].GetStatementId()
detail, err := service.QueryDetail(ctx, statementID)
if err != nil {
	panic(err)
}
if detail != nil {
	fmt.Println("Details:", detail.GetStatementId(), detail.GetStatus(), detail.GetError())
}

profile, err := service.QueryProfile(ctx, statementID)
if err != nil {
	panic(err)
}
fmt.Println("Profile:", profile.GetStatementId(), profile.GetProfile().GetSuccess(), profile.GetProfile().GetMessage())

Next steps

Last updated on