创建仪表盘

调用CreateDashboard接口创建仪表盘。

前提条件

参数说明

def create_dashboard(self, project, detail):

请求参数

名称

类型

是否必填

描述

示例值

project

str

Project名称。

ali-test-project

detail

dict

一个JSON对象,包含创建仪表盘的配置信息。

{
    "charts": [
        {
            "display": {
                "displayName": "",
                "height": 5,
                "width": 5,
                "xAxis": [
                    "province"
                ],
                "xPos": 0,
                "yAxis": [
                    "pv"
                ],
                "yPos": 0
            },
            "search": {
                "end": "now",
                "logstore": "website_log",
                "query": "* | select ip_to_province(remote_addr) as province,count(1) as pv group by province order by pv desc ",
                "start": "-86400s",
                "topic": ""
            },
            "title": "map",
            "type": "map",
            "action": {}
        }
    ],
    "dashboardName": "",
    "description": "",
    "displayName": "dashboard_test"
}

detail参数说明如下:

名称

类型

是否必填

描述

示例值

dashboardName

str

仪表盘的内部显示名称。

dashboard-1609294922657-123456

displayName

str

仪表盘的展示名称。

test-alert

description

str

仪表盘的描述信息。

这是一个仪表盘。

attribute

dict

仪表盘的属性值。

 {"type": "grid"}

charts

list

仪表盘包含图表的JSON数组。

[
    {
        "display": {
            "displayName": "",
            "height": 5,
            "width": 5,
            "xAxis": [
                "province"
            ],
            "xPos": 0,
            "yAxis": [
                "pv"
            ],
            "yPos": 0
        },
        "search": {
            "end": "now",
            "logstore": "website_log",
            "query": "* | select ip_to_province(remote_addr) as province,count(1) as pv group by province order by pv desc ",
            "start": "-86400s",
            "topic": ""
        },
        "title": "map",
        "type": "map"
    },
    {
        "display": {
            "displayName": "",
            "height": 5,
            "width": 5,
            "xAxis": [
                "province"
            ],
            "xPos": 5,
            "yAxis": [
                "pv"
            ],
            "yPos": 0
        },
        "search": {
            "end": "now",
            "logstore": "website_log",
            "query": "* | select ip_to_province(remote_addr) as province,count(1) as pv group by province order by pv desc ",
            "start": "-86400s",
            "topic": ""
        },
        "title": "post_map",
        "type": "map"
    }
]

charts数组元素,参数说明如下:

名称

类型

是否必填

描述

示例值

title

str

图表标题。

my-chart

type

str

图表类型。

linepro

search

dict

查询配置。

{
    "end": "now",
    "logstore": "website_log",
    "query": "* | select ip_to_province(remote_addr) as province,count(1) as pv group by province order by pv desc ",
    "start": "-86400s",
    "topic": ""
}

display

dict

图表的显示配置。

{
    "displayName": "",
    "height": 5,
    "width": 5,
    "xAxis": [
        "province"
    ],
    "xPos": 5,
    "yAxis": [
        "pv"
    ],
    "yPos": 0
}

action

dict

配置图表的交互事件。

{}

返回参数

返回参数说明请参见CreateDashboard - 创建仪表盘

示例代码

from aliyun.log import LogClient
import time
import os

dashboard_detail = {
    "charts": [
        {
            "display": {
                "displayName": "",
                "height": 5,
                "width": 5,
                "xAxis": [
                    "province"
                ],
                "xPos": 0,
                "yAxis": [
                    "pv"
                ],
                "yPos": 0
            },
            "search": {
                "end": "now",
                "logstore": "website_log",
                "query": "* | select ip_to_province(remote_addr) as province,count(1) as pv group by province order by pv desc ",
                "start": "-86400s",
                "topic": ""
            },
            "title": "map",
            "type": "map",
            "action": {}
        }
    ],
    "dashboardName": 'dashboard_test_' + str(time.time()).replace('.', '-'),
    "description": "",
    "displayName": "dashboard_test"
}


def main():
    # 本示例从环境变量中获取AccessKey ID和AccessKey Secret
    accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
    accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')

    # 日志服务的服务接入点
    endpoint = "cn-hangzhou.log.aliyuncs.com"

    project = 'ali-test-project'

    client = LogClient(endpoint, accessKeyId, accessKey, "")

    res = client.create_dashboard(project, dashboard_detail)
    res.log_print()


if __name__ == '__main__':
    main()

相关文档