1 /* Blerg is (C) 2011 The Dominion of Awesome, and is distributed under a
2 * BSD-style license. Please see the COPYING file for details.
11 #include <yajl/yajl_gen.h>
15 #include "configuration.h"
17 yajl_gen_config yajl_config = {0, 0};
31 struct things * things_alloc() {
32 struct things *things = malloc(sizeof(struct things));
35 things->arr = malloc(sizeof(struct thing) * things->size);
38 void things_free(struct things *things) {
41 for (i = 0; i < things->len; i++) {
42 free(things->arr[i].name);
49 void things_add(struct things *things, time_t t, char *name) {
50 things->arr[things->len].t = t;
51 things->arr[things->len].len = strlen(name);
52 things->arr[things->len].name = malloc(things->arr[things->len].len + 1);
53 strcpy(things->arr[things->len].name, name);
55 if (things->len == things->size) {
57 things->arr = realloc(things->arr, sizeof(struct thing) * things->size);
61 int thing_compare(const void *a, const void *b) {
62 struct thing * aa = (struct thing *) a;
63 struct thing * bb = (struct thing *) b;
67 else if (aa->t > bb->t)
73 void things_sort(struct things *things) {
74 qsort(things->arr, things->len, sizeof(struct thing), thing_compare);
77 struct things * latest_things(const char *path, const char *file, int minlen) {
80 struct things * things = things_alloc();
86 while (f = readdir(d)) {
87 if (f->d_name[0] == '.') continue;
88 if (minlen > 0 && strlen(f->d_name) < minlen) continue;
90 snprintf(filename, 512, "%s/%s/%s", path, f->d_name, file);
92 snprintf(filename, 512, "%s/%s", path, f->d_name);
95 things_add(things, st.st_mtime, f->d_name);
104 void latest_tags(yajl_gen g) {
106 struct things * things = latest_things(blergconf.hash_tags_path, NULL, 3);
108 unsigned int count = (things->len >= 50 ? 50 : things->len);
109 yajl_gen_array_open(g);
110 for (i = 0; i < count; i++) {
111 yajl_gen_string(g, things->arr[i].name, things->arr[i].len);
113 yajl_gen_array_close(g);
118 void latest_records(yajl_gen g) {
120 struct things * things = latest_things(blergconf.data_path, "meta", 0);
122 unsigned int count = (things->len >= 50 ? 50 : things->len);
123 yajl_gen_array_open(g);
124 for (i = 0; i < count; i++) {
125 struct blerg * b = blerg_open(things->arr[i].name);
127 fprintf(stderr, "Could not open blerg for %s\n", things->arr[i].name);
130 uint64_t record_count = blerg_get_record_count(b);
131 if (record_count > 0)
132 json_generate_one_record(g, things->arr[i].name, b, record_count - 1, 1);
135 yajl_gen_array_close(g);
139 int main(int argc, char *argv[]) {
140 const unsigned char *buf;
146 yajl_gen g = yajl_gen_alloc(&yajl_config, NULL);
148 yajl_gen_map_open(g);
149 yajl_gen_string(g, "tags", 4);
153 yajl_gen_get_buf(g, &buf, &len);
154 fwrite(buf, len, 1, stdout);
157 yajl_gen_string(g, "records", 7);
161 yajl_gen_map_close(g);
162 yajl_gen_get_buf(g, &buf, &len);
163 fwrite(buf, len, 1, stdout);