格式化提取日志中的统计项

 

 

// 功能:格式化runlog中各个线程的统计项
// 使用方法,输入所要提取统计项的一个关键词,或多个关键词对应值求和
// 正则无记忆方法

var fso = new ActiveXObject("Scripting.FileSystemObject");
var strSrcSplitter = "|"
var strSplitter = "\t";
var WshShell = new ActiveXObject("WScript.Shell");
var g_ScriptName;
var g_ScriptContainFolder;
var g_TmpFolder;

function Output(str)
{
    WScript.StdOut.WriteLine(str);
}

RegExp.prototype.TestNoRecord = function(str){
    var ret = this.test(str);
    this.lastIndex = 0;
    return ret;
}
RegExp.prototype.ExecNoRecord = function(str){
    var ret = this.exec(str);
    this.lastIndex = 0;
    return ret;
}

// 数组排重插入方法
Array.prototype.Insert = function(val)
{
    for(var i = 0; i < this.length; ++i)
    {
        if(this[i] == val)
        {
            return false;
        }
    }
    
    this.push(val);
    return true;
}

function IsBigger(val1, val2)
{
    var bIsVal1Number = !isNaN(val1);
    var bIsVal2Number = !isNaN(val2);
    
    if(bIsVal1Number)
    {
        if(bIsVal2Number)
        {
            return (parseInt(val1) > parseInt(val2));
        }
        else
        {
            return false;
        }
    }
    else
    {
        if(bIsVal2Number)
        {
            return true;
        }
        else
        {
            return (val1 > val2);
        }
    }
}

Array.prototype.Sort = function()
{
    for(var i = 0; i < (this.length - 1); ++i)
    {
        var bSorted = false;
        for(var j = 0; j < (this.length - i - 1); ++j)
        {
            if(IsBigger(this[j], this[j + 1]))
            {
                tmp = this[j];
                this[j] = this[j+1];
                this[j+1] = tmp;
                bSorted = true;
            }
        }
        
        if(!bSorted)
        {
            break;
        }
    }
}

function GetTmpFolder()
{
    var objRet;
    var tmpFolder = fso.GetSpecialFolder(2);
    // 遍历文件夹
    var foldersK = new Enumerator(tmpFolder.SubFolders);
    
    if(!fso.FolderExists(tmpFolder.Path + "\\" + g_ScriptName))
    {
        tmpFolder.SubFolders.Add(g_ScriptName);
    }

    objRet = fso.GetFolder(tmpFolder.Path + "\\" + g_ScriptName);

    return objRet;
}

function ExistFileOrFolder(strPath)
{
    if(fso.FileExists(strPath))
    {
        return true;
    }
    if(fso.FolderExists(strPath))
    {
        return true;
    }
    return false;
}

function ExistInArray(aryValues, value)
{
    for(var i = 0; i < aryValues.length; ++i)
    {
        if(aryValues[i] == value)
        {
            return true;
        }
    }
    return false;
}

function GetRandomFileName()
{
    return ("-" + Math.floor(Math.random() * 999));
}

function OpenResult(strResultPath)
{
    strOpenCmd = "\"" + g_ScriptContainFolder.ParentFolder + "\\" + "EmEditor.exe " + "\"" + " /r " + "\"" + strResultPath + "\"";
    WshShell.Run(strOpenCmd, 10, true);
    var objOutPutFile = fso.GetFile(strResultPath);
    objOutPutFile.Delete(true);
}

function GetFirstNo(strWord)
{
    var iRet = 0;
    if(!isNaN(strWord))
    {
        iRet = parseInt(strWord);
    }
    else
    {
        for(var i = 0; i < strWord.length;)
        {
            var tmpC = strWord.charAt(i);
            if(tmpC >= '0' && tmpC <= '9')
            {
                iRet = iRet * 10 + (tmpC - '0')
                ++i;
            }
            else
            {
                break;
            }
        }
    }
    return iRet;
}

