Removed old code

parent 53160d2b
......@@ -10,37 +10,34 @@
Serve content to clients via TCP. All HTTP, WS etc. handlers belong on here.
"""
import tornado.web
import tornado.httpserver
import tornado.web
from tornado.options import options
from bitsd.common import LOG, bind
from . import auth
from . import handlers
from . import uimodules
from . import auth
from bitsd.common import LOG, bind
def start():
"""Setup HTTP/WS server. **MUST** be called prior to any operation."""
application = tornado.web.Application([
# FIXME daltonism workaround, should be implemented client-side
(r'/(?:|blind)', handlers.HomePageHandler),
(r'/log', handlers.LogPageHandler),
(r'/status', handlers.StatusPageHandler),
(r'/data', handlers.DataPageHandler),
(r'/presence', handlers.PresenceForecastHandler),
(r'/(info)', handlers.MarkdownPageHandler),
(r'/ws', handlers.StatusHandler),
(r'/login', handlers.LoginPageHandler),
(r'/logout', handlers.LogoutPageHandler),
(r'/admin', handlers.AdminPageHandler),
(r'/message', handlers.MessagePageHandler),
(r'/data.php', handlers.RTCHandler),
(r'/macupdate', handlers.MACUpdateHandler),
(r'/presence.svg', handlers.PresenceGraphHandler)
],
# FIXME daltonism workaround, should be implemented client-side
(r'/(?:|blind)', handlers.HomePageHandler),
(r'/log', handlers.LogPageHandler),
(r'/status', handlers.StatusPageHandler),
(r'/data', handlers.DataPageHandler),
(r'/(info)', handlers.MarkdownPageHandler),
(r'/ws', handlers.StatusHandler),
(r'/login', handlers.LoginPageHandler),
(r'/logout', handlers.LogoutPageHandler),
(r'/admin', handlers.AdminPageHandler),
(r'/message', handlers.MessagePageHandler),
(r'/data.php', handlers.RTCHandler),
(r'/macupdate', handlers.MACUpdateHandler),
(r'/presence.svg', handlers.PresenceGraphHandler)
],
ui_modules=uimodules,
gzip=True,
debug=options.developer_mode,
......
......@@ -31,7 +31,6 @@ from bitsd.persistence.models import Status, User, MACToUser, LoginAttempt
from bitsd.server.presence_graph.presence import Presence
from .auth import verify, DoSError
from .notifier import MessageNotifier
from .presence import PresenceForecaster
def cache(seconds):
......@@ -343,19 +342,6 @@ class AdminPageHandler(BaseHandler):
)
class PresenceForecastHandler(BaseHandler):
"""Handler for presence stats.
Upon GET, it will render JSON-encoded probabilities,
as a 2D array (forecast for each weekday, at 30min granularity)."""
FORECASTER = PresenceForecaster()
@cache(86400)
def get(self):
data = self.FORECASTER.forecast()
self.write({"forecast": data})
self.finish()
class MessagePageHandler(BaseHandler):
@tornado.web.authenticated
def get(self):
......
#
# Copyright (C) 2013 Stefano Sanfilippo
# Copyright (C) 2013 BITS development team
#
# This file is part of bitsd, which is released under the terms of
# GNU GPLv3. See COPYING at top level for more information.
#
from bitsd.persistence.engine import session_scope
from bitsd.persistence.models import Status
from sqlalchemy import asc
class PresenceForecaster:
class InvalidResolutionError(Exception):
def __init__(self):
self.message = "Resolution must be a submultiple of 60 minutes!"
def __init__(self, resolution=30, samples_cont=5000):
if self.resolution_is_invalid(resolution):
raise self.InvalidResolutionError()
self.samples_count = samples_cont
self.ticks_per_hour = 60 / resolution
self.minutes_per_tick = resolution
def forecast(self):
#TODO caching
return self.calculate_frequencies()
def calculate_frequencies(self):
samples = self.get_samples()
buckets = self.count_presence_per_slot(samples)
return self.normalize(buckets)
def count_presence_per_slot(self, samples):
buckets = self.init_buckets()
# TODO algorithm here
return buckets
def init_buckets(self):
return [[0] * (24 * self.ticks_per_hour) for i in range(7)]
def get_samples(self):
with session_scope() as session:
samples = session \
.query(Status.timestamp, Status.value) \
.filter((Status.value == Status.OPEN) | (Status.value == Status.CLOSED)) \
.order_by(asc(Status.timestamp)) \
.limit(self.samples_count)
offset = self.first_open_offset(samples)
return samples[offset:]
def first_open_offset(self, samples):
offset = 0
while samples[offset].value != Status.OPEN:
offset += 1
return offset
@staticmethod
def resolution_is_invalid(resolution):
return (60 % resolution) != 0
def calculate_coordinates(self, sample):
timestamp = sample.timestamp
weekday = timestamp.weekday()
timeslot = (self.ticks_per_hour * timestamp.hour) + int(1. * timestamp.minute / self.minutes_per_tick)
return weekday, timeslot
def normalize(self, buckets):
for day in buckets:
for i, slot in enumerate(day):
day[i] = 1. * slot / self.samples_count
return buckets
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment