class Widget(__Widget): queyManager = IntelliFluxQueryManager() # get widget data via query manager def fetch_widget_data(self): user_id = current_user.get_user_id() time_stamp_from = current_user.get_time_stamp_from() time_stamp_to = current_user.get_time_stamp_to() database_id = current_user.get_user_database_id() widget_name = '%s-widget-%s-%s-%s-user-%s' % (self.widget_type, time_stamp_from, time_stamp_to, database_id, user_id) sql_query = 'SELECT 0 as i, COUNT(id) as count FROM cleans WHERE relativeIntensity=0 AND startTime BETWEEN \'%s 00:00:00\' AND \'%s 00:00:00\'' % ( time_stamp_from, time_stamp_to) for i in range(1, self.config['max-intensity']): sql_query += ' UNION ALL SELECT %s as i, COUNT(id) as count FROM cleans WHERE relativeIntensity=%s AND startTime BETWEEN \'%s 00:00:00\' AND \'%s 00:00:00\'' % ( i, i, time_stamp_from, time_stamp_to) cleaning_levels_data = self.queyManager.getWidgetDataFromQueryManager( widget_name, time_stamp_from, time_stamp_to, database_id, sql_query, user_id, True) self.widget_data = [data for data in cleaning_levels_data.values()] # return the fetched data return self.widget_data # generate data for bar chart def get_graph_data(self): if not self.widget_data: return [] # read the query manager dict into a dataframe # data = pd.DataFrame.from_dict(self.widget_data, orient="index") return [ go.Bar( x=[data['i'] for data in self.widget_data], y=[data['count'] for data in self.widget_data], marker_color=['#10739e', '#f2931e', '#ae4132', '#12aab5', '#23445d'], name='data' # Name the data series ) ] # additional layout def get_layout_options(self): return dict(xaxis=dict(showticklabels=False), yaxis=dict(showticklabels=False)) # get graph in the current widget def get_widget_graph(self): return [ dcc.Graph( figure=go.Figure( data=self.get_graph_data(), layout=self.get_graph_layout( self.get_layout_options()) ), className='m-auto py-3' ) ]
class Widget(__Widget): queyManager = IntelliFluxQueryManager() # get widget data via query manager def fetch_widget_data(self): self.widget_data = [] user_id = current_user.get_user_id() time_stamp_from = current_user.get_time_stamp_from() time_stamp_to = current_user.get_time_stamp_to() database_id = current_user.get_user_database_id() widget_name = '%s-widget-%s-%s-%s-user-%s' % ( self.widget_type, time_stamp_from, time_stamp_to, database_id, user_id) for mode in self.config['mode'].split('+'): title = '' if mode == 'total': title = 'Total Cleans' sql_query = 'SELECT COUNT(DISTINCT(startTime)) as count FROM cleans WHERE startTime BETWEEN \'%s 00:00:00\' AND \'%s 00:00:00\'' % ( time_stamp_from, time_stamp_to) elif mode == 'common': title = 'Most Common' sql_query = 'SELECT intensity, count(DISTINCT(startTime)) as count FROM cleans WHERE startTime BETWEEN \'%s 00:00:00\' AND \'%s 00:00:00\' GROUP BY intensity ORDER BY count DESC LIMIT 1' % ( time_stamp_from, time_stamp_to) elif mode == 'time': title = 'Clean Time' sql_query = 'SELECT (SUM(TIMESTAMPDIFF(SECOND, startTime, endTime)) / 60 / 60) as time FROM cleans WHERE startTime BETWEEN \'%s 00:00:00\' AND \'%s 00:00:00\'' % ( time_stamp_from, time_stamp_to) elif mode == 'use': title = 'Water Use' sql_query = 'SELECT SUM(JSON_EXTRACT(profile, \'$.WaterUsage\')) as usage FROM cleans WHERE startTime BETWEEN \'%s 00:00:00\' AND \'%s 00:00:00\'' % ( time_stamp_from, time_stamp_to) data = self.queyManager.getWidgetDataFromQueryManager( widget_name, time_stamp_from, time_stamp_to, database_id, sql_query, user_id, True) self.widget_data.append( dict(mode=mode, title=title, value=data[0]['count'])) # return the fetched data return self.widget_data def get_widget_graph(self): return [ dbc.Row([ dbc.Col([ html.H2(widget_data['value'], className='text-center'), html.H5(widget_data['title'], className='text-center') ], className='col-6 py-4') for widget_data in self.widget_data ]) ]
class Widget(__Widget): queyManager = IntelliFluxQueryManager() # get widget data via query manager def fetch_widget_data(self): user_id = current_user.get_user_id() time_stamp_from = current_user.get_time_stamp_from() time_stamp_to = current_user.get_time_stamp_to() database_id = current_user.get_user_database_id() widget_name = '%s-widget-%s-%s-%s-user-%s' % ( self.widget_type, time_stamp_from, time_stamp_to, database_id, user_id) # t_stamp = 'DATE_FORMAT(t_stamp, \'%Y-%m-%d\')' # sql_query = 'SELECT name, AVG(value) as value, %s as t_stamp FROM data WHERE name=\'%s\' AND value > %s AND t_stamp BETWEEN \'%s\' AND \'%s\' group by %s;' % ( # t_stamp, self.config['metric'], self.config['flow_min'], time_stamp_from, time_stamp_to, t_stamp) sql_query = 'SELECT t_stamp,value, name FROM data WHERE name=\'%s\' AND value > \'%s\' AND t_stamp BETWEEN \'%s 00:00:00\' AND \'%s 00:00:00\';' % ( self.config['metric'], self.config['flow_min'], time_stamp_from, time_stamp_to) flowdata = self.queyManager.getWidgetDataFromQueryManager( widget_name, time_stamp_from, time_stamp_to, database_id, sql_query, user_id, True) temp_flowdata = dict() for idx in flowdata: row = flowdata[idx] t_stamp = datetime.strptime(row['t_stamp'], '%Y-%m-%dT%H:%M:%S.%fZ') key = t_stamp.strftime('%Y-%m-%d') if key not in temp_flowdata: temp_flowdata[key] = [] temp_flowdata[key].append( dict(t_stamp=row['t_stamp'], value=row['value'])) del flowdata self.widget_data = dict() for key in temp_flowdata: flowdata = temp_flowdata[key] prev_time = flowdata[0]['t_stamp'] if isinstance(prev_time, str): prev_time = datetime.strptime(prev_time, '%Y-%m-%dT%H:%M:%S.%fZ') flow_total = 0.0 for row in flowdata[1:]: # get the seconds between the previous and the current times t_stamp = datetime.strptime(row['t_stamp'], '%Y-%m-%dT%H:%M:%S.%fZ') delta = (t_stamp - prev_time).total_seconds() # skip rows longer than 2 minutes apart if delta > 120: prev_time = t_stamp continue # add to the flow totalizer flow_total += (row['value'] / 3600.0) * delta self.widget_data[key] = flow_total # return the fetched data return self.widget_data # generate data for bar chart def get_graph_data(self): if not self.widget_data: return [] # read the query manager dict into a dataframe # data = pd.DataFrame.from_dict(self.widget_data, orient="index") return [ go.Bar( x=list(self.widget_data.keys()), y=list(self.widget_data.values()), marker_color=[ '#10739e', '#f2931e', '#ae4132', '#12aab5', '#23445d' ], name='data' # Name the data series ) ] # additional layout def get_layout_options(self): return dict(xaxis=dict(showticklabels=False), yaxis=dict(showticklabels=False)) # get graph in the current widget def get_widget_graph(self): return [ dcc.Graph(figure=go.Figure(data=self.get_graph_data(), layout=self.get_graph_layout( self.get_layout_options())), className='m-auto') ]
class Widget(__Widget): queryManager = IntelliFluxQueryManager() def fetch_widget_data(self): """ Get widget data via query manager returns: {Dict} A dictionary of the pandas dataframe data from the query manager """ # Create the widget name for the QueryManager cur_user = User.get_instance() user_id = cur_user.get_user_id() time_stamp_from = cur_user.get_time_stamp_from() time_stamp_to = cur_user.get_time_stamp_to() database_id = cur_user.get_user_database_id() widget_name = '%s-widget-%s-%s-%s-%s-user-%s' % ( self.widget_type, self.config['title']['text'], time_stamp_from, time_stamp_to, database_id, user_id) # Define the MySQL query to run sql_query = "SELECT t_stamp, value, name FROM data WHERE name=\'" + self.config[ 'metric'] + "\' AND t_stamp BETWEEN \'%s 00:00:00\' AND \'%s 00:00:00\' order by t_stamp" % ( time_stamp_from, time_stamp_to) # Fetch the data from the query manager self.widget_data = self.queryManager.getWidgetDataFromQueryManager( widget_name, time_stamp_from, time_stamp_to, database_id, sql_query, user_id, True) # return the fetched data return self.widget_data # generate data for bar chart def get_graph_data(self, marker_props=[]): """ Generate data for a scatter plot Arguments: marker_props: {List} Properties for the plot markers returns: {List} A lost containing a graph object """ if not self.widget_data: return [] # read the query manager dict into a dataframe data = pd.DataFrame.from_dict(self.widget_data, orient="index") # return the scatter plot configuration return [ go.Scatter( # Use the t_stamp column of the dataframe as X x=data['t_stamp'], # Use the value column of the dataframe as Y y=data['value'], # Set plot mode to use markers mode=self.config['mode'] if 'mode' in self.config else 'markers', name='data' # Name the data series ) ] # additional layout options def get_layout_options(self): return dict(xaxis=dict(showticklabels=False), yaxis=dict(showticklabels=False), margin=dict(b=30, t=40, r=30)) # get widget graph def get_widget_graph(self): return [ dcc.Graph(figure=go.Figure(data=self.get_graph_data(), layout=self.get_graph_layout( self.get_layout_options())), className='m-auto') ]
def __init__(self, config, widget_type, is_child_widget=False): self.config = config self.widget_type = widget_type self.is_child_widget = is_child_widget self.query_manager = IntelliFluxQueryManager() self.fetch_widget_data()