Browse Source

Show data as it comes in (cached for one hour)

Jakob Borg 11 years ago
parent
commit
85bb725eb6
2 changed files with 38 additions and 6 deletions
  1. 37 5
      main.go
  2. 1 1
      static/index.html

+ 37 - 5
main.go

@@ -96,18 +96,49 @@ var cache map[string]interface{}
 var cacheDate string
 var cacheMut sync.Mutex
 
+func fileList() ([]string, error) {
+	files := make(map[string]string)
+	t0 := time.Now().Add(-24 * time.Hour).Format("20060102")
+	t1 := time.Now().Format("20060102")
+
+	dir := filepath.Join(*dbDir, t0)
+	gr, err := filepath.Glob(filepath.Join(dir, "*.json"))
+	if err != nil {
+		return nil, err
+	}
+	for _, f := range gr {
+		bn := filepath.Base(f)
+		files[bn] = f
+	}
+
+	dir = filepath.Join(*dbDir, t1)
+	gr, err = filepath.Glob(filepath.Join(dir, "*.json"))
+	if err != nil {
+		return nil, err
+	}
+	for _, f := range gr {
+		bn := filepath.Base(f)
+		files[bn] = f
+	}
+
+	l := make([]string, 0, len(files))
+	for _, f := range files {
+		l = append(l, f)
+	}
+
+	return l, nil
+}
+
 func reportHandler(w http.ResponseWriter, r *http.Request) {
-	yesterday := time.Now().Add(-24 * time.Hour).Format("20060102")
+	cd := time.Now().Format("20060102T15")
 
 	cacheMut.Lock()
 	cacheMut.Unlock()
 
-	if cacheDate != yesterday {
+	if cacheDate != cd {
 		cache = make(map[string]interface{})
 
-		dir := filepath.Join(*dbDir, yesterday)
-
-		files, err := filepath.Glob(filepath.Join(dir, "*.json"))
+		files, err := fileList()
 		if err != nil {
 			http.Error(w, "Glob error", 500)
 			return
@@ -156,6 +187,7 @@ func reportHandler(w http.ResponseWriter, r *http.Request) {
 		}
 
 		cache = make(map[string]interface{})
+		cache["cache"] = cd
 		cache["nodes"] = nodes
 		cache["versions"] = versions
 		cache["platforms"] = platforms

+ 1 - 1
static/index.html

@@ -29,7 +29,7 @@ found in the LICENSE file.
       <div class="col-md-12">
         <h1>Syncthing Usage Data</h1>
         <p>
-          This is the aggregated usage report data for yesterday. Data based on <b>{{ report.nodes }}</b> nodes that have reported in.
+          This is the aggregated usage report data for today and yesterday. Data based on <b>{{ report.nodes }}</b> nodes that have reported in.
         </p>
 
         <table class="table table-striped">