Skip to content

Commit

Permalink
Merge pull request #96 from JonasGLund99/add-loader
Browse files Browse the repository at this point in the history
Add loader
  • Loading branch information
JonasGLund99 authored Apr 3, 2024
2 parents f8e17dd + 42aa940 commit 8136bf6
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 53 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ Eller denne:
**Step 3 - Run monaa-backend Flask server**
`cd monaa-backend`
(`git pull`)
`python3 -m flask --app server run`
`nohup python3 -m flask --app server run`

`pgrep python 3`
`sudo kill <process-id>`
(https://medium.com/analytics-vidhya/deploy-a-flask-app-on-amazon-aws-ec2-and-keep-it-running-while-you-are-offline-38d22571e2c5)

**Step 4 - Postman**
Import `P6.postman_collection.json` i din Postman App.
Expand Down
32 changes: 20 additions & 12 deletions TimeTrace/src/components/FileUploadButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import { getFileLines } from "../models/helpers/getFileLines";
import { extractEventsFromFileLines } from "../models/helpers/extractEventsFromFileLines";
import { fileLinesAreValid } from "../models/helpers/validation";


function FileUploadButton() {
const { setFileLines } = useContext(AppdataContext);
const { uploadedFile, setUploadedFile } = useContext(AppdataContext);
const { setMappings } = useContext(AppdataContext);
const { setError } = useContext(AppdataContext);
const { setEvents } = useContext(AppdataContext);
const { setLoading } = useContext(AppdataContext);

const { setMatches } = useContext(AppdataContext);

async function handleFileUpload(e: React.SyntheticEvent) {
const target = e.target as HTMLInputElement;
Expand Down Expand Up @@ -50,16 +51,20 @@ function FileUploadButton() {
callbackTitle: null,
is_dismissible: true
})
//reset
setFileLines([]);
setEvents([]);
setMappings(new Map());
setUploadedFile(null);
setMatches([]);
}
} else {
//reset
setFileLines([]);
setEvents([]);
setMappings(new Map());
setUploadedFile(null);
setMatches([])
}
setLoading(false);
};
Expand All @@ -74,22 +79,25 @@ function FileUploadButton() {
onChange={handleFileUpload}
/>
<div className="flex gap-2">
<button className="relative py-2">
<label htmlFor="contained-button-file" className="px-5 py-2 border-2 rounded-md cursor-pointer border-black-100">
{uploadedFile ? "Current file: " + uploadedFile.name : "Upload file" }
<button className="relative px-4 py-2 text-sm font-medium text-white bg-gray-800 rounded-lg hover:bg-gray-700">
<label htmlFor="contained-button-file" className="px-5 py-2 rounded-md cursor-pointer">
{
uploadedFile ? "Current file: " + uploadedFile.name : "Upload file"
}
</label>
{
uploadedFile === null && (
<span className="absolute top-[-3px] right-[-3px] block w-3 h-3 bg-blue-400 rounded-full animate-ping ring-1 ring-blue-400" style={{ animationDuration: '2s', animationTimingFunction: 'ease-out' }}></span>
)
uploadedFile === null &&
<span id="ping" className="absolute top-[-3px] right-[-3px] block w-3 h-3 bg-yellow-200 rounded-full animate-ping ring-1 ring-yellow-200" style={{ animationDuration: '2s', animationTimingFunction: 'ease-out' }}></span>
}
</button>
</div>
<button onClick={handleFileRemove} data-testid="remove-button">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="w-6 h-6">
<path strokeLinecap="round" strokeLinejoin="round" d="m14.74 9-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 0 1-2.244 2.077H8.084a2.25 2.25 0 0 1-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 0 0-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 0 1 3.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 0 0-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 0 0-7.5 0" />
</svg>
</button>
{ uploadedFile &&
<button onClick={handleFileRemove} data-testid="remove-button">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="w-6 h-6">
<path strokeLinecap="round" strokeLinejoin="round" d="m14.74 9-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 0 1-2.244 2.077H8.084a2.25 2.25 0 0 1-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 0 0-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 0 1 3.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 0 0-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 0 0-7.5 0" />
</svg>
</button>
}
</div>
);
}
Expand Down
24 changes: 16 additions & 8 deletions TimeTrace/src/components/Loader.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
function Loader(){
import { useContext } from "react";
import { AppdataContext } from "../context/AppContext";

function Loader() {

const { loading } = useContext(AppdataContext);

if (!loading) return null;

return (
<div role="status" className="flex items-center justify-center w-full border-2 border-gray-300 rounded-md">
<svg aria-hidden="true" className="h-auto text-gray-200 w-14 animate-spin dark:text-gray-600 fill-gray-600" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"/>
<path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill"/>
</svg>
<span className="sr-only">Loading...</span>
</div>
<div role="status" className="h-screen w-screen flex items-center justify-center backdrop-blur-[3px] fixed z-10">
<svg aria-hidden="true" className="h-auto text-gray-200 w-14 animate-spin fill-gray-600" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor" />
<path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill" />
</svg>
<span className="sr-only">Loading...</span>
</div>
)
}

Expand Down
50 changes: 40 additions & 10 deletions TimeTrace/src/components/LogTable.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useState, useEffect, useContext } from "react";
import { AppdataContext } from "../context/AppContext";
import { FileLine, mapEventsToFileLine } from '../models/Types/FileLine';
import { MonaaZone } from "../models/MonaaZone";
import { LogTableContext } from "../context/LogTableContext";

