Skip to content

Commit

Permalink
api: make census IPFS export async
Browse files Browse the repository at this point in the history
Add new method /censuses/export/ipfs/list to list all exported censuses (runtime).

Signed-off-by: p4u <[email protected]>
  • Loading branch information
p4u committed Sep 16, 2024
1 parent 824ddf0 commit 3f1418c
Showing 1 changed file with 55 additions and 10 deletions.
65 changes: 55 additions & 10 deletions api/censuses.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (
MaxCensusAddBatchSize = 8192

censusIDsize = 32
censusRetrieveTimeout = 5 * time.Minute
censusRetrieveTimeout = 10 * time.Minute
)

func (a *API) enableCensusHandlers() error {
Expand Down Expand Up @@ -170,6 +170,14 @@ func (a *API) enableCensusHandlers() error {
); err != nil {
return err
}
if err := a.Endpoint.RegisterMethod(
"/censuses/export/ipfs/list",
"GET",
apirest.MethodAccessTypeAdmin,
a.censusExportIPFSListDBHandler,
); err != nil {
return err
}
if err := a.Endpoint.RegisterMethod(
"/censuses/export",
"GET",
Expand Down Expand Up @@ -203,6 +211,9 @@ func (a *API) enableCensusHandlers() error {
return err
}

// Initialize the map to store the status of the async export to ipfs
censusIPFSExports = make(map[string]time.Time)

return nil
}

Expand Down Expand Up @@ -972,6 +983,9 @@ func (a *API) censusListHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext)
return ctx.Send(data, apirest.HTTPstatusOK)
}

// censusIPFSExports is a map of ipfs uri to the time when the export was requested
var censusIPFSExports = map[string]time.Time{}

// censusExportDBHandler
//
// @Summary Export census database
Expand All @@ -986,27 +1000,58 @@ func (a *API) censusListHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext)
func (a *API) censusExportDBHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext) error {
isIPFSExport := strings.HasSuffix(ctx.Request.URL.Path, "ipfs")
buf := bytes.Buffer{}
if err := a.censusdb.ExportCensusDB(&buf); err != nil {
return err
}
var data []byte
if isIPFSExport {
uri, err := a.storage.PublishReader(ctx.Request.Context(), &buf)
if err != nil {
return err
}
go func() {
log.Infow("exporting census database to ipfs async")
startTime := time.Now()
if err := a.censusdb.ExportCensusDB(&buf); err != nil {
log.Errorw(err, "could not export census database")
return
}
log.Infow("census database exported", "duration (s)", time.Since(startTime).Seconds())
startTime = time.Now()
uri, err := a.storage.PublishReader(context.Background(), &buf)
if err != nil {
log.Errorw(err, "could not publish census database to ipfs")
return
}
log.Infow("census database published to ipfs", "uri", uri, "duration (s)", time.Since(startTime).Seconds())
censusIPFSExports[uri] = time.Now()
}()
var err error
data, err = json.Marshal(map[string]string{
"uri": uri,
"message": "scheduled, check /censuses/export/ipfs/list",
})
if err != nil {
return err
log.Errorw(err, "could not marshal response")
}
} else {
if err := a.censusdb.ExportCensusDB(&buf); err != nil {
return err
}
data = buf.Bytes()
}
return ctx.Send(data, apirest.HTTPstatusOK)
}

// censusExportIPFSListDBHandler
//
// @Summary List export census database to IPFS
// @Description List the IPFS URIs of the census database exports
// @Tags Censuses
// @Accept json
// @Produce json
// @Success 200 {object} object{valid=bool}
// @Router /censuses/export/ipfs/list [get]
func (a *API) censusExportIPFSListDBHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext) error {
data, err := json.Marshal(censusIPFSExports)
if err != nil {
return err
}
return ctx.Send(data, apirest.HTTPstatusOK)
}

// censusImportHandler
//
// @Summary Import census database
Expand Down

0 comments on commit 3f1418c

Please sign in to comment.