Random Bits of Open Code

Check-in [16897f5b92]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Initial charts working in cstats
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:16897f5b92def46190d3807c23f792508c2db2b9
User & Date: jmoon18 2019-04-01 22:45:44
Context
2019-04-01
23:49
Got colors better, removed grid lines, and got charts onto a single line check-in: b6626cc112 user: jmoon18 tags: trunk
22:45
Initial charts working in cstats check-in: 16897f5b92 user: jmoon18 tags: trunk
19:49
Added initial chart code, cstats is somewhat broken check-in: eceeb69e4b user: jmoon18 tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to ringchart/cstats.scm.

66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
...
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186


187
188

189
190
191
192
193
194
195
...
198
199
200
201
202
203
204

205
206
207
208
209
210
211
212
213
214
...
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232





















233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260





261
262
263
264
265
266
267
...
269
270
271
272
273
274
275
276
277
278
279
280
281

















282
283
284
285
















286
287
288
289
290
291
292











293
294
295
296
297
298
299
...
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
	res)))

(define (add-person db id uid mgr-id)
  (exec (sql db "INSERT INTO people (id,uid,mgr_id) VALUES (?,?,?);")
	id uid mgr-id))

(define (db-get-emps db mgr-id)
  (query fetch-rows (sql db "SELECT id,uid,mgr_id FROM people WHERE mgr_id=?;") mgr-id))

(define (db-get-everyone-below db mgr-id)
  (let ((emps (db-get-emps db mgr-id)))
    (apply append emps (map (lambda (rec)
			      (let ((id (car rec)))
				(db-get-everyone-below db id)))
			    emps))))
................................................................................
	     (cores     (fold
			 (lambda (x res)
			   (+ (lookup-rollup-cpu  db x) ;;(hash-table-ref/default cpu-usage-ht x 0)
			      res))
			 cpus-a ;; start with the parent value
			 emp-uids))) ;; put something real here
	;; now insert data for parent-id user
        (print "Employee ids: " emp-uids)
	(exec (sql db "INSERT OR REPLACE INTO rollup (id,uid,pid,total,actual,cores_tot,cores_act) VALUES (?,?,?,?,?,?,?);")
	      my-id my-uid my-mgr my-usage disk-a cores cores-act)))
     (else (print "no parent for " parent-id))
     )))

(define (regen-rollup top-mgr)
  (let ((dht (cache-all-usage "disk.db" ))
	(cht (cache-all-usage "jobs.db")))
    (rollup-recursively #f #f top-mgr dht cht "" fresh-start: #t)))

(define (create-datafiles db-in top-id)
  ;;(print "UID: " top-id)
  (let* ((db    (or db-in (open-db "people.db")))
	 (emps  (db-get-emps db top-id))
	 (my-rec (get-person-by-id db top-id))
	 (my-usage (get-rollup-record db top-id))


	 (my-cores (list-ref my-usage 4))
	 (my-disk  (list-ref my-usage 3)))

    (match
     my-rec
     ((id uid pid)
      ;;(print "UID: " uid)
      (with-output-to-file
	  (conc uid ".html")
	(lambda ()
................................................................................
		 "</head><body>\n")
      ;;(print "<script src=canvasjs.min.js></script>")
      (print "<script src=Chart.bundle.min.js></script>")
	  (print "<h2>" uid "</h2>"
		 "<p>Totals: " (to-int my-disk) " GB, cores: " (to-int my-cores) " (includes managers direct usage).</p>"
		 "<table>")
	  (print "<tr><th>Manager</th><th>Storage (GB)</th><th>Cores (normalized)</th></tr>")

	  (for-each
	   (lambda (rec)
         (print "Rec is: " rec)
	     (match
	      rec
	      ((e-id e-uid e-pid e-total e-cpu-tot)
	       (let ((emps     (db-get-emps db e-id))
		     (disk-tot (to-int e-total))
		     (cpu-tot  (to-int e-cpu-tot))
		     (has-emps (not (null? emps))))
................................................................................
		     (create-datafiles db e-id))
		 ;; emit record to this file 
		 (if (or (> disk-tot 0)(> cpu-tot 0))
		     (print "<tr><td>"
			    (if has-emps (conc "<a href=\"" e-uid ".html\">" e-uid "</a>") e-uid)
			    "</td><td>" disk-tot "</td><td>" cpu-tot "</td></tr>"))))
	      (else (print "ERROR: bad record " rec))))
	   (delete-duplicates (query fetch-rows (sql db "SELECT id,uid,pid,total,cores_tot FROM rollup WHERE pid=? OR id=?;") id id)))
	  (print "</table>")
(print "<canvas id='myChart' style='width: 250px; height: 250px'></canvas>")
(print "<script>")
(print "var ctx = document.getElementById('myChart').getContext('2d');")
(print "var myChart = new Chart(ctx, {")
(print "    type: 'pie',")
(print "    data: {")
(print "        labels: [")





















(for-each
  (lambda (rec)
    (match
     rec
     ((e-id e-uid e-pid e-total e-cpu-tot)
      (let (
	     (disk-tot (to-int e-total))
	     (cpu-tot  (to-int e-cpu-tot))
	     )
	 (if (or (> disk-tot 0)(> cpu-tot 0))
	     (print "'" e-uid "'")
     )))))
   (delete-duplicates (query fetch-rows (sql db "SELECT id,uid,pid,total,cores_tot FROM rollup WHERE pid=? OR id=?;") id id)))
  
;;'Bob', 'Fred', 'Jan', 'Julia', 'Fran', 'Orange'
(print "],")
(print "        datasets: [{")
(print "            label: '# of Votes',")
(print "            data: [")
;;12, 19, 3, 5, 2, 3
(print "],")
(print "            backgroundColor: [")
(print "                'rgba(255, 99, 132, 0.2)',")
(print "                'rgba(54, 162, 235, 0.2)',")
(print "                'rgba(255, 206, 86, 0.2)',")
(print "                'rgba(75, 192, 192, 0.2)',")
(print "                'rgba(153, 102, 255, 0.2)',")
(print "                'rgba(255, 159, 64, 0.2)'")





(print "            ],")
(print "            borderWidth: 1")
(print "        }]")
(print "    },")
(print "    options: {")
(print "        scales: {")
(print "            yAxes: [{")
................................................................................
(print "                    beginAtZero: true")
(print "                }")
(print "            }]")
(print "        }")
(print "    }")
(print "});")
(print "</script>")
(print "<canvas id='myChart2' style='width: 250px; height: 250px'></canvas>")
(print "<script>")
(print "var ctx = document.getElementById('myChart2').getContext('2d');")
(print "var myChart = new Chart(ctx, {")
(print "    type: 'pie',")
(print "    data: {")

















(print "        labels: ['Bob', 'Fred', 'Jan', 'Julia', 'Fran', 'Orange'],")
(print "        datasets: [{")
(print "            label: '# of Votes',")
(print "            data: [12, 19, 3, 5, 2, 3],")
















(print "            backgroundColor: [")
(print "                'rgba(255, 99, 132, 0.2)',")
(print "                'rgba(54, 162, 235, 0.2)',")
(print "                'rgba(255, 206, 86, 0.2)',")
(print "                'rgba(75, 192, 192, 0.2)',")
(print "                'rgba(153, 102, 255, 0.2)',")
(print "                'rgba(255, 159, 64, 0.2)'")











(print "            ],")
(print "            borderWidth: 1")
(print "        }]")
(print "    },")
(print "    options: {")
(print "        scales: {")
(print "            yAxes: [{")
................................................................................
     (else
      (print "ERROR: Bad record! " my-rec)))
    (if (not db-in)
	(close-database db))))


(define (get-rollup-record db id)
  (query fetch-row (sql db "SELECT id,uid,pid,total,cores_tot FROM rollup WHERE id=?;") id))

;;======================================================================
;; CDIS Queries
;;======================================================================

(define *top-mgr* 10069380)








|







 







|











|




>
>


>







 







>


|







 







|

|






>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>










|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
>
>
>
>







 







|





>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|


|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

|
|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>







 







|







66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
...
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
...
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
...
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
...
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
...
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
	res)))

(define (add-person db id uid mgr-id)
  (exec (sql db "INSERT INTO people (id,uid,mgr_id) VALUES (?,?,?);")
	id uid mgr-id))

(define (db-get-emps db mgr-id)
  (delete-duplicates (query fetch-rows (sql db "SELECT id,uid,mgr_id FROM people WHERE mgr_id=?;") mgr-id)))

(define (db-get-everyone-below db mgr-id)
  (let ((emps (db-get-emps db mgr-id)))
    (apply append emps (map (lambda (rec)
			      (let ((id (car rec)))
				(db-get-everyone-below db id)))
			    emps))))
................................................................................
	     (cores     (fold
			 (lambda (x res)
			   (+ (lookup-rollup-cpu  db x) ;;(hash-table-ref/default cpu-usage-ht x 0)
			      res))
			 cpus-a ;; start with the parent value
			 emp-uids))) ;; put something real here
	;; now insert data for parent-id user
        ;;(print "Employee ids: " emp-uids)
	(exec (sql db "INSERT OR REPLACE INTO rollup (id,uid,pid,total,actual,cores_tot,cores_act) VALUES (?,?,?,?,?,?,?);")
	      my-id my-uid my-mgr my-usage disk-a cores cores-act)))
     (else (print "no parent for " parent-id))
     )))

(define (regen-rollup top-mgr)
  (let ((dht (cache-all-usage "disk.db" ))
	(cht (cache-all-usage "jobs.db")))
    (rollup-recursively #f #f top-mgr dht cht "" fresh-start: #t)))

(define (create-datafiles db-in top-id)
  (format (current-error-port) "UID: ~A\n" top-id)
  (let* ((db    (or db-in (open-db "people.db")))
	 (emps  (db-get-emps db top-id))
	 (my-rec (get-person-by-id db top-id))
	 (my-usage (get-rollup-record db top-id))
	 (my-actual-disk (list-ref my-usage 5))
	 (my-actual-cores (list-ref my-usage 6))
	 (my-cores (list-ref my-usage 4))
	 (my-disk  (list-ref my-usage 3)))
    (format (current-error-port) "Emps: ~A\n" emps)
    (match
     my-rec
     ((id uid pid)
      ;;(print "UID: " uid)
      (with-output-to-file
	  (conc uid ".html")
	(lambda ()
................................................................................
		 "</head><body>\n")
      ;;(print "<script src=canvasjs.min.js></script>")
      (print "<script src=Chart.bundle.min.js></script>")
	  (print "<h2>" uid "</h2>"
		 "<p>Totals: " (to-int my-disk) " GB, cores: " (to-int my-cores) " (includes managers direct usage).</p>"
		 "<table>")
	  (print "<tr><th>Manager</th><th>Storage (GB)</th><th>Cores (normalized)</th></tr>")
      (print "<tr><td>" uid "</td><td>" (to-int my-actual-disk) "</td><td>" (to-int my-actual-cores) "</th></tr>")
	  (for-each
	   (lambda (rec)
         ;;(print "Rec is: " rec)
	     (match
	      rec
	      ((e-id e-uid e-pid e-total e-cpu-tot)
	       (let ((emps     (db-get-emps db e-id))
		     (disk-tot (to-int e-total))
		     (cpu-tot  (to-int e-cpu-tot))
		     (has-emps (not (null? emps))))
................................................................................
		     (create-datafiles db e-id))
		 ;; emit record to this file 
		 (if (or (> disk-tot 0)(> cpu-tot 0))
		     (print "<tr><td>"
			    (if has-emps (conc "<a href=\"" e-uid ".html\">" e-uid "</a>") e-uid)
			    "</td><td>" disk-tot "</td><td>" cpu-tot "</td></tr>"))))
	      (else (print "ERROR: bad record " rec))))
	   (delete-duplicates (query fetch-rows (sql db "SELECT id,uid,pid,total,cores_tot FROM rollup WHERE pid=? ;") id )))
	  (print "</table>")
(print "<div id='canvas-holder' style='width: 40%'><canvas id='myChart' style='width: 250px; height: 250px'></canvas></div>")
(print "<script>")
(print "var ctx = document.getElementById('myChart').getContext('2d');")
(print "var myChart = new Chart(ctx, {")
(print "    type: 'pie',")
(print "    data: {")
(print "        labels: [")
(print "'" uid"',")
(for-each
  (lambda (rec)
    (match
     rec
     ((e-id e-uid e-pid e-total e-cpu-tot)
      (let (
	     (disk-tot (to-int e-total))
	     (cpu-tot  (to-int e-cpu-tot))
	     )
	 (if (or (> disk-tot 0)(> cpu-tot 0))
	     (print "'" e-uid "',")
     )))))
   (delete-duplicates (query fetch-rows (sql db "SELECT id,uid,pid,total,cores_tot FROM rollup WHERE pid=?;") id)))
  
;;'Bob', 'Fred', 'Jan', 'Julia', 'Fran', 'Orange'
(print "],")
(print "        datasets: [{")
(print "            label: '# of Votes',")
(print "            data: [")
(print "'" (to-int my-actual-disk) "',")
(for-each
  (lambda (rec)
    (match
     rec
     ((e-id e-uid e-pid e-total e-cpu-tot)
      (let (
	     (disk-tot (to-int e-total))
	     (cpu-tot  (to-int e-cpu-tot))
	     )
	 (if (or (> disk-tot 0)(> cpu-tot 0))
	     (print "'" disk-tot "',")
     )))))
   (delete-duplicates (query fetch-rows (sql db "SELECT id,uid,pid,total,cores_tot FROM rollup WHERE pid=?;") id)))
;;12, 19, 3, 5, 2, 3
(print "],")
(print "            backgroundColor: [")
(print "                'rgba(255, 0, 0, 0.2)',")
(print "                'rgba(255, 255, 0, 0.2)',")
(print "                'rgba(255, 0, 255, 0.2)',")
(print "                'rgba(255, 128, 0, 0.2)',")
(print "                'rgba(255, 0, 128, 0.2)',")
(print "                'rgba(255, 128, 128, 0.2)',")
(print "                'rgba(128, 0, 0, 0.2)',")
(print "                'rgba(128, 255, 0,  0.2)',")
(print "                'rgba(128, 0, 255, 0.2)',")
(print "                'rgba(128, 128, 0, 0.2)',")
(print "                'rgba(128, 0, 128, 0.2)',")
(print "                'rgba(0,  0, 255, 0.2)',")
(print "                'rgba(0,  255, 255, 0.2)',")
(print "                'rgba(0,  128, 0, 0.2)',")
(print "                'rgba(0,  0, 128, 0.2)',")
(print "                'rgba(0,  128, 128, 0.2)',")
(print "                'rgba(0, 255, 0, 0.2)'")
(print "            ],")
(print "            borderWidth: 1")
(print "        }]")
(print "    },")
(print "    options: {")
(print "        scales: {")
(print "            yAxes: [{")
................................................................................
(print "                    beginAtZero: true")
(print "                }")
(print "            }]")
(print "        }")
(print "    }")
(print "});")
(print "</script>")
(print "<div id='canvas-holder2' style='width: 40%'><canvas id='myChart2' style='width: 250px; height: 250px'></canvas></div>")
(print "<script>")
(print "var ctx = document.getElementById('myChart2').getContext('2d');")
(print "var myChart = new Chart(ctx, {")
(print "    type: 'pie',")
(print "    data: {")
(print "        labels: [")
(print "'" uid"',")
(for-each
  (lambda (rec)
    (match
     rec
     ((e-id e-uid e-pid e-total e-cpu-tot)
      (let (
	     (disk-tot (to-int e-total))
	     (cpu-tot  (to-int e-cpu-tot))
	     )
	 (if (or (> disk-tot 0)(> cpu-tot 0))
	     (print "'" e-uid "',")
     )))))
   (delete-duplicates (query fetch-rows (sql db "SELECT id,uid,pid,total,cores_tot FROM rollup WHERE pid=?;") id)))
(print "],")

;;'Bob', 'Fred', 'Jan', 'Julia', 'Fran', 'Orange'],")
(print "        datasets: [{")
(print "            label: '# of Votes',")
(print "            data: [")
(print "'" (to-int my-actual-cores) "',")
(for-each
  (lambda (rec)
    (match
     rec
     ((e-id e-uid e-pid e-total e-cpu-tot)
      (let (
	     (disk-tot (to-int e-total))
	     (cpu-tot  (to-int e-cpu-tot))
	     )
	 (if (or (> disk-tot 0)(> cpu-tot 0))
	     (print "'" cpu-tot "',")
     )))))
   (delete-duplicates (query fetch-rows (sql db "SELECT id,uid,pid,total,cores_tot FROM rollup WHERE pid=?;") id)))
(print "],")
;;12, 19, 3, 5, 2, 3],")
(print "            backgroundColor: [")
(print "                'rgba(255, 0, 0, 0.2)',")
(print "                'rgba(255, 255, 0, 0.2)',")
(print "                'rgba(255, 0, 255, 0.2)',")
(print "                'rgba(255, 128, 0, 0.2)',")
(print "                'rgba(255, 0, 128, 0.2)',")
(print "                'rgba(255, 128, 128, 0.2)',")
(print "                'rgba(128, 0, 0, 0.2)',")
(print "                'rgba(128, 255, 0,  0.2)',")
(print "                'rgba(128, 0, 255, 0.2)',")
(print "                'rgba(128, 128, 0, 0.2)',")
(print "                'rgba(128, 0, 128, 0.2)',")
(print "                'rgba(0,  0, 255, 0.2)',")
(print "                'rgba(0,  255, 255, 0.2)',")
(print "                'rgba(0,  128, 0, 0.2)',")
(print "                'rgba(0,  0, 128, 0.2)',")
(print "                'rgba(0,  128, 128, 0.2)',")
(print "                'rgba(0, 255, 0, 0.2)'")
(print "            ],")
(print "            borderWidth: 1")
(print "        }]")
(print "    },")
(print "    options: {")
(print "        scales: {")
(print "            yAxes: [{")
................................................................................
     (else
      (print "ERROR: Bad record! " my-rec)))
    (if (not db-in)
	(close-database db))))


(define (get-rollup-record db id)
  (query fetch-row (sql db "SELECT id,uid,pid,total,cores_tot,actual, cores_act FROM rollup WHERE id=?;") id))

;;======================================================================
;; CDIS Queries
;;======================================================================

(define *top-mgr* 10069380)