(in-package :travels) (defun update-indexes (storage old new) (let ((id (or (getf old :|id|) (getf new :|id|))) (old-user (getf old :|user|)) (new-user (getf new :|user|)) (old-loc (getf old :|location|)) (new-loc (getf new :|location|))) (labels ((update (s o n) (when o (setf (gethash o s) (remove id (gethash o s)))) (when n (push id (gethash n s))))) (when (or (null new) (null old) (and new-user (not (= old-user new-user)))) (update (or (gethash :user-visits storage) (setf (gethash :user-visits storage) (make-hash-table))) old-user new-user)) (when (or (null new) (null old) (and new-loc (not (= old-loc new-loc)))) (update (or (gethash :location-visits storage) (setf (gethash :location-visits storage) (make-hash-table))) old-loc new-loc))))) (defun load-data (pathname) (let ((storage (make-hash-table))) (zip:with-zipfile (zip pathname) (zip:do-zipfile-entries (name entry zip) (let ((data (jojo:parse (flex:octets-to-string (zip:zipfile-entry-contents entry) :external-format :utf-8) :as :plist))) (destructuring-bind (entity elements) data (let ((entity-storage (or (gethash entity storage) (setf (gethash entity storage) (make-hash-table :size (length elements)))))) (loop for item in elements do (setf (gethash (getf item :|id|) entity-storage) item) when (eq entity :|visits|) do (update-indexes storage nil item))))))) storage)) (defvar *storage* nil "data store") (let ((keyword-package (find-package :keyword))) (defun getkey (place indicator) (declare (optimize (speed 3) (safety 0)) (type list place) (type symbol indicator)) (intern (getf place indicator) keyword-package))) (defparameter +400+ '(400 nil nil)) (defparameter +404+ '(404 nil nil)) (defparameter +200-empty+ '(200 (:content-type "application/json" :content-length 2) ("{}"))) (defun 200-json (data) (declare (optimize (speed 3) (safety 0)) (type list data)) (let ((content (jojo:to-json data))) `(200 (:content-type "application/json" :content-length ,(trivial-utf-8:utf-8-byte-length content)) (,content)))) (defun get-entity (params) (let ((id (parse-integer (getf params :id) :junk-allowed t)) (entity (getkey params :entity))) (if id (case entity ((:|users| :|visits| :|locations|) (let ((entity (gethash id (gethash entity *storage*)))) (if entity (200-json entity) +404+))) (otherwise +404+)) +404+))) (defun validate-item (item) (declare (type list item) (optimize (speed 3) (safety 0))) (loop for (k v) on item by #'cddr unless v return nil finally (return t))) (defun post-entity (params) (let ((id (parse-integer (getf params :id) :junk-allowed t)) (entity (getkey params :entity))) (if (or id (string= (getf params :id) "new")) (case entity ((:|users| :|visits| :|locations|) (handler-case (let ((body (make-string (getf myway:*env* :content-length)))) (read-sequence body (flex:make-flexi-stream (getf myway:*env* :raw-body) :external-format :utf8)) (let ((item (jojo:parse body)) (entity-storage (gethash entity *storage*))) (if (validate-item item) (if id (let ((existing (gethash id entity-storage))) (if existing (progn (when (eq entity :|visits|) (update-indexes *storage* existing item)) (loop for (k v) on item by #'cddr unless (eq k :|id|) do (setf (getf existing k) v)) +200-EMPTY+) +404+)) (let ((e (gethash (getf item :|id|) entity-storage))) (if e +400+ (progn (when (eq entity :|visits|) (update-indexes *storage* nil item)) (setf (gethash (getf item :|id|) entity-storage) item) +200-EMPTY+)))) +400+))) (error () +400+))) (otherwise +404+)) +400+))) (defun aget (place indicator &key (test #'equal)) (declare (type list place) (type (or string symbol) indicator) (type function test) (optimize (speed 3) (safety 0))) (cdr (assoc indicator place :test test))) (defun may-integer (string) (declare (type (or null string) string)) (when string (parse-integer string))) (defun matching-location (location country to-distance) (and (or (not country) (equal (getf location :|country|) country)) (or (not to-distance) (< (getf location :|distance|) to-distance)))) (defun matching-visit (visit from-date to-date) (and (or (not from-date) (> (getf visit :|visited_at|) from-date)) (or (not to-date) (< (getf visit :|visited_at|) to-date)))) (defun user-visits (params) (let ((id (parse-integer (getf params :id) :junk-allowed t))) (if id (let ((user (gethash id (gethash :|users| *storage*)))) (if user (handler-case (let* ((user-visit-ids (gethash id (gethash :user-visits *storage*))) (locations (gethash :|locations| *storage*)) (visits (gethash :|visits| *storage*)) (query-string (getf myway:*env* :query-string)) (query-params (and query-string (quri:url-decode-params query-string)))) (let ((from-date (may-integer (aget query-params "fromDate"))) (to-date (may-integer (aget query-params "toDate"))) (to-distance (may-integer (aget query-params "toDistance"))) (country (aget query-params "country"))) ;; TODO: Smarter indexes (let ((user-visits (loop for v-id in user-visit-ids for visit = (gethash v-id visits) for loc = (gethash (getf visit :|location|) locations) when (and visit (matching-visit visit from-date to-date)) when (and loc (matching-location loc country to-distance)) collect (list :|mark| (getf visit :|mark|) :|visited_at| (getf visit :|visited_at|) :|place| (getf loc :|place|))))) (sort user-visits #'< :key (lambda (v) (getf v :|visited_at|))) (200-json (list :|visits| user-visits))))) (error () +400+)) +404+)) +404+))) (defun smart-f (arg &optional digits) (with-output-to-string (s) (prin1 (cond ((= (round arg) arg) (round arg)) (digits (float (/ (round (* arg (expt 10 digits))) (expt 10 digits)))) (t arg)) s))) (defvar *unix-epoch-difference* (encode-universal-time 0 0 0 1 1 1970 0)) (defvar *year* (round (* 60 60 24 365.25))) (defun universal-to-unix-time (universal-time) (- universal-time *unix-epoch-difference*)) (defun unix-to-universal-time (unix-time) (+ unix-time *unix-epoch-difference*)) (defun get-unix-time () (universal-to-unix-time (get-universal-time))) (defmethod jojo::%to-json ((ratio ratio)) (jojo:%write-string (smart-f ratio 5))) (defun matching-user (user from-age to-age gender now) (let ((age (/ (- now (getf user :|birth_date|)) *year*))) (and (or (not gender) (equal (getf user :|gender|) gender)) (or (not from-age) (> age from-age)) (or (not to-age) (< age to-age))))) (defun location-avg-mark (params) (let ((id (parse-integer (getf params :id) :junk-allowed t))) (if id (let ((location (gethash id (gethash :|locations| *storage*)))) (if location (handler-case (let* ((location-visit-ids (gethash id (gethash :location-visits *storage*))) (users (gethash :|users| *storage*)) (visits (gethash :|visits| *storage*)) (query-string (getf myway:*env* :query-string)) (query-params (and query-string (quri:url-decode-params query-string)))) (let ((from-date (may-integer (aget query-params "fromDate"))) (to-date (may-integer (aget query-params "toDate"))) (now (get-unix-time)) (from-age (may-integer (aget query-params "fromAge"))) (to-age (may-integer (aget query-params "toAge"))) (gender (aget query-params "gender"))) (let ((marks (loop for v-id in location-visit-ids for visit = (gethash v-id visits) for user = (gethash (getf visit :|user|) users) when (and visit (matching-visit visit from-date to-date)) when (and user (matching-user user from-age to-age gender now)) collect (getf visit :|mark|)))) (200-json (list :|avg| (if marks (/ (apply #'+ marks) (length marks)) 0.0)))))) (error () +400+)) +404+)) +404+))) (defvar *mapper* (myway:make-mapper)) (myway:connect *mapper* "/:entity/:id" 'post-entity :method :post) (myway:connect *mapper* "/:entity/:id" 'get-entity) (myway:connect *mapper* "/users/:id/visits" 'user-visits) (myway:connect *mapper* "/locations/:id/avg" 'location-avg-mark) (myway:connect *mapper* "*" (lambda (p) (declare (ignore p)) +404+)) (defun main (&rest args &key (data "data.zip") (address "0.0.0.0") (port 5000) (debug nil) (use-thread nil) &allow-other-keys) (setf *storage* (load-data data)) (format t "Loaded ~A~%" data) (apply #'clack:clackup (myway:to-app *mapper*) :server :woo :address address :port port :debug debug :use-default-middlewares nil :use-thread use-thread (alexandria:remove-from-plist args :data :address :port :debug :use-thread)))