-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from anyproto/go-3582-filenode-space-size-met…
…hod-to-debug-server-analytics GO-3582: filenode space size method to debug server analytics
- Loading branch information
Showing
4 changed files
with
111 additions
and
6 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
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,86 @@ | ||
package stat | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/anyproto/any-sync/app" | ||
"github.com/anyproto/any-sync/commonfile/fileproto" | ||
) | ||
|
||
const CName = "stat.identity" | ||
|
||
type accountInfoProvider interface { | ||
AccountInfo(ctx context.Context, identity string) (*fileproto.AccountInfoResponse, error) | ||
BatchAccountInfo(ctx context.Context, identities []string) ([]*fileproto.AccountInfoResponse, error) | ||
} | ||
|
||
type Stat interface { | ||
app.ComponentRunnable | ||
} | ||
|
||
func New() Stat { | ||
return &identityStat{} | ||
} | ||
|
||
type identityStat struct { | ||
accountInfoProvider accountInfoProvider | ||
} | ||
|
||
func (i *identityStat) Init(a *app.App) (err error) { | ||
i.accountInfoProvider = app.MustComponent[accountInfoProvider](a) | ||
return | ||
} | ||
|
||
func (i *identityStat) Name() (name string) { | ||
return CName | ||
} | ||
|
||
func (i *identityStat) Run(ctx context.Context) (err error) { | ||
http.HandleFunc("/stat/identity/{identity}", func(writer http.ResponseWriter, request *http.Request) { | ||
identity := request.PathValue("identity") | ||
if identity == "" { | ||
http.Error(writer, "identity is empty", http.StatusBadRequest) | ||
return | ||
} | ||
accountInfo, err := i.accountInfoProvider.AccountInfo(request.Context(), identity) | ||
if err != nil { | ||
http.Error(writer, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
writer.Header().Set("Content-Type", "application/json") | ||
writer.WriteHeader(http.StatusOK) | ||
err = json.NewEncoder(writer).Encode(accountInfo) | ||
if err != nil { | ||
http.Error(writer, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
}) | ||
http.HandleFunc("/stat/identities", func(writer http.ResponseWriter, request *http.Request) { | ||
data := struct { | ||
Ids []string `json:"ids"` | ||
}{} | ||
if err := json.NewDecoder(request.Body).Decode(&data); err != nil { | ||
http.Error(writer, "invalid JSON", http.StatusBadRequest) | ||
return | ||
} | ||
accountInfos, err := i.accountInfoProvider.BatchAccountInfo(request.Context(), data.Ids) | ||
if err != nil { | ||
http.Error(writer, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
writer.Header().Set("Content-Type", "application/json") | ||
writer.WriteHeader(http.StatusOK) | ||
err = json.NewEncoder(writer).Encode(accountInfos) | ||
if err != nil { | ||
http.Error(writer, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
}) | ||
return nil | ||
} | ||
|
||
func (i *identityStat) Close(ctx context.Context) (err error) { | ||
return | ||
} |