This repository has been archived by the owner on Oct 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
CountUsers.groovy
115 lines (88 loc) · 4.03 KB
/
CountUsers.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
@Grab('org.codehaus.groovy:groovy-json:2.5.6')
@Grab('org.codehaus.groovy:groovy-dateutil:2.5.6')
@Grab('info.picocli:picocli:3.9.3')
@Command(
name = "count-user-activity.sh",
description = "Count users from /out/aggregated-user-activity.json",
showDefaultValues = true
)
@picocli.groovy.PicocliScript
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import groovy.transform.Field
import groovy.transform.SourceURI
import java.text.SimpleDateFormat
import static picocli.CommandLine.*
Map buildSummary(File reportFile, Date from) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd")
def report = new JsonSlurper().parseText(reportFile.text)
def authAccess = report.authAccess.findAll { dateFormat.parse(it.firstDayOfMonth).after(from) }
def authAccessFrom = authAccess*.firstDayOfMonth.min() ?: dateFormat.format(from)
def authAccessTo = authAccess*.firstDayOfMonth.max() ?: dateFormat.format(new Date())
def authAccessUsers = authAccess*.name.unique()
println "${authAccessUsers.size()} user(s) counted as Authenticated from ${authAccessFrom} to ${authAccessTo}"
def scmAccess = report.scmAccess.findAll { dateFormat.parse(it.firstDayOfMonth).after(from) }
def scmAccessFrom = scmAccess*.firstDayOfMonth.min() ?: dateFormat.format(from)
def scmAccessTo = scmAccess*.firstDayOfMonth.max() ?: dateFormat.format(new Date())
def scmAccessUsers = scmAccess*.name.unique()
println "${scmAccessUsers.size()} user(s) counted as SCM contributor from ${scmAccessFrom} to ${scmAccessTo}"
def totalAccessFrom = [authAccessFrom, scmAccessFrom].min() ?: dateFormat.format(from)
def totalAccessTo = [authAccessTo, scmAccessTo].max() ?: dateFormat.format(new Date())
def totalUsers = (authAccessUsers + scmAccessUsers).unique()
println "${totalUsers.size()} user(s) counted as CloudBees Core users from ${totalAccessFrom} to ${totalAccessTo}"
return [
"from" : totalAccessFrom,
"to" : totalAccessTo,
"authUsers": authAccessUsers,
"scmUsers" : scmAccessUsers]
}
String getRelativePath(File base, File path) {
return base.toURI().relativize(path.toURI()).getPath();
}
File reportFile(File workDir) {
File reportFile = new File(workDir, "/out/aggregated-user-activity.json")
if (reportFile.exists() && !reportFile.isFile()) {
println "[ERROR] The path " + getRelativePath(workDir, reportFile) + " isn't a file."
System.exit(1)
}
if (!reportFile.exists()) {
println "[ERROR] The report file " + getRelativePath(workDir, reportFile) + " doesn't exist."
System.exit(1)
}
return reportFile
}
File summaryFile(File workDir) {
File summaryFile = new File(workDir, "/out/aggregated-user-activity-summary.json")
if (summaryFile.exists() && !summaryFile.isFile()) {
println "[ERROR] The path " + getRelativePath(workDir, summaryFile) + " isn't a file."
System.exit(2)
}
if (summaryFile.exists()) {
println "[WARN] The summary file " + getRelativePath(workDir, summaryFile) + " is existing and will be overriden."
}
return summaryFile
}
// The path of the Groovy script
@SourceURI
URI sourceUri
// The folder where the Groovy script is
File workDir = new File(sourceUri).parentFile
println "Working directory: ${workDir}"
// Help CLI option
@Option(names = ["-h", "--help"], usageHelp = true, description = "Show this help message and exit.")
@Field boolean helpRequested
// From CLI option
@Option(names = ["-f", "--from"], description = "Start date to count the number of users. Optional, default is 1 year ago.")
@Field Date from = new Date().minus(365)
// The summary file we generate
File summaryFile = summaryFile(workDir)
println "Counting users ..."
summaryFile.withWriter { writer ->
writer.write(JsonOutput.prettyPrint(
JsonOutput.toJson(
buildSummary(reportFile(workDir), from)
)
))
}
println "Detailed summary available in: " + getRelativePath(workDir, summaryFile)
System.exit(0)