Skip to content

Func Custom Query Functions

This article describes how to write custom query functions with DataFlux Func, process external data, and organize return results according to chart requirements. QueryHub native queries and DQL Like are processed by the server, so their results do not need to be wrapped in the Func chart data structure.

Configure a Connector

In Func, go to Development > Connectors to create a connector matching the target data source type, enter the connection address and credentials, and test the connection.

The following example assumes that a MySQL connector with the ID mysql has been created:

mysql = DFF.CONN('mysql')

If the script also needs to call the Guance API, you can configure the corresponding connector and Workspace API Key separately. Database connector and API connector credentials serve different purposes. For configuration, see Access via Func.

Write a Data Query Function

Use category='dataPlatform.dataQueryFunc' to register a function that can be used for data queries.

The following example reads the my_table table and assumes that:

  • createTime stores a Unix timestamp in seconds;
  • reqCost is the request duration;
  • The time_range passed in by the chart contains the start and end times in milliseconds.
@DFF.API('查询请求耗时', category='dataPlatform.dataQueryFunc')
def query_request_cost(time_range):
    mysql = DFF.CONN('mysql')

    sql = '''
        SELECT createTime, reqCost
        FROM my_table
        WHERE createTime >= ? AND createTime < ?
        ORDER BY createTime
        LIMIT 100
    '''
    parameters = [
        int(time_range[0] / 1000),
        int(time_range[1] / 1000),
    ]
    rows = mysql.query(sql, parameters)

    return {
        'series': [[{
            'columns': ['time', 'req_cost'],
            'tags': {},
            'values': [
                [row['createTime'] * 1000, row['reqCost']]
                for row in rows
            ],
        }]],
    }

Replace the connector ID, table name, and field names, and adjust the conversion according to the time unit of the actual data. The SQL parameters in the example are bound in the order in which ? appears and do not concatenate time values as strings.

Understand the Return Structure

Field Meaning
series Data series used for query display; the example retains a two-level array on the outer layer
columns Field order of values in each row; in the example, the first column is time and the second column is req_cost
tags Tags for the current series; different tag combinations need to be organized into separate series
values Two-dimensional data aligned with columns; the time column in the example is output uniformly in milliseconds

This is a Func chart return structure and does not mean that the script is executing a QueryHub DQL Like expression.

Organize Results by Chart Type

Different charts have different requirements for series, time columns, and tags. For detailed formats, see:

For function configuration and invocation instructions, see also Function External Function Configuration and Chart Data Source Query.

Feedback

Is this page helpful?