« examples/perl-weekly-challenges/113-2-recreate_binary_tree
↑ Summary
lib/FP/Abstract/Sequence/t.pm »
| 1 | # | ||||||||||
| 2 | # Copyright (c) 2016-2022 Christian Jaeger, copying@christianjaeger.ch | ||||||||||
| 3 | # | ||||||||||
| 4 | # This is free software, offered under either the same terms as perl 5 | ||||||||||
| 5 | # or the terms of the Artistic License version 2 or the terms of the | ||||||||||
| 6 | # MIT License (Expat version). See the file COPYING.md that came | ||||||||||
| 7 | # bundled with this file. | ||||||||||
| 8 | # | ||||||||||
| 9 | |||||||||||
| 10 | # Depends: (sensors) | ||||||||||
| 11 | |||||||||||
| 12 | =head1 NAME | ||||||||||
| 13 | |||||||||||
| 14 | Chj::Linux::LmSensors | ||||||||||
| 15 | |||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||
| 17 | |||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||
| 19 | |||||||||||
| 20 | |||||||||||
| 21 | =head1 SEE ALSO | ||||||||||
| 22 | |||||||||||
| 23 | Implements: L<FP::Struct::Show>, L<FP::Abstract::Pure> | ||||||||||
| 24 | |||||||||||
| 25 | =head1 NOTE | ||||||||||
| 26 | |||||||||||
| 27 | This is alpha software! Read the status section in the package README | ||||||||||
| 28 | or on the L<website|http://functional-perl.org/>. | ||||||||||
| 29 | |||||||||||
| 30 | =cut | ||||||||||
| 31 | |||||||||||
| 32 | package Chj::Linux::LmSensors; | ||||||||||
| 33 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 1 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | 1 | ▶ | use Exporter "import"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | |||||||||||
| 38 | 1 | our @EXPORT = qw(); | |||||||||
| 39 | 1 | our @EXPORT_OK = qw(sensors_get | |||||||||
| 40 | Selector | ||||||||||
| 41 | Value | ||||||||||
| 42 | ValueNA | ||||||||||
| 43 | ValueGroup | ||||||||||
| 44 | Measurement | ||||||||||
| 45 | ); | ||||||||||
| 46 | 1 | our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]); | |||||||||
| 47 | |||||||||||
| 48 | 1 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 49 | 1 | ▶ | use Chj::TEST; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 50 | |||||||||||
| 51 | { | ||||||||||
| 52 | |||||||||||
| 53 | package Chj::Linux::LmSensors::Selector; | ||||||||||
| 54 | 1 | ▶ | use FP::Predicates qw(is_string); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 55 | |||||||||||
| 56 | 1 | ▶ | use FP::Struct [[\&is_string, 'groupname'], [\&is_string, 'sensorname'],], | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 57 | 1 | 'FP::Struct::Show', 'FP::Abstract::Pure'; | |||||||||
| 58 | 1 | _END_ | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | { | ||||||||||
| 62 | |||||||||||
| 63 | 1 | package Chj::Linux::LmSensors::ValueBase; | |||||||||
| 64 | 1 | ▶ | use FP::Struct [ | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 65 | 1 | 'name', # string | |||||||||
| 66 | ], | ||||||||||
| 67 | 1 | 'FP::Struct::Show', 'FP::Abstract::Pure'; | |||||||||
| 68 | 1 | _END_ | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | { | ||||||||||
| 72 | |||||||||||
| 73 | 1 | package Chj::Linux::LmSensors::Value; | |||||||||
| 74 | 1 | ▶ | use FP::Struct [ | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 75 | 1 | 'value', # bare number | |||||||||
| 76 | 'unit', # °C etc. | ||||||||||
| 77 | 'high_crit', # maybe string | ||||||||||
| 78 | ], | ||||||||||
| 79 | 1 | 'Chj::Linux::LmSensors::ValueBase'; | |||||||||
| 80 | |||||||||||
| 81 | 0 | ▶ | sub maybe_value { | ||||||||
|
Subroutine
maybe_value: not called CC=1 SCAR=6.9
| |||||||||||
| 82 | shift->value | ||||||||||
| 83 | 0 | } | |||||||||
| 84 | |||||||||||
| 85 | 1 | ▶ | sub value_or { | ||||||||
|
Subroutine
value_or: called CC=1 SCAR=0.0
| |||||||||||
| 86 | shift->value | ||||||||||
| 87 | 1 | } | |||||||||
| 88 | |||||||||||
| 89 | 1 | _END_ | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | { | ||||||||||
| 93 | |||||||||||
| 94 | 1 | package Chj::Linux::LmSensors::ValueNA; | |||||||||
| 95 | 1 | ▶ | use FP::Struct [], 'Chj::Linux::LmSensors::ValueBase'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 96 | |||||||||||
| 97 | 0 | ▶ | sub maybe_value { | ||||||||
|
Subroutine
maybe_value: not called CC=1 SCAR=6.9
| |||||||||||
| 98 | undef | ||||||||||
| 99 | 0 | } | |||||||||
| 100 | |||||||||||
| 101 | 1 | ▶ | sub value_or { | ||||||||
|
Subroutine
value_or: called CC=1 SCAR=0.0
| |||||||||||
| 102 | 1 | $_[1] | |||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | 1 | _END_ | |||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | { | ||||||||||
| 109 | |||||||||||
| 110 | 1 | package Chj::Linux::LmSensors::ValueGroup; | |||||||||
| 111 | |||||||||||
| 112 | 1 | ▶ | use FP::Struct [ | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 113 | 1 | 'name', # string | |||||||||
| 114 | 'values', # list of ::Value | ||||||||||
| 115 | ], | ||||||||||
| 116 | 1 | 'FP::Struct::Show', 'FP::Abstract::Pure'; | |||||||||
| 117 | 1 | _END_ | |||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | { | ||||||||||
| 121 | |||||||||||
| 122 | 1 | package Chj::Linux::LmSensors::Measurement; | |||||||||
| 123 | |||||||||||
| 124 | 4 | ▶ | sub by { | ||||||||
|
Subroutine
by: called CC=1 SCAR=0.0
| |||||||||||
| 125 | 4 | my ($method, $str) = @_; | |||||||||
| 126 | sub { | ||||||||||
| 127 | my ($v) = @_; | ||||||||||
| 128 | $v->$method eq $str | ||||||||||
| 129 | } | ||||||||||
| 130 | 4 | } | |||||||||
| 131 | |||||||||||
| 132 | 1 | ▶ | use FP::Struct [ | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 133 | 1 | 'time', # unixtime value, ok? | |||||||||
| 134 | 'groups', # list of ::ValueGroup | ||||||||||
| 135 | ], | ||||||||||
| 136 | 1 | 'FP::Struct::Show', 'FP::Abstract::Pure'; | |||||||||
| 137 | |||||||||||
| 138 | # "expecting 1 element, got 0" meaning the field doesn't exist. | ||||||||||
| 139 | 2 | ▶ | sub select { | ||||||||
|
Subroutine
select: called CC=1 SCAR=0.0
| |||||||||||
| 140 | 2 | my ($self, $selector) = @_; | |||||||||
| 141 | 2 | $self->groups->filter(by "name", $selector->groupname) | |||||||||
| 142 | ->xone->values->filter(by "name", $selector->sensorname)->xone | ||||||||||
| 143 | } | ||||||||||
| 144 | |||||||||||
| 145 | 1 | _END_ | |||||||||
| 146 | } | ||||||||||
| 147 | |||||||||||
| 148 | 1 | import Chj::Linux::LmSensors::Selector::constructors; | |||||||||
| 149 | 1 | import Chj::Linux::LmSensors::Value::constructors; | |||||||||
| 150 | 1 | import Chj::Linux::LmSensors::ValueNA::constructors; | |||||||||
| 151 | 1 | import Chj::Linux::LmSensors::ValueGroup::constructors; | |||||||||
| 152 | 1 | import Chj::Linux::LmSensors::Measurement::constructors; | |||||||||
| 153 | |||||||||||
| 154 | 22 | ▶ | sub parse_value { | ||||||||
|
Subroutine
parse_value: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||
| 155 | 22 | my ($s) = @_; | |||||||||
| 156 | 22 | chomp $s; | |||||||||
| 157 | 22 | ▶ | my ($name, $rest1) = $s =~ m{^([\w -]+):[ \t]*(.*)\z}sx | ||||||||
|
Branch: 1 branch
| |||||||||||
| 158 | or die "no name match: '$s'"; | ||||||||||
| 159 | |||||||||||
| 160 | 22 | chomp $rest1; | |||||||||
| 161 | |||||||||||
| 162 | 22 | ▶ | if ($rest1 =~ m{^N/A\s*\z}sx) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 163 | 9 | ValueNA($name) | |||||||||
| 164 | } else { | ||||||||||
| 165 | 13 | ▶ | my ($value, $unit, $rest) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 166 | = $rest1 =~ m{^([+-]?\d+(?:\.\d*)?)\s*([°A-Za-z]\w*)\s*(.*)\z}sx | ||||||||||
| 167 | |||||||||||
| 168 | # wow ewil, space in ' *' would be dropped due to /x of course | ||||||||||
| 169 | or die "no values match: '$rest1'"; | ||||||||||
| 170 | 13 | Value($name, $value, $unit, $rest) | |||||||||
| 171 | } | ||||||||||
| 172 | } | ||||||||||
| 173 | |||||||||||
| 174 | 1 | ▶ | TEST { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 175 | 1 | parse_value 'fan1: 3770 RPM | |||||||||
| 176 | ' | ||||||||||
| 177 | } | ||||||||||
| 178 | 1 | Value('fan1', '3770', 'RPM', ''); | |||||||||
| 179 | |||||||||||
| 180 | 1 | ▶ | use FP::PureArray qw(purearray array_to_purearray); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 181 | |||||||||||
| 182 | 1 | ▶ | sub parse_measurement { | ||||||||
|
Subroutine
parse_measurement: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 183 | 1 | my ($str, $time) = @_; | |||||||||
| 184 | my @groups = map { | ||||||||||
| 185 | 1 | my $s = $_; | |||||||||
| 186 | 3 | ▶ | my ($groupname, $groupvalues) = $s =~ m{^([\w -]+)\n | ||||||||
|
Branch: 1 branch
| |||||||||||
| 187 | (.*) | ||||||||||
| 188 | \z}sx | ||||||||||
| 189 | or die "no group match: '$s'"; | ||||||||||
| 190 | |||||||||||
| 191 | 3 | my @values = map { parse_value $_ } split /\n/, $groupvalues; | |||||||||
| 192 | |||||||||||
| 193 | 3 | ValueGroup($groupname, array_to_purearray \@values) | |||||||||
| 194 | } split /\n\n/, $str; | ||||||||||
| 195 | |||||||||||
| 196 | 1 | Measurement($time, array_to_purearray \@groups) | |||||||||
| 197 | } | ||||||||||
| 198 | |||||||||||
| 199 | 1 | ▶ | use Chj::IO::Command; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 200 | |||||||||||
| 201 | 0 | ▶ | sub sensors_get_string { | ||||||||
|
Subroutine
sensors_get_string: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 202 | 0 | my $p = Chj::IO::Command->new_sender({ LANG => "C" }, "sensors", "-A"); | |||||||||
| 203 | 0 | $p->set_encoding("utf-8"); | |||||||||
| 204 | 0 | my $str = $p->xcontent; | |||||||||
| 205 | 0 | $p->xxfinish; | |||||||||
| 206 | 0 | $str | |||||||||
| 207 | } | ||||||||||
| 208 | |||||||||||
| 209 | 0 | ▶ | sub sensors_get { | ||||||||
|
Subroutine
sensors_get: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 210 | 0 | parse_measurement(sensors_get_string, time) | |||||||||
| 211 | } | ||||||||||
| 212 | |||||||||||
| 213 | 1 | my $s = 'acpitz-virtual-0 | |||||||||
| 214 | temp1: +40.0°C (crit = +127.0°C) | ||||||||||
| 215 | temp2: +38.0°C (crit = +104.0°C) | ||||||||||
| 216 | |||||||||||
| 217 | coretemp-isa-0000 | ||||||||||
| 218 | Core 0: +35.0°C (high = +105.0°C, crit = +105.0°C) | ||||||||||
| 219 | Core 1: +37.0°C (high = +105.0°C, crit = +105.0°C) | ||||||||||
| 220 | |||||||||||
| 221 | thinkpad-isa-0000 | ||||||||||
| 222 | fan1: 3770 RPM | ||||||||||
| 223 | temp1: +40.0°C | ||||||||||
| 224 | temp2: +53.0°C | ||||||||||
| 225 | temp3: N/A | ||||||||||
| 226 | temp4: +44.0°C | ||||||||||
| 227 | temp5: +33.0°C | ||||||||||
| 228 | temp6: N/A | ||||||||||
| 229 | temp7: +32.0°C | ||||||||||
| 230 | temp8: N/A | ||||||||||
| 231 | temp9: +49.0°C | ||||||||||
| 232 | temp10: +39.0°C | ||||||||||
| 233 | temp11: N/A | ||||||||||
| 234 | temp12: N/A | ||||||||||
| 235 | temp13: N/A | ||||||||||
| 236 | temp14: N/A | ||||||||||
| 237 | temp15: N/A | ||||||||||
| 238 | temp16: N/A | ||||||||||
| 239 | |||||||||||
| 240 | '; | ||||||||||
| 241 | |||||||||||
| 242 | 1 | my $m; | |||||||||
| 243 | 1 | ▶ | TEST { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 244 | 1 | $m = parse_measurement($s, 1234567) | |||||||||
| 245 | } | ||||||||||
| 246 | 1 | Measurement( | |||||||||
| 247 | 1234567, | ||||||||||
| 248 | purearray( | ||||||||||
| 249 | ValueGroup( | ||||||||||
| 250 | 'acpitz-virtual-0', | ||||||||||
| 251 | purearray( | ||||||||||
| 252 | Value('temp1', '+40.0', '°C', '(crit = +127.0°C)'), | ||||||||||
| 253 | Value('temp2', '+38.0', '°C', '(crit = +104.0°C)') | ||||||||||
| 254 | ) | ||||||||||
| 255 | ), | ||||||||||
| 256 | ValueGroup( | ||||||||||
| 257 | 'coretemp-isa-0000', | ||||||||||
| 258 | purearray( | ||||||||||
| 259 | Value( | ||||||||||
| 260 | 'Core 0', '+35.0', | ||||||||||
| 261 | '°C', '(high = +105.0°C, crit = +105.0°C)' | ||||||||||
| 262 | ), | ||||||||||
| 263 | Value( | ||||||||||
| 264 | 'Core 1', '+37.0', | ||||||||||
| 265 | '°C', '(high = +105.0°C, crit = +105.0°C)' | ||||||||||
| 266 | ) | ||||||||||
| 267 | ) | ||||||||||
| 268 | ), | ||||||||||
| 269 | ValueGroup( | ||||||||||
| 270 | 'thinkpad-isa-0000', | ||||||||||
| 271 | purearray( | ||||||||||
| 272 | Value('fan1', '3770', 'RPM', ''), | ||||||||||
| 273 | Value('temp1', '+40.0', '°C', ''), | ||||||||||
| 274 | Value('temp2', '+53.0', '°C', ''), | ||||||||||
| 275 | ValueNA('temp3'), | ||||||||||
| 276 | Value('temp4', '+44.0', '°C', ''), | ||||||||||
| 277 | Value('temp5', '+33.0', '°C', ''), | ||||||||||
| 278 | ValueNA('temp6'), | ||||||||||
| 279 | Value('temp7', '+32.0', '°C', ''), | ||||||||||
| 280 | ValueNA('temp8'), | ||||||||||
| 281 | Value('temp9', '+49.0', '°C', ''), | ||||||||||
| 282 | Value('temp10', '+39.0', '°C', ''), | ||||||||||
| 283 | ValueNA('temp11'), | ||||||||||
| 284 | ValueNA('temp12'), | ||||||||||
| 285 | ValueNA('temp13'), | ||||||||||
| 286 | ValueNA('temp14'), | ||||||||||
| 287 | ValueNA('temp15'), | ||||||||||
| 288 | ValueNA('temp16') | ||||||||||
| 289 | ) | ||||||||||
| 290 | ) | ||||||||||
| 291 | ) | ||||||||||
| 292 | ); | ||||||||||
| 293 | |||||||||||
| 294 | 1 | ▶ | TEST { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 295 | purearray( | ||||||||||
| 296 | Selector('coretemp-isa-0000', 'Core 1'), | ||||||||||
| 297 | Selector('thinkpad-isa-0000', 'temp14') | ||||||||||
| 298 | )->map( | ||||||||||
| 299 | 2 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 300 | 2 | my ($sel) = @_; | |||||||||
| 301 | 2 | $m->select($sel)->value_or("no val") | |||||||||
| 302 | } | ||||||||||
| 303 | ) | ||||||||||
| 304 | 1 | } | |||||||||
| 305 | 1 | purearray('+37.0', "no val"); | |||||||||
| 306 | |||||||||||
| 307 | 1 | 1 | |||||||||