Skip to content

Commit

Permalink
Download file for logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Wilson committed Apr 13, 2023
1 parent abc9ddc commit c8a6c86
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 10 deletions.
12 changes: 12 additions & 0 deletions frontend/pages/api/clients/DayTraderClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,16 @@ export function Quote(userId, stockSymbol, requestNum) {
});
}

export function GetFile(filename) {
return new Promise((accept, reject) => {
DayTraderClient.File({ filename }, (err, value) => {
if (err == null) {
accept(value);
} else {
reject(err);
}
});
});
}


10 changes: 10 additions & 0 deletions frontend/pages/api/clients/day-trader.proto
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ service DayTrader {
rpc Login(LoginRequest) returns (LoginResponse);

rpc Quote(QuoteRequest) returns (QuoteRequestSimple);

rpc File(FileRequest) returns (FileResponse);
}

message FileRequest {
string filename = 1;
}

message FileResponse {
bytes contents = 1;
}

message QuoteRequestSimple {
Expand Down
8 changes: 5 additions & 3 deletions frontend/pages/api/log/system.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DumpLog } from "../clients/DayTraderClient";
import { DumpLog, GetFile } from "../clients/DayTraderClient";


export default async function dumplog(req, res){
Expand All @@ -9,9 +9,11 @@ export default async function dumplog(req, res){
}
return res.status(200).json(response)
}else{
const grpcCall = await DumpLog("dumplog.xml", -1);
const filename = "dumplog.xml";
const grpcCall = await DumpLog(filename, -1);
const grpcFileCall = await GetFile(grpcCall.xml)
const response = {
xml: grpcCall.xml,
file: grpcFileCall.contents,
success: true,
}
return res.status(200).json(response)
Expand Down
8 changes: 5 additions & 3 deletions frontend/pages/api/log/user.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DumpLogUser } from "../clients/DayTraderClient";
import { DumpLogUser, GetFile } from "../clients/DayTraderClient";

export default async function userLog(req, res){
const user = req.body.username;
Expand All @@ -9,9 +9,11 @@ export default async function userLog(req, res){
}
return res.status(200).json(response)
}else{
const grpcCall = await DumpLogUser(user, "dumploguser.xml", -1);
const filename = "dumploguser.xml";
const grpcCall = await DumpLogUser(user, filename, -1);
const grpcFileCall = await GetFile(grpcCall.xml)
const response = {
xml: grpcCall.xml,
file: grpcFileCall.contents,
success: true,
}
return res.status(200).json(response)
Expand Down
18 changes: 18 additions & 0 deletions frontend/pages/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ import { Button, Container, Divider, Typography } from '@mui/material'
import React, { useContext } from 'react'
import { UserContext } from './_app'

const downloadFile = (filename, data) => {
const link = document.createElement('a');
link.download = filename;
const bufferData = new Uint8Array(data);
const textData = new TextDecoder().decode(bufferData);
const blob = new Blob([textData], { type: 'text/xml' });
link.href = URL.createObjectURL(blob);
link.click();
};



function settings() {
const user = useContext(UserContext).user
if(!user){
Expand All @@ -22,6 +34,9 @@ function settings() {
try{
const response_parsed = await (await fetch(url, fetchArgs)).json()
console.log(response_parsed);
if (response_parsed.success) {
downloadFile('SystemDumpLog.xml', response_parsed.file.data);
}
}catch(error){
console.log(error);
}
Expand All @@ -41,6 +56,9 @@ function settings() {
try{
const response_parsed = await (await fetch(url, fetchArgs)).json()
console.log(response_parsed);
if (response_parsed.success) {
downloadFile('UserDumpLog.xml', response_parsed.file.data);
}
}catch(error){
console.log(error);
}
Expand Down
5 changes: 1 addition & 4 deletions frontend/src/components/SignUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ export default function SignUp() {
const responseParsed = await response.json();
if (responseParsed.success) {
localStorage.setItem('jwt', responseParsed.user);
window.location.href = "/"
setError("");
}else{
setError("Invalid username or password, please try again");
}
window.location.href = "/"
}catch(error){
console.log("error:",error);
setError("Invalid username or password, please try again");
Expand Down

0 comments on commit c8a6c86

Please sign in to comment.