def change_slug(self, old_slug, new_slug): if old_slug is not None and new_slug is not None: try: game = self._slugs[old_slug] del(self._slugs[old_slug]) if new_slug in self._slugs.keys(): new_slug = SlugDetail(self.make_slug_unique(new_slug)) game.slug = new_slug self._slugs[new_slug] = game except KeyError: LOG.error('Error swapping slugs:' + old_slug + ' for ' + new_slug) else: MetricsSession.rename(old_slug, new_slug) cache_dir = config.get('deploy.cache_dir', None) Deployment.rename_cache(cache_dir, old_slug, new_slug)
def delete(cls, slug, timestamp): if not MetricsSession.delete(slug, timestamp): response.status_int = 404 return {'ok': False, 'msg': 'Session does not exist: %s' % timestamp} response.headers['Cache-Control'] = 'no-store, no-cache, max-age=0' return {'ok': True}
def change_slug(self, old_slug, new_slug): if old_slug is not None and new_slug is not None: try: game = self._slugs[old_slug] del (self._slugs[old_slug]) if new_slug in self._slugs.keys(): new_slug = SlugDetail(self.make_slug_unique(new_slug)) game.slug = new_slug self._slugs[new_slug] = game except KeyError: LOG.error('Error swapping slugs:' + old_slug + ' for ' + new_slug) else: MetricsSession.rename(old_slug, new_slug) cache_dir = config.get('deploy.cache_dir', None) Deployment.rename_cache(cache_dir, old_slug, new_slug)
def stop_recording(cls, slug): if MetricsSession.stop_recording(slug): return {'ok': True} else: response.status_int = 404 return { 'ok': False, 'msg': 'No active session for game: "%s"' % slug }
def delete(cls, slug, timestamp): if not MetricsSession.delete(slug, timestamp): response.status_int = 404 return { 'ok': False, 'msg': 'Session does not exist: %s' % timestamp } response.headers['Cache-Control'] = 'no-store, no-cache, max-age=0' return {'ok': True}
def as_json(cls, slug, timestamp): timestamp_format = '%Y-%m-%d_%H-%M-%S' try: filename = '%s-%s.json' % (slug, time.strftime(timestamp_format, time.gmtime(float(timestamp)))) except ValueError: abort(404, 'Invalid timestamp: %s' % timestamp) response.content_disposition = 'attachment; filename=%s' % filename data = MetricsSession.get_data_as_json(slug, timestamp) if not data: abort(404, 'Session does not exist: %s' % timestamp) return data
def details(self, slug, timestamp): game = get_game_by_slug(slug) if not game: response.status_int = 404 return {'ok': False, 'msg': 'Game does not exist: %s' % slug} self._update_metrics(slug, game) session = MetricsSession.get_data(slug, timestamp) if not session: response.status_int = 404 return {'ok': False, 'msg': 'Session does not exist: %s' % timestamp} return {'ok': True, 'data': session}
def as_json(cls, slug, timestamp): timestamp_format = '%Y-%m-%d_%H-%M-%S' try: filename = '%s-%s.json' % ( slug, time.strftime(timestamp_format, time.gmtime(float(timestamp)))) except ValueError: abort(404, 'Invalid timestamp: %s' % timestamp) response.content_disposition = 'attachment; filename=%s' % filename data = MetricsSession.get_data_as_json(slug, timestamp) if not data: abort(404, 'Session does not exist: %s' % timestamp) return data
def details(self, slug, timestamp): game = get_game_by_slug(slug) if not game: response.status_int = 404 return {'ok': False, 'msg': 'Game does not exist: %s' % slug} self._update_metrics(slug, game) session = MetricsSession.get_data(slug, timestamp) if not session: response.status_int = 404 return { 'ok': False, 'msg': 'Session does not exist: %s' % timestamp } return {'ok': True, 'data': session}
def _update_metrics(self, slug, game): metrics = MetricsSession.get_metrics(slug) inverse_mapping = get_inverse_mapping_table(game) for session in metrics: try: s = _Session(session['timestamp']) fileDict = {} for entry in session['entries']: try: (filename, size, mimetype, status) = \ (entry['file'], int(entry['size']), entry['type'], entry['status']) except TypeError: break try: asset_name = inverse_mapping[os.path.basename( filename)] except KeyError: asset_name = filename _, ext = os.path.splitext(asset_name) ext = ext[1:] if ext else 'unknown' # Add the request to the session. s.add_request(size) # Add the request to the by_file metrics. if filename not in fileDict: fileDict[filename] = _File(asset_name, filename, size, mimetype, status) s.add_file(size) s.humanize() timestamp = s.timestamp self._session_overviews.append((timestamp, s)) except KeyError as e: LOG.error( "Potentially corrupted file found. Can't extract metrics data: %s" % str(e))
def _update_metrics(self, slug, game): metrics = MetricsSession.get_metrics(slug) inverse_mapping = get_inverse_mapping_table(game) for session in metrics: try: s = _Session(session['timestamp']) fileDict = {} for entry in session['entries']: try: (filename, size, mimetype, status) = \ (entry['file'], int(entry['size']), entry['type'], entry['status']) except TypeError: break try: asset_name = inverse_mapping[os.path.basename(filename)] except KeyError: asset_name = filename _, ext = os.path.splitext(asset_name) ext = ext[1:] if ext else 'unknown' # Add the request to the session. s.add_request(size) # Add the request to the by_file metrics. if filename not in fileDict: fileDict[filename] = _File(asset_name, filename, size, mimetype, status) s.add_file(size) s.humanize() timestamp = s.timestamp self._session_overviews.append((timestamp, s)) except KeyError as e: LOG.error("Potentially corrupted file found. Can't extract metrics data: %s", str(e))
def has_metrics(self): return MetricsSession.has_metrics(self.slug)
def __call__(self, environ, start_response): # check whether the the request should be logged, i.e. starts with # 'play' and is longer than a mere request for the playable-versions # page request_path = environ.get('PATH_INFO', '') path_parts = request_path.strip('/').split('/', 2) if len(path_parts) == 3 and path_parts[0] == 'play': slug = path_parts[1] if self.gamelist.get_by_slug(slug): file_name = path_parts[2] # find user id on cookies or create a new one cookies = request.get_cookies(environ) if cookies.has_key(self.cookie_session_name): user_id = cookies[self.cookie_session_name].value is_new_user = False else: self.user_id_counter += 1 user_id = '%x' % self.user_id_counter is_new_user = True slug_sessions = MetricsSession.get_sessions(slug) # make sure there is a session when an html file is requested # ignore otherwise session = slug_sessions.get(user_id, None) if file_name.endswith(('.html', '.htm')) or \ (file_name.endswith(('.tzjs', '.canvas.js', '.swf')) and 'HTTP_REFERER' in environ and \ not environ['HTTP_REFERER'].endswith(('.html', '.htm'))): if session: session.finish() session = None try: session = MetricsSession(slug) except IOError: return self.app(environ, start_response) slug_sessions[user_id] = session elif not session: return self.app(environ, start_response) # define function to capture status and headers from response response_headers = [] def metrics_start_response(status, headers, exc_info=None): if is_new_user: headers.append(('Set-Cookie', '%s=%s; Path=/play/%s/' % (self.cookie_session_name, user_id, slug))) response_headers.append(status) response_headers.append(headers) return start_response(status, headers, exc_info) # pass through request and get response response = self.app(environ, metrics_start_response) status = response_headers[0] file_size = 0 if status.startswith('404'): file_type = 'n/a' else: file_type = None if status.startswith('200'): for k, v in response_headers[1]: if k == 'Content-Length': file_size = v if file_type: break elif k == 'Content-Type': file_type = v if file_size: break else: for k, v in response_headers[1]: if k == 'Content-Type': file_type = v break if not file_type: file_type = mimetypes.guess_type(file_name)[0] if not file_type: file_type = 'n/a' else: file_type = file_type.split(';')[0].split(',')[0] session.append(file_name, file_size, file_type, status) # send the response back up the WSGI layers return response return self.app(environ, start_response)
def stop_recording(cls, slug): if MetricsSession.stop_recording(slug): return {'ok': True} else: response.status_int = 404 return {'ok': False, 'msg': 'No active session for game: "%s"' % slug}
def __call__(self, environ, start_response): # check whether the the request should be logged, i.e. starts with # 'play' and is longer than a mere request for the playable-versions # page request_path = environ.get('PATH_INFO', '') path_parts = request_path.strip('/').split('/', 2) if len(path_parts) == 3 and path_parts[0] == 'play': slug = path_parts[1] if self.gamelist.get_by_slug(slug): file_name = path_parts[2] # find user id on cookies or create a new one cookies = request.get_cookies(environ) if cookies.has_key(self.cookie_session_name): user_id = cookies[self.cookie_session_name].value is_new_user = False else: self.user_id_counter += 1 user_id = '%x' % self.user_id_counter is_new_user = True slug_sessions = MetricsSession.get_sessions(slug) # make sure there is a session when an html file is requested # ignore otherwise session = slug_sessions.get(user_id, None) if file_name.endswith(('.html', '.htm')) or \ (file_name.endswith(('.tzjs', '.canvas.js', '.swf')) and 'HTTP_REFERER' in environ and \ not environ['HTTP_REFERER'].endswith(('.html', '.htm'))): if session: session.finish() session = None try: session = MetricsSession(slug) except IOError: return self.app(environ, start_response) slug_sessions[user_id] = session elif not session: return self.app(environ, start_response) # define function to capture status and headers from response response_headers = [] def metrics_start_response(status, headers, exc_info=None): if is_new_user: headers.append( ('Set-Cookie', '%s=%s; Path=/play/%s/' % (self.cookie_session_name, user_id, slug))) response_headers.append(status) response_headers.append(headers) return start_response(status, headers, exc_info) # pass through request and get response response = self.app(environ, metrics_start_response) status = response_headers[0] file_size = 0 if status.startswith('404'): file_type = 'n/a' else: file_type = None if status.startswith('200'): for k, v in response_headers[1]: if k == 'Content-Length': file_size = v if file_type: break elif k == 'Content-Type': file_type = v if file_size: break else: for k, v in response_headers[1]: if k == 'Content-Type': file_type = v break if not file_type: file_type = mimetypes.guess_type(file_name)[0] if not file_type: file_type = 'n/a' else: file_type = file_type.split(';')[0].split(',')[0] session.append(file_name, file_size, file_type, status) # send the response back up the WSGI layers return response return self.app(environ, start_response)