Create History Alerts in Batch


批量创建历史告警进。

备注

一旦有一条告警的格式校验不通过,所有告警均无法创建成功。

请求格式

POST https://{apigw-address}/event-service/v2.1/history-alerts?action=batchCreate

请求参数(URI)

名称

位置(Path/Query)

必需/可选

数据类型

描述

orgId

Query

必需

String

资产所属的组织 ID。如何获取 orgId 信息>>

请求参数(Body)

名称

必需/可选

数据类型

描述

historyAlertList

必需

HistoryAlert 结构体数组

有关historyAlert结构体的定义。见 HistoryAlert 结构体>>

响应参数

名称

数据类型

描述

data

EnosBatchEachData结构体数组

有关EnosBatchEachData结构体的定义,参见下表。

EnosBatchEachData结构体

名称

数据类型

描述

code

Integer

单条记录创建成功或失败,0:成功,-1:失败

msg

String

提示格式正确还是错误

data

String

若创建成功,则为 eventId,若失败,则为错误信息

示例

请求示例

url: POST
https://{apigw-address}/event-service/v2.1/history-alerts?action=batchCreate&orgId=yourOrgId
method: POST
requestBody:
{
    "historyAlertList":[
        {
            "assetId":"asset",
            "assetPaths": ["treeId1:/assetId1/assetId2/assetIdx", "treeId2:/assetId3/assetIdx"],
            "contentDesc":{
                "defaultValue":"default2",
                "i18nValue":{
                    "en_US":"english",
                    "zh_CN":"中文"
                }
            },
            "contentId":"content",
            "localOccurTime":"2019-11-07 15:36:32",
            "localRecoverTime":"2019-11-18 00:00:00",
            "measurepointId":"point111",
            "modelId":"model",
            "modelIdPath":"/path-path-path",
            "occurTime":1573025793008,
            "recoverTime":1573112193008,
            "severityDesc":{
                "defaultValue":"default",
                "i18nValue":{
                    "en_US":"english",
                    "zh_CN":"中文"
                }
            },
            "severityId":"severity",
            "tag":{
                "key1":" v1 "
            },
            "typeDesc":{
                "defaultValue":"default1",
                "i18nValue":{
                    "en_US":"english",
                    "zh_CN":"中文"
                }
            },
            "typeId":"type",
            "value":1.0
        },
        {
            "assetId":"asset",
            "contentDesc":{
                "defaultValue":"default2",
                "i18nValue":{
                    "en_US":"english",
                    "zh_CN":"中文"
                }
            },
            "contentId":"content",
            "localOccurTime":"2019-11-07 15:36:33",
            "localRecoverTime":"2019-11-18 00:00:00",
            "measurepointId":"point111",
            "modelId":"model",
            "modelIdPath":"/path-path-path",
            "occurTime":1573112193008,
            "orgId":"yourOrgId",
            "severityDesc":{
                "defaultValue":"default",
                "i18nValue":{
                    "en_US":"english",
                    "zh_CN":"中文"
                }
            },
            "severityId":"severity",
            "tag":{
                "key2":" v2 "
            },
            "typeDesc":{
                "defaultValue":"default1",
                "i18nValue":{
                    "en_US":"english",
                    "zh_CN":"中文"
                }
            },
            "typeId":"type",
            "value":1.0
        },
        {
            "assetId":"asset",
            "contentDesc":{
                "defaultValue":"default2",
                "i18nValue":{
                    "en_US":"english",
                    "zh_CN":"中文"
                }
            },
            "contentId":"content",
            "localOccurTime":"2019-11-07 15:36:33",
            "localRecoverTime":"2019-11-18 00:00:00",
            "measurepointId":"point111",
            "modelId":"model",
            "modelIdPath":"/path-path-path",
            "occurTime":1573112193012,
            "orgId":"yourOrgId",
            "recoverTime":1573112193016,
            "severityDesc":{
                "defaultValue":"default",
                "i18nValue":{
                    "en_US":"english",
                    "zh_CN":"中文"
                }
            },
            "severityId":"severity",
            "tag":{
                "key3":" v3 "
            },
            "typeDesc":{
                "defaultValue":"default1",
                "i18nValue":{
                    "en_US":"english",
                    "zh_CN":"中文"
                }
            },
            "typeId":"type",
            "value":1.0
        }
    ],
  "action": "batchCreate"
}

返回示例

