travels.lisp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. (in-package :travels)
  2. (defun update-indexes (storage old new)
  3. (let ((id (or (getf old :|id|) (getf new :|id|)))
  4. (old-user (getf old :|user|))
  5. (new-user (getf new :|user|))
  6. (old-loc (getf old :|location|))
  7. (new-loc (getf new :|location|)))
  8. (labels ((update (s o n)
  9. (when o (setf (gethash o s) (remove id (gethash o s))))
  10. (when n (push id (gethash n s)))))
  11. (when (or (null new)
  12. (null old)
  13. (and new-user (not (= old-user new-user))))
  14. (update (or (gethash :user-visits storage)
  15. (setf (gethash :user-visits storage) (make-hash-table)))
  16. old-user new-user))
  17. (when (or (null new) (null old)
  18. (and new-loc (not (= old-loc new-loc))))
  19. (update (or (gethash :location-visits storage)
  20. (setf (gethash :location-visits storage) (make-hash-table)))
  21. old-loc new-loc)))))
  22. (defun load-data (pathname)
  23. (let ((storage (make-hash-table)))
  24. (zip:with-zipfile (zip pathname)
  25. (zip:do-zipfile-entries (name entry zip)
  26. (let ((data (jojo:parse
  27. (flex:octets-to-string (zip:zipfile-entry-contents entry) :external-format :utf-8)
  28. :as :plist)))
  29. (destructuring-bind (entity elements) data
  30. (let ((entity-storage
  31. (or (gethash entity storage)
  32. (setf (gethash entity storage)
  33. (make-hash-table :size (length elements))))))
  34. (loop for item in elements
  35. do (setf (gethash (getf item :|id|) entity-storage) item)
  36. when (eq entity :|visits|)
  37. do (update-indexes storage nil item)))))))
  38. storage))
  39. (defvar *storage* nil "data store")
  40. (defvar *storage-users* nil "users store")
  41. (defvar *storage-locations* nil "locations store")
  42. (defvar *storage-visits* nil "visits store")
  43. (let ((keyword-package (find-package :keyword)))
  44. (defun getkey (place indicator)
  45. (declare
  46. (optimize (speed 3) (safety 0))
  47. (type list place)
  48. (type symbol indicator))
  49. (intern (getf place indicator) keyword-package)))
  50. (defparameter +400+ '(400 nil nil))
  51. (defparameter +404+ '(404 nil nil))
  52. (defparameter +200-empty+ '(200 (:content-type "application/json" :content-length 2) ("{}")))
  53. (defun 200-json (data)
  54. (declare (optimize (speed 3) (safety 0))
  55. (type list data))
  56. (let ((content (jojo:to-json data)))
  57. `(200 (:content-type "application/json"
  58. :content-length ,(trivial-utf-8:utf-8-byte-length content))
  59. (,content))))
  60. (defun get-entity (params)
  61. (let ((id (parse-integer (getf params :id) :junk-allowed t))
  62. (entity (getkey params :entity)))
  63. (if id
  64. (case entity
  65. ((:|users| :|visits| :|locations|)
  66. (let ((entity (gethash id (gethash entity *storage*))))
  67. (if entity
  68. (200-json entity)
  69. +404+)))
  70. (otherwise +404+))
  71. +404+)))
  72. (defun validate-item (item)
  73. (declare (type list item)
  74. (optimize (speed 3) (safety 0)))
  75. (loop for (k v) on item by #'cddr
  76. unless v return nil
  77. when (and (eq k :|id|) (not (integerp v))) return nil
  78. when (and (eq k :|distance|) (not (integerp v))) return nil
  79. when (and (eq k :|visited_at|) (not (integerp v))) return nil
  80. when (and (eq k :|country|) (> (length v) 50)) return nil
  81. when (and (eq k :|city|) (> (length v) 50)) return nil
  82. when (and (eq k :|mark|) (or (not (integerp v)) (not (<= 0 (the fixnum v) 5)))) return nil
  83. when (and (eq k :|user|) (or (not (integerp v)) (not (gethash v *storage-users*)))) return nil
  84. when (and (eq k :|location|) (or (not (integerp v)) (not (gethash v *storage-locations*)))) return nil
  85. finally (return t)))
  86. (defun post-entity (params)
  87. (let ((id (parse-integer (getf params :id) :junk-allowed t))
  88. (entity (getkey params :entity)))
  89. (if (or id (string= (getf params :id) "new"))
  90. (case entity
  91. ((:|users| :|visits| :|locations|)
  92. (handler-case
  93. (let ((body (make-string (getf myway:*env* :content-length))))
  94. (read-sequence body (flex:make-flexi-stream
  95. (getf myway:*env* :raw-body)
  96. :external-format :utf8))
  97. (let ((item (jojo:parse body))
  98. (entity-storage (gethash entity *storage*)))
  99. (if (validate-item item)
  100. (let ((item-id (getf item :|id|)))
  101. (if id
  102. (if item-id +400+
  103. (let ((existing (gethash id entity-storage)))
  104. (if existing
  105. (progn
  106. (when (eq entity :|visits|)
  107. (update-indexes *storage* existing item))
  108. (loop for (k v) on item by #'cddr
  109. do (setf (getf existing k) v))
  110. +200-EMPTY+)
  111. +404+)))
  112. (let ((e (gethash item-id entity-storage)))
  113. (if e +400+
  114. (progn
  115. (when (eq entity :|visits|)
  116. (update-indexes *storage* nil item))
  117. (setf (gethash item-id entity-storage) item)
  118. +200-EMPTY+)))))
  119. +400+)))
  120. (error () +400+)))
  121. (otherwise +404+))
  122. +400+)))
  123. (defun aget (place indicator &key (test #'equal))
  124. (declare (type list place)
  125. (type (or string symbol) indicator)
  126. (type function test)
  127. (optimize (speed 3) (safety 0)))
  128. (cdr (assoc indicator place :test test)))
  129. (defun may-integer (string)
  130. (declare (type (or null string) string))
  131. (when string
  132. (parse-integer string)))
  133. (defun matching-location (location country to-distance)
  134. (and (or (not country) (equal (getf location :|country|) country))
  135. (or (not to-distance) (< (getf location :|distance|) to-distance))))
  136. (defun matching-visit (visit from-date to-date)
  137. (and (or (not from-date) (> (getf visit :|visited_at|) from-date))
  138. (or (not to-date) (< (getf visit :|visited_at|) to-date))))
  139. (defun user-visits (params)
  140. (let ((id (parse-integer (getf params :id) :junk-allowed t)))
  141. (if id
  142. (let ((user (gethash id *storage-users*)))
  143. (if user
  144. (handler-case
  145. (let* ((user-visit-ids (gethash id (gethash :user-visits *storage*)))
  146. (query-string (getf myway:*env* :query-string))
  147. (query-params (and query-string (quri:url-decode-params query-string))))
  148. (let ((from-date (may-integer (aget query-params "fromDate")))
  149. (to-date (may-integer (aget query-params "toDate")))
  150. (to-distance (may-integer (aget query-params "toDistance")))
  151. (country (aget query-params "country")))
  152. (assert (or (null country) (<= (length country) 50)))
  153. ;; TODO: Smarter indexes
  154. (let ((user-visits (loop for v-id in user-visit-ids
  155. for visit = (gethash v-id *storage-visits*)
  156. for loc = (gethash (getf visit :|location|) *storage-locations*)
  157. when (and visit (matching-visit visit from-date to-date))
  158. when (and loc (matching-location loc country to-distance))
  159. collect (list :|mark| (getf visit :|mark|)
  160. :|visited_at| (getf visit :|visited_at|)
  161. :|place| (getf loc :|place|)))))
  162. (200-json (list :|visits|
  163. (sort user-visits #'< :key (lambda (v) (getf v :|visited_at|))))))))
  164. (error () +400+))
  165. +404+))
  166. +404+)))
  167. (defun smart-f (arg &optional digits)
  168. (with-output-to-string (s)
  169. (prin1 (cond ((= (round arg) arg) (round arg))
  170. (digits (float (/ (round (* arg (expt 10 digits)))
  171. (expt 10 digits))))
  172. (t arg))
  173. s)))
  174. (defvar *unix-epoch-difference*
  175. (encode-universal-time 0 0 0 1 1 1970 0))
  176. (defvar *year*
  177. (round (* 60 60 24 365.25)))
  178. (defun universal-to-unix-time (universal-time)
  179. (- universal-time *unix-epoch-difference*))
  180. (defun unix-to-universal-time (unix-time)
  181. (+ unix-time *unix-epoch-difference*))
  182. (defun get-unix-time ()
  183. (universal-to-unix-time (get-universal-time)))
  184. (defmethod jojo::%to-json ((ratio ratio))
  185. (jojo:%write-string (smart-f ratio 5)))
  186. (defun matching-user (user from-age to-age gender now)
  187. (let ((age (/ (- now (getf user :|birth_date|)) *year*)))
  188. (and (or (not gender) (equal (getf user :|gender|) gender))
  189. (or (not from-age) (> age from-age))
  190. (or (not to-age) (< age to-age)))))
  191. (defun location-avg-mark (params)
  192. (let ((id (parse-integer (getf params :id) :junk-allowed t)))
  193. (if id
  194. (let ((location (gethash id (gethash :|locations| *storage*))))
  195. (if location
  196. (handler-case
  197. (let* ((location-visit-ids (gethash id (gethash :location-visits *storage*)))
  198. (query-string (getf myway:*env* :query-string))
  199. (query-params (and query-string (quri:url-decode-params query-string))))
  200. (let ((from-date (may-integer (aget query-params "fromDate")))
  201. (to-date (may-integer (aget query-params "toDate")))
  202. (now (get-unix-time))
  203. (from-age (may-integer (aget query-params "fromAge")))
  204. (to-age (may-integer (aget query-params "toAge")))
  205. (gender (aget query-params "gender")))
  206. (assert (or (null gender) (string= gender "m") (string= gender "f")))
  207. (let ((marks (loop for v-id in location-visit-ids
  208. for visit = (gethash v-id *storage-visits*)
  209. for user = (gethash (getf visit :|user|) *storage-users*)
  210. when (and visit (matching-visit visit from-date to-date))
  211. when (and user (matching-user user from-age to-age gender now))
  212. collect (getf visit :|mark|))))
  213. (200-json (list :|avg|
  214. (if marks
  215. (/ (apply #'+ marks) (length marks))
  216. 0.0))))))
  217. (error () +400+))
  218. +404+))
  219. +404+)))
  220. (defvar *mapper* (myway:make-mapper))
  221. (myway:connect *mapper* "/:entity/:id" 'post-entity :method :post)
  222. (myway:connect *mapper* "/:entity/:id" 'get-entity)
  223. (myway:connect *mapper* "/users/:id/visits" 'user-visits)
  224. (myway:connect *mapper* "/locations/:id/avg" 'location-avg-mark)
  225. (myway:connect *mapper* "*" (lambda (p) (declare (ignore p)) +404+))
  226. (defun main (&rest args
  227. &key
  228. (data "data.zip")
  229. (address "0.0.0.0")
  230. (port 5000)
  231. (debug nil)
  232. (use-thread nil) &allow-other-keys)
  233. (setf *storage* (load-data data))
  234. (setf *storage-users* (gethash :|users| *storage*)
  235. *storage-locations* (gethash :|locations| *storage*)
  236. *storage-visits* (gethash :|visits| *storage*))
  237. (format t "Loaded ~A~%" data)
  238. (apply #'clack:clackup
  239. (myway:to-app *mapper*)
  240. :server :woo
  241. :address address
  242. :port port
  243. :debug debug
  244. :use-default-middlewares nil
  245. :use-thread use-thread
  246. (alexandria:remove-from-plist args :data :address :port :debug :use-thread)))