diff --git a/ocat.c b/ocat.c index d048b4a..e87cd28 100644 --- a/ocat.c +++ b/ocat.c @@ -9,13 +9,6 @@ #include "util.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 * 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-DD\n"); printf(" YYYY-MM\n"); + printf(" --limit -N last points\n"); printf(" --format json -f output format (default: JSON)\n"); printf(" csv (overrides $OCAT_FORMAT\n"); printf(" geojson\n"); @@ -127,7 +121,7 @@ int main(int argc, char **argv) { char *progname = *argv, *p; 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; JsonNode *json, *obj, *locs; time_t now, s_lo, s_hi; @@ -164,6 +158,7 @@ int main(int argc, char **argv) { "device", required_argument, 0, 'd'}, { "from", required_argument, 0, 'F'}, { "to", required_argument, 0, 'T'}, + { "limit", required_argument, 0, 'N'}, { "format", required_argument, 0, 'f'}, { "storage", required_argument, 0, 'S'}, { "last", no_argument, 0, 'L'}, @@ -173,7 +168,7 @@ int main(int argc, char **argv) }; 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) break; @@ -194,6 +189,9 @@ int main(int argc, char **argv) case 'F': time_from = strdup(optarg); break; + case 'N': + limit = atoi(optarg); + break; case 'S': strcpy(STORAGEDIR, optarg); break; @@ -277,6 +275,11 @@ int main(int argc, char **argv) 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) { fprintf(stderr, "%s: bad time(s) specified\n", progname); return (-2); @@ -345,14 +348,30 @@ int main(int argc, char **argv) * process each and build the JSON `obj' with an array of locations. */ - if ((json = lister(username, device, s_lo, s_hi)) != NULL) { - if ((arr = json_find_member(json, "results")) != NULL) { // get array - json_foreach(f, arr) { - // fprintf(stderr, "%s\n", f->string_); - locations(f->string_, obj, locs, s_lo, s_hi, (otype == RAW) ? 1 : 0); + 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 ((arr = json_find_member(json, "results")) != NULL) { // get array + json_foreach(f, arr) { + // fprintf(stderr, "%s\n", f->string_); + locations(f->string_, obj, locs, s_lo, s_hi, (otype == RAW) ? 1 : 0); + } + } + json_delete(json); } - json_delete(json); } } diff --git a/storage.c b/storage.c index d1317c0..5317b24 100644 --- a/storage.c +++ b/storage.c @@ -396,6 +396,123 @@ JsonNode *lister(char *user, char *device, time_t s_lo, time_t s_hi) 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 * objects at the JSON array `arr`. `obj' is a JSON object which diff --git a/storage.h b/storage.h index 63aa9a3..2bcf38b 100644 --- a/storage.h +++ b/storage.h @@ -4,9 +4,16 @@ #include #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); 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); JsonNode *geo_json(JsonNode *json); JsonNode *kill_datastore(char *username, char *device);