mirror of
https://github.com/owntracks/recorder.git
synced 2024-11-15 09:58:40 -07:00
ocat: NEW feature: print last N (--limit)
This commit is contained in:
parent
89cd0a7d98
commit
e689d2c1e2
37
ocat.c
37
ocat.c
@ -9,13 +9,6 @@
|
|||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
GEOJSON = 0,
|
|
||||||
CSV,
|
|
||||||
JSON,
|
|
||||||
RAW,
|
|
||||||
} output_type;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Print the value in a single JSON node. If string, easy. If number account for
|
* Print the value in a single JSON node. If string, easy. If number account for
|
||||||
* what we call 'integer' types which shouldn't be printed as floats.
|
* what we call 'integer' types which shouldn't be printed as floats.
|
||||||
@ -111,6 +104,7 @@ void usage(char *prog)
|
|||||||
printf(" YYYY-MM-DDTHH\n");
|
printf(" YYYY-MM-DDTHH\n");
|
||||||
printf(" YYYY-MM-DD\n");
|
printf(" YYYY-MM-DD\n");
|
||||||
printf(" YYYY-MM\n");
|
printf(" YYYY-MM\n");
|
||||||
|
printf(" --limit <number> -N last <number> points\n");
|
||||||
printf(" --format json -f output format (default: JSON)\n");
|
printf(" --format json -f output format (default: JSON)\n");
|
||||||
printf(" csv (overrides $OCAT_FORMAT\n");
|
printf(" csv (overrides $OCAT_FORMAT\n");
|
||||||
printf(" geojson\n");
|
printf(" geojson\n");
|
||||||
@ -127,7 +121,7 @@ int main(int argc, char **argv)
|
|||||||
{
|
{
|
||||||
char *progname = *argv, *p;
|
char *progname = *argv, *p;
|
||||||
int c;
|
int c;
|
||||||
int list = 0, killdata = 0, last = 0;
|
int list = 0, killdata = 0, last = 0, limit = 0;
|
||||||
char *username = NULL, *device = NULL, *time_from = NULL, *time_to = NULL;
|
char *username = NULL, *device = NULL, *time_from = NULL, *time_to = NULL;
|
||||||
JsonNode *json, *obj, *locs;
|
JsonNode *json, *obj, *locs;
|
||||||
time_t now, s_lo, s_hi;
|
time_t now, s_lo, s_hi;
|
||||||
@ -164,6 +158,7 @@ int main(int argc, char **argv)
|
|||||||
{ "device", required_argument, 0, 'd'},
|
{ "device", required_argument, 0, 'd'},
|
||||||
{ "from", required_argument, 0, 'F'},
|
{ "from", required_argument, 0, 'F'},
|
||||||
{ "to", required_argument, 0, 'T'},
|
{ "to", required_argument, 0, 'T'},
|
||||||
|
{ "limit", required_argument, 0, 'N'},
|
||||||
{ "format", required_argument, 0, 'f'},
|
{ "format", required_argument, 0, 'f'},
|
||||||
{ "storage", required_argument, 0, 'S'},
|
{ "storage", required_argument, 0, 'S'},
|
||||||
{ "last", no_argument, 0, 'L'},
|
{ "last", no_argument, 0, 'L'},
|
||||||
@ -173,7 +168,7 @@ int main(int argc, char **argv)
|
|||||||
};
|
};
|
||||||
int optindex = 0;
|
int optindex = 0;
|
||||||
|
|
||||||
c = getopt_long(argc, argv, "hlu:d:F:T:f:KLS:", long_options, &optindex);
|
c = getopt_long(argc, argv, "hlu:d:F:T:f:KLS:N:", long_options, &optindex);
|
||||||
if (c == -1)
|
if (c == -1)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -194,6 +189,9 @@ int main(int argc, char **argv)
|
|||||||
case 'F':
|
case 'F':
|
||||||
time_from = strdup(optarg);
|
time_from = strdup(optarg);
|
||||||
break;
|
break;
|
||||||
|
case 'N':
|
||||||
|
limit = atoi(optarg);
|
||||||
|
break;
|
||||||
case 'S':
|
case 'S':
|
||||||
strcpy(STORAGEDIR, optarg);
|
strcpy(STORAGEDIR, optarg);
|
||||||
break;
|
break;
|
||||||
@ -277,6 +275,11 @@ int main(int argc, char **argv)
|
|||||||
return (-2);
|
return (-2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* If no from time specified but limit, set from to this month */
|
||||||
|
if (limit) {
|
||||||
|
time_from = strdup(yyyymm(now));
|
||||||
|
}
|
||||||
|
|
||||||
if (make_times(time_from, &s_lo, time_to, &s_hi) != 1) {
|
if (make_times(time_from, &s_lo, time_to, &s_hi) != 1) {
|
||||||
fprintf(stderr, "%s: bad time(s) specified\n", progname);
|
fprintf(stderr, "%s: bad time(s) specified\n", progname);
|
||||||
return (-2);
|
return (-2);
|
||||||
@ -345,6 +348,21 @@ int main(int argc, char **argv)
|
|||||||
* process each and build the JSON `obj' with an array of locations.
|
* process each and build the JSON `obj' with an array of locations.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
if (limit) {
|
||||||
|
/* get a list of .rec files from lister() without time limits,
|
||||||
|
* process them in reverse order */
|
||||||
|
|
||||||
|
printf("*** LIMIT %d\n", limit);
|
||||||
|
if ((json = lister(username, device, s_lo, s_hi)) != NULL) {
|
||||||
|
if ((arr = json_find_member(json, "results")) != NULL) { // get array
|
||||||
|
json_foreach(f, arr) {
|
||||||
|
reverse_locations(f->string_, obj, locs, s_lo, s_hi, (otype == RAW) ? 1 : 0, limit);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
json_delete(json);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
|
||||||
if ((json = lister(username, device, s_lo, s_hi)) != NULL) {
|
if ((json = lister(username, device, s_lo, s_hi)) != NULL) {
|
||||||
if ((arr = json_find_member(json, "results")) != NULL) { // get array
|
if ((arr = json_find_member(json, "results")) != NULL) { // get array
|
||||||
json_foreach(f, arr) {
|
json_foreach(f, arr) {
|
||||||
@ -355,6 +373,7 @@ int main(int argc, char **argv)
|
|||||||
json_delete(json);
|
json_delete(json);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
json_append_member(obj, "locations", locs);
|
json_append_member(obj, "locations", locs);
|
||||||
|
|
||||||
|
117
storage.c
117
storage.c
@ -396,6 +396,123 @@ JsonNode *lister(char *user, char *device, time_t s_lo, time_t s_hi)
|
|||||||
return (json);
|
return (json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct jparam {
|
||||||
|
JsonNode *obj;
|
||||||
|
JsonNode *locs;
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* line is a single valid '*' line from a .rec file. Turn it into a JSON object.
|
||||||
|
* objectorize it. Is that a word? :)
|
||||||
|
*/
|
||||||
|
|
||||||
|
static JsonNode *line_to_location(char *line)
|
||||||
|
{
|
||||||
|
JsonNode *json, *o, *j;
|
||||||
|
char *ghash;
|
||||||
|
char tstamp[64], *bp;
|
||||||
|
double lat, lon;
|
||||||
|
long tst;
|
||||||
|
|
||||||
|
snprintf(tstamp, 21, "%s", line);
|
||||||
|
|
||||||
|
if ((bp = strchr(line, '{')) == NULL)
|
||||||
|
return (NULL);
|
||||||
|
|
||||||
|
if ((json = json_decode(bp)) == NULL) {
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
o = json_mkobject();
|
||||||
|
|
||||||
|
if (json_copy_to_object(o, json, FALSE) == FALSE) {
|
||||||
|
json_delete(o);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
lat = lon = 0.0;
|
||||||
|
if ((j = json_find_member(o, "lat")) != NULL) {
|
||||||
|
lat = j->number_;
|
||||||
|
}
|
||||||
|
if ((j = json_find_member(o, "lon")) != NULL) {
|
||||||
|
lon = j->number_;
|
||||||
|
}
|
||||||
|
|
||||||
|
ghash = geohash_encode(lat, lon, GEOHASH_PREC);
|
||||||
|
json_append_member(o, "ghash", json_mkstring(ghash));
|
||||||
|
json_append_member(o, "isorcv", json_mkstring(tstamp));
|
||||||
|
|
||||||
|
tst = 0L;
|
||||||
|
if ((j = json_find_member(o, "tst")) != NULL) {
|
||||||
|
tst = j->number_;
|
||||||
|
}
|
||||||
|
json_append_member(o, "isotst", json_mkstring(isotime(tst)));
|
||||||
|
|
||||||
|
get_geo(o, ghash);
|
||||||
|
|
||||||
|
return (o);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Invoked via tac() and cat(). Verify that line is indeed a location
|
||||||
|
* line from a .rec file. Then objectorize it and add to the locations
|
||||||
|
* JSON array and update the counter in our JSON object.
|
||||||
|
*/
|
||||||
|
|
||||||
|
static int candidate_line(char *line, void *param)
|
||||||
|
{
|
||||||
|
long counter = 0L;
|
||||||
|
JsonNode *j, *obj, *locs, *o;
|
||||||
|
struct jparam *jarg = (struct jparam*)param;
|
||||||
|
char *bp;
|
||||||
|
|
||||||
|
obj = jarg->obj;
|
||||||
|
locs = jarg->locs;
|
||||||
|
|
||||||
|
if (obj == NULL || obj->tag != JSON_OBJECT)
|
||||||
|
return (-1);
|
||||||
|
|
||||||
|
/* Do we have candidate lines? */
|
||||||
|
if ((bp = strstr(line, "Z\t* ")) == NULL) { /* Not a location line */
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((bp = strrchr(bp, '\t')) == NULL) {
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Initialize our counter to what the JSON obj currently has */
|
||||||
|
if ((j = json_find_member(obj, "count")) != NULL) {
|
||||||
|
counter = j->number_;
|
||||||
|
json_delete(j);
|
||||||
|
}
|
||||||
|
|
||||||
|
// fprintf(stderr, "-->[%s]\n", line);
|
||||||
|
|
||||||
|
if ((o = line_to_location(line)) != NULL) {
|
||||||
|
json_append_element(locs, o);
|
||||||
|
++counter;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Add the (possibly) incremented counter back into `obj' */
|
||||||
|
json_append_member(obj, "count", json_mknumber(counter));
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void reverse_locations(char *filename, JsonNode *obj, JsonNode *arr, time_t s_lo, time_t s_hi, int rawmode, int limit)
|
||||||
|
{
|
||||||
|
int rc;
|
||||||
|
struct jparam jarg;
|
||||||
|
|
||||||
|
if (obj == NULL || obj->tag != JSON_OBJECT)
|
||||||
|
return;
|
||||||
|
|
||||||
|
jarg.obj = obj;
|
||||||
|
jarg.locs = arr;
|
||||||
|
|
||||||
|
rc = tac(filename, limit, candidate_line, &jarg);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Read the file at `filename' (- is stdin) and store location
|
* Read the file at `filename' (- is stdin) and store location
|
||||||
* objects at the JSON array `arr`. `obj' is a JSON object which
|
* objects at the JSON array `arr`. `obj' is a JSON object which
|
||||||
|
@ -4,9 +4,16 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include "json.h"
|
#include "json.h"
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GEOJSON = 0,
|
||||||
|
CSV,
|
||||||
|
JSON,
|
||||||
|
RAW,
|
||||||
|
} output_type;
|
||||||
|
|
||||||
JsonNode *lister(char *username, char *device, time_t s_lo, time_t s_hi);
|
JsonNode *lister(char *username, char *device, time_t s_lo, time_t s_hi);
|
||||||
void locations(char *filename, JsonNode *obj, JsonNode *arr, time_t s_lo, time_t s_hi, int rawmode);
|
void locations(char *filename, JsonNode *obj, JsonNode *arr, time_t s_lo, time_t s_hi, int rawmode);
|
||||||
|
void reverse_locations(char *filename, JsonNode *obj, JsonNode *arr, time_t s_lo, time_t s_hi, int rawmode, int limit);
|
||||||
int make_times(char *time_from, time_t *s_lo, char *time_to, time_t *s_to);
|
int make_times(char *time_from, time_t *s_lo, char *time_to, time_t *s_to);
|
||||||
JsonNode *geo_json(JsonNode *json);
|
JsonNode *geo_json(JsonNode *json);
|
||||||
JsonNode *kill_datastore(char *username, char *device);
|
JsonNode *kill_datastore(char *username, char *device);
|
||||||
|
Loading…
Reference in New Issue
Block a user