<commit_before>package api

import (
	"strings"
	"time"

	"golang.org/x/net/context"

	"chain/api/appdb"
	"chain/api/asset"
	"chain/database/pg"
	"chain/fedchain-sandbox/hdkey"
	"chain/metrics"
	"chain/net/http/httpjson"
)

// POST /v3/projects/:projID/manager-nodes
func createManagerNode(ctx context.Context, projID string, wReq *asset.CreateNodeReq) (interface{}, error) {
	if err := projectAuthz(ctx, projID); err != nil {
		return nil, err
	}

	dbtx, ctx, err := pg.Begin(ctx)
	if err != nil {
		return nil, err
	}
	defer dbtx.Rollback()

	managerNode, err := asset.CreateNode(ctx, asset.ManagerNode, projID, wReq)
	if err != nil {
		return nil, err
	}

	err = dbtx.Commit()
	if err != nil {
		return nil, err
	}

	return managerNode, nil
}

// PUT /v3/manager-nodes/:mnodeID
func updateManagerNode(ctx context.Context, mnodeID string, in struct{ Label *string }) error {
	if err := managerAuthz(ctx, mnodeID); err != nil {
		return err
	}
	return appdb.UpdateManagerNode(ctx, mnodeID, in.Label)
}

// DELETE /v3/manager-nodes/:mnodeID
func deleteManagerNode(ctx context.Context, mnodeID string) error {
	if err := managerAuthz(ctx, mnodeID); err != nil {
		return err
	}
	return appdb.DeleteManagerNode(ctx, mnodeID)
}

// GET /v3/projects/:projID/manager-nodes
func listManagerNodes(ctx context.Context, projID string) (interface{}, error) {
	if err := projectAuthz(ctx, projID); err != nil {
		return nil, err
	}
	return appdb.ListManagerNodes(ctx, projID)
}

// GET /v3/manager-nodes/:mnodeID
func getManagerNode(ctx context.Context, mnodeID string) (interface{}, error) {
	if err := managerAuthz(ctx, mnodeID); err != nil {
		return nil, err
	}
	return appdb.GetManagerNode(ctx, mnodeID)
}

// GET /v3/manager-nodes/:mnodeID/activity
func getManagerNodeActivity(ctx context.Context, mnID string) (interface{}, error) {
	if err := managerAuthz(ctx, mnID); err != nil {
		return nil, err
	}
	prev, limit, err := getPageData(ctx, defActivityPageSize)
	if err != nil {
		return nil, err
	}

	activity, last, err := appdb.ManagerNodeActivity(ctx, mnID, prev, limit)
	if err != nil {
		return nil, err
	}

	ret := map[string]interface{}{
		"last":       last,
		"activities": httpjson.Array(activity),
	}
	return ret, nil
}

// GET /v3/manager-nodes/:mnodeID/transactions/:txID
func managerNodeTxActivity(ctx context.Context, mnodeID, txID string) (interface{}, error) {
	if err := managerAuthz(ctx, mnodeID); err != nil {
		return nil, err
	}
	return appdb.ManagerNodeTxActivity(ctx, mnodeID, txID)
}

// GET /v3/manager-nodes/:mnodeID/balance
func managerNodeBalance(ctx context.Context, managerNodeID string) (interface{}, error) {
	if err := managerAuthz(ctx, managerNodeID); err != nil {
		return nil, err
	}
	prev, limit, err := getPageData(ctx, defBalancePageSize)
	if err != nil {
		return nil, err
	}

	balances, last, err := appdb.ManagerNodeBalance(ctx, managerNodeID, prev, limit)
	if err != nil {
		return nil, err
	}

	ret := map[string]interface{}{
		"last":     last,
		"balances": httpjson.Array(balances),
	}
	return ret, nil
}

