Guest User

Untitled

a guest
May 17th, 2013
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 1.79 KB | None | 0 0
  1. package skillz.admin
  2.  
  3. import org.joda.time.DateTime
  4. import org.joda.time.Hours
  5. import org.joda.time.Interval
  6. import org.joda.time.format.DateTimeFormat
  7. import org.springframework.transaction.annotation.Transactional
  8. import skillz.Game
  9. import skillz.utils.TimeUtils
  10.  
  11. @Transactional
  12. class GameMetricsJob {
  13.  
  14.     private static final String BOOTSTRAP_DATE_STRING = '05/05/2013'
  15.     private static final String BOOTSTRAP_DATE_PATTERN = 'MM/dd/yyyy'
  16.     private static final DateTime BOOTSTRAP_DATE
  17.  
  18.     static {
  19.         def f = DateTimeFormat.forPattern(BOOTSTRAP_DATE_PATTERN)
  20.         BOOTSTRAP_DATE = f.parseDateTime(BOOTSTRAP_DATE_STRING)
  21.     }
  22.  
  23.     def createGameMetricsService
  24.  
  25.     static triggers = {
  26.       simple repeatInterval: 5000l * 600 // execute job once in 5 seconds
  27.     }
  28.  
  29.     def execute() {
  30.         log.info('executing GameMetricsJob')
  31.  
  32.         def games = Game.list() + [ id: 'skillz' ]
  33.  
  34.         def lastMetric = GameMetrics.first()
  35.         def startDate
  36.         if (!lastMetric) {
  37.             // bootstrap start date
  38.             startDate = BOOTSTRAP_DATE
  39.         } else {
  40.             startDate = new DateTime(lastMetric.dateCreated)
  41.         }
  42.  
  43.         // TEST code
  44.         def f = DateTimeFormat.forPattern(BOOTSTRAP_DATE_PATTERN)
  45.         def end = f.parseDateTime('05/06/2013')
  46.  
  47.         def intervals = TimeUtils.generateIntervals(Hours.ONE, startDate, end)
  48.  
  49.         GameMetrics.withTransaction {
  50.             intervals.each { Interval interval ->
  51.                 games.each { game ->
  52.                     def m = createGameMetricsService.create(
  53.                             game.id.toString(), 'hourly', interval.start.toDate())
  54.                     m.save()
  55.                 }
  56.             }
  57.         }
  58.  
  59.         log.info('finished GameMetricsJob')
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment