Random Bits of Open Code

Check-in [a76a9c267b]
Login

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

Overview
Comment:Iterate over areas (not 100% sure this is a good idea).
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:a76a9c267bb104409bd3616783c9eecd583db0fb
User & Date: matt 2018-05-02 22:39:16
Context
2018-05-04
23:09
Added debug stuff check-in: fe6adf7d20 user: mrwellan tags: trunk
2018-05-02
22:39
Iterate over areas (not 100% sure this is a good idea). check-in: a76a9c267b user: matt tags: trunk
06:06
Added megatest colors support to iuputils (as good a place as any). check-in: 772dfc1869 user: matt tags: trunk, worked-at-home
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to mtutils/mtdb/mtdb.scm.

304
305
306
307
308
309
310


311
312
313
314
315
316
317
...
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
...
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
....
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066






1067
1068
1069















1070
1071
1072
1073
1074
1075

1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095



1096
1097
1098
1099
1100
1101
1102
1103
....
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
	(s:with-transaction
	 db
	 (lambda ()
	   (for-each
	    (lambda (stmt)
	      (s:exec (s:sql db stmt)))
	    *sql-de-lite-schema* ))))


    dbh))

;; do NOT close the db in the following three routines.

;; for sql-de-lite access we keep main and runs in separate files
;;
(define (with-common-db adat proc . params)
................................................................................
;;      common:config-get-fields
;;      get-run-info-1.X
;;      get-ttype
;;      get-target-1.X
;;      get-run-id
;;      string-intersperse
;;      )
    (print "area-id: " area-id)
    (if runinf
	runinf ;; already cached
	(let* ((common-dbh (with-common-db   adat1 #f)) ;; opens the db
	       (mt-dbh     (with-megatest-db adat1 #f))
	       (main-dbh   common-dbh) ;; (with-main-db     adat2 #f))
	       (run-dat    (get-run-info-1.X adat1 run-id))            ;; NOTE: get-run-info returns a vector < row header >
	       (run-name   (get-run-name-from-id mt-dbh run-id))
................................................................................
	       (keytarg    (string-intersperse keys "/")) ;; e.g. version/iteration/platform
	       ;; this is the target info from the source. BUG, this is currently WRONG
	       (target     (get-target-1.X adat1 run-id))                 ;; e.g. v1.63/a3e1/ubuntu
	       (spec-id    (get-ttype main-dbh keytarg))
	       (new-run-id (get-run-id main-dbh spec-id target run-name area-id))
	       ;; (area-id    (db:get-value-by-header row header "area_id)"))
	       )
	  (print "Got here! new-run-id=" new-run-id)
	  (if new-run-id
	      (begin
		(hash-table-set! runs-ht run-id new-run-id)
		;; ensure key fields are up to date
		(refresh-run-info
		 main-dbh
		 new-run-id
................................................................................
     test-data-ids)))

;; adat1 is source 1.X area
;; adat2 is target 2.X area
;; they can be the same adat structure in some (many?) cases
;;
(define (sync-tests-data adat1 adat2 cached-info test-ids area-info)
  (let* ((test-ht    (hash-table-ref cached-info 'tests))
	 (common-dbh (with-common-db   adat2 #f)) ;; opens the db
	 (mt-dbh     (with-megatest-db adat1 #f))
	 (main-dbh   common-dbh)) ;; (with-main-db     adat2 #f)))






    (for-each
     (lambda (test-id)
       (let* ((test-info    (get-test-info-by-id mt-dbh test-id)))















	 (match-let
	  (((test-id  run-id    test-name  state    status     event-time    host     cpuload
		      diskfree  uname      run-dir  item-path  run-duration  log-file comment shortdir attemptnum archived)
	    (vector->list test-info)))
	  (print "test-id: " test-id " test-name: " test-name)
	  (let* ((run-dbh      (with-run-db adat2 #f run-id))

		 (pgdb-run-id  (run-id->mtpg-run-id adat1 adat2 cached-info run-id area-info))
		 (pgdb-test-id (if pgdb-run-id 
				   (begin
				     (get-test-id adat2 pgdb-run-id test-name item-path))
				   #f)))
	    ;; "id"           "run_id"        "testname"  "state"      "status"      "event_time"
	    ;; "host"         "cpuload"       "diskfree"  "uname"      "rundir"      "item_path"
	    ;; "run_duration" "final_logf"    "comment"   "shortdir"   "attemptnum"  "archived"
	    (if pgdb-run-id
		(begin
		  (if pgdb-test-id ;; have a record
		      (begin ;; let ((key-name (conc run-id "/" test-name "/" item-path)))
			(print "Updating existing test with run-id: " run-id " and test-id: " test-id " pgdb run id: " pgdb-run-id)
			(update-test run-dbh pgdb-test-id pgdb-run-id test-name item-path state status host cpuload diskfree uname run-dir log-file run-duration comment event-time archived))
		      (begin 
			(print "Inserting test with run-id: " run-id " and test-id: " test-id  " pgdb run id: " pgdb-run-id)
			(insert-test run-dbh pgdb-run-id test-name item-path state status host cpuload diskfree uname run-dir log-file run-duration comment event-time archived)
			(set! pgdb-test-id (get-test-id adat2 pgdb-run-id test-name item-path))))
		  (hash-table-set! test-ht test-id pgdb-test-id))
		(print "WARNING: Skipping run with run-id:" run-id ". This run was created after previous sync and removed before this sync."))))))



	 test-ids)))

(define (add-area-tag dbh area-info tag) 
  (let* ((tag-info (get-tag-info-by-name dbh tag)))
    (if (not tag-info)
	(begin   
	  (if (handle-exceptions
		  exn
................................................................................
;;
;;======================================================================

;; get an alist of record ids changed since time since-time
;;   '((runs . (1 2 3 ...))(steps . (5 6 7 ...) ...))
;;
(define (get-changed-record-ids adat since-time)
  (print "get-changed-record-ids called with since-time=" since-time)
  (with-megatest-db
   adat
   (lambda (db)
     (let ((backcons (lambda (lst item)(cons item lst)))
	   (conscar  (lambda (row lst)(cons (car row) lst)))) ;; conscar should be cheaper than append
       `((runs       . ,(dbi:fold-row conscar '() db "SELECT id FROM runs  WHERE last_update>?;" since-time))
	 (tests      . ,(dbi:fold-row conscar '() db "SELECT id FROM tests WHERE last_update>?;" since-time))







>
>







 







<







 







|







 







|
|
|
|
>
>
>
>
>
>
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
<
<
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
>
>
|







 







|







304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
...
915
916
917
918
919
920
921

922
923
924
925
926
927
928
...
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
....
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095


1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
....
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
	(s:with-transaction
	 db
	 (lambda ()
	   (for-each
	    (lambda (stmt)
	      (s:exec (s:sql db stmt)))
	    *sql-de-lite-schema* ))))
    (s:exec (s:sql db "PRAGMA journal_mode=WAL"))
    (s:exec (s:sql db "PRAGMA synchronous=0"))
    dbh))

;; do NOT close the db in the following three routines.

;; for sql-de-lite access we keep main and runs in separate files
;;
(define (with-common-db adat proc . params)
................................................................................
;;      common:config-get-fields
;;      get-run-info-1.X
;;      get-ttype
;;      get-target-1.X
;;      get-run-id
;;      string-intersperse
;;      )

    (if runinf
	runinf ;; already cached
	(let* ((common-dbh (with-common-db   adat1 #f)) ;; opens the db
	       (mt-dbh     (with-megatest-db adat1 #f))
	       (main-dbh   common-dbh) ;; (with-main-db     adat2 #f))
	       (run-dat    (get-run-info-1.X adat1 run-id))            ;; NOTE: get-run-info returns a vector < row header >
	       (run-name   (get-run-name-from-id mt-dbh run-id))
................................................................................
	       (keytarg    (string-intersperse keys "/")) ;; e.g. version/iteration/platform
	       ;; this is the target info from the source. BUG, this is currently WRONG
	       (target     (get-target-1.X adat1 run-id))                 ;; e.g. v1.63/a3e1/ubuntu
	       (spec-id    (get-ttype main-dbh keytarg))
	       (new-run-id (get-run-id main-dbh spec-id target run-name area-id))
	       ;; (area-id    (db:get-value-by-header row header "area_id)"))
	       )
	  (print "INFO: for area-id " area-id " have new run-id " new-run-id " for run " run-name)
	  (if new-run-id
	      (begin
		(hash-table-set! runs-ht run-id new-run-id)
		;; ensure key fields are up to date
		(refresh-run-info
		 main-dbh
		 new-run-id
................................................................................
     test-data-ids)))

;; adat1 is source 1.X area
;; adat2 is target 2.X area
;; they can be the same adat structure in some (many?) cases
;;
(define (sync-tests-data adat1 adat2 cached-info test-ids area-info)
  (let* ((test-ht     (hash-table-ref cached-info 'tests))
	 (common-dbh  (with-common-db   adat2 #f)) ;; opens the db
	 (mt-dbh      (with-megatest-db adat1 #f))
	 (main-dbh    common-dbh) ;; (with-main-db     adat2 #f)))
	 (tinfo-cache (make-hash-table)) ;; test-id -> test-record
	 (run-ids     #f))
    ;; first cache the tests info in memory
    (dbi:with-transaction
     mt-dbh
     (lambda ()
       (for-each
	(lambda (test-id)
	  (hash-table-set! tinfo-cache test-id (get-test-info-by-id mt-dbh test-id)))
	test-ids)))

    (set! run-ids (delete-duplicates (map (lambda (tdat)(vector-ref tdat 1)) (hash-table-values tinfo-cache))))

    (for-each
     (lambda (run-id)
       (let* ((start-time (current-seconds))
	      (tests     (filter (lambda (t)(eq? run-id (vector-ref t 1)))(hash-table-values tinfo-cache)))
	      (run-dbh   (with-run-db adat2 #f run-id)))
	 (print "INFO: filtering took " (- (current-seconds) start-time) " seconds. Now syncing " (length tests) " for run " run-id)
	 (dbi:with-transaction
	  run-dbh
	  (lambda ()
	    (for-each
	     (lambda (test-info)
	       (match-let
		(((test-id  run-id    test-name  state    status     event-time    host     cpuload
			    diskfree  uname      run-dir  item-path  run-duration  log-file comment shortdir attemptnum archived)
		  (vector->list test-info)))


		(print "INFO: updating/creating record for test with id " test-id " and test name " test-name)
		(let* ((pgdb-run-id  (run-id->mtpg-run-id adat1 adat2 cached-info run-id area-info))
		       (pgdb-test-id (if pgdb-run-id 
					 (begin
					   (get-test-id adat2 pgdb-run-id test-name item-path))
					 #f)))
		  ;; "id"           "run_id"        "testname"  "state"      "status"      "event_time"
		  ;; "host"         "cpuload"       "diskfree"  "uname"      "rundir"      "item_path"
		  ;; "run_duration" "final_logf"    "comment"   "shortdir"   "attemptnum"  "archived"
		  (if pgdb-run-id
		      (begin
			(if pgdb-test-id ;; have a record
			    (begin ;; let ((key-name (conc run-id "/" test-name "/" item-path)))
			      (print "Updating existing test with run-id: " run-id " and test-id: " test-id " pgdb run id: " pgdb-run-id)
			      (update-test run-dbh pgdb-test-id pgdb-run-id test-name item-path state status host cpuload diskfree uname run-dir log-file run-duration comment event-time archived))
			    (begin 
			      (print "Inserting test with run-id: " run-id " and test-id: " test-id  " pgdb run id: " pgdb-run-id)
			      (insert-test run-dbh pgdb-run-id test-name item-path state status host cpuload diskfree uname run-dir log-file run-duration comment event-time archived)
			      (set! pgdb-test-id (get-test-id adat2 pgdb-run-id test-name item-path))))
			(hash-table-set! test-ht test-id pgdb-test-id))
		      (print "WARNING: Skipping run with run-id:" run-id ". This run was created after previous sync and removed before this sync.")))))
	     tests)))
	 (print "INFO: sync for run " run-id " completed in " (- (current-seconds) start-time) " seconds.")
	 ))
     run-ids)))

(define (add-area-tag dbh area-info tag) 
  (let* ((tag-info (get-tag-info-by-name dbh tag)))
    (if (not tag-info)
	(begin   
	  (if (handle-exceptions
		  exn
................................................................................
;;
;;======================================================================

;; get an alist of record ids changed since time since-time
;;   '((runs . (1 2 3 ...))(steps . (5 6 7 ...) ...))
;;
(define (get-changed-record-ids adat since-time)
  (print "INFO: getting id's for records changed since time " since-time)
  (with-megatest-db
   adat
   (lambda (db)
     (let ((backcons (lambda (lst item)(cons item lst)))
	   (conscar  (lambda (row lst)(cons (car row) lst)))) ;; conscar should be cheaper than append
       `((runs       . ,(dbi:fold-row conscar '() db "SELECT id FROM runs  WHERE last_update>?;" since-time))
	 (tests      . ,(dbi:fold-row conscar '() db "SELECT id FROM tests WHERE last_update>?;" since-time))