Skip to content

Commit

Permalink
Add simple stats table (betaflight#661)
Browse files Browse the repository at this point in the history
* Add simple stats table

* PR fixes
  • Loading branch information
atomgomba authored Nov 12, 2023
1 parent 1483793 commit 9771888
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
21 changes: 21 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,26 @@ <h4>Field values</h4>
</tr>
</tbody>
</table>

<a href="#log-field-values">
<h4>Statistics</h4>
</a>
<table id="stats-table" class="table table-condensed">
<caption>Min/max values from this log</caption>
<thead>
<tr>
<th>&nbsp;</th>
<th>Min</th>
<th>Max</th>
<th>Mean</th>
</tr>
</thead>
<tbody>
<tr>
<!-- auto filled by updateValues function -->
</tr>
</tbody>
</table>
</div>
<div class="configuration-file" id="configuration-file">
<!-- auto filled by configuration function -->
Expand Down Expand Up @@ -3232,6 +3252,7 @@ <h4 class="modal-title">Advanced User Settings</h4>
<script src="js/laptimer.js"></script>
<script src="js/localization.js"></script>
<script src="js/graph_map.js"></script>
<script src="js/simple-stats.js"></script>
<script src="js/main.js"></script>
<script src="index.js"></script>

Expand Down
21 changes: 21 additions & 0 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,27 @@ function BlackboxLogViewer() {

table.append(rows.join(""));

const statRows = [];
const statsTable = $(".log-field-values #stats-table");
$("tr:not(:first)", statsTable).remove();
const stats = SimpleStats(flightLog).calculate();
const tpl = _.template("<tr><td><%= name %></td><td><%= min %> (<%= min_raw %>)</td><td><%= max %> (<%= max_raw %>)</td><td><%= mean %> (<%= mean_raw %>)</td></tr>");
for (const field in stats) {
const stat = stats[field];
if (stat === undefined) {
continue;
}
statRows.push(tpl({
name: fieldPresenter.fieldNameToFriendly(stat.name, flightLog.getSysConfig().debug_mode),
min_raw: atMost2DecPlaces(stat.min),
min: FlightLogFieldPresenter.decodeFieldToFriendly(flightLog, stat.name, stat.min),
max_raw: atMost2DecPlaces(stat.max),
max: FlightLogFieldPresenter.decodeFieldToFriendly(flightLog, stat.name, stat.max),
mean_raw: atMost2DecPlaces(stat.mean),
mean: FlightLogFieldPresenter.decodeFieldToFriendly(flightLog, stat.name, stat.mean),
}));
}
statsTable.append(statRows.join(""));
}

// Update flight mode flags on status bar
Expand Down
42 changes: 42 additions & 0 deletions js/simple-stats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const SimpleStats = function (flightLog) {
const frames = _(flightLog.getChunksInTimeRange(flightLog.getMinTime(), flightLog.getMaxTime()))
.map(chunk => chunk.frames).flatten().value(),
fields = _.map(flightLog.getMainFieldNames(), (f) => {
// fix typo. potential bug in either FW or BBE
if (f === "BaroAlt") { return "baroAlt"; } else { return f; }
});

const getMinMaxMean = (fieldName) => {
const index = _.findIndex(fields, (f) => f === fieldName);
if (index === -1 || !frames.length || !(index in frames[0])) {
return undefined;
}
const result = _.mapValues({
"min": _.minBy(frames, (f) => f[index])[index],
"max": _.maxBy(frames, (f) => f[index])[index],
"mean": _.meanBy(frames, (f) => f[index]),
});
result["name"] = fieldName;
return result;
};

const template = {
"roll": () => getMinMaxMean("rcCommand[0]"),
"pitch": () => getMinMaxMean("rcCommand[1]"),
"yaw": () => getMinMaxMean("rcCommand[2]"),
"throttle": () => getMinMaxMean("rcCommand[3]"),
"vbat": () => getMinMaxMean("vbatLatest"),
"amps": () => getMinMaxMean("amperageLatest"),
"rssi": () => getMinMaxMean("rssi"),
"alt_baro": () => getMinMaxMean("baroAlt"),
"alt_gps": () => getMinMaxMean("GPS_altitude"),
};

function calculate() {
return _.mapValues(template, (f) => f.call());
}

return {
calculate: calculate,
};
};

0 comments on commit 9771888

Please sign in to comment.