package com.x.h
import java.io.File
import scala.io.Source
/**
* Created by xxxxx on 3/7/2017.
* read logs and anlysis the interval time
* Notes://ltsgdb001b/8525691C004D9994/0000035F7480610585255D74006B9E95/5A5A500686EEF43F852580DC000884BB
*/
object AnlysisSnsLogs extends App{
getTheLastedFile
def getTheLastedFile(): Unit ={
var file = new File("C:\Users\IBM_ADMIN\Desktop\cognos\datastage\anlysislogs")
for (f <- file.listFiles()){ // 这里可以增加根据文章标题时间戳获取最新文章 增加排序功能
println(f.getName.toString.substring(0,8))
readFiles(f.toString)
}
}
def readFiles(file :String): Unit ={
import scala.collection.mutable
//Scala 列表类似于数组,它们所有元素的类型都相同,但是它们也有所不同:列表是不可变的,值一旦被定义了就不能改变,其次列表 具有递归的结构(也就是链接表结构)而数组不是。。
var jobName =new Array[String](30) //这里定义数组还是有问题的
var job = 0
var jobTime =new Array[String](30) //这里定义数组还是有问题的
var job1 = 0
val source = Source.fromFile(file)
for (line <- source.getLines()){
if(line.contains("STATUS REPORT FOR JOB:")){ //找包含有特定字符串的行
jobName(job) = line.toString.substring(23)
job +=1
}
if(line.contains("Job elapsed time=")){
jobTime(job1) = line.toString.substring(20)
job1 +=1
}
}
val lastArray = jobName.toList.zip(jobTime.toList)
for( loop <- 0 to job -1){ // 上面定义的数组出现异常 ,所有这里用真实的数组长度
println(lastArray(loop)._1.toString +" : "+ lastArray(loop)._2)
}
// lastArray.foreach(x => println(x._1.toString +" : "+ x._2))
var totalTime = 0
for(getTime <- 0 to job1 -1){
val times = jobTime(getTime).split(":")
totalTime += times(0).toInt * 3600 + times(1).toInt * 60 + times(2).toInt // 计算总时间
}
println("Total time is : " + totalTime/3600 + ":" + ((totalTime%3600)/60) + ":" + ((totalTime%3600)%60) ) // 标准时间 Total time is : 6:28:8
}
}