Skip to content

Commit c158091

Browse files
committed
Added support for result image themes
1 parent 11346eb commit c158091

File tree

5 files changed

+64
-18
lines changed

5 files changed

+64
-18
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ Compatible with all librespeed clients :
141141
142142
# redact IP addresses
143143
redact_ip_addresses=false
144+
145+
# set telemetry result image theme : light, dark
146+
# default is light
147+
result_image_theme="light"
144148
145149
# database config for : mysql, postgres, sqlite, memory, or disable by write none
146150
# after restarting the service, the in-memory database is reset

configs.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ stats_password=""
2323
# redact IP addresses
2424
redact_ip_addresses=false
2525

26+
# set telemetry result image theme : light, dark
27+
# default is light
28+
result_image_theme="light"
29+
2630
# database config for : mysql, postgres, sqlite, memory, or disable by write none
2731
# after restarting the service, the in-memory database is reset
2832
# if none is specified, no telemetry/stats will be recorded, and no result JPG will be generated

src/cmd.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub struct Cmd {
1111
pub assets_path : Option<String>,
1212
pub stats_password : Option<String>,
1313
pub redact_ip_addresses : Option<bool>,
14+
pub result_image_theme : Option<String>,
1415
pub database_type : Option<String>,
1516
pub database_hostname : Option<String>,
1617
pub database_name : Option<String>,
@@ -89,6 +90,12 @@ impl Cmd {
8990
.help("Redact IP addresses")
9091
.value_parser(value_parser!(bool))
9192
)
93+
.arg(
94+
Arg::new("result-image-theme")
95+
.long("result-image-theme")
96+
.help("Specify telemetry result image theme : light, dark")
97+
.value_parser(value_parser!(String))
98+
)
9299
.arg(
93100
Arg::new("database-type")
94101
.long("database-type")
@@ -153,6 +160,7 @@ impl Cmd {
153160
let assets_path : Option<String> = args.get_one::<String>("assets-path").map(|s| s.to_owned());
154161
let stats_password : Option<String> = args.get_one::<String>("stats-password").map(|s| s.to_owned());
155162
let redact_ip_addresses : Option<bool> = args.get_one::<bool>("redact-ips").map(|s| s.to_owned());
163+
let result_image_theme : Option<String> = args.get_one::<String>("result-image-theme").map(|s| s.to_owned());
156164
let database_type : Option<String> = args.get_one::<String>("database-type").map(|s| s.to_owned());
157165
let database_hostname : Option<String> = args.get_one::<String>("database-hostname").map(|s| s.to_owned());
158166
let database_name : Option<String> = args.get_one::<String>("database-name").map(|s| s.to_owned());
@@ -172,6 +180,7 @@ impl Cmd {
172180
assets_path,
173181
stats_password,
174182
redact_ip_addresses,
183+
result_image_theme,
175184
database_type,
176185
database_hostname,
177186
database_name,

src/config/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub struct ServerConfig {
4444
pub ipinfo_api_key : String,
4545
pub stats_password : String,
4646
pub redact_ip_addresses : bool,
47+
pub result_image_theme : String,
4748
pub assets_path : String,
4849
pub database_type : String,
4950
pub database_hostname : Option<String>,
@@ -66,6 +67,7 @@ impl Default for ServerConfig {
6667
ipinfo_api_key: "".to_string(),
6768
stats_password: "".to_string(),
6869
redact_ip_addresses: false,
70+
result_image_theme: "light".to_string(),
6971
assets_path: "".to_string(),
7072
database_type: "none".to_string(),
7173
database_hostname: None,
@@ -194,6 +196,7 @@ fn initialize (mut config: ServerConfig,cmd : Cmd) -> std::io::Result<()> {
194196
config.assets_path.set_if_some(cmd.assets_path);
195197
config.stats_password.set_if_some(cmd.stats_password);
196198
config.redact_ip_addresses.set_if_some(cmd.redact_ip_addresses);
199+
config.result_image_theme.set_if_some(cmd.result_image_theme);
197200
config.database_type.set_if_some(cmd.database_type);
198201
config.database_hostname.set_if_some(cmd.database_hostname);
199202
config.database_name.set_if_some(cmd.database_name);

src/results/telemetry.rs

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,33 @@ pub async fn record_result (request : &Request, database : &mut Arc<Mutex<dyn Da
6565
}
6666
}
6767

68+
struct ImageTheme {
69+
background : Rgb<u8>,
70+
text_head : Rgb<u8>,
71+
text_value : Rgb<u8>,
72+
text_unit : Rgb<u8>,
73+
}
74+
fn get_theme(is_dark : bool) -> ImageTheme {
75+
if is_dark {
76+
ImageTheme {
77+
background: Rgb([42,42,42]),
78+
text_head: Rgb([255,255,255]),
79+
text_value: Rgb([120,166,240]),
80+
text_unit: Rgb([174,174,174]),
81+
}
82+
} else {
83+
ImageTheme {
84+
background: Rgb([255,255,255]),
85+
text_head: Rgb([0,0,0]),
86+
text_value: Rgb([96,96,170]),
87+
text_unit: Rgb([110,110,110]),
88+
}
89+
}
90+
}
6891
pub fn draw_result (data : &TelemetryData) -> Vec<u8> {
6992

7093
fn cal_text_size (font : &FontRef,text : &str,scale : f32) -> (u32,u32) {
71-
text_size(PxScale::from(scale),&font,text)
94+
text_size(PxScale::from(scale),font,text)
7295
}
7396

7497
//scales
@@ -113,84 +136,87 @@ pub fn draw_result (data : &TelemetryData) -> Vec<u8> {
113136

114137
//drawing ....
115138
//background
116-
draw_filled_rect_mut(&mut img,Rect::at(0,0).of_size(500,286),Rgb([255,255,255]));
139+
let config = SERVER_CONFIG.get().unwrap();
140+
let theme = get_theme(config.result_image_theme == "dark");
141+
142+
draw_filled_rect_mut(&mut img,Rect::at(0,0).of_size(500,286),theme.background);
117143

118144
let width_quarter = img.width() / 4;
119145
let width_3_quarter = width_quarter * 3;
120146

121147
//ping
122148
let mut x = width_quarter - (ping_text_size.0 / 2) + h_padding; // ping label
123149
let mut y = v_padding; // ping label
124-
draw_text_mut(&mut img, Rgb([0, 0, 0]), x as i32, y as i32, PxScale::from(ping_jitter_name_scale), font, l_ping); // ping label
150+
draw_text_mut(&mut img, theme.text_head, x as i32, y as i32, PxScale::from(ping_jitter_name_scale), font, l_ping); // ping label
125151

126152
x = width_quarter - (ping_value_text_size.0 / 2) + h_padding - (ms_text_size.0 / 2); // ping value
127153
y = ping_text_size.1 + (v_padding * 2); // ping value
128-
draw_text_mut(&mut img, Rgb([96, 96, 170]), x as i32, y as i32, PxScale::from(ping_jitter_value_scale), font, &data.ping); // ping value
154+
draw_text_mut(&mut img, theme.text_value, x as i32, y as i32, PxScale::from(ping_jitter_value_scale), font, &data.ping); // ping value
129155

130156
x = width_quarter + (ping_value_text_size.0 / 2) + unit_padding + h_padding - (ms_text_size.0 / 2); // ping unit
131157
y = ping_text_size.1 + (v_padding * 2) + ping_value_text_size.1 - ms_text_size.1; // ping unit
132-
draw_text_mut(&mut img, Rgb([110,110,110]), x as i32, y as i32, PxScale::from(unit_scale), font, l_ms); // ping unit
158+
draw_text_mut(&mut img, theme.text_unit, x as i32, y as i32, PxScale::from(unit_scale), font, l_ms); // ping unit
133159

134160
//jitter
135161
x = width_3_quarter - (jitter_text_size.0 / 2) - h_padding; // jitter label
136162
y = v_padding; // jitter value
137-
draw_text_mut(&mut img, Rgb([0, 0, 0]), x as i32, y as i32, PxScale::from(ping_jitter_name_scale), font, l_jitter); // jitter value
163+
draw_text_mut(&mut img, theme.text_head, x as i32, y as i32, PxScale::from(ping_jitter_name_scale), font, l_jitter); // jitter value
138164

139165
x = width_3_quarter - (jitter_value_text_size.0 / 2) - h_padding - (ms_text_size.0 / 2); // jitter value
140166
y = jitter_text_size.1 + (v_padding * 2); // jitter value
141-
draw_text_mut(&mut img, Rgb([96, 96, 170]), x as i32, y as i32, PxScale::from(ping_jitter_value_scale), font, &data.jitter); // jitter value
167+
draw_text_mut(&mut img, theme.text_value, x as i32, y as i32, PxScale::from(ping_jitter_value_scale), font, &data.jitter); // jitter value
142168

143169
x = width_3_quarter + (jitter_value_text_size.0 / 2) + unit_padding - h_padding - (ms_text_size.0 / 2);// jitter unit
144170
y = jitter_text_size.1 + (v_padding * 2) + jitter_value_text_size.1 - ms_text_size.1;// jitter unit
145-
draw_text_mut(&mut img, Rgb([110,110,110]), x as i32, y as i32, PxScale::from(unit_scale), font, l_ms);// jitter unit
171+
draw_text_mut(&mut img, theme.text_unit, x as i32, y as i32, PxScale::from(unit_scale), font, l_ms);// jitter unit
146172

147173
//download
148174
x = width_quarter - (download_text_size.0 / 2) + h_padding; // download label
149175
y = ping_text_size.1 + ping_value_text_size.1 + (v_padding * 6); // download label
150-
draw_text_mut(&mut img, Rgb([0, 0, 0]), x as i32, y as i32, PxScale::from(d_u_name_scale), font, l_dl); // download label
176+
draw_text_mut(&mut img, theme.text_head, x as i32, y as i32, PxScale::from(d_u_name_scale), font, l_dl); // download label
151177

152178
x = width_quarter - (download_value_text_size.0 / 2) + h_padding;// download value
153179
y = ping_text_size.1 + ping_value_text_size.1 + download_text_size.1 + (v_padding * 7);// download value
154-
draw_text_mut(&mut img, Rgb([96, 96, 170]), x as i32, y as i32, PxScale::from(d_u_value_scale), font, &data.download);// download value
180+
draw_text_mut(&mut img, theme.text_value, x as i32, y as i32, PxScale::from(d_u_value_scale), font, &data.download);// download value
155181

156182
x = width_quarter - (mbps_text_size.0 / 2) + h_padding;//download unit
157183
y = ping_text_size.1 + (unit_padding * 2) + ping_value_text_size.1 + download_text_size.1 + download_value_text_size.1 + (v_padding * 8);//download unit
158-
draw_text_mut(&mut img, Rgb([110,110,110]), x as i32, y as i32, PxScale::from(unit_scale), font, l_mbps);//download unit
184+
draw_text_mut(&mut img, theme.text_unit, x as i32, y as i32, PxScale::from(unit_scale), font, l_mbps);//download unit
159185

160186
//upload
161187
x = width_3_quarter - (upload_text_size.0 / 2) - h_padding; // upload label
162188
y = jitter_text_size.1 + jitter_value_text_size.1 + (v_padding * 6); // upload label
163-
draw_text_mut(&mut img, Rgb([0, 0, 0]), x as i32, y as i32, PxScale::from(d_u_name_scale), font, l_ul); // upload label
189+
draw_text_mut(&mut img, theme.text_head, x as i32, y as i32, PxScale::from(d_u_name_scale), font, l_ul); // upload label
164190

165191
x = width_3_quarter - (upload_value_text_size.0 / 2) - h_padding;// upload value
166192
y = jitter_text_size.1 + jitter_value_text_size.1 + upload_text_size.1 + (v_padding * 7);// upload value
167-
draw_text_mut(&mut img, Rgb([96, 96, 170]), x as i32, y as i32, PxScale::from(d_u_value_scale), font, &data.upload);// upload value
193+
draw_text_mut(&mut img, theme.text_value, x as i32, y as i32, PxScale::from(d_u_value_scale), font, &data.upload);// upload value
168194

169195
x = width_3_quarter - (mbps_text_size.0 / 2) - h_padding;//upload unit
170196
y = jitter_text_size.1 + (unit_padding * 2) + jitter_value_text_size.1 + upload_text_size.1 + upload_value_text_size.1 + (v_padding * 8);//upload unit
171-
draw_text_mut(&mut img, Rgb([110,110,110]), x as i32, y as i32, PxScale::from(unit_scale), font, l_mbps);//upload unit
197+
draw_text_mut(&mut img, theme.text_unit, x as i32, y as i32, PxScale::from(unit_scale), font, l_mbps);//upload unit
172198

173199
//isp_info
174200
x = unit_padding;
175201
y = img.height() - (watermark_text_size.1 * 2) - (unit_padding * 5);
176202
let isp_info : IPInfo = serde_json::from_str(&data.isp_info).unwrap();
177-
draw_text_mut(&mut img,Rgb([40,40,40]),x as i32,y as i32,PxScale::from(footer_scale),font,&isp_info.processedString);
203+
draw_text_mut(&mut img,theme.text_head,x as i32,y as i32,PxScale::from(footer_scale),font,&isp_info.processedString);
178204
drop(isp_info);
179205

180206
//footer divider
181207
let divider_y = (img.height() - watermark_text_size.1 - (unit_padding * 3)) as f32;
182-
draw_line_segment_mut(&mut img, (0.0, divider_y), (500f32, divider_y), Rgb([110,110,110]));
208+
draw_line_segment_mut(&mut img, (0.0, divider_y), (500f32, divider_y), theme.text_unit);
183209

184210
//watermark
185211
x = img.width() - watermark_text_size.0 - unit_padding;
186212
y = img.height() - watermark_text_size.1 - (unit_padding * 2);
187-
draw_text_mut(&mut img,Rgb([110,110,110]),x as i32,y as i32,PxScale::from(footer_scale),font,l_watermark);
213+
draw_text_mut(&mut img,theme.text_unit,x as i32,y as i32,PxScale::from(footer_scale),font,l_watermark);
188214

189215
//time
190216
x = unit_padding;
191217
y = img.height() - watermark_text_size.1 - (unit_padding * 2);
192218
let time = convert_time_local(data.timestamp);
193-
draw_text_mut(&mut img,Rgb([110,110,110]),x as i32,y as i32,PxScale::from(footer_scale),font,&time);
219+
draw_text_mut(&mut img,theme.text_unit,x as i32,y as i32,PxScale::from(footer_scale),font,&time);
194220

195221
let mut buffer: Cursor<Vec<u8>> = Cursor::new(Vec::new());
196222
if let Err(e) = img.write_to(&mut buffer, ImageFormat::Jpeg) {

0 commit comments

Comments
 (0)