• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 - 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15import Logger, { LOG_MODULE_TYPE } from 'arkanalyzer/lib/utils/logger';
16import { FileReports } from './Defects';
17import { GeneratingJsonFile } from '../utils/common/GeneratingJsonFile';
18import path from 'path';
19
20const logger = Logger.getLogger(LOG_MODULE_TYPE.HOMECHECK, 'Message');
21
22export interface Message {
23    sendResult(fileReports: FileReports[], reportDir?: string): void;
24    messageNotify(messageLevel: MessageType, msg: string): void;
25    progressNotify(progress: number, msg: string): void;
26}
27
28export class DefaultMessage implements Message {
29    /**
30     * 发送消息
31     *
32     * @param msg 要发送的消息内容
33     */
34    async sendResult(fileReports: FileReports[], reportDir?: string | undefined): Promise<void> {
35        if (reportDir && reportDir.length !== 0) {
36            GeneratingJsonFile.generatingJsonFile(path.resolve(reportDir, 'issuesReport.json'), fileReports);
37        } else {
38            process.stdout.write(JSON.stringify(fileReports));
39        }
40    }
41
42
43    /**
44     * 消息通知函数
45     *
46     * @param messageLevel 消息类型,类型为MessageType枚举
47     * @param msg 消息内容,类型为字符串
48     */
49    messageNotify(messageLevel: MessageType, msg: string): void {
50        logger.error(JSON.stringify(msg));
51        return;
52    }
53
54
55    /**
56     * 通知进度更新
57     *
58     * @param progress 进度值,取值范围为 0 到 1 之间
59     * @param msg 与进度相关的消息
60     */
61    progressNotify(progress: number, msg: string): void {
62        const checkPercent = Math.floor(progress * 100);
63        if (checkPercent % 20 === 0) {
64            logger.info(`===== progress: ${checkPercent}% ======`);
65        }
66    }
67}
68
69/**
70 * 告警消息类型
71 */
72export enum MessageType {
73    BASE_ERROR = 0,
74    CHECK_ERROR = -1,
75    CHECK_WARN = -2,
76    CHECK_INFO = -3
77}