storagenode/console/api/helamount: periods with heldamount data endpoint added
Change-Id: Ie893f56f02c7a76bcfc21c32c10bd1f1d05660e7
This commit is contained in:
parent
49ad90dcd8
commit
2c9afe7f17
@ -185,6 +185,50 @@ func (heldAmount *HeldAmount) HeldbackHistory(w http.ResponseWriter, r *http.Req
|
||||
}
|
||||
}
|
||||
|
||||
// HeldAmountPeriods retrieves all periods in which we have some heldamount data.
|
||||
// Have optional parameter - satelliteID.
|
||||
// If satelliteID specified - will retrieve periods only for concrete satellite.
|
||||
func (heldAmount *HeldAmount) HeldAmountPeriods(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
var err error
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
w.Header().Set(contentType, applicationJSON)
|
||||
|
||||
queryParams := r.URL.Query()
|
||||
|
||||
id := queryParams.Get("id")
|
||||
if id == "" {
|
||||
payStubs, err := heldAmount.service.AllPeriods(ctx)
|
||||
if err != nil {
|
||||
heldAmount.serveJSONError(w, http.StatusInternalServerError, ErrHeldAmountAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(payStubs); err != nil {
|
||||
heldAmount.log.Error("failed to encode json response", zap.Error(ErrHeldAmountAPI.Wrap(err)))
|
||||
return
|
||||
}
|
||||
} else {
|
||||
satelliteID, err := storj.NodeIDFromString(id)
|
||||
if err != nil {
|
||||
heldAmount.serveJSONError(w, http.StatusBadRequest, ErrHeldAmountAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
payStubs, err := heldAmount.service.SatellitePeriods(ctx, satelliteID)
|
||||
if err != nil {
|
||||
heldAmount.serveJSONError(w, http.StatusInternalServerError, ErrHeldAmountAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(payStubs); err != nil {
|
||||
heldAmount.log.Error("failed to encode json response", zap.Error(ErrHeldAmountAPI.Wrap(err)))
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// serveJSONError writes JSON error to response output stream.
|
||||
func (heldAmount *HeldAmount) serveJSONError(w http.ResponseWriter, status int, err error) {
|
||||
w.WriteHeader(status)
|
||||
|
@ -386,6 +386,51 @@ func TestHeldAmountApi(t *testing.T) {
|
||||
|
||||
require.Equal(t, "{\"error\":\"heldAmount console web error: node ID error: checksum error\"}\n", string(body2))
|
||||
})
|
||||
|
||||
t.Run("test Periods", func(t *testing.T) {
|
||||
url := fmt.Sprintf("%s/periods", baseURL)
|
||||
res, err := http.Get(url)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, res)
|
||||
require.Equal(t, http.StatusOK, res.StatusCode)
|
||||
|
||||
var periods []string
|
||||
periods = append(periods, "2020-03", "2020-02")
|
||||
|
||||
expected, err := json.Marshal(periods)
|
||||
require.NoError(t, err)
|
||||
|
||||
defer func() {
|
||||
err = res.Body.Close()
|
||||
require.NoError(t, err)
|
||||
}()
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, string(expected)+"\n", string(body))
|
||||
|
||||
//
|
||||
url = fmt.Sprintf("%s/periods?id=%s", baseURL, paystub2.SatelliteID.String())
|
||||
res2, err := http.Get(url)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, res)
|
||||
require.Equal(t, http.StatusOK, res.StatusCode)
|
||||
|
||||
var periods2 []string
|
||||
periods2 = append(periods2, "2020-03")
|
||||
|
||||
expected2, err := json.Marshal(periods2)
|
||||
require.NoError(t, err)
|
||||
|
||||
defer func() {
|
||||
err = res2.Body.Close()
|
||||
require.NoError(t, err)
|
||||
}()
|
||||
body2, err := ioutil.ReadAll(res2.Body)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, string(expected2)+"\n", string(body2))
|
||||
})
|
||||
},
|
||||
)
|
||||
}
|
||||
|
@ -82,6 +82,7 @@ func NewServer(logger *zap.Logger, assets http.FileSystem, notifications *notifi
|
||||
heldAmountRouter.HandleFunc("/paystubs/{period}", heldAmountController.PayStubMonthly).Methods(http.MethodGet)
|
||||
heldAmountRouter.HandleFunc("/paystubs/{start}/{end}", heldAmountController.PayStubPeriod).Methods(http.MethodGet)
|
||||
heldAmountRouter.HandleFunc("/heldback/{id}", heldAmountController.HeldbackHistory).Methods(http.MethodGet)
|
||||
heldAmountRouter.HandleFunc("/periods", heldAmountController.HeldAmountPeriods).Methods(http.MethodGet)
|
||||
|
||||
if assets != nil {
|
||||
fs := http.FileServer(assets)
|
||||
|
@ -45,8 +45,10 @@ func TestHeldAmountDB(t *testing.T) {
|
||||
Held: 14,
|
||||
Owed: 15,
|
||||
Disposed: 16,
|
||||
Paid: 17,
|
||||
}
|
||||
paystub2 := paystub
|
||||
paystub2.Period = "2020-02"
|
||||
paystub2.Created = paystub.Created.Add(time.Hour * 24 * 30)
|
||||
|
||||
t.Run("Test StorePayStub", func(t *testing.T) {
|
||||
err := heldAmount.StorePayStub(ctx, paystub)
|
||||
@ -127,6 +129,49 @@ func TestHeldAmountDB(t *testing.T) {
|
||||
assert.Equal(t, heldback[0].Held, paystub.Held)
|
||||
assert.Equal(t, heldback[0].Period, paystub.Period)
|
||||
})
|
||||
|
||||
t.Run("Test SatellitePeriods", func(t *testing.T) {
|
||||
periods, err := heldAmount.SatellitePeriods(ctx, paystub.SatelliteID)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, periods)
|
||||
assert.Equal(t, 1, len(periods))
|
||||
assert.Equal(t, paystub.Period, periods[0])
|
||||
|
||||
err = heldAmount.StorePayStub(ctx, paystub2)
|
||||
require.NoError(t, err)
|
||||
|
||||
periods, err = heldAmount.SatellitePeriods(ctx, paystub.SatelliteID)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, periods)
|
||||
assert.Equal(t, 2, len(periods))
|
||||
assert.Equal(t, paystub.Period, periods[0])
|
||||
assert.Equal(t, paystub2.Period, periods[1])
|
||||
})
|
||||
|
||||
t.Run("Test AllPeriods", func(t *testing.T) {
|
||||
periods, err := heldAmount.AllPeriods(ctx)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, periods)
|
||||
assert.Equal(t, 2, len(periods))
|
||||
assert.Equal(t, paystub.Period, periods[0])
|
||||
assert.Equal(t, paystub2.Period, periods[1])
|
||||
|
||||
paystub3 := paystub2
|
||||
paystub3.SatelliteID = storj.NodeID{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
|
||||
paystub3.Period = "2020-03"
|
||||
paystub3.Created = paystub2.Created.Add(time.Hour * 24 * 30)
|
||||
|
||||
err = heldAmount.StorePayStub(ctx, paystub3)
|
||||
require.NoError(t, err)
|
||||
|
||||
periods, err = heldAmount.AllPeriods(ctx)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, periods)
|
||||
assert.Equal(t, 3, len(periods))
|
||||
assert.Equal(t, paystub.Period, periods[0])
|
||||
assert.Equal(t, paystub2.Period, periods[1])
|
||||
assert.Equal(t, paystub3.Period, periods[2])
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -24,6 +24,10 @@ type DB interface {
|
||||
AllPayStubs(ctx context.Context, period string) ([]PayStub, error)
|
||||
// SatellitesHeldbackHistory retrieves heldback history for specific satellite from DB.
|
||||
SatellitesHeldbackHistory(ctx context.Context, satelliteID storj.NodeID) ([]Heldback, error)
|
||||
// SatellitePeriods retrieves all periods for concrete satellite in which we have some heldamount data.
|
||||
SatellitePeriods(ctx context.Context, satelliteID storj.NodeID) ([]string, error)
|
||||
// AllPeriods retrieves all periods in which we have some heldamount data.
|
||||
AllPeriods(ctx context.Context) ([]string, error)
|
||||
}
|
||||
|
||||
// ErrNoPayStubForPeriod represents errors from the heldamount database.
|
||||
|
@ -239,6 +239,20 @@ func (service *Service) AllPayStubsPeriodCached(ctx context.Context, periodStart
|
||||
return payStubs, nil
|
||||
}
|
||||
|
||||
// SatellitePeriods retrieves all periods for concrete satellite in which we have some heldamount data.
|
||||
func (service *Service) SatellitePeriods(ctx context.Context, satelliteID storj.NodeID) (_ []string, err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
return service.db.SatellitePeriods(ctx, satelliteID)
|
||||
}
|
||||
|
||||
// AllPeriods retrieves all periods in which we have some heldamount data.
|
||||
func (service *Service) AllPeriods(ctx context.Context) (_ []string, err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
return service.db.AllPeriods(ctx)
|
||||
}
|
||||
|
||||
// HeldbackPeriod amount of held for specific percent rate period.
|
||||
type HeldbackPeriod struct {
|
||||
PercentageRate int
|
||||
|
@ -252,3 +252,63 @@ func (db *heldamountDB) SatellitesHeldbackHistory(ctx context.Context, id storj.
|
||||
|
||||
return heldback, nil
|
||||
}
|
||||
|
||||
// SatellitePeriods retrieves all periods for concrete satellite in which we have some heldamount data.
|
||||
func (db *heldamountDB) SatellitePeriods(ctx context.Context, satelliteID storj.NodeID) (_ []string, err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
query := `SELECT distinct period FROM paystubs WHERE satellite_id = ? ORDER BY created_at`
|
||||
|
||||
rows, err := db.QueryContext(ctx, query, satelliteID[:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer func() { err = errs.Combine(err, rows.Close()) }()
|
||||
|
||||
var periodList []string
|
||||
for rows.Next() {
|
||||
var period string
|
||||
err := rows.Scan(&period)
|
||||
if err != nil {
|
||||
return nil, ErrHeldAmount.Wrap(err)
|
||||
}
|
||||
|
||||
periodList = append(periodList, period)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, ErrHeldAmount.Wrap(err)
|
||||
}
|
||||
|
||||
return periodList, nil
|
||||
}
|
||||
|
||||
// AllPeriods retrieves all periods in which we have some heldamount data.
|
||||
func (db *heldamountDB) AllPeriods(ctx context.Context) (_ []string, err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
query := `SELECT distinct period FROM paystubs ORDER BY created_at`
|
||||
|
||||
rows, err := db.QueryContext(ctx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer func() { err = errs.Combine(err, rows.Close()) }()
|
||||
|
||||
var periodList []string
|
||||
for rows.Next() {
|
||||
var period string
|
||||
err := rows.Scan(&period)
|
||||
if err != nil {
|
||||
return nil, ErrHeldAmount.Wrap(err)
|
||||
}
|
||||
|
||||
periodList = append(periodList, period)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, ErrHeldAmount.Wrap(err)
|
||||
}
|
||||
|
||||
return periodList, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user