/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import React, { useState, useEffect } from "react"; import { LogType, MultiLineLogParser } from "API"; import Button from "components/Button"; import FormItem from "components/FormItem"; import TextArea from "components/TextArea"; import TextInput from "components/TextInput"; import { InfoBarTypes } from "reducer/appReducer"; import { useTranslation } from "react-i18next"; import Select from "components/Select"; import { FB_TYPE_LIST, generateTimeZoneList } from "assets/js/const"; import Alert from "components/Alert"; import { Alert as AlertInfo } from "assets/js/alert"; import { ExLogConf } from "./LogConfigComp"; import { getLogFormatByUserLogConfig, IsJsonString, JsonToDotNotate, replaceSpringbootTimeFormat, } from "assets/js/utils"; import { appSyncRequestQuery } from "assets/js/request"; import { checkTimeFormat } from "graphql/queries"; import { OptionType } from "components/AutoComplete/autoComplete"; export interface RegexListType { key: string; type: string; format: string; value: string; loadingCheck: boolean; showError: boolean; showSuccess: boolean; } interface SampleLogParsingProps { changeSpecs: (specs: any) => void; changeSampleLog: (log: string) => void; logConfig: ExLogConf; logType: LogType; showSampleLogRequiredError?: boolean; sampleLogInvalid: boolean; changeSampleLogInvalid: (valid: boolean) => void; changeTimeKey?: (key: string) => void; changeRegExpList?: (list: RegexListType[]) => void; changeSelectKeyList?: (list: OptionType[]) => void; changeTimeOffset?: (offset: string) => void; } const SampleLogParsing: React.FC = ( props: SampleLogParsingProps ) => { const { logType, logConfig, showSampleLogRequiredError, changeSpecs, changeSampleLog, sampleLogInvalid, changeSampleLogInvalid, changeTimeKey, changeRegExpList, changeSelectKeyList, changeTimeOffset, } = props; const { t } = useTranslation(); const [logResMap, setLogResMap] = useState({}); const [showValidInfo, setShowValidInfo] = useState(false); const [timeFormatForSpringBoot, setTimeFormatForSpringBoot] = useState(""); const [loadingCheckTimeKeyFormat, setLoadingCheckTimeKeyFormat] = useState(false); const [timeKeyFormatInvalid, setTimeKeyFormatInvalid] = useState(false); const [timeKeyFormatValid, setTimeKeyFormatValid] = useState(false); const isCustomType = () => { return ( logType === LogType.JSON || logType === LogType.SingleLineText || logType === LogType.Syslog || (logConfig.logType === LogType.MultiLineText && logConfig.multilineLogParser === MultiLineLogParser.CUSTOM) ); }; const isSpringBootType = () => { return ( logConfig.logType === LogType.MultiLineText && logConfig.multilineLogParser === MultiLineLogParser.JAVA_SPRING_BOOT ); }; const getDefaultType = (key: string, value?: string): string => { if (key === "time") { return "date"; } if (value) { if (typeof value === "number" && isFinite(value)) { if (Number.isInteger(value)) { return "integer"; } return ""; } } if ( logConfig.logType === LogType.MultiLineText && logConfig.multilineLogParser === MultiLineLogParser.JAVA_SPRING_BOOT ) { if (key === "level") { return "keyword"; } } return "text"; }; const convertJSONToKeyValueList = () => { if (!logConfig?.userSampleLog) { AlertInfo(t("resource:config.parsing.inputJSON")); return; } if (!IsJsonString(logConfig?.userSampleLog || "")) { AlertInfo(t("resource:config.parsing.notJSONFormat")); return; } const tmpJsonObj = JsonToDotNotate( JSON.parse(logConfig.userSampleLog || "") ); console.info("tmpJsonFormat:", tmpJsonObj); const initArr: any = []; Object.keys(tmpJsonObj).forEach((key) => { initArr.push({ key: key, type: getDefaultType(key, tmpJsonObj[key]), format: "", value: tmpJsonObj[key], }); }); changeRegExpList && changeRegExpList(initArr); }; const parseLog = () => { const regex = logConfig.regularExpression || ""; if (!regex.trim()) { return; } let isValid = true; try { new RegExp(regex); } catch (e) { isValid = false; } if (!isValid) { Alert(t("resource:config.parsing.alert")); return; } const found: any = logConfig?.userSampleLog?.match(regex); if (logType === LogType.Nginx || logType === LogType.Apache) { if (found && found.groups) { setShowValidInfo(true); changeSampleLogInvalid(false); } else { setShowValidInfo(false); changeSampleLogInvalid(true); } setLogResMap(found?.groups || {}); if (found && found.groups) { changeRegExpList && changeRegExpList( Object.entries(found.groups).map((key) => { return { key: key[0] } as any; }) ); } } if ( logType === LogType.SingleLineText || logType === LogType.Syslog || logType === LogType.MultiLineText ) { const initArr: RegexListType[] = []; if (found && found.groups) { setShowValidInfo(true); changeSampleLogInvalid(false); const foundObjectList = Object.entries(found.groups); if (foundObjectList.length) { console.info("foundObjectList:", foundObjectList); foundObjectList.forEach((element: any) => { initArr.push({ key: element[0], type: getDefaultType(element[0], element[1]), format: "", value: element[1]?.length > 450 ? element[1].substr(0, 448) + "..." : element[1], loadingCheck: false, showError: false, showSuccess: false, }); }); } } else { setShowValidInfo(false); changeSampleLogInvalid(true); } changeRegExpList && changeRegExpList(initArr); } }; const validateTimeFormat = async ( index: number, timeStr: string, formatStr: string ) => { if (logConfig.regexKeyList) { const tmpDataLoading: RegexListType[] = JSON.parse( JSON.stringify(logConfig.regexKeyList) ); tmpDataLoading[index].loadingCheck = true; changeRegExpList && changeRegExpList(tmpDataLoading); const resData: any = await appSyncRequestQuery(checkTimeFormat, { timeStr: timeStr, formatStr: formatStr, }); const tmpDataRes: RegexListType[] = JSON.parse( JSON.stringify(logConfig.regexKeyList) ); tmpDataRes[index].loadingCheck = false; tmpDataRes[index].showSuccess = resData?.data?.checkTimeFormat?.isMatch || false; tmpDataRes[index].showError = !resData?.data?.checkTimeFormat?.isMatch; changeRegExpList && changeRegExpList(tmpDataRes); console.info("resData:", resData); } }; const validTimeKeyFormat = async () => { if (logConfig.regexKeyList) { const timeStr = logConfig.regexKeyList.find( (element) => element.key === logConfig.timeKey )?.value; const formatStr = logConfig.regexKeyList.find( (element) => element.key === logConfig.timeKey )?.format; setLoadingCheckTimeKeyFormat(true); const resData: any = await appSyncRequestQuery(checkTimeFormat, { timeStr: timeStr, formatStr: formatStr, }); setLoadingCheckTimeKeyFormat(false); setTimeKeyFormatInvalid( resData?.data?.checkTimeFormat?.isMatch === false ); setTimeKeyFormatValid(resData?.data?.checkTimeFormat?.isMatch === true); console.info("resData:", resData); } }; useEffect(() => { setShowValidInfo(false); changeRegExpList && changeRegExpList([]); setLogResMap({}); setTimeFormatForSpringBoot(""); }, [ logConfig.logType, logConfig.multilineLogParser, logConfig.userLogFormat, ]); useEffect(() => { if (logConfig.userLogFormat) { if (isSpringBootType()) { let tmpTimeFormat = getLogFormatByUserLogConfig( logConfig.userLogFormat ); tmpTimeFormat = replaceSpringbootTimeFormat(tmpTimeFormat); setTimeFormatForSpringBoot(tmpTimeFormat); } } }, [logConfig.userLogFormat]); useEffect(() => { // set format undefine when format is empty if (logConfig.regexKeyList && logConfig.regexKeyList.length > 0) { const tmpSpecList = []; for (let i = 0; i < logConfig.regexKeyList.length; i++) { if (isSpringBootType()) { tmpSpecList.push({ key: logConfig.regexKeyList[i].key, type: logConfig.regexKeyList[i].type, format: logConfig.regexKeyList[i].key === "time" ? timeFormatForSpringBoot : undefined, }); } else { tmpSpecList.push({ key: logConfig.regexKeyList[i].key, type: logConfig.regexKeyList[i].type, format: logConfig.regexKeyList[i].format ? logConfig.regexKeyList[i].format : undefined, }); } } changeSpecs(tmpSpecList); } // Set Time Key Option List const tmpTimeKeyList: OptionType[] = [ { name: t("none"), value: "", }, ]; logConfig?.regexKeyList?.map((element) => { tmpTimeKeyList.push({ name: element.key, value: element.key, }); }); changeSelectKeyList && changeSelectKeyList(tmpTimeKeyList); setTimeKeyFormatInvalid(false); setTimeKeyFormatValid(false); }, [logConfig.regexKeyList, timeFormatForSpringBoot]); useEffect(() => { if (logConfig.logType !== LogType.JSON) { parseLog(); } }, [logConfig?.userSampleLog]); return (