Skip to content

Instantly share code, notes, and snippets.

@arestov
Created December 14, 2010 10:31
Show Gist options
  • Save arestov/740247 to your computer and use it in GitHub Desktop.
Save arestov/740247 to your computer and use it in GitHub Desktop.
javascript: var uid, nav = ge("nav");each(nav.childNodes, function (c, b) {
if (b.firstChild != null && b.firstChild.href.match(/mail.php/)) uid = b.firstChild.href.split("?id=")[1]
});
var stats = {},
f = [],
href2name = {},
dates = {},
times = {},
ei = 0,
eo = 0,
index = 0,
finished = false,
p = {
st: 0,
out: 0,
hd: function () {
p.st = 0;
index += p.st;
p.out = 1;
p.hd = function () {
out.debug("finished");
calculation();
finished = true
};
la()
}
};index = p.st;
var out = {
countDiv: null,
timeDiv: null,
init: function () {
ge("content").innerHTML = "";
ge("content").appendChild(ce("img", {
src: "/images/progress7.gif"
}, {
paddingLeft: "240px"
}));
ge("header").innerHTML = "<h1>Статистика личной переписки 2</h1>"
},
loaded: function () {
ge("content").innerHTML = "";
var c = ce("div", {
id: "sprogr"
}, {
position: "relative",
width: "100%",
height: "30px",
margin: "3px",
backgroundColor: "#DAE2E8"
});
c.appendChild(ce("div", {
id: "sprogb"
}, {
width: "0",
height: "inherit",
backgroundColor: "#45688E"
}));
c.appendChild(ce("div", {
id: "scnt"
}, {
position: "absolute",
left: "10px",
top: "7px",
width: "200px",
height: "inherit",
color: "#fff",
zIndex: 69
}));
ge("content").appendChild(c)
},
progress: function () {
ge("sprogb").style.width = 100 * index / (1 * ei + 1 * eo) + "%";
ge("scnt").innerHTML = "Обработано сообщений: " + index
},
debug: function () {},
generateCount: function () {
this.countDiv =
ce("div");
var c = ce("table", {
className: "wikiTable"
});
c.innerHTML += '<thead><th></th><th>Имя</th><th onclick="javascript: calculation();" style="cursor: pointer">Всего сообщений</th><th onclick="javascript: calculation(\'out\');" style="cursor: pointer">Вы отправили</th><th onclick="javascript: calculation(\'in\');" style="cursor: pointer">Вы получили</th></thead>';
var b = ce("tbody");
c.appendChild(b);
for (var d = 0; d < f.length; d++) {
var a = ce("tr"),
e = ce("td", {
innerHTML: d + 1
}),
j = ce("td", {
innerHTML: '<a href="' + f[d].href + '">' + f[d].name + "</a>"
}),
l = ce("td", {
innerHTML: f[d].count
}),
i = ce("td", {
innerHTML: f[d].count_outgoing
}),
g = ce("td", {
innerHTML: f[d].count_incoming
});
a.appendChild(e);
a.appendChild(j);
a.appendChild(l);
a.appendChild(i);
a.appendChild(g);
b.appendChild(a)
}
this.countDiv.appendChild(c);
ge("content").appendChild(this.countDiv)
},
generateTime: function () {
this.timeDiv =
ce("div");
var c = "",
b = 0,
d = "",
a = 0,
e;
for (e in dates) if (dates[e].inb + dates[e].out > a) {
d = e;
a = dates[e].inb + dates[e].out
}
for (e in times) if (times[e].inb + times[e].out > b) {
c = e;
b = times[e].inb + times[e].out
}
this.timeDiv.innerHTML = "Больше всего сообщений было " + d + " &mdash; " + a;
this.timeDiv.innerHTML += "<br/>Больше всего сообщений " + c + " &mdash; " + b;
ge("content").appendChild(this.timeDiv)
},
generate: function () {
ge("content").innerHTML = "Спасибо, что дождались, надеюсь, оно того стоило!<br/><br/>";
this.generateTime();
ge("content").innerHTML += '<a href="#" onclick="javascript: out.exportDT();">Экспорт статистики по времени</a> | <a href="#" onclick="javascript: out.exportMsgs();">Экспорт статистики по сообщениям</a><br/>';
ge("content").appendChild(ce("div", {
id: "export"
}));
ge("content").innerHTML += "<br/>";
out.generateCount()
},
showCount: function () {},
showTime: function () {},
exportDT: function () {
ge("export").innerHTML = "";
var c = ce("textarea", {}, {
width: "100%",
height: "200px"
}),
b = [],
d = [],
a;
for (a in dates) b.push({
date: a,
inb: dates[a].inb,
out: dates[a].out
});
for (a in times) d.push({
time: a,
inb: times[a].inb,
out: times[a].out
});
c.innerHTML = '{"dates":[';
for (a = 0; a < b.length; a++) {
c.innerHTML += '{"d":"' + b[a].date + '","i":' + b[a].inb + ',"o":' + b[a].out + "}";
if (a < b.length - 1) c.innerHTML += ","
}
c.innerHTML += '],"times":[';
for (a = 0; a < d.length; a++) {
c.innerHTML += '{"t":"' + d[a].time + '","i":' + d[a].inb + ',"o":' + d[a].out + "}";
if (a < d.length - 1) c.innerHTML += ","
}
c.innerHTML += "]}";
ge("export").appendChild(c)
},
exportMsgs: function () {
ge("export").innerHTML = "";
var c = ce("textarea", {}, {
width: "100%",
height: "200px"
});
c.innerHTML = "[";
for (var b = 0; b < f.length; b++) {
var d = f[b].href.split("/")[3];
c.innerHTML += '{"h":"' + d + '","n":"' + f[b].name + '","o":' + f[b].count_outgoing + ',"i":' + f[b].count_incoming + "}";
if (b < f.length - 1) c.innerHTML += ","
}
c.innerHTML += "]";
ge("export").appendChild(c)
}
};
function calculation(c) {
if (f.length == 0) for (var b in stats) f.push({
href: b,
name: href2name[b],
count: stats[b].count_out + stats[b].count_in,
count_incoming: stats[b].count_in,
count_outgoing: stats[b].count_out
});
if (c == undefined) f.sort(function (d, a) {
return a.count - d.count
});
else c == "out" ? f.sort(function (d, a) {
return a.count_outgoing - d.count_outgoing
}) : f.sort(function (d, a) {
return a.count_incoming - d.count_incoming
});
out.generate()
}
out.init();
Ajax.Get({
url: "mail.php?id=" + uid,
onDone: function (c, b) {
ei = eval("(" + b + ")").count;
out.debug("end of incoming = " + ei);
Ajax.Get({
url: "mail.php?id=" + uid + "&out=1",
onDone: function (d, a) {
eo = eval("(" + a + ")").count;
out.debug("end of outgoing = " + eo);
out.loaded();
setTimeout("la()", 1E3)
}
})
}
});
function la() {
var c = new Ajax,
b = (new Date).getTime();
c.onDone = function (d, a) {
if (!(d.data.st != p.st || d.data.out != p.out)) {
var e = eval("(" + a + ")").content,
j = ce("div");
j.innerHTML = e;
each(geByClass("name", j, "div"), function (l, i) {
var g = i.children[0].href,
m = i.children[0].innerHTML,
h = i.parentNode.children;
h = h[h.length - 1].innerHTML.split(" \u0432 ");
var k = h[1].split("<br>");
if (dates[h[0]] == undefined) dates[h[0]] = {
inb: 0,
out: 0
};
if (times[k[0]] == undefined) times[k[0]] = {
inb: 0,
out: 0
};
if (href2name[g] == undefined) href2name[g] =
m;
if (stats[g] == undefined) stats[g] = {
count_out: 0,
count_in: 0
};
if (d.data.out == 0) {
stats[g].count_in++;
dates[h[0]].inb++;
times[k[0]].inb++
} else {
stats[g].count_out++;
dates[h[0]].out++;
times[k[0]].out++
}
});
out.debug("[ajax] handled");
index += 20;
out.progress();
e = (new Date).getTime() - b;
p.st += 20;
if (p.st < (p.out == 0 ? ei : eo)) if (e < 1E3) {
setTimeout("la()", 1E3 - e);
out.debug("[timeout] " + (1E3 - e) + "ms")
} else la();
else p.hd()
}
};
out.debug("[ajax] st=" + p.st + ", out=" + p.out);
c.post("mail.php", {
out: p.out,
mid: uid,
st: p.st
})
}
var prev = -1;
function stable() {
if (prev == index && !finished) {
la();
out.debug("[restarted]")
}
prev = index;
finished || setTimeout("stable()", 5E3)
}
stable();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment