timestamper/resources/main/start-script.groovy

92 lines
3.3 KiB
Groovy
Raw Normal View History

import com.jdbernard.timeanalyzer.categories.FilteredCategory
import com.jdbernard.timeanalyzer.categories.TimeIntervalCategory
import com.jdbernard.timeanalyzer.categorizationplans.DailyCategorizationPlan
import com.jdbernard.timeanalyzer.categorizationplans.DescriptionBasedCategorizationPlan
import com.jdbernard.timeanalyzer.categorizationplans.TwoLevelCategorizationPlan
import com.jdbernard.timeanalyzer.processors.TimelineEventProcessor
import com.jdbernard.timestamper.core.FileTimelineSource
import org.jfree.chart.ChartFactory
import org.jfree.chart.ChartFrame
import org.jfree.data.general.DefaultPieDataset
import org.jfree.util.SortOrder
import org.joda.time.DateTime
import org.joda.time.Interval
import org.joda.time.Period
import org.joda.time.format.PeriodFormat
pf = PeriodFormat.getDefault()
printDuration = { duration ->
pf.print(duration.toPeriod()) }
loadEvents = { file, processor ->
fileSource = new FileTimelineSource(file.toURI())
timeline = fileSource.read()
return processor.process(timeline) }
makePieDataset = { category ->
DefaultPieDataset dpds = new DefaultPieDataset()
category.categories.each { cat ->
dpds.setValue(cat.description, cat.duration.standardSeconds) }
category.events.each { entry ->
dpds.setValue(entry.description, entry.duration.standardSeconds) }
dpds.sortByValues(SortOrder.DESCENDING)
return dpds }
makePieFrame = { categoryName, category ->
def dataset = makePieDataset(category)
return new ChartFrame(categoryName,
ChartFactory.createPieChart("Time Spent", dataset, true, true, false)) }
analyze = { file ->
def tep = new TimelineEventProcessor()
tep.exclusions << /.*Home.*/
def twoLevelPlan = new TwoLevelCategorizationPlan()
def descriptionPlan = new DescriptionBasedCategorizationPlan()
def setupCat = { cat ->
cat.categorizationPlans << twoLevelPlan
cat.categorizationPlans << descriptionPlan }
def dailyPlan = new DailyCategorizationPlan(setupCat)
def events = loadEvents(file, tep)
def topcat = new FilteredCategory("Time Analysis: Jonathan Bernard")
topcat.categorizationPlans << dailyPlan
topcat.categorizationPlans << twoLevelPlan
topcat.categorizationPlans << descriptionPlan
events.each { if (topcat.matchesEvent(it)) topcat.addEvent(it) }
return [topcat: topcat, rawEvents: events] }
listEvents = {topcat ->
topcat.events.eachWithIndex { event, index ->
println "${index}: ${event} (${printDuration(event.duration)})" }}
listCategories = { topcat ->
topcat.categories.eachWithIndex { cat, index ->
println "${index}: ${cat} (${printDuration(cat.duration)})" }}
details = { topcat ->
println "Categories"
listCategories(topcat)
println "Events"
listEvents(topcat) }
weeklySummary = { events ->
def todayMidnight = new DateTime()
def friday = todayMidnight.withDayOfWeek(5)
def monday = todayMidnight.withDayOfWeek(1)
def week = new Interval(monday, Period.days(7))
def weekCat = new TimeIntervalCategory(
"Week of ${friday.toString('MMM dd, yyyy')}", week)
weekCat.categorizationPlans << new TwoLevelCategorizationPlan()
weekCat.categorizationPlans << new DescriptionBasedCategorizationPlan()
events.each { if (weekCat.matchesEvent(it)) weekCat.addEvent(it) }
return weekCat }