func listAccountsWithAsset(ctx context.Context, mnodeID, assetID string) (interface{}, error) {
	if err := managerAuthz(ctx, mnodeID); err != nil {
		return nil, err
	}
	prev, limit, err := getPageData(ctx, defBalancePageSize)
	if err != nil {
		return nil, err
	}

	balances, last, err := appdb.AccountsWithAsset(ctx, mnodeID, assetID, prev, limit)
	if err != nil {
		return nil, err
	}

	return map[string]interface{}{
		"balances": balances,
		"last":     last,<commit_msg>api: ensure array response in listAccountsWithAsset<commit_after>package api

import (
	"strings"
	"time"

	"golang.org/x/net/context"

	"chain/api/appdb"
	"chain/api/asset"
	"chain/database/pg"
	"chain/fedchain-sandbox/hdkey"
	"chain/metrics"
	"chain/net/http/httpjson"
)

// POST /v3/projects/:projID/manager-nodes
func createManagerNode(ctx context.Context, projID string, wReq *asset.CreateNodeReq) (interface{}, error) {
	if err := projectAuthz(ctx, projID); err != nil {
		return nil, err
	}

	dbtx, ctx, err := pg.Begin(ctx)
	if err != nil {
		return nil, err
	}
	defer dbtx.Rollback()

	managerNode, err := asset.CreateNode(ctx, asset.ManagerNode, projID, wReq)
	if err != nil {
		return nil, err
	}

	err = dbtx.Commit()
	if err != nil {
		return nil, err
	}

	return managerNode, nil
}

// PUT /v3/manager-nodes/:mnodeID
func updateManagerNode(ctx context.Context, mnodeID string, in struct{ Label *string }) error {
	if err := managerAuthz(ctx, mnodeID); err != nil {
		return err
	}
	return appdb.UpdateManagerNode(ctx, mnodeID, in.Label)
}

// DELETE /v3/manager-nodes/:mnodeID
func deleteManagerNode(ctx context.Context, mnodeID string) error {
	if err := managerAuthz(ctx, mnodeID); err != nil {
		return err
	}
	return appdb.DeleteManagerNode(ctx, mnodeID)
}

// GET /v3/projects/:projID/manager-nodes
func listManagerNodes(ctx context.Context, projID string) (interface{}, error) {
	if err := projectAuthz(ctx, projID); err != nil {
		return nil, err
	}
	return appdb.ListManagerNodes(ctx, projID)
}

// GET /v3/manager-nodes/:mnodeID
func getManagerNode(ctx context.Context, mnodeID string) (interface{}, error) {
	if err := managerAuthz(ctx, mnodeID); err != nil {
		return nil, err
	}
	return appdb.GetManagerNode(ctx, mnodeID)
}

// GET /v3/manager-nodes/:mnodeID/activity
func getManagerNodeActivity(ctx context.Context, mnID string) (interface{}, error) {
	if err := managerAuthz(ctx, mnID); err != nil {
		return nil, err
	}
	prev, limit, err := getPageData(ctx, defActivityPageSize)
	if err != nil {
		return nil, err
	}

	activity, last, err := appdb.ManagerNodeActivity(ctx, mnID, prev, limit)
	if err != nil {
		return nil, err
	}

	ret := map[string]interface{}{
		"last":       last,
		"activities": httpjson.Array(activity),
	}
	return ret, nil
}

// GET /v3/manager-nodes/:mnodeID/transactions/:txID
func managerNodeTxActivity(ctx context.Context, mnodeID, txID string) (interface{}, error) {
	if err := managerAuthz(ctx, mnodeID); err != nil {
		return nil, err
	}
	return appdb.ManagerNodeTxActivity(ctx, mnodeID, txID)
}

// GET /v3/manager-nodes/:mnodeID/balance
func managerNodeBalance(ctx context.Context, managerNodeID string) (interface{}, error) {
	if err := managerAuthz(ctx, managerNodeID); err != nil {
		return nil, err
	}
	prev, limit, err := getPageData(ctx, defBalancePageSize)
	if err != nil {
		return nil, err
	}

	balances, last, err := appdb.ManagerNodeBalance(ctx, managerNodeID, prev, limit)
	if err != nil {
		return nil, err
	}

	ret := map[string]interface{}{
		"last":     last,
		"balances": httpjson.Array(balances),
	}
	return ret, nil
}

func listAccountsWithAsset(ctx context.Context, mnodeID, assetID string) (interface{}, error) {
	if err := managerAuthz(ctx, mnodeID); err != nil {
		return nil, err
	}
	prev, limit, err := getPageData(ctx, defBalancePageSize)
	if err != nil {
		return nil, err
	}

	balances, last, err := appdb.AccountsWithAsset(ctx, mnodeID, assetID, prev, limit)
	if err != nil {
		return nil, err
	}

	return map[string]interface{}{
		"balances": httpjson.Array(balances),
		"last":     last,
{'fraction_non_alphanumeric': 0.13984243106359032, 'fraction_numerical': 0.0022509848058525606, 'mean_word_length': 4.223365172667156, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3785478', 'n_tokens_mistral': 2615, 'n_tokens_neox': 2564, 'n_words': 654}