Instance stats: sync with gomif update & add time range support
authorMikael Berthe <mikael@lilotux.net>
Sun, 30 Apr 2017 17:58:06 +0200
changeset 38 891a3c46a62a
parent 37 9bc03db114c3
child 39 3fa876b70df7
Instance stats: sync with gomif update & add time range support
cmd/instance.go
printer/plain.go
--- a/cmd/instance.go	Sun Apr 30 17:07:16 2017 +0200
+++ b/cmd/instance.go	Sun Apr 30 17:58:06 2017 +0200
@@ -13,23 +13,32 @@
 
 	"github.com/m0t0k1ch1/gomif"
 	"github.com/spf13/cobra"
-
-	"github.com/McKael/madonctl/printer"
 )
 
 var instanceOpts struct {
-	stats  bool
-	server string
+	stats      bool
+	server     string
+	start, end int64
 }
 
 // timelinesCmd represents the timelines command
 var instanceCmd = &cobra.Command{
 	Use:   "instance",
 	Short: "Display current instance information",
-	RunE:  instanceRunE,
+	Long: `Display instance information
+
+This command display the instance information returned by the server.
+
+With '--stats', the instances.mastodon.xyz API is queried and instance
+statistics will be returned (the instance server can be specified).
+To get a range of statistics, both '--start' and '--end' should be provided
+with EPOCH timestamps (e.g. "date +%s").
+`,
+	RunE: instanceRunE,
 	Example: `  madonctl instance
   madonctl instance -i mastodon.social
   madonctl instance --stats
+  madonctl instance --stats --start 1493565000 --end 1493566000
   madonctl instance --stats --server mastodon.social --template '{{.Users}}'`,
 }
 
@@ -38,37 +47,15 @@
 
 	instanceCmd.Flags().BoolVar(&instanceOpts.stats, "stats", false, "Display server statistics (from instances.mastodon.xyz)")
 	instanceCmd.Flags().StringVar(&instanceOpts.server, "server", "", "Display statistics for a specific server (for --stats)")
+	instanceCmd.Flags().Int64Var(&instanceOpts.start, "start", 0, "Start timestamp (for --stats)")
+	instanceCmd.Flags().Int64Var(&instanceOpts.end, "end", 0, "End timestamp (for --stats)")
 }
 
 func instanceRunE(cmd *cobra.Command, args []string) error {
 	opt := instanceOpts
 
-	p, err := getPrinter()
-	if err != nil {
-		return err
-	}
-
 	if opt.stats {
-		// Get instance statistics using gomif
-		if opt.server == "" {
-			if err := madonInit(false); err != nil {
-				return err
-			}
-			opt.server = strings.TrimLeft(gClient.InstanceURL, "https://")
-		}
-		is, err := instanceFetchStatus(opt.server)
-		if err != nil {
-			errPrint("Error: %s", err.Error())
-			os.Exit(1)
-		}
-		if is == nil {
-			return nil
-		}
-		istats := &printer.InstanceStatistics{
-			InstanceName:   opt.server,
-			InstanceStatus: *is,
-		}
-		return p.PrintObj(istats, nil, "")
+		return instanceStats()
 	}
 
 	// Get current instance data through the API
@@ -81,19 +68,61 @@
 		return nil
 	}
 
+	p, err := getPrinter()
+	if err != nil {
+		return err
+	}
 	return p.PrintObj(i, nil, "")
 }
 
-func instanceFetchStatus(server string) (*gomif.InstanceStatus, error) {
-	if server == "" {
-		return nil, errors.New("no instance server name")
+func instanceStats() error {
+	opt := instanceOpts
+
+	// Get instance statistics using gomif
+	if opt.server == "" {
+		if err := madonInit(false); err != nil {
+			return err
+		}
+		opt.server = strings.TrimLeft(gClient.InstanceURL, "https://")
+	}
+
+	if opt.server == "" {
+		return errors.New("no instance server name")
 	}
 
 	client := gomif.NewClient()
+	var obj interface{}
+	var err error
 
-	return client.FetchLastInstanceStatus(
-		context.Background(),
-		server,
-		3600, // span (sec)
-	)
+	if opt.start > 0 && opt.end > 0 {
+		var isl []*gomif.InstanceStatus
+		isl, err = client.FetchInstanceStatuses(
+			context.Background(),
+			opt.server, opt.start, opt.end,
+		)
+		obj = isl
+	} else if opt.start > 0 || opt.end > 0 {
+		return errors.New("invalid parameters: missing timestamp")
+	} else {
+		var is *gomif.InstanceStatus
+		is, err = client.FetchLastInstanceStatus(
+			context.Background(),
+			opt.server,
+			3600, // span (sec)
+		)
+		obj = is
+	}
+	if err != nil {
+		errPrint("Error: %s", err.Error())
+		os.Exit(1)
+	}
+	if obj == nil {
+		return nil
+	}
+
+	p, err := getPrinter()
+	if err != nil {
+		return err
+	}
+	return p.PrintObj(obj, nil, "")
 }
--- a/printer/plain.go	Sun Apr 30 17:07:16 2017 +0200
+++ b/printer/plain.go	Sun Apr 30 17:58:06 2017 +0200
@@ -34,12 +34,6 @@
 	return &PlainPrinter{Indent: indentInc}, nil
 }
 
-// InstanceStatistics embeds a gomif.InstanceStatus with an ID in a new type
-type InstanceStatistics struct {
-	InstanceName string `json:"instance_name"`
-	gomif.InstanceStatus
-}
-
 // PrintObj sends the object as text to the writer
 // If the writer w is nil, standard output will be used.
 // For PlainPrinter, the option parameter contains the initial indent.
@@ -51,7 +45,8 @@
 	case []madon.Account, []madon.Attachment, []madon.Card, []madon.Context,
 		[]madon.Instance, []madon.Mention, []madon.Notification,
 		[]madon.Relationship, []madon.Report, []madon.Results,
-		[]madon.Status, []madon.StreamEvent, []madon.Tag:
+		[]madon.Status, []madon.StreamEvent, []madon.Tag,
+		[]*gomif.InstanceStatus:
 		return p.plainForeach(o, w, initialIndent)
 	case *madon.Account:
 		return p.plainPrintAccount(o, w, initialIndent)
@@ -97,9 +92,9 @@
 		return p.plainPrintUserToken(o, w, initialIndent)
 	case madon.UserToken:
 		return p.plainPrintUserToken(&o, w, initialIndent)
-	case *InstanceStatistics:
+	case *gomif.InstanceStatus:
 		return p.plainPrintInstanceStatistics(o, w, initialIndent)
-	case InstanceStatistics:
+	case gomif.InstanceStatus:
 		return p.plainPrintInstanceStatistics(&o, w, initialIndent)
 	}
 	// TODO: Mention
@@ -283,7 +278,10 @@
 	return nil
 }
 
-func (p *PlainPrinter) plainPrintInstanceStatistics(is *InstanceStatistics, w io.Writer, indent string) error {
+func (p *PlainPrinter) plainPrintInstanceStatistics(is *gomif.InstanceStatus, w io.Writer, indent string) error {
+	if is == nil {
+		return nil
+	}
 	indentedPrint(w, indent, true, false, "Instance", "%s", is.InstanceName)
 	indentedPrint(w, indent, false, false, "Users", "%d", is.Users)
 	indentedPrint(w, indent, false, false, "Statuses", "%d", is.Statuses)