-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Batch report worker poc #1
Changes from all commits
161a50c
2ede736
1125060
8cb535f
3a501e6
098761d
5770f8c
e42b778
568bd88
c37b4a2
0a5e1f9
ee26263
3e521fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. | ||
// See LICENSE.txt for license information. | ||
|
||
package app | ||
|
||
import ( | ||
"bytes" | ||
"encoding/csv" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/mattermost/mattermost/server/public/model" | ||
) | ||
|
||
func (a *App) SaveReportChunk(format string, prefix string, count int, reportData []model.ReportableObject) *model.AppError { | ||
switch format { | ||
case "csv": | ||
return a.saveCSVChunk(prefix, count, reportData) | ||
} | ||
return model.NewAppError("SaveReportChunk", "", nil, "unsupported report format", http.StatusInternalServerError) | ||
} | ||
|
||
func (a *App) saveCSVChunk(prefix string, count int, reportData []model.ReportableObject) *model.AppError { | ||
var buf bytes.Buffer | ||
w := csv.NewWriter(&buf) | ||
|
||
for _, report := range reportData { | ||
err := w.Write(report.ToReport()) | ||
if err != nil { | ||
return model.NewAppError("saveCSVChunk", "", nil, "failed to write report data to CSV", http.StatusInternalServerError) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to wrap original |
||
} | ||
} | ||
|
||
w.Flush() | ||
_, appErr := a.WriteFile(&buf, makeFilename(prefix, count, "csv")) | ||
if appErr != nil { | ||
return appErr | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (a *App) CompileReportChunks(format string, prefix string, numberOfChunks int, headers []string) (string, *model.AppError) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once the job has batched everything, upon completion we should merge all the files together. |
||
switch format { | ||
case "csv": | ||
return a.compileCSVChunks(prefix, numberOfChunks, headers) | ||
} | ||
return "", model.NewAppError("CompileReportChunks", "", nil, "unsupported report format", http.StatusInternalServerError) | ||
} | ||
|
||
func (a *App) compileCSVChunks(prefix string, numberOfChunks int, headers []string) (string, *model.AppError) { | ||
filename := fmt.Sprintf("batch_report_%s.csv", prefix) | ||
|
||
var headerBuf bytes.Buffer | ||
w := csv.NewWriter(&headerBuf) | ||
err := w.Write(headers) | ||
if err != nil { | ||
return "", model.NewAppError("compileCSVChunks", "", nil, "failed to write headers", http.StatusInternalServerError) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to wrap original |
||
} | ||
w.Flush() | ||
_, appErr := a.WriteFile(&headerBuf, filename) | ||
if appErr != nil { | ||
return "", appErr | ||
} | ||
|
||
for i := 0; i < numberOfChunks; i++ { | ||
chunkFilename := makeFilename(prefix, i, "csv") | ||
chunk, err := a.ReadFile(chunkFilename) | ||
if err != nil { | ||
return "", err | ||
} | ||
if _, err = a.AppendFile(bytes.NewReader(chunk), filename); err != nil { | ||
return "", err | ||
} | ||
if err = a.RemoveFile(chunkFilename); err != nil { | ||
return "", err | ||
} | ||
} | ||
|
||
return filename, nil | ||
} | ||
|
||
func (a *App) SendReportToUser(userID string, filename string) *model.AppError { | ||
return nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should send the report to the user via DM. Will be implemented later. |
||
} | ||
|
||
func makeFilename(prefix string, count int, extension string) string { | ||
return fmt.Sprintf("batch_report_%s__%d.%s", prefix, count, extension) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should probably be a status bad request as the incorrect format comes from client.