~mpontillo/maas/discovery-js-factory

« back to all changes in this revision

Viewing changes to src/maasserver/websockets/handlers/discovery.py

  • Committer: Mike Pontillo
  • Date: 2016-09-02 20:25:22 UTC
  • Revision ID: mike.pontillo@canonical.com-20160902202522-3gid0v8jz1sn0hri
Add discovery websocket handler.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2016 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""The Discovery handler for the WebSocket connection."""
 
5
from maasserver.websockets import dehydrate_datetime
 
6
 
 
7
__all__ = [
 
8
    "DiscoveryHandler",
 
9
    ]
 
10
 
 
11
from maasserver.websockets.handlers.viewmodel import ViewModelHandler
 
12
from maasserver.models import Discovery
 
13
from provisioningserver.logger import get_maas_logger
 
14
 
 
15
 
 
16
maaslog = get_maas_logger("websockets.discovery")
 
17
 
 
18
 
 
19
class DiscoveryHandler(ViewModelHandler):
 
20
 
 
21
    class Meta:
 
22
        queryset = (
 
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.
 
26
            .extra(
 
27
                select={
 
28
                    '_row_number':
 
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)'
 
33
                }
 
34
            )
 
35
        )
 
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
 
39
        # compromise for now.
 
40
        batch_key = '_row_number'
 
41
        pk = 'discovery_id'
 
42
        allowed_methods = [
 
43
            'list',
 
44
            'get',
 
45
        ]
 
46
 
 
47
    def dehydrate_last_seen(self, datetime):
 
48
        return dehydrate_datetime(datetime)