Newer
Older
weather-servlet / src / main / scala / uk / org / floop / msc / rest / Graph.scala
package uk.org.floop.msc.rest

import net.liftweb.http._
import net.liftweb.util._
import uk.org.floop.msc.rrd._
import net.liftweb.common.Box
import net.liftweb.common.Full
import net.liftweb.common.Empty

object Graph {

  def apply(args: List[String]): Box[LiftResponse] = {
    args match {
      case arg1 :: arg2 :: Nil => {
        val timePeriod = arg1 match {
          case "hour"  => Some(TimePeriod.HOUR)
          case "day"   => Some(TimePeriod.DAY)
          case "week"  => Some(TimePeriod.WEEK)
          case "month" => Some(TimePeriod.MONTH)
          case "year"  => Some(TimePeriod.YEAR)
          case _       => None 
        }
        timePeriod match {
          case Some(p) => 
            DataStore !? GenerateGraph("/" + arg2 + ".xml", p) match {
              case Some(ImageGenerated(bytes)) => Full(InMemoryResponse(bytes, List(("Content-Type", "image/png")), Nil, 200))
              case None => 
                println("Unable to generate graph.")
                Empty
            }
          case _ =>
            println("Unrecognised time period.")
            Empty
        }
      }
      case _ =>
        println("Undefined graph type or period.")
        Empty
    }  
  }
  
}