Skip to content

Commit

Permalink
feat(Recorder): add DELETE HTTP API (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwkwc authored Jun 2, 2024
1 parent fd73430 commit 377a8e2
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ records, _ := rec.GetRecords(job.Id)
|---------------|-------------|---------------------------|
| GetRecords | GET | /recorder/records/:job_id |
| GetAllRecords | GET | /recorder/records |
| DeleteRecords | DELETE | /recorder/records/:job_id |
| DeleteAllRecords | DELETE | /recorder/records |

## Examples

Expand Down
2 changes: 2 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ records, _ := rec.GetRecords(job.Id)
|---------------|-------------|---------------------------|
| GetRecords | GET | /recorder/records/:job_id |
| GetAllRecords | GET | /recorder/records |
| DeleteRecords | DELETE | /recorder/records/:job_id |
| DeleteAllRecords | DELETE | /recorder/records |

## 示例

Expand Down
12 changes: 12 additions & 0 deletions services/recorder_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,21 @@ func (rhs *rHTTPService) getRecords(c *gin.Context) {
})
}

func (rhs *rHTTPService) deleteRecords(c *gin.Context) {
err := rhs.recorder.DeleteRecords(c.Param("job_id"))
c.JSON(200, gin.H{"data": nil, "error": rhs.handleErr(err)})
}

func (rhs *rHTTPService) deleteAllRecords(c *gin.Context) {
err := rhs.recorder.DeleteAllRecords()
c.JSON(200, gin.H{"data": nil, "error": rhs.handleErr(err)})
}

func (rhs *rHTTPService) registerRoutes(r *gin.Engine) {
r.GET("/recorder/records/:job_id", rhs.getRecords)
r.GET("/recorder/records", rhs.getRecords)
r.DELETE("/recorder/records/:job_id", rhs.deleteRecords)
r.DELETE("/recorder/records", rhs.deleteAllRecords)
}

func fixPositiveNum(num, numDef int) int {
Expand Down
94 changes: 91 additions & 3 deletions services/recorder_http_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,74 @@
package services

import (
"bytes"
"encoding/json"
"io"
"net/http"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/agscheduler/agscheduler"
)

func testRecorderHTTP(t *testing.T, baseUrl string) {
resp, err := http.Get(baseUrl + "/recorder/records/test")
client := &http.Client{}

req, err := http.NewRequest(http.MethodDelete, baseUrl+"/recorder/records", nil)
assert.NoError(t, err)
resp, err := client.Do(req)
assert.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)

_, err = http.Post(baseUrl+"/scheduler/start", CONTENT_TYPE, nil)
assert.NoError(t, err)

mJ := map[string]any{
"name": "Job",
"type": agscheduler.JOB_TYPE_DATETIME,
"start_at": "2023-09-22 07:30:08",
"func_name": "github.com/agscheduler/agscheduler/services.dryRunHTTP",
}
bJ, err := json.Marshal(mJ)
assert.NoError(t, err)
resp, err = http.Post(baseUrl+"/scheduler/job", CONTENT_TYPE, bytes.NewReader(bJ))
assert.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
rJ := &result{}
err = json.Unmarshal(body, &rJ)
assert.NoError(t, err)
total := rJ.Data.(map[string]any)["total"].(float64)

time.Sleep(1500 * time.Millisecond)

jobId := rJ.Data.(map[string]any)["id"].(string)
resp, err = http.Get(baseUrl + "/recorder/records" + "/" + jobId)
assert.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
body, err = io.ReadAll(resp.Body)
assert.NoError(t, err)
rJ = &result{}
err = json.Unmarshal(body, &rJ)
assert.NoError(t, err)
total := int(rJ.Data.(map[string]any)["total"].(float64))
assert.Equal(t, 1, total)

mJ = map[string]any{
"name": "Job2",
"type": agscheduler.JOB_TYPE_DATETIME,
"start_at": "2023-09-22 07:30:08",
"func_name": "github.com/agscheduler/agscheduler/services.dryRunHTTP",
}
bJ, err = json.Marshal(mJ)
assert.NoError(t, err)
resp, err = http.Post(baseUrl+"/scheduler/job", CONTENT_TYPE, bytes.NewReader(bJ))
assert.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)

time.Sleep(1500 * time.Millisecond)

resp, err = http.Get(baseUrl + "/recorder/records")
assert.NoError(t, err)
Expand All @@ -28,7 +78,45 @@ func testRecorderHTTP(t *testing.T, baseUrl string) {
rJ = &result{}
err = json.Unmarshal(body, &rJ)
assert.NoError(t, err)
totalAll := rJ.Data.(map[string]any)["total"].(float64)
totalAll := int(rJ.Data.(map[string]any)["total"].(float64))
assert.Equal(t, 2, totalAll)

assert.Less(t, total, totalAll)

req, err = http.NewRequest(http.MethodDelete, baseUrl+"/recorder/records"+"/"+jobId, nil)
assert.NoError(t, err)
resp, err = client.Do(req)
assert.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)

resp, err = http.Get(baseUrl + "/recorder/records")
assert.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
body, err = io.ReadAll(resp.Body)
assert.NoError(t, err)
rJ = &result{}
err = json.Unmarshal(body, &rJ)
assert.NoError(t, err)
totalAll = int(rJ.Data.(map[string]any)["total"].(float64))
assert.Equal(t, 1, totalAll)

req, err = http.NewRequest(http.MethodDelete, baseUrl+"/recorder/records", nil)
assert.NoError(t, err)
resp, err = client.Do(req)
assert.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)

resp, err = http.Get(baseUrl + "/recorder/records")
assert.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
body, err = io.ReadAll(resp.Body)
assert.NoError(t, err)
rJ = &result{}
err = json.Unmarshal(body, &rJ)
assert.NoError(t, err)
totalAll = int(rJ.Data.(map[string]any)["total"].(float64))
assert.Equal(t, 0, totalAll)

_, err = http.Post(baseUrl+"/scheduler/stop", CONTENT_TYPE, nil)
assert.NoError(t, err)
}

0 comments on commit 377a8e2

Please sign in to comment.