basechart.py 10.3 KB
Newer Older
1 2 3 4
"""
This module holds classes that create the HTML and supply the data for Charts and
Reports.
"""
5
import time, logging, copy, importlib
6
from django.conf import settings
7
import yaml
8
import bmsapp.models, bmsapp.readingdb.bmsdata
9
import bmsapp.schedule
10
import bmsapp.view_util, bmsapp.data_util
11
from . import chart_config
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

# Make a logger for this module
_logger = logging.getLogger('bms.' + __name__)


class BldgChartType:
    """Class to provide descriptive information about a particular type of chart related
    to one building.
    """

    def __init__(self, id, title, class_name):
        """Initializes the chart type.

            Args:
                id (int): ID number uniquely identifying this chart type.
                title (string): Text that will be displayed in select controls and chart titles.
                class_name (string): The name of the Python class in this charts.py used to create the chart.
        """
        self.id = id        
        self.title = title  
        self.class_name = class_name   

# These are the possible chart types currently implemented, in the order they will be 
# presented to the User.
BLDG_CHART_TYPES = [
37 38 39 40
    BldgChartType(0, 'Dashboard', 'dashboard.Dashboard'),
    BldgChartType(1, 'Current Sensor Values', 'currentvalues.CurrentValues'),
    BldgChartType(2, 'Plot Sensor Values over Time', 'timeseries.TimeSeries'),
    BldgChartType(3, 'Hourly Profile of a Sensor', 'hourlyprofile.HourlyProfile'),
41 42 43
    BldgChartType(4, 'Heat Map Hourly Profile', 'hourly_heatmap.HourlyHeatMap'),
    BldgChartType(5, 'Histogram of a Sensor', 'histogram.Histogram'),
    BldgChartType(6, 'Sensor X vs Y Scatter Plot', 'xyplot.XYplot'),
Alan Mitchell's avatar
Alan Mitchell committed
44
    BldgChartType(8, 'Cycle Time Analysis', 'cycle_info.CycleInfo'),
45
    BldgChartType(7, 'Download Sensor Data to Excel', 'exportdata.ExportData')
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
]

# The ID of the Time Series chart above, as it is needed in code below.
TIME_SERIES_CHART_ID = 2


def find_chart_type(chart_id):
    """Returns the BldgChartType for a given ID.

        Args:
            chart_id (int): The ID number of the requested chart type.

        Returns:
            The BldgChartType having the requested ID.  Returns None if no match.
    """
    for ch in BLDG_CHART_TYPES:
        if ch.id == chart_id:
            return ch

    return None

Ian Moore's avatar
Ian Moore committed
67
def get_chart_object(request):
68 69 70
    """Returns the appropriate chart object identified by the arguments.

        Args:
Ian Moore's avatar
Ian Moore committed
71
            request: The http request passed in by the user further qualifying the chart.
72 73 74 75 76

        Returns:
            A chart object descending from BaseChart.
    """

Ian Moore's avatar
Ian Moore committed
77 78 79
    # Get the request parameters
    request_params = request.GET

80
    # Get building ID and chart ID from the request parameters
81 82
    bldg_id = bmsapp.view_util.to_int(request_params['select_bldg'])
    chart_id = bmsapp.view_util.to_int(request_params['select_chart'])
83 84

    if bldg_id=='multi':
85
        chart_info = bmsapp.models.MultiBuildingChart.objects.get(id=chart_id)
86
        class_name = chart_info.chart_class
87 88 89 90
    else:
        chart_info = find_chart_type(chart_id)
        class_name = chart_info.class_name

91 92 93 94 95
    # need to dynamically get a class object based on the class_name.
    # class_name is in the format <module name>.<class name>; break it apart.
    mod_name, bare_class_name = class_name.split('.')
    mod = importlib.import_module('bmsapp.reports.%s' % mod_name.strip())
    
96
    # get a reference to the class referred to by class_name
97
    chart_class = getattr(mod, bare_class_name.strip())
98 99

    # instantiate and return the chart from this class
Ian Moore's avatar
Ian Moore committed
100
    return chart_class(chart_info, bldg_id, request)
101 102 103 104 105 106


class BaseChart(object):
    """Base class for all of the chart classes.
    """

107 108 109 110 111 112
    # Constants to override, if needed for the specific chart being created.
    # These constants affect configuration of the browser user interface that 
    # will be used for this particular chart.

    # This is a comma-separated list of the client HTML controls that need to be
    # visible for this chart.
113
    CTRLS = 'time_period_group, refresh'
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

    # 1 if the Sensor selection control should allow for selecting more than one
    # sensor.
    MULTI_SENSOR = 0

    # 1 if the chart should automatically recalculate and refresh when the user 
    # changes inputs.
    AUTO_RECALC = 1

    # 1 if the chart should automatically refresh every 10 minutes even without 
    # changes in user inputs
    TIMED_REFRESH = 0

    @classmethod
    def data_attributes(cls):
        '''This class method returns an string of HTML data attributes that will
        control aspects of the browser user interface.  Javascript in the browser
        reads these attributes and configures the interface appropriately.
        '''
        return 'data-ctrls="%s" data-multi_sensor="%s" data-auto_recalc="%s" data-timed_refresh="%s"' % \
            (cls.CTRLS, cls.MULTI_SENSOR, cls.AUTO_RECALC, cls.TIMED_REFRESH)


Ian Moore's avatar
Ian Moore committed
137
    def __init__(self, chart_info, bldg_id, request):
