;;;;
;;;; Produce HTML document describing BUS LOCATION DATABASE.
;;;;
;;; January, 2003 (c) Toshihiro Matsui
;;;
;;;
(unless (unix:isatty 0)
(defun euserror (code msg1 form &optional (msg2))
(format *error-output*
"eus error ~s ~s ~s ~s~%" code msg1 form msg2)
(unix::exit 1))
(setq *error-handler* 'euserror)
)
(require :http "http")
(require :time "time")
(require :webnews "webnews")
(require :pgsql "pgsql")
(require :httpcgi)
(require :web-counter "web-counter")
(defvar *action* "/cgi-bin/bus.cgi") ;;override this for fast-cgi
(defparameter 30min-list
'( ;; "06:00" "06:30"
"07:00" "07:30" "08:00" "08:30" "09:00" "09:30"
"10:00" "10:30" "11:00" "11:30" "12:00" "12:30"
"13:00" "13:30" "14:00" "14:30" "15:00" "15:30"
"16:00" "16:30" "17:00" "17:30" "18:00" "18:30"
"19:00" "19:30" "20:00" "20:30" "21:00" "21:30"
"22:00" "22:30" "23:00" "23:30")
)
;; (defun select (db fields table &key where limit limit-offset order-by))
;;
;; extract data between time1 and time2
;;
(defun time-segmented-record (date hour min1 min2)
(let (db-result)
(setq db-result (select db
'(recdate rectime up_minutes down_minutes)
"tsukuba_bus"
:where (format nil
"recdate = date '~a' and rectime >= time '~a:~a'
and rectime < time '~a:~a'"
(send date :iso-date-string)
hour min1 hour min2))
)
db-result))
(defun dated-record (date)
(let (res)
(dolist (hour '(7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23))
(push (time-segmented-record date hour 0 30) res)
(push (time-segmented-record date hour 30 59) res))
(nreverse res)) )
;; recdate, rectime, weekday,
;; up_time_string, up_minutes,
;; down_time_string, down_minutes
;;
;; extract records between hour:min1 till hour:min2
;; from the given one-day records
(defun collect-timed-records (records hour min1 min2)
(mapcan #'(lambda (r)
(let ((h (send (second r) :hours))
(m (send (second r) :minutes)))
(if (and (= h hour) (>= m min1) (< m min2))
(list r))))
records))
(defun min-to-hourmin-string (min)
(cond ((not (numberp min)) "")
((< min 60) (format nil "~a分" min))
((not (integerp min)) "N/A")
(t (format nil "~d時間~d分" (/ min 60) (mod min 60)))) )
(defun average-time-records (records)
(list
(round (/ (float (apply #'+ (mapcar #'fifth records))) (length records)))
(round (/ (float (apply #'+ (mapcar #'seventh records))) (length records)))))
#|
(defun 30min-times (records)
(let (result)
(dolist (hour '(7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23))
(push (mapcar #'min-to-hourmin-string (average-time-records
(collect-timed-records records hour 0 30)))
result)
(push (mapcar #'min-to-hourmin-string (average-time-records
(collect-timed-records records hour 30 59)))
result)
)
(nreverse result)) )
|#
(defun 30min-times (records)
(let (result)
(dolist (hour '(7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23))
(push (average-time-records
(collect-timed-records records hour 0 30))
result)
(push (average-time-records
(collect-timed-records records hour 30 59))
result)
)
(nreverse result)) )
(defun get-db-day-data (offset &optional (abs nil))
(send db :exec (format nil
"select recdate, rectime, weekday,
up_time_string, up_minutes,
down_time_string, down_minutes
from tsukuba_bus
where recdate = date '~a'"
(if abs
(send offset :iso-date-string)
(send (send now :offset-day offset) :iso-date-string))))
)
(defun generate-weather-page ()
(let (weather-data
(year (xqval 'year *forms*))
(month (xqval 'month *forms*))
(day (xqval 'day *forms*))
date sdate)
;; (gen "~a
~%" *forms*)
(setq date
(if month
(instance calendar-time :make :year year :month month :day day)
now))
(setq sdate (send date :offset-day -7)) ;; one week
(setq weather-data
(pq:select db '(recdate rectime weather temp wet_probability)
'weather_report
:where
(format nil "recdate> date '~a' and recdate<= date '~a'"
(send sdate :iso-date-string)
(send date :iso-date-string))))
(goback-button)
(gen "~a~%"
(html-table
(mapcar #'(lambda (x)
(cons (send (car x) :iso-date-string) (cdr x)))
weather-data)
:border 1))
))
(defun generate-raw-table ()
(let (trip-data
(year (xqval 'year *forms*))
(month (xqval 'month *forms*))
(day (xqval 'day *forms*)))
(gen "
昨日、本日の生のデータ
")
;; (gen "~a~%" *forms*)
(gen-action *action* 'post)
(gen "~a~%"
(html-table (list
(list "本日、昨日の30分平均"
(input-form 'submit 'command 'average 50 ))
(list "年" (input-form 'label 'year (string (send now :year)) 5))
(list "月" (input-form 'label 'month (string (send now :month)) 5))
(list "日" (input-form 'label 'day (string (send now :day)) 5))
(list "生データ表示" (input-form 'submit 'command 'raw 50 ))
(list "週間天気表示" (input-form 'submit 'command 'weather 50 ))
)))
(gen "")
(cond ((null day)
(setq trip-data (append (get-db-day-data -1) (get-db-day-data 0)))
(gen (html-table
(mapcar #'(lambda (r) (list
(send (first r) :iso-date-string)
(format nil "~2a:~2a"
(digits-string (send (second r) :hours) 2)
(digits-string (send (second r) :minutes) 2))
(fourth r) (fifth r) (sixth r) (seventh r)))
trip-data)
:heading '(date time 上り 分 下り 分)
:table-option "border=1")) )
(t (setq date (instance calendar-time :make
:year (send now :year)
:month month
:day day))
(gen "date=~a~%" (send date :iso-date-string))
(setq trip-data (get-db-day-data date t)) ;absolute date
(gen (html-table
(append
(list (list "天気" "" (get-weather date)))
(mapcar #'(lambda (r) (list
(send (first r) :iso-date-string)
(format nil "~2a:~2a"
(digits-string (send (second r) :hours) 2)
(digits-string (send (second r) :minutes) 2))
(fourth r) (fifth r) (sixth r) (seventh r)))
trip-data))
:heading '(date time 上り 分 下り 分)
:table-option "border=1")) )
)))
(defun add-detail-href (minute-list date)
(mapcar #'(lambda (display time)
(format nil
"~a"
*action*
(send date :iso-date-string)
(subseq time 0 2) (subseq time 3 4)
display))
minute-list
30min-list
)
)
(defun get-weather (date)
(let (r wether temp wet time
(date-string (send date :iso-date-string)))
(setq r
(send db :exec (format nil
"select recdate,weather,temp,wet_probability from weather_report
where recdate=date '~a' and rectime