Commit 190f0153 authored by Steven Murdoch's avatar Steven Murdoch
Browse files

Parse the status file and produce a Set of runnign nodes

parent 6f3b0b22
Loading
Loading
Loading
Loading
+40 −4
Original line number Diff line number Diff line
@@ -5,9 +5,27 @@ import _root_.java.text.SimpleDateFormat
import _root_.java.util.Date
import _root_.scala.collection.immutable._

class Status(fn: String)

class Status(runningRouters: Set[AutoPromotion.RouterName]) {
    override def toString = runningRouters.size.toString + " running routers"
}

object AutoPromotion {

    type RouterName = String

    /** Allow Java comparable objects to be put into a TreeMap
      * From: http://thread.gmane.org/gmane.comp.lang.scala/16102 */
    implicit def comparable2ordered[A <: Comparable[A]](x: A): Ordered[A] =
        new Ordered[A] with Proxy {
            val self = x
            def compare(y: A): Int = { x.compareTo(y) }
    } 

    def debug(message: String) =
        System.out.println(message)

    /** Return a list of all files recursively found in <root> */
    def getAllFiles(root: File): List[File] = {
        var allFiles: List[File] = Nil
        for (f <- root.listFiles) {
@@ -19,20 +37,38 @@ object AutoPromotion {
        allFiles
    }

    /** Parse a consensus, and return a set of running routers */
    def parseStatus(f: File) = {
        def parseStatus(br: BufferedReader, acc: Set[RouterName]): Set[RouterName] = {
            val line = br.readLine
            if (null == line)
                acc
            else if (!line.startsWith("r "))
                parseStatus(br, acc)
            else
                parseStatus(br, acc + line.split(" ")(2))
        }
        debug("Reading " + f)
        val br = new BufferedReader(new FileReader(f))
        parseStatus(br, Set.empty)
    }

    /** Given a list of Files, return a mapping of Date -> Status */
    def getRunningNodes(allFiles: List[File]) = {
        val parseFormat = new SimpleDateFormat("yyyyMMdd-HHmmss")

        def getRunningNodes(allFiles: List[File], acc: Map[Date, Status]): Map[Date, Status] = {
        def getRunningNodes(allFiles: List[File], acc: SortedMap[Date, Status]): SortedMap[Date, Status] = {
            allFiles match {
                case Nil => acc
                case f :: fs => {
                    val fn = f.getName
                    val timestamp = parseFormat.parse(fn.substring(0, 15))
                    getRunningNodes(fs, acc + (timestamp -> new Status(fn)))
                    val runningRouters = parseStatus(f)
                    getRunningNodes(fs, acc + (timestamp -> new Status(runningRouters)))
                }
            }
        }
        getRunningNodes(allFiles, HashMap.empty)
        getRunningNodes(allFiles, TreeMap.empty)
    }

    def main(args: Array[String]) {