Created
July 21, 2022 19:14
-
-
Save gesslar/cb712ecba1bb19cb1966fe524a56ad53 to your computer and use it in GitHub Desktop.
LPC code for graphing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// /adm/daemons/global_graphing.c | |
// Plugin to the globals daemon for graphing ([ string label : int value ]) | |
// can also take an array of strings in the form of: label|value | |
// or a string with lines in the same string form as above | |
// | |
// Created: 2010/09/07: Gesslar | |
// Last Change: 2010/09/07: Gesslar | |
// | |
// 2010/09/07: Gesslar - Created | |
class Bar { | |
string label ; | |
int value ; | |
int percent ; | |
string color ; | |
string value_string ; | |
string percent_string ; | |
string display ; | |
} | |
varargs string graph_lines(mixed data, string delimiter, string sort, int descending, function colorfunc) | |
{ | |
string *lines, mess ; | |
class Bar *bars ; | |
int total, len, label_len, max_percent ; | |
if(pointerp(data)) lines = data; | |
else if(stringp(data)) lines = explode(data, "\n"); | |
else return 0; | |
// Filter out lines which are comments, beginning with # | |
lines = filter(lines, (: $1[0] != '#' :)) ; | |
if(!undefinedp(delimiter)) delimiter = "|"; | |
if(!undefinedp(sort)) sort = "value"; | |
if(!undefinedp(descending)) descending = 1; | |
// Build the initial array based on splitting the info | |
bars = map(lines, function(string line, string delimiter, function func) { | |
class Bar bar ; | |
string label ; | |
int value ; | |
sscanf(line, "%s"+delimiter+"%d", label, value) ; | |
bar = new(class Bar) ; | |
bar->label = label ; | |
bar->value = value ; | |
bar->value_string = add_commas(value) ; | |
if(functionp(func)) bar->color = (*func)(label, value) ; | |
else bar->color = "`<z07>`" ; | |
return bar ; | |
}, delimiter, colorfunc) ; | |
// Find the total of all the values passed | |
total = array_sum(map(bars, (: $1->value :))) ; | |
// Set the percent in each bar | |
bars = map(bars, function(class Bar bar, int total) { | |
bar->percent = percent(bar->value, total) ; | |
bar->percent_string = sprintf("(%d%%)", bar->percent) ; | |
return bar ; | |
}, total) ; | |
// Find the max label length | |
label_len = max(map(bars, (: strlen($1->label) :))) ; | |
// Find the length of label, value_string, and percent_string, so | |
// we know how long to make our bars, | |
len = max(map(bars, function(class Bar bar, int label_len) { | |
return 5 // " | " + 2 spaces | |
+ label_len | |
+ strlen(bar->value_string) | |
+ strlen(bar->percent_string) ; | |
}, label_len)) ; | |
// Find the max percent, this is to maximise the space used for bar | |
// sizes. | |
max_percent = max(map(bars, (: $1->percent :))) ; | |
switch(sort) | |
{ | |
case "value" : | |
if(descending) bars = sort_array(bars, (: $2->value - $1->value :)) ; | |
else bars = sort_array(bars, (: $1->value - $2->value :)) ; | |
break ; | |
case "label" : | |
if(descending) bars = sort_array(bars, (: strcmp($2->label, $1->label) :)) ; | |
else bars = sort_array(bars, (: strcmp($1->label, $2->label) :)) ; | |
break ; | |
} | |
// Set the display member to the printed bar | |
bars = map(bars, function(class Bar bar, int len, int label_len, int max_percent) { | |
int width = 80 - len ; | |
int bar_percent = percent(bar->percent, max_percent) ; | |
int num_dots = percent_of(bar_percent, width) ; | |
string dots = num_dots > 0 ? repeat_string("@", num_dots) : "|" ; | |
bar->display = sprintf("%s | %s%s`<res>` %s %s", | |
sprintf("%"+label_len+"s", bar->label), | |
bar->color, | |
dots, | |
bar->value_string, | |
bar->percent_string | |
) ; | |
return bar ; | |
}, len, label_len, max_percent) ; | |
// Get the printable message | |
mess = implode(map(bars, (: $1->display :)), "\n") ; | |
return mess + "\n" ; | |
} | |
varargs string graph_mapping(mapping data, string sort, int descending, function colorfunc) | |
{ | |
int data_num, sz ; | |
string *labels, *lines; | |
if(!(sz = sizeof(data))) return 0; | |
if(!undefinedp(sort)) sort = "value"; | |
if(!undefinedp(descending)) descending = 1; | |
lines = allocate(sz) ; | |
labels = keys(data); | |
while(sz--) | |
{ | |
lines[sz] = sprintf("%s|%d", labels[sz], data[labels[sz]]) ; | |
} | |
return graph_lines(lines, "|", sort, descending, colorfunc); | |
} |
Author
gesslar
commented
Jul 21, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment