Created
July 28, 2015 01:04
-
-
Save xlcommunity/396496cc56c854868c16 to your computer and use it in GitHub Desktop.
acme.CsvExportHook example
This file contains 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
# | |
# THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS | |
# FOR A PARTICULAR PURPOSE. THIS CODE AND INFORMATION ARE NOT SUPPORTED BY XEBIALABS. | |
# | |
import os | |
def stripApplication(releaseId): | |
return releaseId.replace('Applications/', '') | |
def noneToEmpty(value): | |
if value is None: | |
return '' | |
else: | |
return value | |
def oneIfAutomated(taskType): | |
if taskType == 'xlrelease.Task': | |
return 0 | |
else: | |
return 1 | |
def oneIfOverrun(plannedDuration, duration): | |
if plannedDuration and duration > plannedDuration: | |
return 1 | |
else: | |
return 0 | |
def isEmpty(path): | |
return os.path.getsize(path) == 0 | |
def divOrEmpty(dividend, divisor): | |
if divisor != 0: | |
return float(dividend) / divisor | |
else: | |
return '' | |
def expandGroup(taskOrGroup): | |
if taskOrGroup.type != 'xlrelease.ParallelGroup': | |
return [taskOrGroup] | |
else: | |
tasks = [] | |
for task in taskOrGroup.tasks: | |
tasks.extend(expandGroup(task)) | |
return tasks | |
logger.info("Starting CSV export of the release %s" % release.id) | |
startReleaseMillis = release.startDate.getTime() | |
endReleaseMillis = release.endDate.getTime() | |
releaseDuration = (endReleaseMillis - startReleaseMillis) / 1000 | |
logger.debug("Writing release details for %s" % release.id) | |
release_details_file = open(exportHook.releaseDetailsFile, 'a') | |
try: | |
if isEmpty(exportHook.releaseDetailsFile): | |
release_details_file.write('"Release ID","Template ID","Release title","Release owner","Release start time","Release end time","Release duration","Planned release duration","Release delayed?","Release status","Release flag status","Release Tags","# tasks","# retried tasks","# completed tasks","# skipped tasks","Automation %"\n') | |
numTasks = 0 | |
numAutomatedTasks = 0 | |
numRetriedTasks = 0 | |
numCompletedTasks = 0 | |
numSkippedTasks = 0 | |
for phase in release.phases: | |
for taskOrGroup in phase.tasks: | |
for task in expandGroup(taskOrGroup): | |
numTasks += 1 | |
if oneIfAutomated(task.type): | |
numAutomatedTasks += 1 | |
if task.status == 'COMPLETED': | |
numCompletedTasks += 1 | |
elif task.status == 'SKIPPED': | |
numSkippedTasks += 1 | |
if task.failuresCount: | |
numRetriedTasks += 1 | |
release_details_file.write('%s,%s,"%s","%s",%d,%d,%d,%s,%d,%s,%s,"%s",%d,%s,%s,%s,%s\n' % (stripApplication(release.id), stripApplication(noneToEmpty(release.originTemplateId)), release.title, release.owner, startReleaseMillis, endReleaseMillis, releaseDuration, noneToEmpty(release.plannedDuration), oneIfOverrun(release.plannedDuration, releaseDuration), release.status, release.flagStatus, noneToEmpty(' '.join(release.tags)), numTasks, numRetriedTasks, numCompletedTasks, numSkippedTasks, divOrEmpty(numAutomatedTasks, numTasks))) | |
finally: | |
release_details_file.close() | |
logger.debug("Writing task details for %s" % release.id) | |
task_details_file = open(exportHook.taskDetailsFile, 'a') | |
try: | |
if isEmpty(exportHook.taskDetailsFile): | |
task_details_file.write('"Release ID","Template ID","Release title","Release owner","Release start time","Release end time","Release duration","Planned release duration","Release delayed?","Release status","Release flag status","Release Tags","Phase title","Task type","Automated task?","Task title","Task owner","Task team","Task start time","Task end time","Task duration","Task status","Task failure count","Task flag status"\n') | |
for phase in release.phases: | |
for taskOrGroup in phase.tasks: | |
for task in expandGroup(taskOrGroup): | |
startTaskMillis = task.startDate.getTime() | |
endTaskMillis = task.endDate.getTime() | |
task_details_file.write('%s,%s,"%s","%s",%d,%d,%d,%s,%d,%s,%s,"%s","%s",%s,%d,"%s","%s","%s",%d,%d,%d,%s,%d,%s\n' % (stripApplication(release.id), stripApplication(noneToEmpty(release.originTemplateId)), release.title, release.owner, startReleaseMillis, endReleaseMillis, releaseDuration, noneToEmpty(release.plannedDuration), oneIfOverrun(release.plannedDuration, releaseDuration), release.status, release.flagStatus, noneToEmpty(' '.join(release.tags)), phase.title, task.type, oneIfAutomated(task.type), task.title, noneToEmpty(task.owner), noneToEmpty(task.team), startTaskMillis, endTaskMillis, (endTaskMillis - startTaskMillis) / 1000, task.status, task.failuresCount, task.flagStatus)) | |
finally: | |
task_details_file.close() | |
logger.info("Completed CSV export of the release %s" % release.id) |
This file contains 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
<!-- | |
THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS | |
FOR A PARTICULAR PURPOSE. THIS CODE AND INFORMATION ARE NOT SUPPORTED BY XEBIALABS. | |
--> | |
<type type="acme.CsvExportHook" extends="xlrelease.ExportHook"> | |
<property name="releaseDetailsFile" /> | |
<property name="taskDetailsFile" /> | |
</type> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment