Permalink
Browse files

Add a /stats endpoint for agregrate stats in date order

  • Loading branch information...
1 parent 71eefcc commit da842335c8e782b463e23b6661d89e1332d7f153 @bruntonspall committed May 23, 2012
Showing with 24 additions and 8 deletions.
  1. +19 −2 app/controllers/Application.scala
  2. +2 −5 app/model/GuImporter.scala
  3. +3 −1 conf/routes
@@ -1,11 +1,12 @@
package controllers
import play.api._
+import libs.concurrent.Promise
import libs.json.Reads
import play.api.mvc._
import org.joda.time.DateTime
import play.api.libs.json.Json._
-import model.{NYTStoryCollector, AbstractStory, GuImporter}
+import model.{FTStoryCollector, AbstractStory, GuImporter, NYTStoryCollector}
object Application extends Controller {
implicit val reads = Reads
@@ -14,8 +15,24 @@ object Application extends Controller {
Ok(views.html.index("Your new application is ready."))
}
- def stats_gu = Action {
+ def stats = Action {
+ val dt = DateTime.now.minusHours(24)
+ val allStories : Promise[Seq[AbstractStory]] = for {
+ gustory <- GuImporter.storiesSince(dt)
+ ftstory <- FTStoryCollector.storiesSince(dt)
+ nytstory <- NYTStoryCollector.storiesSince(dt)
+ } yield gustory ++ ftstory ++ nytstory
+
+ val sortedStories = allStories map { stories => stories.sortWith((a,b) => a.publicationDate.isBefore(b.publicationDate))}
+// val stories = GuImporter.storiesSince(dt)
+ Ok(toJson(sortedStories.await.get))
+ }
+ def stats_ft = Action {
+ Ok(toJson(FTStoryCollector.storiesSince(DateTime.now.minusHours(24)).await.get))
+ }
+
+ def stats_gu = Action {
Ok(toJson(GuImporter.storiesSince(DateTime.now.minusHours(24)).await.get))
}
@@ -11,11 +11,8 @@ object GuImporter extends StoryImporter {
val url = "http://content.guardianapis.com/search.json?show-tags=keyword&from-date="+
ISODateTimeFormat.dateTimeNoMillis.withZoneUTC().print(dt)
log.error("Fetching from "+url)
- WS.url(url)
- .get.map {
- response =>
- (response.json \ "response" \ "results").asInstanceOf[JsArray].value.map {
- result =>
+ WS.url(url).get.map { response =>
+ (response.json \ "response" \ "results").asInstanceOf[JsArray].value.map { result =>
AbstractStory(
(result \ "webTitle").as[String],
(result \ "tags" \\ "webTitle").map(_.as[String]),
View
@@ -4,8 +4,10 @@
# Home page
GET / controllers.Application.index
-GET /stories/gu controllers.Application.stats_gu
+GET /stories/ft controllers.Application.stats_ft
GET /stories/nyt controllers.Application.stats_nyt
+GET /stories/gu controllers.Application.stats_gu
+GET /stories controllers.Application.stats
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path="/public", file)

0 comments on commit da84233

Please sign in to comment.