mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-18 16:54:25 +08:00
add parsing request id command utility
This commit is contained in:
@@ -0,0 +1,86 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
|
||||||
|
"github.com/mayswind/ezbookkeeping/pkg/requestid"
|
||||||
|
"github.com/mayswind/ezbookkeeping/pkg/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Utilities represents the utilities command
|
||||||
|
var Utilities = &cli.Command{
|
||||||
|
Name: "utility",
|
||||||
|
Usage: "ezBookkeeping utilities",
|
||||||
|
Subcommands: []*cli.Command{
|
||||||
|
{
|
||||||
|
Name: "parse-default-request-id",
|
||||||
|
Usage: "Parse a request id which is generated by default request generator and show the details",
|
||||||
|
Action: parseRequestId,
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "id",
|
||||||
|
Required: true,
|
||||||
|
Usage: "Request ID",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseRequestId(c *cli.Context) error {
|
||||||
|
config, err := initializeSystem(c)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = requestid.InitializeRequestIdGenerator(config)
|
||||||
|
defaultGenerator, err := requestid.NewDefaultRequestIdGenerator(config)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
requestId := c.String("id")
|
||||||
|
requestIdInfo, err := defaultGenerator.ParseRequestIdInfo(requestId)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
newRequestId := defaultGenerator.GenerateRequestId(net.IPv4zero.String())
|
||||||
|
newRequestIdInfo, err := defaultGenerator.ParseRequestIdInfo(newRequestId)
|
||||||
|
printRequestIdInfo(requestId, requestIdInfo, newRequestIdInfo)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func printRequestIdInfo(requestId string, requestIdInfo *requestid.RequestIdInfo, newRequestIdInfo *requestid.RequestIdInfo) {
|
||||||
|
fmt.Printf("[RequestId] %s\n", requestId)
|
||||||
|
fmt.Printf("[ServerUniqId] %d (Current Server %d)\n", requestIdInfo.ServerUniqId, newRequestIdInfo.ServerUniqId)
|
||||||
|
fmt.Printf("[InstanceUniqId] %d (Current Server %d)\n", requestIdInfo.InstanceUniqId, newRequestIdInfo.InstanceUniqId)
|
||||||
|
|
||||||
|
displayTime, err := utils.ParseFromElapsedSeconds(int(requestIdInfo.SecondsElapsedToday))
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
fmt.Printf("[SecondsElapsedToday] %d (%s)\n", requestIdInfo.SecondsElapsedToday, displayTime)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("[SecondsElapsedToday] %d\n", requestIdInfo.SecondsElapsedToday)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("[RandomNumber] %d\n", requestIdInfo.RandomNumber)
|
||||||
|
fmt.Printf("[RequestSeqId] %d\n", requestIdInfo.RequestSeqId)
|
||||||
|
fmt.Printf("[IsClientIpv6] %t\n", requestIdInfo.IsClientIpv6)
|
||||||
|
|
||||||
|
if requestIdInfo.IsClientIpv6 {
|
||||||
|
fmt.Printf("[ClientIpv6Hash] %d\n", requestIdInfo.ClientIp)
|
||||||
|
} else {
|
||||||
|
ip := make(net.IP, 4)
|
||||||
|
binary.BigEndian.PutUint32(ip, requestIdInfo.ClientIp)
|
||||||
|
fmt.Printf("[ClientIpv4] %s\n", ip.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -37,6 +37,7 @@ func main() {
|
|||||||
cmd.Database,
|
cmd.Database,
|
||||||
cmd.UserData,
|
cmd.UserData,
|
||||||
cmd.SecurityUtils,
|
cmd.SecurityUtils,
|
||||||
|
cmd.Utilities,
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
|
|||||||
@@ -73,6 +73,24 @@ func ParseFromShortDateTime(t string, utcOffset int16) (time.Time, error) {
|
|||||||
return time.ParseInLocation(shortDateTimeFormat, t, timezone)
|
return time.ParseInLocation(shortDateTimeFormat, t, timezone)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ParseFromElapsedSeconds(elapsedSeconds int) (string, error) {
|
||||||
|
if elapsedSeconds < 0 || elapsedSeconds >= 86400 {
|
||||||
|
return "", errs.ErrFormatInvalid
|
||||||
|
}
|
||||||
|
|
||||||
|
second := elapsedSeconds % 60
|
||||||
|
elapsedSeconds = elapsedSeconds - second
|
||||||
|
elapsedSeconds = elapsedSeconds / 60
|
||||||
|
|
||||||
|
minute := elapsedSeconds % 60
|
||||||
|
elapsedSeconds = elapsedSeconds - minute
|
||||||
|
elapsedSeconds = elapsedSeconds / 60
|
||||||
|
|
||||||
|
hour := elapsedSeconds
|
||||||
|
|
||||||
|
return fmt.Sprintf("%02d:%02d:%02d", hour, minute, second), nil
|
||||||
|
}
|
||||||
|
|
||||||
// IsUnixTimeEqualsYearAndMonth returns whether year and month of the unix time are equals to the specified year and month
|
// IsUnixTimeEqualsYearAndMonth returns whether year and month of the unix time are equals to the specified year and month
|
||||||
func IsUnixTimeEqualsYearAndMonth(unixTime int64, timezone *time.Location, year int32, month int32) bool {
|
func IsUnixTimeEqualsYearAndMonth(unixTime int64, timezone *time.Location, year int32, month int32) bool {
|
||||||
date := parseFromUnixTime(unixTime).In(timezone)
|
date := parseFromUnixTime(unixTime).In(timezone)
|
||||||
|
|||||||
@@ -80,6 +80,41 @@ func TestParseFromShortDateTime(t *testing.T) {
|
|||||||
assert.Equal(t, expectedValue, actualValue)
|
assert.Equal(t, expectedValue, actualValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParseFromElapsedSeconds(t *testing.T) {
|
||||||
|
expectedValue := "00:00:00"
|
||||||
|
actualValue, err := ParseFromElapsedSeconds(0)
|
||||||
|
assert.Equal(t, nil, err)
|
||||||
|
assert.Equal(t, expectedValue, actualValue)
|
||||||
|
|
||||||
|
expectedValue = "00:00:09"
|
||||||
|
actualValue, err = ParseFromElapsedSeconds(9)
|
||||||
|
assert.Equal(t, nil, err)
|
||||||
|
assert.Equal(t, expectedValue, actualValue)
|
||||||
|
|
||||||
|
expectedValue = "00:01:08"
|
||||||
|
actualValue, err = ParseFromElapsedSeconds(68)
|
||||||
|
assert.Equal(t, nil, err)
|
||||||
|
assert.Equal(t, expectedValue, actualValue)
|
||||||
|
|
||||||
|
expectedValue = "01:00:07"
|
||||||
|
actualValue, err = ParseFromElapsedSeconds(3607)
|
||||||
|
assert.Equal(t, nil, err)
|
||||||
|
assert.Equal(t, expectedValue, actualValue)
|
||||||
|
|
||||||
|
expectedValue = "23:59:59"
|
||||||
|
actualValue, err = ParseFromElapsedSeconds(86399)
|
||||||
|
assert.Equal(t, nil, err)
|
||||||
|
assert.Equal(t, expectedValue, actualValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseFromElapsedSeconds_InvalidTime(t *testing.T) {
|
||||||
|
_, err := ParseFromElapsedSeconds(-1)
|
||||||
|
assert.NotEqual(t, nil, err)
|
||||||
|
|
||||||
|
_, err = ParseFromElapsedSeconds(86400)
|
||||||
|
assert.NotEqual(t, nil, err)
|
||||||
|
}
|
||||||
|
|
||||||
func TestIsUnixTimeEqualsYearAndMonth(t *testing.T) {
|
func TestIsUnixTimeEqualsYearAndMonth(t *testing.T) {
|
||||||
actualValue := IsUnixTimeEqualsYearAndMonth(1691947440, time.UTC, 2023, 8)
|
actualValue := IsUnixTimeEqualsYearAndMonth(1691947440, time.UTC, 2023, 8)
|
||||||
assert.Equal(t, true, actualValue)
|
assert.Equal(t, true, actualValue)
|
||||||
|
|||||||
Reference in New Issue
Block a user