Skip to content

Commit

Permalink
125 show old data from yesterday (#129)
Browse files Browse the repository at this point in the history
* [Change] #123, to see real user ip instead of localhost proxy

* [Task] #125, show old data

* [fix] #77, update marker cluster design
  • Loading branch information
Type-Style authored Sep 2, 2024
1 parent e17dfb2 commit e5fb3ab
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/client/css/map.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
filter: drop-shadow(0px 0px 3px var(--neutral));
}

.marker-cluster-small[class] { /* overwrite default cluster style */
.marker-cluster[class] { /* overwrite default cluster style */
background: none;
div {
background-color: var(--semiContrastBackground);
Expand Down
5 changes: 4 additions & 1 deletion src/controller/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ router.get('/',
return createError(res, 400, JSON.stringify({ errors: errors.array() }), next)
}

const fileObj: File.Obj = file.getFile(res, next);
const fileObj: File.Obj = file.getFile(res, next, "read");
// no content and no file found show empty data and exit
if (fileObj.content == false) { res.json(JSON.parse('{"entries": []}')); return }

fileObj.content = await file.readAsJson(res, fileObj.path, next)
if (!fileObj.content || !Array.isArray(fileObj.content.entries)) {
return createError(res, undefined, `File corrupt: ${fileObj.path}`, next);
Expand Down
9 changes: 5 additions & 4 deletions src/models/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import logger from '@src/scripts/logger';

export const entry = {
create: async (req: Request, res: Response, next: NextFunction) => {
const fileObj: File.Obj = file.getFile(res, next);
const fileObj: File.Obj = file.getFile(res, next, "write");
if (!fileObj.content) { return createError(res, 500, "File does not exist: " + fileObj.path, next); }

fileObj.content = await file.readAsJson(res, fileObj.path, next);

if (!fileObj.content?.entries) {
return createError(res, 500, "File Content unavailable: " + fileObj.path, next);
}
if (!fileObj.content?.entries) {return createError(res, 500, "File Content unavailable: " + fileObj.path, next); }

const entries = fileObj.content.entries;
const lastEntry = fileObj.content.entries.at(-1);
let previousEntry = fileObj.content.entries.at(-1); // potentially overwritten if entry is set to ignore
Expand Down
69 changes: 55 additions & 14 deletions src/scripts/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,36 @@ import { create as createError } from '@src/middleware/error';
import { NextFunction, Response } from 'express';
import logger from '@src/scripts/logger';

export const getFile = (res: Response, next: NextFunction): File.Obj => {
export const getFile = (res: Response, next: NextFunction, method: File.method): File.Obj => {
const date = new Date();
const formattedDate = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
const dirPath = path.resolve(__dirname, '../data');
const filePath = path.resolve(dirPath, `data-${formattedDate}.json`);
let filePath = path.resolve(dirPath, `data-${formattedDate}.json`);

if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
logger.log("data folder did not exist, but created now");
}

let fileExisted = true;
if (!fs.existsSync(filePath)) { // check if file exist
let olderFile = false;
if (!fs.existsSync(filePath)) { // if file does not exist
fileExisted = false;
try {
fs.writeFileSync(filePath, '{"entries": []}');
logger.log(`file: ${filePath} did not exist, but created now`);
} catch (err) {
createError(res, 500, "File cannot be written to", next);
const mostRecentFile = findMostRecentFile(dirPath);
if (method == "read" && mostRecentFile) { // when reading check old files
olderFile = true;
filePath = mostRecentFile;
} else if (method == "write") {
try {
fs.writeFileSync(filePath, '{"entries": []}');
logger.log(`file: ${filePath} did not exist, but created now`);
} catch (err) {
createError(res, 500, "File cannot be written to", next);
}
}
}

return { path: filePath, content: fileExisted ? undefined : JSON.parse('{"entries": []}') }; // if the file did not exist before, the content is emptyString
return { path: filePath, content: method == "read" ? (fileExisted || olderFile) : JSON.parse('{"entries": []}') };
};


Expand All @@ -39,24 +46,58 @@ export async function readAsJson(res: Response, filePath: string, next: NextFunc
try {
return JSON.parse(data);
} catch (err) {
createError(res, 500, "File contains wrong content: " + filePath, next);
createError(res, 500, "File contains wrong content: " + path.basename(filePath), next);
}
}


export const write = (res:Response, fileObj:File.Obj, next: NextFunction) => {
export const write = (res: Response, fileObj: File.Obj, next: NextFunction) => {

if (!fs.existsSync(fileObj.path)) { // check if file exist
createError(res, 500, "Can not write to file that does not exist: " + fileObj.path, next);
}

if (typeof fileObj.content == "boolean") {
createError(res, 500, `File (${fileObj.path}) cannot be written to, contents are not correct type`, next);
}

try {
const content = JSON.stringify(fileObj.content, undefined, 2);
fs.writeFileSync(fileObj.path, content);
fileObj.content = JSON.parse(content);
logger.log(`written to file: ${fileObj.path} ${fileObj.content ? fileObj.content?.entries.length - 1 : ''}`);
if (typeof fileObj.content != "boolean") {
logger.log(`written to file: ${fileObj.path} ${fileObj.content?.entries ? fileObj.content.entries.length - 1 : ''}`);
}
} catch (err) {
createError(res, 500, `File (${fileObj.path}) cannot be written to`, next);
}
}


return fileObj; // if the file did not exist before, the content is emptyString
return fileObj;
};

const findMostRecentFile = (directoryPath: string) => {
// read all files from the directory
const files = fs.readdirSync(directoryPath);

// initialize variables to keep track of the most recent file
let mostRecentFile = null;
let mostRecentTime = 0;

files.forEach((file) => {
const filePath = path.join(directoryPath, file);

// get file stats (including modified time)
const stats = fs.statSync(filePath);

// check if it is a file (and not a directory) and most recent
if (stats.isFile() && stats.mtimeMs > mostRecentTime) {
if (stats.mtimeMs > mostRecentTime) {
mostRecentTime &&= stats.mtimeMs;
mostRecentFile = filePath;
}
}
});

return mostRecentFile;
}
2 changes: 1 addition & 1 deletion src/tests/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ describe('API calls', () => {
const response = await axios.get(url);
expect(response.status).toBe(200);
}
}, 20000); // adjust this to to fit your setup
}, 22000); // adjust this to to fit your setup

test(`length of json should not exceed 1000`, async () => {
const date = new Date();
Expand Down
4 changes: 3 additions & 1 deletion types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ namespace Response {
namespace File {
interface Obj {
path: string,
content?: Models.IEntries;
content?: Models.IEntries | boolean;
}

type method = 'read' | 'write';
}

namespace Models {
Expand Down

0 comments on commit e5fb3ab

Please sign in to comment.