clmetric.ts 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
import DAMetricService from '@service/dametric/dametric-service';
/**
 * 代码表--指标
 *
 * @export
 * @class CLMetric
 */
export default class CLMetric {

    /**
     * 是否启用缓存
     *
     * @type boolean
     * @memberof CLMetric
     */
    public isEnableCache:boolean = true;

    /**
     * 过期时间
     *
     * @type any
     * @memberof CLMetric
     */
    public static expirationTime:any;

    /**
     * 预定义类型
     *
     * @type string
     * @memberof CLMetric
     */
    public predefinedType:string ='';

    /**
     * 缓存超长时长
     *
     * @type any
     * @memberof CLMetric
     */
40
    public cacheTimeout:any = 300000;
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

    /**
     * 代码表模型对象
     *
     * @type any
     * @memberof CLMetric
     */
    public codelistModel:any = {
        codelistid:"CLMetric"
    };

    /**
     * 获取过期时间
     *
     * @type any
     * @memberof CLMetric
     */
    public getExpirationTime(){
        return CLMetric.expirationTime;
    }

    /**
     * 设置过期时间
     *
     * @type any
     * @memberof CLMetric
     */
    public setExpirationTime(value:any){
        CLMetric.expirationTime = value; 
    }

    /**
     * 自定义参数集合
     *
     * @type any
     * @memberof CLMetric
     */
    public userParamNames:any ={
    }

    /**
     * 查询参数集合
     *
     * @type any
     * @memberof CLMetric
     */
    public queryParamNames:any ={
        sort: 'createdate,asc'
    }

    /**
     * 指标应用实体服务对象
     *
     * @type {DAMetricService}
     * @memberof CLMetric
     */
    public dametricService: DAMetricService = new DAMetricService();


    /**
     * 处理数据
     *
     * @public
     * @param {any[]} items
     * @returns {any[]}
     * @memberof CLMetric
     */
    public doItems(items: any[]): any[] {
        let _items: any[] = [];
        if(items && items instanceof Array && items.length >0){
            items.forEach((item: any) => {
                let itemdata:any = {};
                Object.assign(itemdata,{id:item.metric_id});
                Object.assign(itemdata,{value:item.metric_id});
                Object.assign(itemdata,{text:item.metric_name});
                Object.assign(itemdata,{label:item.metric_name});
sq3536's avatar
sq3536 committed
117
                Object.assign(itemdata,{filter:item.build_id});
118
                
119 120
                
                
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
                _items.push(itemdata);
            });
        }
        return _items;
    }

    /**
     * 获取数据项
     *
     * @param {*} context
     * @param {*} data
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof CLMetric
     */
    public getItems(context: any={}, data: any={}, isloading?: boolean): Promise<any> {
        return new Promise((resolve, reject) => {
            data = this.handleQueryParam(data);
            const promise: Promise<any> = this.dametricService.FetchDefault(context, data, isloading);
            promise.then((response: any) => {
                if (response && response.status === 200) {
                    const data =  response.data;
                    resolve(this.doItems(data));
                } else {
                    resolve([]);
                }
            }).catch((response: any) => {
                console.error(response);
                reject(response);
            });
        });
    }

    /**
     * 处理查询参数
     * @param data 传入data
     * @memberof CLMetric
     */
    public handleQueryParam(data:any){
        let tempData:any = data?JSON.parse(JSON.stringify(data)):{};
        if(this.userParamNames && Object.keys(this.userParamNames).length >0){
            Object.keys(this.userParamNames).forEach((name: string) => {
                if (!name) {
                    return;
                }
                let value: string | null = this.userParamNames[name];
                if (value && value.startsWith('%') && value.endsWith('%')) {
                    const key = value.substring(1, value.length - 1);
                    if (this.codelistModel && this.codelistModel.hasOwnProperty(key)) {
                        value = (this.codelistModel[key] !== null && this.codelistModel[key] !== undefined) ? this.codelistModel[key] : null;
                    } else {
                        value = null;
                    }
                }
                Object.assign(tempData, { [name]: value });
            });
        }
sq3536's avatar
sq3536 committed
178
        Object.assign(tempData,{page: 0, size: 100000});
179 180 181 182 183 184
        if(this.queryParamNames && Object.keys(this.queryParamNames).length > 0){
            Object.assign(tempData,this.queryParamNames);
        }
        return tempData;
    }
}