-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathimport.cpp
225 lines (192 loc) · 7.38 KB
/
import.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// C++
#include <iostream>
#include <string>
#include <vector>
// C
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
// Local
#include "Arglist.h"
#include "Binrec.h"
#include "Channel.h"
#include "DataSample.h"
#include "FilesystemKVS.h"
#include "ImportBT.h"
#include "ImportJson.h"
#include "Log.h"
#include "utils.h"
void usage(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
std::string msg = string_vprintf(fmt, args);
va_end(args);
std::cerr << msg << "\n";
std::cerr << "Usage:\n";
std::cerr << "import store.kvs uid device-nickname [--format format] file1.bt ... fileN.bt\n";
std::cerr << "allows formats: bt json\n";
throw std::runtime_error("Bad arguments: " + msg);
}
void emit_json(Json::Value &json) {
std::string response = rtrim(Json::FastWriter().write(json));
printf("%s\n", response.c_str());
log_f("import: sending: %s", response.c_str());
}
int execute(Arglist args) {
long long begin_time = millitime();
std::string invocation = args.to_string();
std::string format = "";
verbose = true;
std::string storename = "";
int uid = 0;
std::string dev_nickname = "";
std::vector<std::string> files;
while (!args.empty()) {
std::string arg = args.shift();
if (arg == "--format") {
format = args.shift();
} else if (arg =="--verbose") {
verbose = true;
} else if (Arglist::is_flag(arg)) {
usage("Unrecognized flag '%s'", arg.c_str());
} else if (storename == "") {
storename = arg;
} else if (uid == 0) {
uid = Arglist::parse_int(arg);
} else if (dev_nickname == "") {
dev_nickname = arg;
} else {
files.push_back(arg);
}
}
if (storename == "") usage("No store specified");
if (uid <= 0) usage("UID must be positive integer");
if (dev_nickname == "") usage("No device-nickname specified");
if (!files.size()) usage("No files to import");
FilesystemKVS store(storename.c_str());
bool write_partial_on_errors = true;
bool backup_imported_files = false;
for (unsigned i = 0; i < files.size(); i++) {
std::string filename = files[i];
log_f("import: Importing %s into %d %s", filename.c_str(), uid, dev_nickname.c_str());
if (backup_imported_files) {
std::string backup_name = "/var/log/bodytrack/imports/" + filename_sans_directory(filename);
log_f("import: backing up %s to %s", filename.c_str(), backup_name.c_str());
int ret = system(string_printf("rsync '%s' '%s'", filename.c_str(), backup_name.c_str()).c_str());
if (ret != 0) log_f("rsync returns non-zero status %d", ret);
}
ParseInfo info;
std::map<std::string, simple_shared_ptr<std::vector<DataSample<double> > > > numeric_data;
std::map<std::string, simple_shared_ptr<std::vector<DataSample<std::string> > > > string_data;
std::vector<ParseError> errors;
std::string this_format;
if (format != "") {
this_format = format;
} else {
this_format = filename_suffix(filename);
}
if (!strcasecmp(this_format.c_str(), "bt") || !strcasecmp(this_format.c_str(), "binary")) {
parse_bt_file(filename, numeric_data, errors, info);
} else if (!strcasecmp(this_format.c_str(), "json")) {
parse_json_file(filename, numeric_data, string_data, errors, info);
} else {
throw std::runtime_error(string_printf("Unrecognized format or filename suffix '%s'", this_format.c_str()));
}
if (errors.size()) {
log_f("import: Parse errors:");
for (unsigned i = 0; i < errors.size(); i++) {
log_f("import: %s", errors[i].what());
}
if (!numeric_data.size() && !string_data.size()) {
log_f("import: No data returned");
} else if (!write_partial_on_errors) {
log_f("import: Partial data returned, but not adding to store");
} else {
log_f("import: Partial data returned; adding to store");
}
}
std::map<std::string, DataRanges> import_ranges;
std::map<std::string, DataRanges> channel_ranges;
Range import_time_range;
for (std::map<std::string, simple_shared_ptr<std::vector<DataSample<double> > > >::iterator i =
numeric_data.begin(); i != numeric_data.end(); ++i) {
std::string channel_name = i->first;
simple_shared_ptr<std::vector<DataSample<double> > > samples = i->second;
std::sort(samples->begin(), samples->end(), DataSample<double>::time_lessthan);
log_f("import: %.6f: %s %zd numeric samples", (*samples)[0].time, channel_name.c_str(), samples->size());
Channel ch(store, uid, dev_nickname + "." + channel_name);
{
DataRanges cr;
ch.add_data(*samples, &cr);
if (!cr.times.empty()) channel_ranges[channel_name].add(cr);
}
{
DataRanges ir;
for (unsigned i = 0; i < samples->size(); i++) {
DataSample<double> &sample = (*samples)[i];
ir.times.add(sample.time);
ir.double_samples.add(sample.value);
}
import_ranges[channel_name].add(ir);
}
}
for (std::map<std::string, simple_shared_ptr<std::vector<DataSample<std::string> > > >::iterator i =
string_data.begin(); i != string_data.end(); ++i) {
std::string channel_name = i->first;
simple_shared_ptr<std::vector<DataSample<std::string> > > samples = i->second;
std::sort(samples->begin(), samples->end(), DataSample<std::string>::time_lessthan);
log_f("%.6f: %s %zd textual samples", (*samples)[0].time, channel_name.c_str(), samples->size());
Channel ch(store, uid, dev_nickname + "." + channel_name);
{
DataRanges cr;
ch.add_data(*samples, &cr);
if (!cr.times.empty()) channel_ranges[channel_name].add(cr);
}
{
DataRanges ir;
for (unsigned i = 0; i < samples->size(); i++) {
DataSample<std::string> &sample = (*samples)[i];
ir.times.add(sample.time);
}
import_ranges[channel_name].add(ir);
}
}
for (std::map<std::string, DataRanges>::iterator i = import_ranges.begin(); i != import_ranges.end(); ++i) {
info.channel_specs[i->first]["imported_bounds"] = i->second.to_json();
import_time_range.add(i->second.times);
}
for (std::map<std::string, DataRanges>::iterator i = channel_ranges.begin(); i != channel_ranges.end(); ++i) {
info.channel_specs[i->first]["channel_bounds"] = i->second.to_json();
}
Json::Value result(Json::objectValue);
result["successful_records"] = Json::Value(info.good_records);
result["failed_records"] = Json::Value(info.bad_records);
if (!import_time_range.empty()) {
result["min_time"] = Json::Value(import_time_range.min);
result["max_time"] = Json::Value(import_time_range.max);
}
result["channel_specs"] = info.channel_specs;
log_f("import: finished in %lld msec", millitime() - begin_time);
bool include_log = false;
if (include_log) result["log"]=Json::Value(recorded_log());
emit_json(result);
}
return 0;
}
int main(int argc, char **argv)
{
int exit_code = 1;
try {
exit_code = execute(Arglist(argv + 1, argv + argc));
} catch (const std::exception &e) {
log_f("import: caught exception '%s'", e.what());
Json::Value json_response(Json::objectValue);
json_response["failure"] = Json::Value(string_printf("exception: %s", e.what()));
emit_json(json_response);
}
return exit_code;
}