|
18 | 18 | ########################################################################## |
19 | 19 |
|
20 | 20 | import os |
21 | | -import codecs |
22 | | -import datetime |
23 | 21 | import baleen |
24 | | -import baleen.models as db |
25 | 22 |
|
26 | | -from itertools import islice |
27 | | -from flask import Flask, render_template |
28 | 23 | from baleen.config import settings |
| 24 | +from baleen.models import Feed, Post, Job, Log |
| 25 | +from baleen.utils.timez import HUMAN_DATETIME |
29 | 26 |
|
| 27 | +from flask import Flask, render_template, request |
| 28 | +from flask.ext.mongoengine import MongoEngine |
| 29 | +from flask_humanize import Humanize |
30 | 30 |
|
31 | 31 | ########################################################################## |
32 | 32 | ## Flask Application |
33 | 33 | ########################################################################## |
34 | 34 |
|
35 | 35 | # set up an app instance |
36 | 36 | app = Flask(__name__) |
| 37 | + |
37 | 38 | # set debug to true to get debug pages when there is an error |
38 | 39 | app.debug = settings.debug |
39 | 40 |
|
| 41 | +# configure the app with the confire settings |
| 42 | +app.config['MONGODB_SETTINGS'] = { |
| 43 | + 'db': settings.database.name, |
| 44 | + 'host': settings.database.host, |
| 45 | + 'port': settings.database.port, |
| 46 | +} |
| 47 | + |
| 48 | +# connect to the database using the Flask extension |
| 49 | +db = MongoEngine(app) |
| 50 | + |
| 51 | +# add the humanize extension |
| 52 | +humanize = Humanize(app) |
40 | 53 |
|
41 | 54 | ########################################################################## |
42 | 55 | ## Routes |
43 | 56 | ########################################################################## |
44 | 57 |
|
45 | 58 | @app.route("/") |
46 | 59 | def index(): |
47 | | - # connect to the database |
48 | | - db.connect() |
| 60 | + """ |
| 61 | + Displays an index page with the feed listing |
| 62 | + """ |
49 | 63 | # get all the stuff we want |
50 | | - feeds = db.Feed.objects() |
| 64 | + feeds = Feed.objects() |
51 | 65 | feed_count = feeds.count() |
52 | | - topics = set([feed.category for feed in db.Feed.objects.only('category')]) |
| 66 | + topics = set([feed.category for feed in Feed.objects.only('category')]) |
53 | 67 | feeds_topics_counts = len(topics) |
54 | 68 | feeds_topics = { |
55 | | - topic: db.Feed.objects(category=topic) |
| 69 | + topic: Feed.objects(category=topic) |
56 | 70 | for topic in topics |
57 | 71 | } |
58 | 72 |
|
59 | 73 | # load all the data into the templates/feed_list.html template |
60 | | - return render_template('feed_list.html', |
| 74 | + return render_template('index.html', |
61 | 75 | feeds=feeds, |
62 | 76 | feeds_topics=feeds_topics, |
63 | 77 | feed_count=feed_count, |
64 | 78 | topic_count=feeds_topics_counts) |
65 | 79 |
|
66 | | -def get_logs(): |
67 | | - infile = settings.logfile |
68 | | - file = reversed(list(codecs.open(infile,'r',encoding='utf8'))) |
69 | | - logitems = islice(file,20) |
70 | | - return logitems |
71 | 80 |
|
72 | 81 | @app.route("/status/") |
73 | | -def latest_job(): |
74 | | - # get the last job executed |
75 | | - db.connect() |
| 82 | +def status(): |
| 83 | + """ |
| 84 | + Displays the current Baleen status and job listing |
| 85 | + """ |
76 | 86 | version = baleen.get_version() |
77 | | - counts = [db.Feed.objects.count(),db.Post.objects.count(),db.Job.objects.count()] |
78 | | - latest_job = db.Job.objects.order_by('-started').first() |
79 | | - latest_feed = db.Feed.objects.order_by('-updated').first() |
80 | | - latest_post = db.Post.objects.order_by('-id').first() |
81 | | - td = datetime.datetime.now() - latest_job.started |
82 | | - running_time = str(td) |
83 | | - logitems = get_logs() |
| 87 | + counts = { |
| 88 | + 'feeds': Feed.objects.count(), |
| 89 | + 'posts': Post.objects.count(), |
| 90 | + 'jobs': Job.objects.count(), |
| 91 | + } |
| 92 | + latest_job = Job.objects.order_by('-started').first() |
| 93 | + latest_feed = Feed.objects.order_by('-updated').first() |
| 94 | + latest_post = Post.objects.order_by('-id').first() |
84 | 95 |
|
85 | 96 | # load all data into job_status template |
86 | | - return render_template('job_status.html', |
| 97 | + return render_template('status.html', |
87 | 98 | latest_job=latest_job, |
88 | 99 | latest_feed=latest_feed, |
89 | 100 | latest_post=latest_post, |
90 | 101 | version=version, |
91 | 102 | counts=counts, |
92 | | - running_time=running_time, |
93 | | - logitems=logitems) |
| 103 | + dtfmt=HUMAN_DATETIME, |
| 104 | + recent_jobs=Job.objects.order_by('-started').limit(10)) |
| 105 | + |
| 106 | + |
| 107 | +@app.route("/logs/") |
| 108 | +def logs(): |
| 109 | + """ |
| 110 | + Displays log records from the Mongo Database. |
| 111 | + This is paginated and allows flexible per-page counts (max 200 record). |
| 112 | + """ |
| 113 | + # Get pagination information for request |
| 114 | + page = int(request.args.get('page', 1)) |
| 115 | + per_page = min(int(request.args.get('per_page', 50)), 200) |
| 116 | + |
| 117 | + # Compute the pagination variables |
| 118 | + n_logs = Log.objects.count() |
| 119 | + n_pages = (n_logs + per_page // 2) // per_page |
| 120 | + nextp = page + 1 if page + 1 <= n_pages else None |
| 121 | + prevp = page - 1 if page > 1 else None |
| 122 | + |
| 123 | + # Perform query |
| 124 | + offset = (page - 1) * per_page |
| 125 | + logs = Log.objects.order_by('-id').skip(offset).limit(per_page) |
| 126 | + |
| 127 | + return render_template( |
| 128 | + 'logs.html', |
| 129 | + page = page, |
| 130 | + num_pages = n_pages, |
| 131 | + per_page = per_page, |
| 132 | + logs = logs, |
| 133 | + num_logs = n_logs, |
| 134 | + next = nextp, |
| 135 | + prev = prevp, |
| 136 | + ) |
94 | 137 |
|
95 | 138 |
|
96 | 139 | ########################################################################## |
|
0 commit comments