function Proc(strFilePath, strKeys, bOpenResult, strRenameTo, bAnalyse, bRegular)
{
    var aryKeys = strKeys.split(" ");
    var aryKeyNos = new Array();
    for(var i = 0; i < aryKeys.length; )
    {
        var iKeyNo = GetFirstNo(aryKeys[i]);
        if(iKeyNo > 0)
        {
            aryKeyNos.push(iKeyNo);
            ++i;
        }
        else
        {
            aryKeys.splice(i, 1);
        }
    }

    var aryOutput = new Array();
    var tmpOutput;
    var tmpColumn;

    var objFile = fso.OpenTextFile(strFilePath, 1 , false);
    while (!objFile.AtEndOfStream)
    {
        Parser(objFile.ReadLine());
    }

    function Parser(strContentLine)
    {
        if(strContentLine != "")
        {
            aryOutput.push(strContentLine.split(strSrcSplitter));
        }
    }

    var objResultFolder;
    if(bOpenResult)
    {
        objResultFolder = g_TmpFolder;
    }
    else
    {
        objResultFolder = fso.GetFile(strFilePath).ParentFolder;
    }

    var strFileName = fso.GetFile(strFilePath).Name;
    if(strRenameTo == "")
    {
        strRenameTo = strKeys.replace(/[\(\)\-\:\|]/gm, " ");
    }

    var iFileNameNo = 0;
    var strWriteToPath = "";
    do
    {
        var strSubFileName = strFileName + "_" + strRenameTo;
        if(iFileNameNo > 0)
        {
            strSubFileName += "_" + iFileNameNo;
        }
        strSubFileName += ".tsv";
        strWriteToPath = objResultFolder.Path + "\\" + strSubFileName;
        ++iFileNameNo;
    } while(ExistFileOrFolder(strWriteToPath))

    var objOutPutTextStream = fso.OpenTextFile(strWriteToPath, 2 , true);
    var strHeader = "";
    for(var i = 0; i < aryKeys.length; ++i)
    {
        strHeader += aryKeys[i] + strSplitter;
    }
    objOutPutTextStream.WriteLine(strHeader);

    for(var i = 0; i < aryOutput.length; ++i)
    {
        var strLine = "";
        var aryLineContents = aryOutput[i];
        for(var j = 0; j < aryKeyNos.length; ++j)
        {
            if(aryKeyNos[j] <= aryLineContents.length)
            {
                strLine += aryLineContents[aryKeyNos[j] - 1] + strSplitter;
            }
        }
        if(strLine != "")
        {
            objOutPutTextStream.WriteLine(strLine);
        }
    }
    objOutPutTextStream.Close();

    if(bAnalyse)
    {
        
    }

    if(bOpenResult)
    {
        OpenResult(strWriteToPath);
    }
}

function main()
{
    var bExistWScript = false;
    try 
    {
        if(WScript)
        {
            bExistWScript = true;
        }
    }
    catch(e){}

    if(!bExistWScript)
    {
        g_ScriptName = ScriptName;
        g_ScriptFullName = ScriptFullName;
    }
    else
    {
        g_ScriptName = WScript.ScriptName;
        g_ScriptFullName = WScript.ScriptFullName
    }

    // 全局变量初始化
    g_ScriptContainFolder = fso.GetFile(g_ScriptFullName).ParentFolder;
    g_TmpFolder = GetTmpFolder();

    if(!bExistWScript)
    {
        var strCmd = "";
        var strAnswer = prompt("Items:", "");
        if(strAnswer == "")
        {
            return;
        }
        
        strCmd = "cscript /d " + "\"" + ScriptFullName + "\"" + " " + "\"" + document.FullName + "\"" + " " + "\"" + strAnswer + "\"";

        var objTmpStream = fso.OpenTextFile(ScriptFullName + ".txt", 8 , true);
        objTmpStream.WriteLine(strCmd);
        objTmpStream.Close();
    
        //Emeditor无法隐藏exec执行时的窗口。
        WshShell.Run(strCmd, 0, true);
        return;
    }
    else
    {
        if((WScript.Arguments.length >= 1) && (WScript.Arguments(0) != ""))
        {
            var bOpenResult = true;
            var bAnalyse = false;
            var bRegular = false;
            if(WScript.Arguments.length > 2)
            {
                var strOptionList = WScript.Arguments(2);
                var aryOptions = strOptionList.split("|");
                if(ExistInArray(aryOptions, "NotOpen"))
                {
                    bOpenResult = false;
                }
                if(ExistInArray(aryOptions, "Analyse"))
                {
                    bAnalyse = true;
                }
                if(ExistInArray(aryOptions, "Regular"))
                {
                    bRegular = true;
                }
            }

            var strRenameTo = "";
            if(WScript.Arguments.length >=4)
            {
                strRenameTo = WScript.Arguments(3);
            }
            Proc(WScript.Arguments(0), WScript.Arguments(1), bOpenResult, strRenameTo, bAnalyse, bRegular);
            return;
        }
    }
    

    /*if(document.Saved)
    {
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        var objFile = fso.OpenTextFile(document.FullName, 1 , false);
        while (!objFile.AtEndOfStream)
        {
            Parser(objFile.ReadLine());
        }
    }
    else
    {
        document.selection.SelectAll();
        var strContent = document.selection.Text;
        document.selection.StartOfLine();
        var aryContent = strContent.split("\n");
        for(var i = 0; i < aryContent.length; ++i)
        {
            Parser(aryContent[i]);
        }
    }*/
}

main();

 

posted @ 2014-08-21 01:09  dzqabc  阅读(4)  评论(0编辑  收藏  举报