type LogTableProps = {
interface LogTableProps {
mappingsAreEditable: boolean;
};

Expand All @@ -13,8 +13,8 @@ function LogTable({ mappingsAreEditable }: LogTableProps) {
const { fileLines } = useContext(AppdataContext);
const { matches } = useContext(AppdataContext);
const [searchQuery, setSearchQuery] = useState<string>("");
const [currentPage, setCurrentPage] = useState(0);
const [monaaMatchIndex, setMonaaMatchIndex] = useState<number>(0);
const { currentPage, setCurrentPage } = useContext(LogTableContext);
const { monaaMatchIndex, setMonaaMatchIndex } = useContext(LogTableContext);
const [filteredFileLines, setFilteredFileLines] = useState<FileLine[]>(mapEventsToFileLine(events));
const linesPerPage = 100;
const [shownLines, setShownLines] = useState<FileLine[]>(filteredFileLines.slice(0, linesPerPage));
Expand All @@ -30,6 +30,10 @@ function LogTable({ mappingsAreEditable }: LogTableProps) {
if (logTable) logTable.scrollTop = 0;
}, [filteredFileLines]);

useEffect(() => {
setMonaaMatchIndex(0);
}, [matches])

useEffect(() => {
const logTable = document.querySelector("#log-table");
if (!logTable) return;
Expand Down Expand Up @@ -135,6 +139,15 @@ function LogTable({ mappingsAreEditable }: LogTableProps) {
return highlightLine;
}

function eventIsMapped(eventText: string) {
let isMapped = false;

if (mappings.get(eventText)) {
isMapped = true;
}
return isMapped;
}

return (
<div id="fixed-container" className="flex flex-col content-center w-full h-full">
<div id="top-log-table-title-container" className="flex p-1">
Expand All @@ -160,7 +173,7 @@ function LogTable({ mappingsAreEditable }: LogTableProps) {
{shownLines.map((fileLine: FileLine) => {
return <pre key={fileLine.line} className={classNames(
lineIsHighlighted(fileLine.line)
? "bg-yellow-200"
? eventIsMapped(fileLine.text) ? "bg-yellow-200" : "bg-yellow-100"
: "even:bg-white odd:bg-gray-100",
"py-2 pl-3"
)}>{`${fileLine.line + 1}: `} </pre>;
Expand All @@ -175,7 +188,7 @@ function LogTable({ mappingsAreEditable }: LogTableProps) {
{shownLines.map((fileLine: FileLine, i: number) => {
return <pre key={i} className={classNames(
lineIsHighlighted(fileLine.line)
? "bg-yellow-200"
? eventIsMapped(fileLine.text) ? "bg-yellow-200" : "bg-yellow-100"
: "even:bg-white odd:bg-gray-100",
"w-full py-2 "
)}>{`${fileLines[fileLine.line]}`} </pre>;
Expand All @@ -186,7 +199,7 @@ function LogTable({ mappingsAreEditable }: LogTableProps) {
return (
<div key={fileLine.line} className={classNames(
lineIsHighlighted(fileLine.line)
? "bg-yellow-200"
? eventIsMapped(fileLine.text) ? "bg-yellow-200" : "bg-yellow-100"
: "even:bg-white odd:bg-gray-100",
"flex items-center justify-end gap-1 py-2 pl-2 pr-1")}>
<input
Expand Down Expand Up @@ -223,11 +236,28 @@ function LogTable({ mappingsAreEditable }: LogTableProps) {
</div>
</div>
{!mappingsAreEditable && matches.length > 0 && (
<div id="matches-buttons" className="w-full h-[10%] flex flex-row justify-center gap-20">
<button onClick={() => { setMonaaMatchIndex(monaaMatchIndex === 0 ? 0 : monaaMatchIndex - 1) }}>
<div id="matches-buttons" className="mt-4 w-full h-[10%] flex flex-row justify-center items-center gap-20 ">
<button className="px-4 py-2 text-sm font-medium text-white bg-gray-800 rounded-lg hover:bg-gray-700 "
onClick={() => { setMonaaMatchIndex(monaaMatchIndex === 0 ? 0 : monaaMatchIndex - 1) }}>
Previous match
</button>
<button onClick={() => handeNextMatchClick()}>
<div id="monaa-match-input" className="text-gray-800 ">
{/* TODO:
<input
className="text-right "
type="number"
name="matchIndex"
id=""
value={monaaMatchIndex + 1}
min={1}
max={matches.length}
onChange={(e) => {
setMonaaMatchIndex((e.target.value as unknown as number) - 1);
}} /> */}
{monaaMatchIndex + 1} / {matches.length}
</div>
<button className="px-4 py-2 text-sm font-medium text-white bg-gray-800 rounded-lg hover:bg-gray-700"
onClick={() => handeNextMatchClick()}>
Next match
</button>
</div>
Expand Down
2 changes: 1 addition & 1 deletion TimeTrace/src/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function classNames(...classes: String[]) {
}

function Navbar() {
const { uploadedFile, setUploadedFile } = useContext(AppdataContext);
const { uploadedFile } = useContext(AppdataContext);
const { pathname } = useLocation();

return (
Expand Down
5 changes: 3 additions & 2 deletions TimeTrace/src/components/SearchForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ export default function SearchForm() {
onChange={(e) => setTre(e.target.value)}
required
/>
<button
className="text-white absolute end-2.5 bottom-2.5 bg-gray-800 hover:bg-gray-700 font-medium rounded-lg text-sm px-4 py-2 " type="submit">Search</button>
<button className="text-white absolute end-2.5 bottom-2.5 bg-gray-800 hover:bg-gray-700 font-medium rounded-lg text-sm px-4 py-2 " type="submit">
Search
</button>
</div>
</form>
);
Expand Down
34 changes: 34 additions & 0 deletions TimeTrace/src/context/LogTableContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { createContext, ReactNode, useState } from "react";

export type LogTableContextInterface = {
monaaMatchIndex: number;
setMonaaMatchIndex: React.Dispatch<React.SetStateAction<number>>;
currentPage: number;
setCurrentPage: React.Dispatch<React.SetStateAction<number>>;


}
const defaultState = {
monaaMatchIndex: 0,
setMonaaMatchIndex: (monaaMatchIndex: number) => { },
currentPage: 0,
setCurrentPage: (currentPage: number) => { },

} as LogTableContextInterface;

export const LogTableContext = createContext<LogTableContextInterface>(defaultState);

type LogTableProviderProps = {
children: ReactNode
}

export default function LogTableProvider({ children }: LogTableProviderProps) {
const [monaaMatchIndex, setMonaaMatchIndex] = useState<number>(0);
const [currentPage, setCurrentPage] = useState(0);

return (
< LogTableContext.Provider value={{ monaaMatchIndex, setMonaaMatchIndex, currentPage, setCurrentPage }}>
{children}
</ LogTableContext.Provider>
);
}
2 changes: 2 additions & 0 deletions TimeTrace/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import LogPage from './pages/LogPage';
import Navbar from './components/Navbar';
import AppdataProvider from './context/AppContext';
import Warning from './components/Warning';
import Loader from './components/Loader';

export default function App() {
return (
Expand All @@ -22,6 +23,7 @@ export default function App() {
<Warning />
<Navbar />
<div className="w-full h-[94%] p-5">
<Loader />
<Routes>
<Route path="/" element={<Home />} />
<Route path="/create-mappings" element={<MappingsPage />} />
Expand Down
18 changes: 8 additions & 10 deletions TimeTrace/src/pages/LogPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { useContext, useEffect } from "react";
import LogTable from "../components/LogTable";
import SearchForm from "../components/SearchForm";
import { AppdataContext } from "../context/AppContext";
import Loader from "../components/Loader";
import { navigation } from "../components/Navbar";
import LogTableProvider from '../context/LogTableContext';

function LogPage() {
const { uploadedFile } = useContext(AppdataContext);
const { setError } = useContext(AppdataContext);
const { loading } = useContext(AppdataContext);

useEffect(() => {
if (uploadedFile === null) {
Expand All @@ -26,15 +25,14 @@ function LogPage() {

return (
<div id="log-page" className="h-full gap-5">
<div className="h-[15%]">
<h1 className="flex justify-center pb-5 text-4xl ">Search logfile {uploadedFile?.name}</h1>
<SearchForm />
</div>
<div className="w-full h-[85%]">
{loading ? <Loader /> :
<LogTableProvider>
<div className="h-[15%]">
<SearchForm />
</div>
<div className="w-full h-[85%]">
<LogTable mappingsAreEditable={false} />
}
</div>
</div>
</LogTableProvider>
</div>
);
}
Expand Down
20 changes: 11 additions & 9 deletions TimeTrace/src/pages/MappingsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import FileUploadButton from "../components/FileUploadButton";
import MappedItemsList from "../components/MappedItemsList";
import LogTable from "../components/LogTable";
import LogTableProvider from '../context/LogTableContext';

function MappingsPage() {
return (
<div id="mappings-page" className="flex flex-row h-full gap-5" >
<div className="w-[40%]">
<FileUploadButton />
<div className="h-[90%]">
<MappedItemsList />
<LogTableProvider>
<div className="w-[40%]">
<FileUploadButton />
<div className="h-[90%]">
<MappedItemsList />
</div>
</div>

</div>
<div className="w-[60%] h-full">
<LogTable mappingsAreEditable={true} />
</div>
<div className="w-[60%] h-full">
<LogTable mappingsAreEditable={true} />
</div>
</LogTableProvider>
</div>
);
}
Expand Down

0 comments on commit 8136bf6

Please sign in to comment.