-
Notifications
You must be signed in to change notification settings - Fork 444
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* [gh-#632] add migration, service, and model * [gh-#632] use db for storage and retrieval * [gh-#632] oauth refactor * [gh-#632] update models and methods * [gh-#632] add sync type and remove commented out messages * [gh-#632] remove log compose file * [gh-#632] add index on account_id field * [gh-#632] single query using json_agg * [gh-#632] add cron deletion and notice about the deletion process
- Loading branch information
1 parent
9ad2a2b
commit 3a254b3
Showing
17 changed files
with
685 additions
and
455 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,19 @@ | ||
import type { Request, Response } from 'express'; | ||
import type { NextFunction } from 'express'; | ||
import fs from 'fs'; | ||
|
||
import { FILENAME, LogData } from '../utils/file-logger.js'; | ||
import { getUserAndAccountFromSession } from '../utils/utils.js'; | ||
import { getLogsByAccount } from '../services/activity.service.js'; | ||
|
||
class ActivityController { | ||
/** | ||
* Retrieve | ||
* @desc read a file path and send back the contents of the file | ||
* @param {Request} req Express request object | ||
* @param {Response} res Express response object | ||
* @param {NextFuncion} next callback function to pass control to the next middleware function in the pipeline. | ||
*/ | ||
public async retrieve(_req: Request, res: Response, next: NextFunction) { | ||
public async retrieve(req: Request, res: Response, next: NextFunction) { | ||
try { | ||
const filePath = `./${FILENAME}`; | ||
if (fs.existsSync(filePath)) { | ||
const fileContents = fs.readFileSync(filePath, 'utf8'); | ||
|
||
if (fileContents.length === 0) { | ||
res.send([]); | ||
} else { | ||
const mergedLogs = this.mergeSessions(JSON.parse(fileContents) as unknown as LogData[]); | ||
res.send(mergedLogs); | ||
} | ||
} | ||
const account = (await getUserAndAccountFromSession(req)).account; | ||
const logs = await getLogsByAccount(account.id); | ||
res.send(logs); | ||
} catch (error) { | ||
next(error); | ||
} | ||
} | ||
|
||
/** | ||
* Merge Sessions | ||
* @desc append any messages of oauth continuation entries that have a merge property of true | ||
* to an existing session id and update the end time while maintaing | ||
* log ordering | ||
*/ | ||
private mergeSessions(logs: LogData[]) { | ||
const updatedLogs: LogData[] = []; | ||
const sessions: Record<string, number> = {}; | ||
|
||
for (let i = 0; i < logs.length; i++) { | ||
const log = logs[i]; | ||
|
||
if (log?.sessionId && !log.merge) { | ||
sessions[log.sessionId] = i; | ||
} | ||
|
||
if (log?.merge && typeof sessions[log.sessionId as string] !== 'undefined') { | ||
const mergeIndex: number = updatedLogs.findIndex((updated) => { | ||
return updated.sessionId === log.sessionId && !updated.merge; | ||
}); | ||
updatedLogs[mergeIndex]!.messages = [...updatedLogs[mergeIndex]!.messages, ...log.messages]; | ||
updatedLogs[mergeIndex]!.end = log.end as number; | ||
updatedLogs[mergeIndex]!.success = log.success; | ||
} else { | ||
updatedLogs.push(log as LogData); | ||
} | ||
} | ||
return updatedLogs.reverse(); | ||
} | ||
} | ||
|
||
export default new ActivityController(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.