|
| 1 | +# Copyright 2022 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from flask import Flask, render_template, request |
| 16 | +from google.appengine.api import taskqueue |
| 17 | +from google.appengine.ext import ndb |
| 18 | + |
| 19 | +HOUR = 3600 |
| 20 | +LIMIT = 10 |
| 21 | +TASKS = 1000 |
| 22 | +QUEUE = 'pullq' |
| 23 | +app = Flask(__name__) |
| 24 | + |
| 25 | + |
| 26 | +class Visit(ndb.Model): |
| 27 | + 'Visit entity registers visitor IP address & timestamp' |
| 28 | + visitor = ndb.StringProperty() |
| 29 | + timestamp = ndb.DateTimeProperty(auto_now_add=True) |
| 30 | + |
| 31 | +def store_visit(remote_addr, user_agent): |
| 32 | + 'create new Visit in Datastore and queue request to bump visitor count' |
| 33 | + Visit(visitor='{}: {}'.format(remote_addr, user_agent)).put() |
| 34 | + q = taskqueue.Queue(QUEUE) |
| 35 | + q.add(taskqueue.Task(payload=remote_addr, method='PULL')) |
| 36 | + |
| 37 | +def fetch_visits(limit): |
| 38 | + 'get most recent visits' |
| 39 | + return Visit.query().order(-Visit.timestamp).fetch(limit) |
| 40 | + |
| 41 | + |
| 42 | +class VisitorCount(ndb.Model): |
| 43 | + visitor = ndb.StringProperty(repeated=False, required=True) |
| 44 | + counter = ndb.IntegerProperty() |
| 45 | + |
| 46 | +def fetch_counts(limit): |
| 47 | + 'get top visitors' |
| 48 | + return VisitorCount.query().order(-VisitorCount.counter).fetch(limit) |
| 49 | + |
| 50 | + |
| 51 | +@app.route('/log') |
| 52 | +def log_visitors(): |
| 53 | + 'worker processes recent visitor counts and updates them in Datastore' |
| 54 | + # tally recent visitor counts from queue then delete those tasks |
| 55 | + tallies = {} |
| 56 | + q = taskqueue.Queue(QUEUE) |
| 57 | + tasks = q.lease_tasks(HOUR, TASKS) |
| 58 | + for task in tasks: |
| 59 | + visitor = task.payload |
| 60 | + tallies[visitor] = tallies.get(visitor, 0) + 1 |
| 61 | + q.delete_tasks(tasks) |
| 62 | + |
| 63 | + # increment those counts in Datastore and return |
| 64 | + for visitor in tallies: |
| 65 | + counter = VisitorCount.query(VisitorCount.visitor == visitor).get() |
| 66 | + if not counter: |
| 67 | + counter = VisitorCount(visitor=visitor, counter=0) |
| 68 | + counter.put() |
| 69 | + counter.counter += tallies[visitor] |
| 70 | + counter.put() |
| 71 | + return 'DONE (with %d task[s] logging %d visitor[s])\r\n' % (len(tasks), len(tallies)) |
| 72 | + |
| 73 | + |
| 74 | +@app.route('/') |
| 75 | +def root(): |
| 76 | + 'main application (GET) handler' |
| 77 | + store_visit(request.remote_addr, request.user_agent) |
| 78 | + context = { |
| 79 | + 'limit': LIMIT, |
| 80 | + 'visits': fetch_visits(LIMIT), |
| 81 | + 'counts': fetch_counts(LIMIT), |
| 82 | + } |
| 83 | + return render_template('index.html', **context) |
0 commit comments