func (devices) Run(args []string) error { db, err := datastore.NewPostgresDatastore() if err != nil { return err } defer db.Close() params, err := parseDeviceQuery(strings.Join(args, " ")) if err != nil { return err } if len(params.Order) == 0 { params.OrderBy = append(params.OrderBy, datastore.NodeId) params.Order = append(params.Order, datastore.Ascending) } results := db.SelectDevices(params.OrderBy, params.Order, params.Limit, params.NodeLike, params.IpWithin, params.CountryCode, params.VersionEquals, params.StatusEquals) writer := tabwriter.NewWriter(os.Stdout, 0, 8, 2, ' ', 0) defer writer.Flush() fprintWithTabs(writer, "NODE ID", "IP ADDRESS", "COUNTRY", "VERSION", "LAST PROBE", "STATUS", "OUTAGE DURATION") for r := range results { if r.Error != nil { return r.Error } lastSeenText := r.LastSeen.Format("2006-01-02 15:04:05") fprintWithTabs(writer, r.NodeId, r.IpAddress, r.CountryCode, r.Version, lastSeenText, r.DeviceStatus, r.OutageDurationText) } return nil }
func (status) printSummaryTable() error { db, err := datastore.NewPostgresDatastore() if err != nil { return err } defer db.Close() var total, online, stale, offline, offlineHour, offlineDay, offlineWeek, offlineMonth int for r := range db.SelectDevices([]datastore.Identifier{datastore.NodeId}, []datastore.Order{datastore.Ascending}, 0, "", "", "", "", nil) { if r.Error != nil { return r.Error } total++ switch r.DeviceStatus { case datastore.Online: online++ case datastore.Stale: stale++ case datastore.Offline: offline++ } if r.DeviceStatus != datastore.Offline { continue } if r.OutageDuration <= time.Hour { offlineHour++ } if r.OutageDuration <= time.Duration(24)*time.Hour { offlineDay++ } if r.OutageDuration <= time.Duration(24*7)*time.Hour { offlineWeek++ } if r.OutageDuration <= time.Duration(24*30)*time.Hour { offlineMonth++ } } writer := tabwriter.NewWriter(os.Stdout, 0, 8, 2, ' ', 0) defer writer.Flush() fprintWithTabs(writer, "DEVICE STATUS", "COUNT", "PERCENTAGE") fprintWithTabs(writer, "Online", online, percentage(online, total)) fprintWithTabs(writer, "Stale", stale, percentage(stale, total)) fprintWithTabs(writer, "Offline", offline, percentage(offline, total)) fprintWithTabs(writer, " past hour", offlineHour, percentage(offlineHour, total)) fprintWithTabs(writer, " past day", offlineDay, percentage(offlineDay, total)) fprintWithTabs(writer, " past week", offlineWeek, percentage(offlineWeek, total)) fprintWithTabs(writer, " past month", offlineMonth, percentage(offlineMonth, total)) fprintWithTabs(writer, "Total", total, "100%") return nil }
func (countries) Run(args []string) error { db, err := datastore.NewPostgresDatastore() if err != nil { return fmt.Errorf("Error connecting to Postgres database: %s", err) } defer db.Close() writer := tabwriter.NewWriter(os.Stdout, 0, 8, 2, ' ', 0) defer writer.Flush() fprintWithTabs(writer, "COUNTRY", "TOTAL", "ONLINE") for r := range db.SelectCountries() { if r.Error != nil { return r.Error } fprintWithTabs(writer, r.Country, r.Count, r.OnlineCount) } return nil }