Skip to content

Commit c8a6c86

Browse files
author
Peter Wilson
committed
Download file for logs
1 parent abc9ddc commit c8a6c86

File tree

6 files changed

+51
-10
lines changed

6 files changed

+51
-10
lines changed

frontend/pages/api/clients/DayTraderClient.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,4 +249,16 @@ export function Quote(userId, stockSymbol, requestNum) {
249249
});
250250
}
251251

252+
export function GetFile(filename) {
253+
return new Promise((accept, reject) => {
254+
DayTraderClient.File({ filename }, (err, value) => {
255+
if (err == null) {
256+
accept(value);
257+
} else {
258+
reject(err);
259+
}
260+
});
261+
});
262+
}
263+
252264

frontend/pages/api/clients/day-trader.proto

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ service DayTrader {
4242
rpc Login(LoginRequest) returns (LoginResponse);
4343

4444
rpc Quote(QuoteRequest) returns (QuoteRequestSimple);
45+
46+
rpc File(FileRequest) returns (FileResponse);
47+
}
48+
49+
message FileRequest {
50+
string filename = 1;
51+
}
52+
53+
message FileResponse {
54+
bytes contents = 1;
4555
}
4656

4757
message QuoteRequestSimple {

frontend/pages/api/log/system.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DumpLog } from "../clients/DayTraderClient";
1+
import { DumpLog, GetFile } from "../clients/DayTraderClient";
22

33

44
export default async function dumplog(req, res){
@@ -9,9 +9,11 @@ export default async function dumplog(req, res){
99
}
1010
return res.status(200).json(response)
1111
}else{
12-
const grpcCall = await DumpLog("dumplog.xml", -1);
12+
const filename = "dumplog.xml";
13+
const grpcCall = await DumpLog(filename, -1);
14+
const grpcFileCall = await GetFile(grpcCall.xml)
1315
const response = {
14-
xml: grpcCall.xml,
16+
file: grpcFileCall.contents,
1517
success: true,
1618
}
1719
return res.status(200).json(response)

frontend/pages/api/log/user.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DumpLogUser } from "../clients/DayTraderClient";
1+
import { DumpLogUser, GetFile } from "../clients/DayTraderClient";
22

33
export default async function userLog(req, res){
44
const user = req.body.username;
@@ -9,9 +9,11 @@ export default async function userLog(req, res){
99
}
1010
return res.status(200).json(response)
1111
}else{
12-
const grpcCall = await DumpLogUser(user, "dumploguser.xml", -1);
12+
const filename = "dumploguser.xml";
13+
const grpcCall = await DumpLogUser(user, filename, -1);
14+
const grpcFileCall = await GetFile(grpcCall.xml)
1315
const response = {
14-
xml: grpcCall.xml,
16+
file: grpcFileCall.contents,
1517
success: true,
1618
}
1719
return res.status(200).json(response)

frontend/pages/settings.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ import { Button, Container, Divider, Typography } from '@mui/material'
22
import React, { useContext } from 'react'
33
import { UserContext } from './_app'
44

5+
const downloadFile = (filename, data) => {
6+
const link = document.createElement('a');
7+
link.download = filename;
8+
const bufferData = new Uint8Array(data);
9+
const textData = new TextDecoder().decode(bufferData);
10+
const blob = new Blob([textData], { type: 'text/xml' });
11+
link.href = URL.createObjectURL(blob);
12+
link.click();
13+
};
14+
15+
16+
517
function settings() {
618
const user = useContext(UserContext).user
719
if(!user){
@@ -22,6 +34,9 @@ function settings() {
2234
try{
2335
const response_parsed = await (await fetch(url, fetchArgs)).json()
2436
console.log(response_parsed);
37+
if (response_parsed.success) {
38+
downloadFile('SystemDumpLog.xml', response_parsed.file.data);
39+
}
2540
}catch(error){
2641
console.log(error);
2742
}
@@ -41,6 +56,9 @@ function settings() {
4156
try{
4257
const response_parsed = await (await fetch(url, fetchArgs)).json()
4358
console.log(response_parsed);
59+
if (response_parsed.success) {
60+
downloadFile('UserDumpLog.xml', response_parsed.file.data);
61+
}
4462
}catch(error){
4563
console.log(error);
4664
}

frontend/src/components/SignUp.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,8 @@ export default function SignUp() {
2626
const responseParsed = await response.json();
2727
if (responseParsed.success) {
2828
localStorage.setItem('jwt', responseParsed.user);
29-
window.location.href = "/"
30-
setError("");
31-
}else{
32-
setError("Invalid username or password, please try again");
3329
}
30+
window.location.href = "/"
3431
}catch(error){
3532
console.log("error:",error);
3633
setError("Invalid username or password, please try again");

0 commit comments

Comments
 (0)