{
    "code":99206,
    "msg":"Partial Content Success",
    "requestId":"de9281bf-3ebf-4783-97c8-4b94be384351",
    "data":[
        {
            "code":0,
            "msg":"format right",
            "data":"2019110630b8cc38735e8ba9d1b3a6b5e4381e36"
        },
        {
            "code":-1,
            "msg":"format wrong",
            "data":"recoverTime  :parameter empty"
        },
        {
            "code":0,
            "msg":"format right",
            "data":"20191107bd6ab07f9be6ab769b1e7c898b40cc83"
        }
    ],
    "successSize":2,
    "totalSize":3
}

Java SDK调用示例

package com.enos-iot.enos.event_service;

import com.envision.apim.poseidon.config.PConfig;
import com.envision.apim.poseidon.core.Poseidon;
import com.enos-iot.enos.api.common.constant.request.Pagination;
import com.enos-iot.enos.event_service.v2_1.*;
import com.enos-iot.enos.event_service.vo.*;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import org.junit.Test;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

private GenerateActiveAlert buildActiveAlert() {
    GenerateActiveAlert activeAlert = new GenerateActiveAlert();
    activeAlert.setAssetId("asset");
    activeAlert.setModelId("model");
    activeAlert.setModelIdPath("/path-path-path");
    activeAlert.setMeasurepointId("point111");
    activeAlert.setValue(1);
    long now = System.currentTimeMillis();
    activeAlert.setOccurTime(now);
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String localTime = df.format(now);
    activeAlert.setLocalOccurTime(localTime);
    activeAlert.setSeverityId("severity");

    StringI18n desc = new StringI18n();
    desc.setDefaultValue("default");
    Map < String, String > map = new HashMap < > ();
    map.put("zh_CN", "中文");
    map.put("en_US", "english");
    desc.setI18nValue(map);

    activeAlert.setSeverityDesc(desc);

    activeAlert.setTypeId("type");
    StringI18n desc1 = new StringI18n();
    desc1.setDefaultValue("default1");
    Map < String, String > map1 = new HashMap < > ();
    map1.put("zh_CN", "中文");
    map1.put("en_US", "english");
    desc1.setI18nValue(map1);

    activeAlert.setTypeDesc(desc1);

    activeAlert.setContentId("content");

    StringI18n desc2 = new StringI18n();
    desc2.setDefaultValue("default2");
    Map < String, String > map2 = new HashMap < > ();
    map2.put("zh_CN", "中文");
    map2.put("en_US", "english");
    desc2.setI18nValue(map2);
    activeAlert.setContentDesc(desc2);
    return activeAlert;
}

private GenerateHistoryAlert buildHistoryAlert() {
    GenerateActiveAlert activeAlert = buildActiveAlert();
    Gson gson = new Gson();
    GenerateHistoryAlert historyAlert = gson.fromJson(gson.toJson(activeAlert), GenerateHistoryAlert.class);
    historyAlert.setRecoverTime(System.currentTimeMillis());
    historyAlert.setLocalRecoverTime("2019-11-18 00:00:00");
    return historyAlert;
}

public void testBatchCreateHistoryAlert() {
    String accessKey = "yourAppAccessKey";
    String secretKey = "yourAppSecretKey";
    String orgId = "yourOrgId";
    String url = "https://{apigw-address}";
    BatchCreateHistoryAlertsRequest request = new BatchCreateHistoryAlertsRequest();
    request.setOrgId(orgId);
    List < GenerateHistoryAlert > list = Lists.newArrayList();
    GenerateHistoryAlert historyAlert1 = buildHistoryAlert();
    historyAlert1.setOccurTime(System.currentTimeMillis() - 24L * 3600 * 1000);
    Map < String, String > tags = new HashMap < > ();
    tags.put("key1", "v1");
    historyAlert1.setTag(tags);
    list.add(historyAlert1);
    GenerateHistoryAlert historyAlert2 = buildHistoryAlert();
    tags = new HashMap < > ();
    tags.put("key2", "v2");
    historyAlert2.setTag(tags);
    list.add(historyAlert2);
    GenerateHistoryAlert historyAlert3 = buildHistoryAlert();
    tags = new HashMap < > ();
    tags.put("key3", "v3");
    historyAlert3.setTag(tags);
    list.add(historyAlert3);
    request.setHistoryAlertList(list);
    try {
        BatchCreateHistoryAlertsResponse response = Poseidon.config(PConfig.init().appKey(accessKey).appSecret(secretKey).debug())
            .url(url)
            .getResponse(request, BatchCreateHistoryAlertsResponse.class);
        System.out.println(response);
    } catch (Exception e) {
        System.out.print(e);
    }
}