Skip to content

Commit

Permalink
parse data up front
Browse files Browse the repository at this point in the history
  • Loading branch information
DeltaDizzy committed Jan 8, 2025
1 parent 451042b commit 07859a8
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 29 deletions.
2 changes: 1 addition & 1 deletion sawmill/src/main/native/cpp/DataLogJSONWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <wpi/json.h>

void ExportJSON(fs::path outputPath,
std::vector<wpi::log::DataLogRecord> records) {
std::vector<sawmill::DataLogRecord> records) {
// JSON structure
// List of blocks
// Each block is a direct transcription of a record
Expand Down
47 changes: 34 additions & 13 deletions sawmill/src/main/native/cpp/LogLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ LogLoader::LogLoader() {}

LogLoader::~LogLoader() = default;

void LogLoader::Load(std::string_view log_path) {
void LogLoader::Load(fs::path logPath) {
// Handle opening the file
std::error_code ec;
auto buf = wpi::MemoryBuffer::GetFile(log_path);
auto buf = wpi::MemoryBuffer::GetFile(logPath.string());
if (ec) {
m_error = fmt::format("Could not open file: {}", ec.message());
return;
Expand All @@ -40,7 +40,6 @@ void LogLoader::Load(std::string_view log_path) {
}
unload(); // release the actual file, we have the data in the reader now
m_reader = std::make_unique<wpi::DataLogReaderThread>(std::move(reader));
m_entryTree.clear();

// Handle Errors
fmt::println("{}", m_error);
Expand All @@ -58,9 +57,11 @@ void LogLoader::Load(std::string_view log_path) {
if (!m_reader->IsDone()) {
return;
}


}

std::vector<wpi::log::DataLogRecord> LogLoader::GetRecords(
/*std::vector<wpi::log::DataLogRecord> LogLoader::GetRecords(
std::string_view field_name) {
std::vector<wpi::log::DataLogRecord> record_list{};
Expand All @@ -73,15 +74,35 @@ std::vector<wpi::log::DataLogRecord> LogLoader::GetRecords(
}
return record_list;
}

std::vector<wpi::log::DataLogRecord> sawmill::LogLoader::GetAllRecords() {
std::vector<wpi::log::DataLogRecord> record_list{};

for (wpi::log::DataLogRecord record : m_reader->GetReader())
{
record_list.push_back(record);
}*/

std::vector<sawmill::DataLogRecord> sawmill::LogLoader::GetAllRecords() {
if (records.size() == 0) {
std::map<int, wpi::log::StartRecordData, std::less<>> dataMap;
// get all records
for (wpi::log::DataLogRecord record : m_reader->GetReader())
{
if (record.IsStart()) {
wpi::log::StartRecordData data;
if (record.GetStartData(&data)) {
// associate an entry id with a StartRecordData
dataMap[data.entry] = data;
}
} else if (record.IsFinish()) {
// remove the association
int entryId;
if (record.GetFinishEntry(&entryId))
{
dataMap.erase(entryId);
}
}
int entryId = record.GetEntry();
if (dataMap.contains(entryId))
{
records.push_back(sawmill::DataLogRecord{dataMap[entryId], record});
}
}
}

return record_list;
return records;
}
7 changes: 5 additions & 2 deletions sawmill/src/main/native/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ void export_json(fs::path log_path, fs::path output_path) {
writer.ExportJSON(output_path, records);
}

void export_csv(fs::path log_path, fs::path output_path) {
void export_csv(fs::path logPath, fs::path outputPaths) {
sawmill::LogLoader loader{};
loader.Load(logPath);
//

}

Expand Down Expand Up @@ -140,7 +143,7 @@ int main(int argc, char* argv[]) {
return 1;
}

export_csv(logPath, outputPath);
//export_csv(logPath, outputPath);
} else if (extract_entry_command) {
// validate paths
fs::path logPath{extract_entry_command.get("--log-file")};
Expand Down
14 changes: 14 additions & 0 deletions sawmill/src/main/native/include/DataLogExport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

#pragma once

#include <wpi/DataLogReaderThread.h>

namespace sawmill {
struct DataLogRecord {
const wpi::log::StartRecordData entryData;
wpi::log::DataLogRecord record;
};
}
4 changes: 3 additions & 1 deletion sawmill/src/main/native/include/DataLogJSONWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#pragma once

#include "DataLogExport.h"

#include <string>
#include <string_view>
#include <vector>
Expand All @@ -18,7 +20,7 @@ namespace sawmill {
class DataLogJSONWriter {
public:
void ExportJSON(fs::path exportPath,
std::vector<wpi::log::DataLogRecord> records);
std::vector<sawmill::DataLogRecord> records);

private:
};
Expand Down
17 changes: 5 additions & 12 deletions sawmill/src/main/native/include/LogLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#pragma once

#include "DataLogExport.h"

#include <memory>
#include <string>
#include <string_view>
Expand Down Expand Up @@ -41,11 +43,9 @@ class LogLoader {
*/
wpi::sig::Signal<> unload;

void Load(std::string_view log_path);

std::vector<wpi::log::DataLogRecord> GetRecords(std::string_view field_name);
void Load(fs::path logPath);

std::vector<wpi::log::DataLogRecord> GetAllRecords();
std::vector<sawmill::DataLogRecord> GetAllRecords();

private:
std::string m_filename;
Expand All @@ -57,13 +57,6 @@ class LogLoader {

wpi::log::StartRecordData* entryData;

struct EntryTreeNode {
explicit EntryTreeNode(std::string_view name) : name{name} {}
std::string name; // name of just this node
std::string path; // full path if entry is nullptr
const wpi::DataLogReaderEntry* entry = nullptr;
std::vector<EntryTreeNode> children; // children, sorted by name
};
std::vector<EntryTreeNode> m_entryTree;
std::vector<sawmill::DataLogRecord> records;
};
} // namespace sawmill

0 comments on commit 07859a8

Please sign in to comment.