1
# Copyright 2016 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""The Discovery handler for the WebSocket connection."""
5
from maasserver.websockets import dehydrate_datetime
11
from maasserver.websockets.handlers.viewmodel import ViewModelHandler
12
from maasserver.models import Discovery
13
from provisioningserver.logger import get_maas_logger
16
maaslog = get_maas_logger("websockets.discovery")
19
class DiscoveryHandler(ViewModelHandler):
23
Discovery.objects.by_unknown_ip_and_mac()
24
.order_by("-last_seen")
25
# Need an incrementing row number to use for a batch key.
29
# The extra select needs to specify the ordering with which
30
# to apply the row_number(); it must match Django's
31
# order_by() in order to be consistent.
32
'ROW_NUMBER() OVER (ORDER BY last_seen DESC)'
36
# This batch key isn't guaranteed to be stable, since newly-discovered
37
# items can come in as the new first-items in the query. But that's why
38
# we're also going to poll. But using row_number() seems to be a good
40
batch_key = '_row_number'
47
def dehydrate_last_seen(self, datetime):
48
return dehydrate_datetime(datetime)