2013-03-18 138 views
0

我试图从日志文件中找到用户操作之间的时间平均差异。因此,例如,对于用户操作ua, login, login和下一个用户操作ua, disclaimer ok,我想要减去这两个操作之间的时间,并且对于每次出现特定顺序时,将这些时间加在一起并除以发生的次数模式。哈希映射内的哈希映射的平均值

这是我的HashMap

Map<String, HashMap<String, NumberHolder>> uaCount = 
         new HashMap<String, HashMap<String, NumberHolder>>(); 

NumberHolder被定义为

private class NumberHolder 
{ 
    public int occurences; 
    public int sumtime_in_milliseconds; 
} 

sCurrentLinesNextLine都像它们的名字表示。迭代时,sNextLine变为sCurrentLine等等。

在我的代码中,我遍历日志文件,打开它们,检查一行是否是用户操作,声明sCurrentLinesNextLine,并分隔这两行以隔离用户操作部分。然后,我从两行中分离出行动的时间和日期,使用简单的日期格式并解析它们,然后找到差异。我期望的输出是ua, login, login --> ua, disclaimer, ok AVERAGE = 38 seconds。如果你想看到整个代码只是问。

+0

什么是你的问题? – 2013-03-18 15:10:04

+0

@HotLicks以及我不认为我需要保留最后一次值,只是发生次数和总计时间来计算平均值 – user2007843 2013-03-18 15:10:27

+0

我的问题是如何将出现次数和总和时间添加到基于当前行的散列表中,并且下一行以及如何继续匹配过程 – user2007843 2013-03-18 15:18:06

回答

1

伪代码:

function void addLinePair(string1, string2, deltaTime) { 
    // Assumes string1 and string2 have been stripped of variable text 

    keyString = string1 + "|" + string2; 

    hashValue = findInHashTable(keyString); 

    if (hashValue == <not found>) { 

     hashValue = new NumberHolder 
     insertInHashtable(hashValue, keyString) 

    } 

    hashValue.sumtime_in_milliseconds += deltaTime 
    hashValue.occurrences++ 
} 

function void printAverages { 

    for (key,value in hashTable) { 
     string1 = first part of key 
     string2 = second part of key 
     average = (float)value.sumtime_in_milliseconds/(float)value.occurrences 
     print (string1 + " --> " + string2 + ", AVERAGE = " + average + " seconds") 
    } 
} 
+0

我该怎么做? 'hashValue = new NumberHolder'我得到一个错误 – user2007843 2013-03-18 19:53:52

+0

@ user2007843 - 就像我说的,它是伪代码。我假定你知道如何创建你的对象的实例。 – 2013-03-18 19:57:35

+0

对不起java自动更正,我没有注意到。我的另一个问题是你在for循环中做什么? – user2007843 2013-03-18 20:22:01

0

我似乎并不完全清楚为什么你使用两级地图。如果你正在寻找平均时间,你应该经历一个,然后到下一个。即平均时间是基于每个人与之后的人之间的时间,而不是每个人和每个人之间的时间。

List<String> logLines = new List<String(); 

//populate logLines 

getLogTime(String a, String b){ 
    long logTimeA = -1; 
    long totalLogTime = 0; 
    long count; 
    for(String logLine : logLines){ 
     if(logLine.equals(a)){ 
      logTimeA = parseForTime(logLine); 
     } 
     else if(logLine.equals(b) && logTimeA >= 0){ 
      totalLogTime += logTimeA - parseForTime(logLine); 
      count++ 
      logTimeA = -1; 
     } 
     //if you want it to only do the one immediately after 
     else if(!logLine.equals(b)){ 
      logTimeA = -1; 
     } 
    } 
    if(count == 0){ 
     return 0; 
    } 
    return totalLogTime/count; 
}