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
|
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <zip.h>
#define STB_DS_IMPLEMENTATION
#include "stb_ds.h"
struct event {
int64_t t;
double v;
};
struct metric {
char *key;
struct event *value; // array
};
struct metric *metrics;
int
timecmp(const void *a, const void *b)
{
struct event *ea = (struct event *)a;
struct event *eb = (struct event *)b;
if (ea->t < eb->t)
return -1;
else if (ea->t > eb->t)
return 1;
else
return 0; // needed?
}
static int
mystrcmp(const void *a, const void *b)
{
return strcmp((char *)a, (char *)b);
}
void
normalize(char *name, char *end, char *out)
{
// XXX handle ""
char *brace = strchr(name, '{');
end--;
if (!name || !brace || *end != '}') {
fprintf(stderr, "invalid name: %s\n", name);
return;
}
*end = ',';
printf("LINE %s", name);
char **labels = 0;
for (char *s = brace + 1; s && s < end; s = strchr(s + 1, ',')) {
if (*s == ',')
s++;
if (*s == ',')
continue; /* empty label */
if (s == end)
break;
printf("label: %s\n", s);
arrput(labels, s);
}
qsort(labels, arrlen(labels), sizeof (char *), mystrcmp);
memcpy(out, name, brace - name);
char *t = out + (brace - name);
*t++ = '{';
for (ssize_t i = 0; i < arrlen(labels); i++) {
printf("append %s %d\n", labels[i], end - labels[i]);
char *stop = memccpy(t, labels[i], ',', end - labels[i]);
if (stop) {
printf("copied %d\n", stop - t);
t = stop;
} else {
break;
}
}
*t++ = '}';
*t = 0;
printf("outf=%s\n", out);
}
int
main()
{
sh_new_arena(metrics);
char name[255];
char lastname[255];
char *line = 0;
size_t linebuflen = 0;
ssize_t linelen = 0;
int i = 0;
while ((linelen = getdelim(&line, &linebuflen, '\n', stdin)) >= 0) {
char *start = strchr(line, ' ');
if (!start) {
fprintf(stderr, "skipping invalid line: %s\n", line);
continue;
}
char *start2 = strchr(start+1, ' ');
if (!start2) {
fprintf(stderr, "skipping invalid line: %s\n", line);
continue;
}
struct event ev;
char *eend;
ev.v = strtod(start, &eend);
ev.t = strtoll(eend+1, 0, 10);
if (start - line > (ssize_t)sizeof name) {
fprintf(stderr, "metric too long: %s\n", line);
continue;
}
memcpy(name, line, start-line);
name[start-line] = 0;
// XXX normalize(line, start, name);
// ts = ts / 1000; // XXX
if (strcmp(name, lastname) == 0) {
// same name, skip hash lookup, use old i
} else {
if (shgeti(metrics, name) < 0)
shput(metrics, name, 0);
i = shgeti(metrics, name);
}
arrput(metrics[i].value, ev);
strcpy(lastname, name);
}
// XXX output order of metrics?
for (int m = 0; m < shlen(metrics); m++) {
// ensure events are ordered by time
qsort(metrics[m].value, arrlen(metrics[m].value),
sizeof (struct event), timecmp);
int64_t t, pt = INT64_MIN;
double v;
for (ssize_t i = 0; i < arrlen(metrics[m].value); i++) {
t = metrics[m].value[i].t;
v = metrics[m].value[i].v;
if (t == pt) {
fprintf(stderr, "duplicate timestamp: %ld", t);
continue;
}
printf("%s %f %ld\n", metrics[m].key, v, t);
pt = t;
}
}
}
|