88
99 "github.com/anyproto/any-sync/app"
1010 "github.com/anyproto/any-sync/commonfile/fileproto"
11+ "github.com/anyproto/any-sync/coordinator/coordinatorclient"
12+ "github.com/anyproto/any-sync/coordinator/coordinatorproto"
1113
1214 "github.com/anyproto/any-sync-filenode/index"
1315)
@@ -30,11 +32,13 @@ func New() Stat {
3032type statService struct {
3133 accountInfoProvider accountInfoProvider
3234 index index.Index
35+ coordinator coordinatorclient.CoordinatorClient
3336}
3437
3538func (i * statService ) Init (a * app.App ) (err error ) {
3639 i .accountInfoProvider = app.MustComponent [accountInfoProvider ](a )
3740 i .index = app.MustComponent [index.Index ](a )
41+ i .coordinator = app.MustComponent [coordinatorclient.CoordinatorClient ](a )
3842 return
3943}
4044
@@ -118,6 +122,46 @@ func (i *statService) Run(ctx context.Context) (err error) {
118122 return
119123 }
120124 })
125+ http .HandleFunc ("/stat/check_deletion/{identity}" , func (writer http.ResponseWriter , request * http.Request ) {
126+ identity := request .PathValue ("identity" )
127+ if identity == "" {
128+ http .Error (writer , "identity is empty" , http .StatusBadRequest )
129+ return
130+ }
131+ isDoFix := request .URL .Query ().Get ("fix" ) != ""
132+
133+ st := time .Now ()
134+ res , err := i .index .CheckDeletedSpaces (ctx , index.Key {GroupId : identity }, func (spaceIds []string ) (deletedIds []string , err error ) {
135+ statuses , _ , err := i .coordinator .StatusCheckMany (ctx , spaceIds )
136+ if err != nil {
137+ return nil , err
138+ }
139+ for idx , status := range statuses {
140+ if status .Status == coordinatorproto .SpaceStatus_SpaceStatusDeleted {
141+ deletedIds = append (deletedIds , spaceIds [idx ])
142+ }
143+ }
144+ return
145+ }, isDoFix )
146+ if err != nil {
147+ http .Error (writer , err .Error (), http .StatusInternalServerError )
148+ }
149+ resp := struct {
150+ Results []string `json:"results"`
151+ Duration string `json:"duration"`
152+ }{
153+ Results : res ,
154+ Duration : time .Since (st ).String (),
155+ }
156+
157+ writer .Header ().Set ("Content-Type" , "application/json" )
158+ writer .WriteHeader (http .StatusOK )
159+ err = json .NewEncoder (writer ).Encode (resp )
160+ if err != nil {
161+ http .Error (writer , err .Error (), http .StatusInternalServerError )
162+ return
163+ }
164+ })
121165 return nil
122166}
123167
0 commit comments