Browse Source

cmd/ursrv: Summarize known distribution channels

That is, whether the binary was downloaded from GitHub, from our APT
repository, etc.
Jakob Borg 6 years ago
parent
commit
e16a65bacb
3 changed files with 62 additions and 14 deletions
  1. 2 2
      cmd/ursrv/analytics.go
  2. 39 12
      cmd/ursrv/main.go
  3. 21 0
      cmd/ursrv/static/index.html

+ 2 - 2
cmd/ursrv/analytics.go

@@ -24,10 +24,10 @@ type analyticList []analytic
 
 func (l analyticList) Less(a, b int) bool {
 	if l[a].Key == "Others" {
-		return true
+		return false
 	}
 	if l[b].Key == "Others" {
-		return false
+		return true
 	}
 	return l[b].Count < l[a].Count // inverse
 }

+ 39 - 12
cmd/ursrv/main.go

@@ -33,20 +33,36 @@ import (
 )
 
 var (
-	useHTTP          = os.Getenv("UR_USE_HTTP") != ""
-	debug            = os.Getenv("UR_DEBUG") != ""
-	keyFile          = getEnvDefault("UR_KEY_FILE", "key.pem")
-	certFile         = getEnvDefault("UR_CRT_FILE", "crt.pem")
-	dbConn           = getEnvDefault("UR_DB_URL", "postgres://user:password@localhost/ur?sslmode=disable")
-	listenAddr       = getEnvDefault("UR_LISTEN", "0.0.0.0:8443")
-	geoIPPath        = getEnvDefault("UR_GEOIP", "GeoLite2-City.mmdb")
-	tpl              *template.Template
-	compilerRe       = regexp.MustCompile(`\(([A-Za-z0-9()., -]+) \w+-\w+(?:| android| default)\) ([\[email protected]]+)`)
-	progressBarClass = []string{"", "progress-bar-success", "progress-bar-info", "progress-bar-warning", "progress-bar-danger"}
-	featureOrder     = []string{"Various", "Folder", "Device", "Connection", "GUI"}
-	knownVersions    = []string{"v2", "v3"}
+	useHTTP            = os.Getenv("UR_USE_HTTP") != ""
+	debug              = os.Getenv("UR_DEBUG") != ""
+	keyFile            = getEnvDefault("UR_KEY_FILE", "key.pem")
+	certFile           = getEnvDefault("UR_CRT_FILE", "crt.pem")
+	dbConn             = getEnvDefault("UR_DB_URL", "postgres://user:password@localhost/ur?sslmode=disable")
+	listenAddr         = getEnvDefault("UR_LISTEN", "0.0.0.0:8443")
+	geoIPPath          = getEnvDefault("UR_GEOIP", "GeoLite2-City.mmdb")
+	tpl                *template.Template
+	compilerRe         = regexp.MustCompile(`\(([A-Za-z0-9()., -]+) \w+-\w+(?:| android| default)\) ([\[email protected]]+)`)
+	progressBarClass   = []string{"", "progress-bar-success", "progress-bar-info", "progress-bar-warning", "progress-bar-danger"}
+	featureOrder       = []string{"Various", "Folder", "Device", "Connection", "GUI"}
+	knownVersions      = []string{"v2", "v3"}
+	knownDistributions = []distributionMatch{
+		// Maps well known builders to the official distribution method that
+		// they represent
+		{regexp.MustCompile("android-.*[email protected]"), "Google Play"},
+		{regexp.MustCompile("[email protected]"), "GitHub"},
+		{regexp.MustCompile("[email protected]"), "APT"},
+		{regexp.MustCompile("[email protected]"), "Docker Hub"},
+		{regexp.MustCompile("[email protected]"), "GitHub"},
+		{regexp.MustCompile("[email protected]"), "Snappy"},
+		{regexp.MustCompile("."), "Others"},
+	}
 )
 
+type distributionMatch struct {
+	matcher      *regexp.Regexp
+	distribution string
+}
+
 var funcs = map[string]interface{}{
 	"commatize":  commatize,
 	"number":     number,
@@ -1001,6 +1017,7 @@ func getReport(db *sql.DB) map[string]interface{} {
 	var uptime []int
 	var compilers []string
 	var builders []string
+	var distributions []string
 	locations := make(map[location]int)
 	countries := make(map[string]int)
 
@@ -1070,10 +1087,19 @@ func getReport(db *sql.DB) map[string]interface{} {
 		nodes++
 		versions = append(versions, transformVersion(rep.Version))
 		platforms = append(platforms, rep.Platform)
+
 		if m := compilerRe.FindStringSubmatch(rep.LongVersion); len(m) == 3 {
 			compilers = append(compilers, m[1])
 			builders = append(builders, m[2])
+		loop:
+			for _, d := range knownDistributions {
+				if d.matcher.MatchString(rep.LongVersion) {
+					distributions = append(distributions, d.distribution)
+					break loop
+				}
+			}
 		}
+
 		if rep.NumFolders > 0 {
 			numFolders = append(numFolders, rep.NumFolders)
 		}
@@ -1366,6 +1392,7 @@ func getReport(db *sql.DB) map[string]interface{} {
 	r["platforms"] = group(byPlatform, analyticsFor(platforms, 2000), 5)
 	r["compilers"] = group(byCompiler, analyticsFor(compilers, 2000), 5)
 	r["builders"] = analyticsFor(builders, 12)
+	r["distributions"] = analyticsFor(distributions, 10)
 	r["featureOrder"] = featureOrder
 	r["locations"] = locations
 	r["contries"] = countryList

+ 21 - 0
cmd/ursrv/static/index.html

@@ -506,6 +506,27 @@ found in the LICENSE file.
         </table>
       </div>
 
+      <div class="col-md-6">
+        <table class="table table-striped">
+          <thead>
+            <tr>
+              <th>Distribution Channel</th>
+              <th class="text-right">Devices</th>
+              <th class="text-right">Share</th>
+            </tr>
+          </thead>
+          <tbody>
+            {{range .distributions}}
+            <tr>
+              <td>{{.Key}}</td>
+              <td class="text-right">{{.Count}}</td>
+              <td class="text-right">{{.Percentage | printf "%.01f"}}%</td>
+            </tr>
+            {{end}}
+          </tbody>
+        </table>
+      </div>
+
     </div>
 
     <div class="row">