157 result = append(result, oldItems[j:]...) |
158 result = append(result, oldItems[j:]...) |
158 } |
159 } |
159 return result |
160 return result |
160 } |
161 } |
161 |
162 |
|
163 func parseDate(dateStr string) (date time.Time, err error) { |
|
164 if dateStr == "" { |
|
165 return |
|
166 } |
|
167 |
|
168 var yy, mm, dd, h, m, s int |
|
169 n, e := fmt.Sscanf(dateStr, "%d-%d-%d %d:%d:%d", &yy, &mm, &dd, &h, &m, &s) |
|
170 if e != nil && n < 3 { |
|
171 err = e |
|
172 return |
|
173 } |
|
174 if n < 6 { |
|
175 log.Printf("Date parsed with only %d fields\n", n) |
|
176 } |
|
177 date = time.Date(yy, time.Month(mm), dd, h, m, s, 0, time.Local) |
|
178 return |
|
179 } |
|
180 |
162 func main() { |
181 func main() { |
163 inFile := flag.String([]string{"-input-file", "i"}, "", "Input JSON file") |
182 inFile := flag.String([]string{"-input-file", "i"}, "", "Input JSON file") |
164 outFile := flag.String([]string{"-output-file", "o"}, "", "Output JSON file") |
183 outFile := flag.String([]string{"-output-file", "o"}, "", "Output JSON file") |
165 limit := flag.Uint([]string{"-limit", "l"}, 0, "Limit number of items") |
184 limit := flag.Uint([]string{"-limit", "l"}, 0, "Limit number of items to N first") |
|
185 since := flag.String([]string{"-since"}, "", |
|
186 "Filter records from date (YYYY-mm-dd HH:MM:SS)") |
166 format := flag.String([]string{"-format", "f"}, "", "Output format (csv, json)") |
187 format := flag.String([]string{"-format", "f"}, "", "Output format (csv, json)") |
167 avg := flag.Bool([]string{"-average", "a"}, false, "Compute average") |
188 avg := flag.Bool([]string{"-average", "a"}, false, "Compute average") |
168 merge := flag.Bool([]string{"-merge", "m"}, false, |
189 merge := flag.Bool([]string{"-merge", "m"}, false, |
169 "Try to merge input JSON file with fetched data") |
190 "Try to merge input JSON file with fetched data") |
170 device := flag.String([]string{"-device", "d"}, "/dev/ttyUSB0", "Serial device") |
191 device := flag.String([]string{"-device", "d"}, "/dev/ttyUSB0", "Serial device") |
205 } else { |
230 } else { |
206 items = fileItems |
231 items = fileItems |
207 } |
232 } |
208 } |
233 } |
209 |
234 |
|
235 if !startDate.IsZero() { |
|
236 log.Printf("Filtering out records before %v...\n", startDate) |
|
237 for i := range items { |
|
238 iDate := time.Date(items[i].Year, time.Month(items[i].Month), |
|
239 items[i].Day, items[i].Hour, items[i].Minute, 0, 0, |
|
240 time.Local) |
|
241 if iDate.Sub(startDate) < 0 { |
|
242 items = items[0:i] |
|
243 break |
|
244 } |
|
245 } |
|
246 } |
|
247 |
210 if *limit > 0 && len(items) > int(*limit) { |
248 if *limit > 0 && len(items) > int(*limit) { |
211 items = items[0:*limit] |
249 items = items[0:*limit] |
212 } |
250 } |
213 |
251 |
214 var avgMeasure measurement |
252 var avgMeasure measurement |