def sendUserViewData(email): """Send data for views chart for the specified user's email.""" database_helper.connectToDatabase() views = database_helper.getViewsOnWallDuringLast6Months(email) posts = database_helper.getPostsOnWallDuringLast6Months(email) data = {'labels': ['', '', '', '', '', ''], 'datasets': []} # Setup the labels for the months, starts with todays month and adds the last 6 months in reverse order. # The current month is at the end of the array. today = datetime.date.today() for i in range(0, 6): data['labels'][5 - i] = getMonthName(today.strftime('%m')) newMonth = today.month newMonth -= 1 if newMonth == 0: newMonth = 12 today = today.replace(month=newMonth) # Setup the data in the form that chart.js expects it in. data['datasets'].append({'label': 'Number of views', 'data': [0, 0, 0, 0, 0, 0]}) data['datasets'].append({'label': 'Number of posts', 'data': [0, 0, 0, 0, 0, 0]}) data['datasets'][0]['fillColor'] = 'rgba(200,200,200,0.2)' data['datasets'][0]['strokeColor'] = 'rgba(200,200,200,1)' data['datasets'][0]['pointColor'] = 'rgba(200,200,200,1)' data['datasets'][1]['fillColor'] = 'rgba(157,224,173,0.2)' data['datasets'][1]['strokeColor'] = 'rgba(157,224,173,1)' data['datasets'][1]['pointColor'] = 'rgba(157,224,173,1)' for view in views: dataIndex = data['labels'].index(getMonthName(view[1])) data['datasets'][0]['data'][dataIndex] = view[2] for post in posts: dataIndex = data['labels'].index(getMonthName(post[1])) data['datasets'][1]['data'][dataIndex] = post[2] if webSockets.has_key(email): webSockets[email].send(json.dumps({'type': 'viewCounter', 'data': data}))
def sendUserPostData(email): """Send data for posts chart for the specified user's email.""" database_helper.connectToDatabase() numberOfPostsByUser = database_helper.getNumberOfPostsByUserOnWall(email, email) data = [{'value': numberOfPostsByUser, 'color': '#9DE0AD', 'label': email}] topTwo = database_helper.getTopTwoNumberOfPostsOnWallByOthers(email) numberOfPostsOnWall = database_helper.getNumberOfPostsOnWall(email) colors = ['#45ADA8', '#4F7A79'] colorIndex = 0 # Loop through the two top posters on the user's wall and assign them a color form the colors list. for writer in topTwo: data.append({'value': writer[1], 'color': colors[colorIndex], 'label': writer[0]}) colorIndex += 1 if webSockets.has_key(email): webSockets[email].send(json.dumps({'type': 'messageCounter', 'data': data}))
def beforeRequest(): database_helper.connectToDatabase()
def sendUserPostTotalData(email): """Send number of total posts for the specified user's email.""" database_helper.connectToDatabase() numberOfPostsOnWall = database_helper.getNumberOfPostsOnWall(email) if webSockets.has_key(email): webSockets[email].send(json.dumps({'type': 'messageCounterTotal', 'data': numberOfPostsOnWall}))
def sendUsersCounter(): """Send the number of signed in users to all web sockets.""" database_helper.connectToDatabase() usersCounter = database_helper.getNumberOfSignedInUsers(); for key in webSockets: webSockets[key].send(json.dumps({'type': 'usersCounter', 'data': usersCounter}))