-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(region): allow clean history data (#20040)
- Loading branch information
Showing
7 changed files
with
313 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2019 Yunion | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package misc | ||
|
||
import ( | ||
"fmt" | ||
|
||
"yunion.io/x/onecloud/pkg/mcclient" | ||
"yunion.io/x/onecloud/pkg/mcclient/modules" | ||
) | ||
|
||
func init() { | ||
type HistoryDataCleanOptions struct { | ||
SERVICE string `help:"Service type"` | ||
Day *int `default:"30"` | ||
} | ||
R(&HistoryDataCleanOptions{}, "history-data-clean", "clean history data", func(s *mcclient.ClientSession, args *HistoryDataCleanOptions) error { | ||
body, err := modules.HistoryDataClean(s, args.SERVICE, args.Day) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(body.PrettyString()) | ||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright 2019 Yunion | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package db | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"yunion.io/x/jsonutils" | ||
"yunion.io/x/log" | ||
"yunion.io/x/pkg/gotypes" | ||
|
||
"yunion.io/x/onecloud/pkg/appsrv" | ||
"yunion.io/x/onecloud/pkg/httperrors" | ||
"yunion.io/x/onecloud/pkg/mcclient/auth" | ||
) | ||
|
||
func AddHistoryDataCleanHandler(prefix string, app *appsrv.Application) { | ||
prefix = fmt.Sprintf("%s/history-data-clean", prefix) | ||
app.AddHandler2("POST", prefix, auth.Authenticate(historyDataCleanHandler), nil, "history_data_clean", nil) | ||
} | ||
|
||
func historyDataCleanHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { | ||
userCred := fetchUserCredential(ctx) | ||
if !userCred.HasSystemAdminPrivilege() { | ||
httperrors.ForbiddenError(ctx, w, "only sysadmin can clean history data") | ||
return | ||
} | ||
input, err := appsrv.FetchJSON(r) | ||
if err != nil { | ||
httperrors.InputParameterError(ctx, w, "FetchJSON") | ||
return | ||
} | ||
date := time.Now().AddDate(0, -1, 0) | ||
if !gotypes.IsNil(input) && input.Contains("day") { | ||
day, _ := input.Int("day") | ||
date = time.Now().AddDate(0, 0, int(day)*-1) | ||
} | ||
go func() { | ||
for _, manager := range globalTables { | ||
if hM, ok := manager.(IHistoryDataManager); ok { | ||
start := time.Now() | ||
cnt, err := hM.HistoryDataClean(ctx, date) | ||
if err != nil { | ||
log.Errorf("clean %s data error: %v", manager.Keyword(), err) | ||
continue | ||
} | ||
log.Debugf("clean %d %s history data cost %s", cnt, manager.Keyword(), time.Now().Sub(start).Round(time.Second)) | ||
} | ||
} | ||
}() | ||
appsrv.SendJSON(w, jsonutils.Marshal(map[string]string{"status": "ok"})) | ||
} | ||
|
||
type IHistoryDataManager interface { | ||
HistoryDataClean(ctx context.Context, timeBefor time.Time) (int, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2019 Yunion | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package modulebase | ||
|
||
import ( | ||
"io" | ||
"strings" | ||
|
||
"yunion.io/x/jsonutils" | ||
|
||
"yunion.io/x/onecloud/pkg/mcclient" | ||
) | ||
|
||
func HistoryDataClean(s *mcclient.ClientSession, serviceType string, day *int) (jsonutils.JSONObject, error) { | ||
man := &BaseManager{serviceType: serviceType} | ||
input := jsonutils.NewDict() | ||
if day != nil { | ||
input.Set("day", jsonutils.NewInt(int64(*day))) | ||
} | ||
resp, err := man.rawRequest(s, "POST", "/history-data-clean", nil, strings.NewReader(input.String())) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return jsonutils.Parse(body) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2019 Yunion | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package modules | ||
|
||
import ( | ||
"yunion.io/x/jsonutils" | ||
|
||
"yunion.io/x/onecloud/pkg/mcclient" | ||
"yunion.io/x/onecloud/pkg/mcclient/modulebase" | ||
) | ||
|
||
func HistoryDataClean(s *mcclient.ClientSession, serviceType string, day *int) (jsonutils.JSONObject, error) { | ||
return modulebase.HistoryDataClean(s, serviceType, day) | ||
} |