138
        """
139 140 141 142
        'chart_info' is the models.MultiBuildingChart object for the chart if it
        is a multi-building chart; for single-building charts, it is the BldgChartType
        object (the BldgChartType class is in this module).  'bldg_id'
        is the id of the building being charted, or 'multi' for multi-building
Ian Moore's avatar
Ian Moore committed
143
        charts. 'request' contains the parameters
144 145 146 147 148
        passed in by the user through the Get http request.
        """
        self.chart_info = chart_info
        self.bldg_id = bldg_id

149
        # if this is a chart for a single building, get the associated building model object,
150 151
        # and the occupied schedule for the building if it is present.  Also, determine a 
        # timezone appropriate for this chart.
152
        self.schedule = None
153
        self.timezone = getattr(settings, 'TIME_ZONE', 'US/Alaska').strip()
154
        if bldg_id != 'multi':
155
            self.building = bmsapp.models.Building.objects.get(id=bldg_id)
156 157 158
            # override  the timezone if the building has one explicitly set
            if len(self.building.timezone.strip()):
                self.timezone = self.building.timezone.strip()
159
            if len(self.building.schedule.strip()):
160
                self.schedule = bmsapp.schedule.Schedule(self.building.schedule, self.timezone)
161

Ian Moore's avatar
Ian Moore committed
162 163
        self.request = request
        self.request_params = request.GET
164 165

        # for the multi-building chart object, take the keyword parameter string 
166
        # and convert it to a Python dictionary or list.
167
        if bldg_id == 'multi':
168
            self.chart_params = yaml.load(chart_info.parameters, Loader=yaml.FullLoader)
169 170 171

        # open the reading database and save it for use by the methods of this object.
        # It is closed automatically in the destructor of the BMSdata class.
172
        self.reading_db = bmsapp.readingdb.bmsdata.BMSdata()
173 174 175 176 177 178 179 180

    def get_ts_range(self):
        """
        Returns the start and stop timestamp as determined by the GET parameters that were posted
        from the "time_period" Select control.
        """
        tm_per = self.request_params['time_period']
        if tm_per != "custom":
181
            st_ts = int(time.time()) - float(tm_per) * 24 * 3600
182 183 184
            end_ts = time.time() + 3600.0    # adding an hour to be sure all records are caught
        else:
            st_date = self.request_params['start_date']
185
            st_ts = bmsapp.data_util.datestr_to_ts(st_date) if len(st_date) else 0
186
            end_date = self.request_params['end_date']
187
            end_ts = bmsapp.data_util.datestr_to_ts(end_date + " 23:59:59") if len(end_date) else time.time() + 3600.0
188 189 190

        return st_ts, end_ts

Ian Moore's avatar
Ian Moore committed
191
    def get_chart_options(self, chart_type='plotly'):
192
        """
Ian Moore's avatar
Ian Moore committed
193
        Returns a configuration object for the chart.  Must make a
194 195
        copy of the original so that it is not modified.
        """
Ian Moore's avatar
Ian Moore committed
196
        if chart_type == 'plotly':
Ian Moore's avatar
Ian Moore committed
197
            opt = chart_config.plotly_opt
198 199
        else:
            raise ValueError('Invalid Chart Type.')
200 201
        opt = copy.deepcopy(opt)
        if hasattr(self, 'building'):
Ian Moore's avatar
Ian Moore committed
202
            opt['layout']['title'] = '%s: %s' % (self.chart_info.title, self.building.title)
203
        else:
Ian Moore's avatar
Ian Moore committed
204
            opt['layout']['title'] = self.chart_info.title
205 206
        return opt

207 208 209 210 211 212 213 214 215 216 217 218
    def occupied_resolution(self):
        """Returns a string indicating the resolution to use when determining
        whether a timestamp is in the Occupied or Unoccupied period.  The return
        value depends on how many hours the data is averaged over; this is 
        indicated by the 'averaging_time' GET parameter.
        The possible return values are 
            'exact': classify the timestamp based on the exact schedule times.
            'day': classify the timestamp according to whether it falls in a day
                that is predominantly occupied.
            None: Data averaging is across long intervals that make occupied / 
                unoccupied classification not meaningful.
        """
219 220 221 222 223 224
        # get info about the requested chart
        cht_info = find_chart_type(int(self.request_params['select_chart']))

        # get the requested averaging interval in hours.  The relevant 
        # averaging input control depends on the chart type.
        if 'XY' in cht_info.class_name:
Alan Mitchell's avatar
Alan Mitchell committed
225
            averaging_hours = float(self.request_params['averaging_time_xy'])    
226
        elif 'Export' in cht_info.class_name:
Alan Mitchell's avatar
Alan Mitchell committed
227 228 229
            averaging_hours = float(self.request_params['averaging_time_export'])
        else:
            averaging_hours = float(self.request_params['averaging_time'])
230 231 232 233 234 235 236 237

        if averaging_hours < 24.0:
            return 'exact'
        elif averaging_hours==24.0:
            return 'day'
        else:
            return None

238 239 240 241 242 243 244
    def result(self):
        '''
        This method should be overridden to return a dictionary with an 
        'html' key holding the HTML of the results and an 'objects' key
        holding a list of JavaScript objects to create.  Each object is a
        two-tuple with the first element being the string identifying the
        object type and the second element being a configuration dictionary
245 246 247 248 249 250
        for that object type.  'bmsappX-Y.Z.js' must understand the string
        describing the JavaScript object.
        Alternatively, this method can return a django.http.HttpResponse
        object, which will be returned directly to the client application;
        this approach is used the exportdata.ExportData class to return an
        Excel spreadsheet.
251 252 253
        '''
        return {'html': self.__class__.__name__, 'objects': []}