Skip to content

Instantly share code, notes, and snippets.

@crashmax-dev
Created August 9, 2018 04:50
Show Gist options
  • Save crashmax-dev/12c420e0bb379ea0f85f458c33161188 to your computer and use it in GitHub Desktop.
Save crashmax-dev/12c420e0bb379ea0f85f458c33161188 to your computer and use it in GitHub Desktop.
Spaces.ru API Debug
var DEBUG_TIME_LEVEL = {
"QUICKLY": 1,
"VERY_QUICKLY": 0,
"VERY_SLOWLY": 4,
"COMPREHENSIBLE": 2,
"SLOWLY": 3
};
var DEBUG_TIME_LEVEL_LC_NAME = {
"4": "very_slowly",
"1": "quickly",
"3": "slowly",
"0": "very_quickly",
"2": "comprehensible"
};
var DEBUG_TIME_LEVEL_TITLE = ["Очень быстро", "Быстро", "Приемлемо", "Долго", "Очень долго"];
var DEBUG_TIME_LEVEL_CLASS = ["very_quickly", "quickly", "comprehensible", "slowly", "very_slowly"];
var DEBUG_TIME_LEVEL_NAMES = ["Очень быстро", "Быстро", "Приемлемо", "Долго", "Очень долго"];
var DEBUG_TIME_LEVEL_SORT = [0, 1, 2, 3, 4];
var debug_db_queries_list = [{
"placeholders": [],
"query": "SELECT * FROM spaces_users WHERE (nid = ?) LIMIT ?",
"slow": 0,
"time": "1.063"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_users_remarks WHERE user_id=? AND active = 1 AND flags & 1 LIMIT 1",
"slow": 0,
"time": "0.853"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_users_anketa WHERE (user_id = ?) LIMIT ?",
"slow": 0,
"time": "1.380"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_pictures WHERE (file_id = ?) LIMIT ?",
"slow": 0,
"time": "0.807"
}, {
"placeholders": [],
"query": "SELECT share_type, share_id, COUNT(*) AS cnt FROM spaces_diary_share_objects WHERE (share_type = ? AND share_id = ? AND user_id = ?) GROUP BY share_type, share_id",
"slow": 0,
"time": "0.600"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_voting_counter WHERE (object_id = ? AND object_type = ?) LIMIT ?",
"slow": 0,
"time": "0.656"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_voting WHERE (object_id = ? AND object_type = ? AND user_id = ?) LIMIT ?",
"slow": 0,
"time": "0.729"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_ghost WHERE (user_id = ?) LIMIT ?",
"slow": 0,
"time": "0.474"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_files_owners WHERE (type = ? AND owner = ? AND owner_type = ?) LIMIT ?",
"slow": 0,
"time": "0.707"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_file_directories WHERE (dir_id = ?) LIMIT ?",
"slow": 0,
"time": "0.908"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_files_owners WHERE (type = ? AND owner = ? AND owner_type = ?) LIMIT ?",
"slow": 0,
"time": "0.946"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_music_directories WHERE (dir_id = ?) LIMIT ?",
"slow": 0,
"time": "0.529"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_files_owners WHERE (type = ? AND owner = ? AND owner_type = ?) LIMIT ?",
"slow": 0,
"time": "0.528"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_pictures_albums WHERE (dir_id = ?) LIMIT ?",
"slow": 0,
"time": "0.551"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_files_owners WHERE (type = ? AND owner = ? AND owner_type = ?) LIMIT ?",
"slow": 0,
"time": "0.507"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_video_directories WHERE (dir_id = ?) LIMIT ?",
"slow": 0,
"time": "0.785"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_users_events WHERE (user_id = ?) LIMIT ?",
"slow": 0,
"time": "0.600"
}, {
"placeholders": [],
"query": "SELECT nid FROM spaces_events WHERE nid > ? AND flags = 0 ORDER BY nid LIMIT 1",
"slow": 0,
"time": "0.402"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_users_external_sites WHERE spaces_user_id = ?",
"slow": 0,
"time": "0.716"
}, {
"placeholders": [],
"query": "SHOW FIELDS FROM spaces_users_external_sites",
"slow": 0,
"time": "1.343"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_lenta_subscribers_counters WHERE user_id IN(22003524)",
"slow": 0,
"time": "0.602"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_friends_recommendations WHERE to_user_id = ? AND status = ?",
"slow": 0,
"time": "0.997"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_users_similarity_settings WHERE (user_id = ?) LIMIT ?",
"slow": 0,
"time": "0.873"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_chest WHERE user_id = ? AND type = ? LIMIT 1",
"slow": 0,
"time": "0.853"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_diary_topics_cnt WHERE (id = ?) LIMIT ?",
"slow": 0,
"time": "0.809"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_gifts WHERE user_id = ? ORDER BY nid DESC LIMIT ?, ?",
"slow": 0,
"time": "0.592"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_gift_items WHERE (nid = ?) OR (nid = ?) OR (nid = ?) LIMIT ?",
"slow": 0,
"time": "1.049"
}, {
"placeholders": [],
"query": "SELECT COUNT(*) FROM spaces_mylenta_userlist WHERE lenta_user_id = ? AND owner_type = ?",
"slow": 0,
"time": "0.936"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_users_comm_counter WHERE (user_id = ?) LIMIT ?",
"slow": 0,
"time": "0.594"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_guestbook_settings WHERE (user_id = ?) LIMIT ?",
"slow": 0,
"time": "0.588"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_user_bookmarks_count WHERE (user_id = ?) LIMIT ?",
"slow": 0,
"time": "0.841"
}, {
"placeholders": [],
"query": "SELECT COUNT(*) FROM spaces_app_users WHERE user_id = ?",
"slow": 0,
"time": "0.563"
}, {
"placeholders": [],
"query": "SELECT COUNT(*) FROM spaces_friends_pending WHERE whom_id = ?",
"slow": 0,
"time": "0.709"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_communities WHERE (com_id = ?) LIMIT ?",
"slow": 0,
"time": "1.151"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_ghost WHERE (user_id = ?) LIMIT ?",
"slow": 0,
"time": "0.929"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_communities WHERE (com_id = ?) LIMIT ?",
"slow": 0,
"time": "0.863"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_ghost WHERE (user_id = ?) LIMIT ?",
"slow": 0,
"time": "0.481"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_users_achievements WHERE (user_id = ? AND achievement_id = ?) LIMIT ?",
"slow": 0,
"time": "0.884"
}, {
"placeholders": [],
"query": "SELECT * FROM spaces_app WHERE (nid = ?) LIMIT ?",
"slow": 0,
"time": "10.884"
}];
var debug_memcached_queries_list = [];
var debug_func_profiling = null || [];
function ShowCheckPointFullStat(point_db_queries_list, point_memcached_queries_list, memcached_full_time) {
var db_queries_info = '';
var db_queries_time = 0;
var db_queries_count = 0;
for (var i = 0; i < point_db_queries_list.length; i++) {
var query = debug_db_queries_list[point_db_queries_list[i]];
var placeholders_str = query["placeholders"].join(", ");
if (placeholders_str) placeholders_str = ' <span class="placeholders">[' + placeholders_str + ']</span>';
db_queries_info += '<div class="query">' + query["query"] + placeholders_str + ' <span class="time"' + (query["slow"] ? ' style="color:red;"' : '') + '"> ' + query["time"] + ' ms</span></div>';
db_queries_time += parseFloat(query["time"]);
db_queries_count++;
}
if (!db_queries_info) db_queries_info = '<div class="query">Запросов нет</div>';
var memcached_queries_info = '';
for (var i = 0; i < point_memcached_queries_list.length; i++) {
var query = debug_memcached_queries_list[point_memcached_queries_list[i]];
if (query) {
var keys_str = query["keys"].join(", ");
memcached_queries_info += '<div class="query"><span style="width:100px; float:left;">' + query["query"] + "</span>" + keys_str + '</div>';
} else {
memcached_queries_info += '<div class="query">Ошибка! Почему-то не пришёл мемкеш-запрос.</div>';
}
}
if (!memcached_queries_info) memcached_queries_info = '<div class="query">Запросов нет</div>';
var html_str = '<div style="text-align:right; padding:5px 5px 10px 5px;"><span style="float:left;"><b>Статистика запросов в базу</b> (Запросов: ' + db_queries_count + '; время: ' + db_queries_time.toFixed(3) + ' ms)</span><a href="#" onclick="CloseCheckPointFullStat(); return false;" style="color:red;">Закрыть</a></div>' + db_queries_info + '<div style="text-align:left; clear:both; padding:20px 5px 10px 5px; border-top:1px solid #EEE;"><b>Статистика запросов в memcached</b> (Запросов: ' + point_memcached_queries_list.length + '; время: ' + memcached_full_time + ' ms)</div>' + memcached_queries_info;
var screen_size = getScreenSize();
var div_info = document.getElementById("check_point_full_info");
var margin = 30;
div_info.style.width = screen_size.width - margin * 2 + "px";
div_info.style.maxHeight = screen_size.height - margin * 2 + "px";
div_info.style.left = margin + "px";
div_info.style.top = margin + "px";
div_info.innerHTML = html_str;
div_info.style.display = "block";
}
function CloseCheckPointFullStat() {
var div_info = document.getElementById("check_point_full_info");
div_info.innerHTML = '';
div_info.style.display = "none";
}
function getAbsolutePosition(element) {
var element_pos = {
x: element.offsetLeft,
y: element.offsetTop
};
if (element.offsetParent) {
var parent_pos = getAbsolutePosition(element.offsetParent);
element_pos.x += parent_pos.x;
element_pos.y += parent_pos.y;
}
return element_pos;
}
function getScreenSize() {
var width;
var height;
if (self.innerWidth) width = self.innerWidth;
else if (document.documentElement && document.documentElement.clientWidth) width = document.documentElement.clientWidth;
else if (document.body) width = document.body.clientWidth;
if (self.innerHeight) height = self.innerHeight;
else if (document.documentElement && document.documentElement.clientHeight) height = document.documentElement.clientHeight;
else if (document.body) height = document.body.clientHeight;
return {
width: width,
height: height
};
}
function PointTimeLevelName(point, level) {
if (!point.title) {
point.title = DEBUG_TIME_LEVEL_TITLE[level];
}
}
function debug_toggle() {
var link = document.getElementById("stat_link");
var functions_stat = document.getElementById("functions_stat");
var check_point_stat = document.getElementById("check_point_stat");
if (link.is_profiler_opened === undefined) link.is_profiler_opened = false;
else link.is_profiler_opened = !link.is_profiler_opened;
if (link.is_profiler_opened) {
link.textContent = 'Контр. точки';
functions_stat.style.display = 'block';
check_point_stat.style.display = 'none';
if (!link.is_debug_inited) {
init_profiler_tree();
print_tree(document.getElementById('functions_stat'), nodes, '');
link.is_debug_inited = true;
}
} else {
link.textContent = 'Функции ' + debug_func_profiling.length;
functions_stat.style.display = 'none';
check_point_stat.style.display = 'block';
}
return false;
}
var T_NODES = 0;
var T_FUNCS = 1;
var T_TIME = 2;
var nodes = [{}, {}, 0];
function init_profiler_tree() {
var name;
for (var i = 0; i < debug_func_profiling.length; ++i) {
var parts = debug_func_profiling[i][0].split('::');
var ref = nodes,
key;
var function_time = parseFloat(debug_func_profiling[i][1]);
var function_speed = parseInt(debug_func_profiling[i][2]);
var function_offset = parseInt(debug_func_profiling[i][3]);
for (var j = 0; j < parts.length; ++j) {
key = parts[j];
ref[T_TIME] += function_time;
if (j == parts.length - 1) {
if (ref[T_FUNCS][key] === undefined) ref[T_FUNCS][key] = [];
ref[T_FUNCS][key].push([parseFloat(debug_func_profiling[i][1]), parseInt(debug_func_profiling[i][2]), parseFloat(debug_func_profiling[i][3]), parseFloat(debug_func_profiling[i][4])]);
} else {
if (ref[T_NODES][key] === undefined) ref[T_NODES][key] = [{}, {}, 0];
ref = ref[T_NODES][key];
}
}
}
delete debug_func_profiling;
}
function print_tree(parent_element, ref, ns) {
var tree_storage = document.createElement('div');
tree_storage.id = 'points-' + ns;
tree_storage.style.display = !ns ? 'block' : 'none';
parent_element.appendChild(tree_storage);
for (var name in ref[T_NODES]) {
var current_ns = (ns ? ns + '::' : '') + name;
var tree_el = document.createElement('div');
tree_el.className = 'tree_element';
tree_el_name_block = document.createElement('div');
tree_el_name_block.className = 'ns';
tree_el_name_block.onclick = trigger_tree_element;
var tree_el_collapse_icon = document.createElement('img');
tree_el_collapse_icon.src = 'https://beta.spac.me/i/debug_plus.png';
tree_el_name_block.appendChild(tree_el_collapse_icon);
var tree_el_name = document.createElement('span');
tree_el_name.textContent = name;
tree_el_name_block.appendChild(tree_el_name);
var tree_el_time = document.createElement('span');
tree_el_time.textContent = ' ' + (Math.round(ref[T_NODES][name][T_TIME] * 10000) / 10000) + ' ms';
tree_el_time.className = 'tree_el_time';
tree_el_name_block.appendChild(tree_el_time);
tree_el_name_block.sp_debug_meta = {
icon: tree_el_collapse_icon,
ref: ref[T_NODES][name],
ns: current_ns
};
tree_el.appendChild(tree_el_name_block);
print_node_timeline(tree_el, ref[T_NODES][name]);
tree_storage.appendChild(tree_el);
if (localStorage.getItem('debug_open:' + current_ns)) tree_el_name_block.onclick(null);
}
for (var name in ref[T_FUNCS]) {
var func_el = document.createElement('div');
func_el.className = 'tree_element function';
var func_name = document.createElement('a');
func_el.appendChild(func_name);
tree_storage.appendChild(func_el);
var total_time = 0;
var total_calls = 0;
var show_timelines = !(!localStorage.getItem('debug_open_func:' + current_ns + '::' + name));
for (var i = 0; i < ref[T_FUNCS][name].length; ++i) {
var func = ref[T_FUNCS][name][i];
total_time += func[0];
if (show_timelines) print_function_timeline(func_el, func);
++total_calls;
}
func_name.href = '#';
func_name.sp_debug_meta = {
ref: ref[T_FUNCS],
ns: current_ns + '::',
name: name
};
func_name.sp_debug_inited = func_name.sp_debug_trigger_open = show_timelines;
func_name.onclick = trigger_function_calls;
func_name.textContent = name + '() [' + total_calls + ']';
var func_time = document.createElement('div');
func_time.className = 'tree_el_time';
func_time.textContent = (Math.round(total_time * 1000) / 1000) + ' ms';
func_name.appendChild(func_time);
print_timeline(func_name, ref[T_FUNCS][name]);
}
}
function print_function_timeline(el, func) {
var func_tm_el = document.createElement('div');
func_tm_el.title = DEBUG_TIME_LEVEL_NAMES[func[1]];
func_tm_el.className = 'function_row';
func_tm_el.style.display = 'block';
var func_time = document.createElement('div');
func_time.style.fontSize = 'small';
func_time.textContent = (Math.round(func[0] * 10000) / 10000) + ' ms';
func_tm_el.appendChild(func_time);
print_timeline(func_tm_el, [func]);
el.appendChild(func_tm_el);
}
function trigger_function_calls(e) {
if (!this.sp_debug_inited) {
for (var i = 0; i < this.sp_debug_meta.ref[this.sp_debug_meta.name].length; ++i) print_function_timeline(this.parentElement, this.sp_debug_meta.ref[this.sp_debug_meta.name][i]);
this.sp_debug_trigger_open = true;
this.sp_debug_inited = true;
} else {
var elements = this.parentElement.getElementsByClassName('function_row');
for (var i = 0; i < elements.length; ++i) {
elements[i].style.display = (!this.sp_debug_trigger_open ? 'block' : 'none');
}
this.sp_debug_trigger_open = !this.sp_debug_trigger_open;
}
if (!this.sp_debug_trigger_open) localStorage.removeItem('debug_open_func:' + this.sp_debug_meta.ns + this.sp_debug_meta.name, 1);
else localStorage.setItem('debug_open_func:' + this.sp_debug_meta.ns + this.sp_debug_meta.name, 1);
return false;
}
function print_node_timeline(el, ref) {
var timeline_bg = document.createElement('div');
timeline_bg.className = 'timeline_bg';
print_node_timeline_pointer(timeline_bg, ref, 0);
el.appendChild(timeline_bg);
}
function print_node_timeline_pointer(el, ref, offset) {
for (var name in ref[T_NODES]) offset = print_node_timeline_pointer(el, ref[T_NODES][name], offset);
for (var name in ref[T_FUNCS]) {
var funcs = ref[T_FUNCS][name];
for (var i = 0; i < funcs.length; ++i) {
if (!funcs[i][3]) continue;
var timeline_pointer = document.createElement('div');
timeline_pointer.className = 'timeline_pointer level_' + DEBUG_TIME_LEVEL_CLASS[funcs[i][1]];
timeline_pointer.style.width = funcs[i][3] + '%';
timeline_pointer.style.top = '-' + offset + 'px';
timeline_pointer.style.left = funcs[i][2] + '%';
el.appendChild(timeline_pointer);
offset += 3;
}
}
return offset;
}
function print_timeline(el, elements) {
var timeline_bg = document.createElement('div');
timeline_bg.className = 'timeline_bg';
el.appendChild(timeline_bg);
var offset = 0;
for (var i in elements) {
var timeline_pointer = document.createElement('div');
timeline_pointer.className = 'timeline_pointer level_' + DEBUG_TIME_LEVEL_CLASS[elements[i][1]];
timeline_pointer.style.width = elements[i][3] + '%';
timeline_pointer.style.top = '-' + offset + 'px';
timeline_pointer.style.left = elements[i][2] + '%';
timeline_bg.appendChild(timeline_pointer);
offset += 3;
}
}
function trigger_tree_element(e) {
if (!this.spaces_debug_tree_inited) {
print_tree(this.parentNode, this.sp_debug_meta.ref, this.sp_debug_meta.ns);
this.spaces_debug_tree_inited = true;
}
var content = document.getElementById('points-' + this.sp_debug_meta.ns);
var icon = document.getElementById('points_icon_' + this.sp_debug_meta.ns);
if (content.style.display == 'block') {
content.style.display = 'none';
this.sp_debug_meta.icon.src = 'https://beta.spac.me/i/debug_plus.png';
localStorage.removeItem('debug_open:' + this.sp_debug_meta.ns);
} else {
content.style.display = 'block';
this.sp_debug_meta.icon.src = 'https://beta.spac.me/i/debug_minus.png';
localStorage.setItem('debug_open:' + this.sp_debug_meta.ns, 1);
}
if (e) {
e.preventDefault();
e.stopPropagation();
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment