@ -1,4 +1,91 @@
( define-module ( yacswa data )
# :use-module ( csv csv )
# :use-module ( ice-9 textual-ports )
# :use-module ( ice-9 threads )
# :use-module ( json )
# :use-module ( srfi srfi-1 )
# :use-module ( srfi srfi-19 )
# :use-module ( sxml simple )
# :use-module ( sxml transform )
# :use-module ( tk listlogic )
# :use-module ( tk mcron )
# :use-module ( tk short )
# :use-module ( yacswa debugging )
# :use-module ( yacswa git )
# :export ( get-by-postal
get-by-hr ) )
;; Prevent the GC from collecting the associations
( define-once hr-syms ' ( ) )
( define-once pc-syms ' ( ) )
( define-once text-syms ' ( ) )
( display "Updating stats repository..." )
( git:init )
( display "Done.\n" )
( define-once $hr-tree
( csv->sxml ( open-input-file path-hr-info )
# :record-sym ( lambda x "hr" ) ) )
( define-once $pc-tree
( csv->sxml ( open-input-file path-hr-pc-map )
# :record-sym ( lambda x "pc" ) ) )
( define-syntax hr
( syntax-rules ( )
( ( hr ( ( $sym ) ( province $prov ) ( province_full $prov_text )
( province_short $prov_abbr ) ( health_region $hr_terse )
( health_region_esri $hr_full ) $pop ) )
( begin ( set! hr-syms ( cons '$sym hr-syms ) )
( let ( ( $text-sym ( ssa> $prov "-" $hr_terse ) ) )
( begin ( set! text-syms ( cons $text-sym text-syms ) )
( symbol-fset! $text-sym '$sym ) ) )
( set-symbol-property! '$sym 'prov-code $prov_abbr )
( set-symbol-property! '$sym 'province $prov )
( set-symbol-property! '$sym 'terse $hr_terse )
( set-symbol-property! '$sym 'hr-full $hr_full )
( set-symbol-property! '$sym 'population $pop ) ) ) ) )
( define ( hrinfo! )
( for-each ( lambda ( x ) ( eval x ( interaction-environment ) ) )
( pre-post-order
( cdr $hr-tree )
` ( ( HR_UID . , ( lambda ( l x ) ` ( , ( ssa> "HR" x ) ) ) )
( pop . , ( lambda ( l x ) ( string->number x ) ) )
( *text* . , ( lambda ( l x ) x ) )
( *default* . , ( lambda x x ) ) ) ) ) )
( define-syntax pc
( syntax-rules ( )
( ( pc ( ( $pc ) ( $sym ) ( Prov $prov_num ) ( ENGNAME $eng_name )
( FRENAME $fre_name ) ( EstimatedPop $pop ) ) )
( begin ( set! pc-syms ( cons '$pc pc-syms ) )
( symbol-fset! '$pc ( cons '$sym ( if-some ( symbol-fref '$pc ) ) ) ) ) ) ) )
( define ( pcinfo! )
( for-each ( lambda ( x ) ( eval x ( interaction-environment ) ) )
( pre-post-order
( cdr $pc-tree )
` ( ( FSA . , ( lambda ( l x ) ` ( , ( ssa> "PC" x ) ) ) )
( HR_UID . , ( lambda ( l x ) ` ( , ( ssa> "HR" x ) ) ) )
( *text* . , ( lambda ( l x ) x ) )
( *default* . , ( lambda x x ) ) ) ) ) )
( begin ( hrinfo! )
( set! hr-syms ( delq! 'HR9999 hr-syms ) )
( if test-mode?
( begin ( display "Testing hr symbols" ) ( newline )
( for-each ( lambda ( s ) ( run-hook test-hr s ) ) hr-syms ) ) )
( pcinfo! )
( if test-mode?
( begin ( display "Testing pc symbols" ) ( newline )
( for-each ( lambda ( s ) ( run-hook test-pc s ) ) pc-syms ) ) ) )
;; Data update logic
( define-once $stat-tree
( csv->sxml ( open-input-file path-hr-cases )
# :record-sym ( lambda x "rec" ) ) )
@ -107,8 +194,8 @@
'admin )
' ( ) ) )
( define mcron:user ( getpw ( if-some-else ( getlogin )
( "covInd" ) )) )
;;(define mcron:user (getpw (if-some-else (getlogin )
;; "covInd")))
( define ( job-loop )
( let loop ( )
@ -139,7 +226,21 @@
;; (sleep 1))
; Functional parts of the asynchronous code above
( git:fetch )
;; Functional parts of the asynchronous code above
( display "Processing stats..." )
( process-stats! $stat-tree )
( display "and calculating json strings..." )
( calculate-strings! )
( display "Done.\n" )
;; Public Interface
( define ( get-by-postal code )
( let* ( ( pc-sym ( ssa> "PC" code ) )
( hr-sym ( car ( if-some-else ( symbol-fref pc-sym ) ' ( ndef ) ) ) ) )
( symbol-property hr-sym 'json-string ) ) )
( define ( get-by-hr code )
( let ( ( hr-sym ( ssa> "HR" code ) ) )
( symbol-property hr-sym 'json-string ) ) )