github.com/decred/politeia@v1.4.0/politeiawww/cmd/cmswww/codestats.go (about)

     1  // Copyright (c) 2017-2019 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"time"
    10  
    11  	cms "github.com/decred/politeia/politeiawww/api/cms/v1"
    12  	"github.com/decred/politeia/politeiawww/cmd/shared"
    13  )
    14  
    15  // CodeStatsCmd requests a user's information.
    16  type CodeStatsCmd struct {
    17  	Args struct {
    18  		UserID     string `positional-arg-name:"userid" optional:"true"`
    19  		StartMonth uint   `positional-arg-name:"startmonth" optional:"true"`
    20  		StartYear  uint   `positional-arg-name:"startyear" optional:"true"`
    21  		EndMonth   uint   `positional-arg-name:"endmonth" optional:"true"`
    22  		EndYear    uint   `positional-arg-name:"endyear" optional:"true"`
    23  	} `positional-args:"true" optional:"true"`
    24  }
    25  
    26  // Execute executes the cms user information command.
    27  func (cmd *CodeStatsCmd) Execute(args []string) error {
    28  	startDate := int64(0)
    29  	endDate := int64(0)
    30  
    31  	if cmd.Args.StartMonth != 0 {
    32  		if cmd.Args.StartYear == 0 {
    33  			return fmt.Errorf("most supply a start year if giving an start month")
    34  		}
    35  		startDate = time.Date(int(cmd.Args.StartYear),
    36  			time.Month(cmd.Args.StartMonth), 1, 0, 0, 0, 0, time.UTC).Unix()
    37  	}
    38  
    39  	if int(cmd.Args.EndYear) > time.Now().Year() {
    40  		return fmt.Errorf("invalid year")
    41  	}
    42  
    43  	if cmd.Args.EndMonth != 0 {
    44  		if cmd.Args.EndYear == 0 {
    45  			return fmt.Errorf("most supply a end year if giving an end month")
    46  		}
    47  		endDate = time.Date(int(cmd.Args.EndYear),
    48  			time.Month(cmd.Args.EndMonth), 1, 0, 0, 0, 0, time.UTC).Unix()
    49  	} else {
    50  		fmt.Println("no end date provided, just getting the start date month")
    51  		endDate = startDate
    52  	}
    53  
    54  	uid := cmd.Args.UserID
    55  	if uid == "" {
    56  		lr, err := client.Me()
    57  		if err != nil {
    58  			return err
    59  		}
    60  		uid = lr.UserID
    61  	}
    62  
    63  	ucs := cms.UserCodeStats{
    64  		UserID:    uid,
    65  		StartTime: startDate,
    66  		EndTime:   endDate,
    67  	}
    68  
    69  	csr, err := client.CodeStats(ucs)
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	// Print user information reply.
    75  	return shared.PrintJSON(csr)
    76  }
    77  
    78  // codeStatsDetailsHelpMsg is the output of the help command when 'userdetails' is
    79  // specified.
    80  const codeStatsDetailsHelpMsg = `codestats "userid" 
    81  
    82  Fetch code stats by user id. 
    83  
    84  Arguments:
    85  1. userid      (string, required)   User id 
    86  `