Created
May 21, 2019 09:39
-
-
Save rah003/85d5417634c22bfb42fc99f0436310d7 to your computer and use it in GitHub Desktop.
Aggregate times
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Calendar; | |
cal = Calendar.getInstance(); | |
def currentYear = cal.get(Calendar.YEAR) % 100; | |
s = ctx.getJCRSession("foflinks"); | |
n = s.getNode("/") | |
println ("at root") | |
// consolidate years first | |
yearPeriod = 17..(currentYear-1) | |
yearPeriod.each() { | |
println(" consolidating year " + it) | |
def searchedPeriod = "" + it | |
def newPropName = "count"+searchedPeriod+"1231_23-59-59" | |
consolidateCount(searchedPeriod, n, newPropName); | |
println(" done") | |
} | |
// months next | |
(1..<(cal.get(Calendar.MONTH)+1)).each() { | |
month = String.format("%02d", it) | |
println(" consolidating " +currentYear + "-" + month) | |
def searchedPeriod = "" + currentYear + month | |
def newPropName = "count"+searchedPeriod+"31_23-59-59" | |
consolidateCount(searchedPeriod, n, newPropName); | |
println(" done") | |
} | |
// days last | |
(1..<(cal.get(Calendar.DAY_OF_MONTH))).each() { | |
month = String.format("%02d", cal.get(Calendar.MONTH)+1) | |
day = String.format("%02d", it) | |
println(" consolidating " +currentYear + "-" + month + "-" + day) | |
def searchedPeriod = "" + currentYear + month+day | |
def newPropName = "count"+searchedPeriod+"_23-59-59" | |
consolidateCount(searchedPeriod, n, newPropName); | |
println(" done") | |
} | |
def consolidateCount(period, parent, newPropName) { | |
def iter = parent.getNodes() | |
while (iter.hasNext()) { | |
node = iter.nextNode(); | |
if (!(node.isNodeType("link") || node.isNodeType("mgnl:folder"))) { | |
continue; | |
} | |
println("looking for count props in " + node.getName()) | |
propIter = node.getProperties("count"+period+"*"); | |
long count = 0 | |
long consolidatedCount = 0 | |
while (propIter.hasNext()) { | |
prop = propIter.nextProperty() | |
count += prop.getLong(); | |
print(".") | |
if (prop.getName().endsWith("_23-59-59")) { | |
// was possibly already consolidated | |
consolidatedCount = prop.getLong(); | |
} else { | |
prop.remove(); | |
} | |
} | |
println() | |
println("for [" + node.getName() + "] found total of " + count) | |
if (count > 0 && count != consolidatedCount) { | |
node.setProperty(newPropName, count) | |
s.save(); | |
} | |
consolidateCount(period, node, newPropName) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment