Skip to content

Commit

Permalink
backend lint
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaLRussell committed Oct 27, 2023
1 parent c32b02e commit 26f5001
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 27 deletions.
18 changes: 12 additions & 6 deletions app/server/src/db/sessionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as md5 from "md5";
import { generateId } from "zoo-ids";
import { Request } from "express";
import { AppLocals, SessionMetadata } from "../types";
import {Session} from "inspector";

export const cleanFriendlyId = (id: string): string => {
let ret = id.toLowerCase();
Expand Down Expand Up @@ -42,10 +41,14 @@ export class SessionStore {

private sessionKey = (name: string) => `${this._sessionPrefix}${name}`;

private hashForData = (data: string) => md5(data);
private static hashForData = (data: string) => md5(data);

private saveMissingSessionHash = async (sessionId: string, hash: string) => {
await this._redis.hset(this.sessionKey("hash"), sessionId, hash);
};

async saveSession(id: string, data: string) {
const hash = this.hashForData(data);
const hash = SessionStore.hashForData(data);
await this._redis.pipeline()
.hset(this.sessionKey("time"), id, new Date(Date.now()).toISOString())
.hset(this.sessionKey("data"), id, data)
Expand Down Expand Up @@ -75,7 +78,9 @@ export class SessionStore {
const labelledSessions: SessionMetadata[] = [];
const latestByHash: Record<string, SessionMetadata> = {};

for (const [idx, id] of ids.entries()) {
const saveMissingHashes = [];
// eslint-disable-next-line no-restricted-syntax
for await (const [idx, id] of ids.entries()) {
if (times[idx] !== null) {
const session = buildSessionMetadata(id, idx) as SessionMetadata;

Expand All @@ -88,14 +93,15 @@ export class SessionStore {
if (hash === null) {
const data = await this.getSession(id);
hash = this.hashForData(data!);
await this._redis.hset(this.sessionKey("hash"), id, hash);
saveMissingHashes.push(this.saveMissingSessionHash(id, hash));
}

if (!latestByHash[hash] || session.time > latestByHash[hash].time) {
latestByHash[hash] = session;
}
}
}
await Promise.all(saveMissingHashes);
allResults = [
...labelledSessions,
...Object.values(latestByHash).filter((s) => !s.label) // don't include labelled sessions twice
Expand All @@ -105,7 +111,7 @@ export class SessionStore {
allResults = ids.map((id: string, idx: number) => buildSessionMetadata(id, idx))
.filter((session) => session.time !== null);
}
return allResults.sort((a, b) => a.time!! < b.time!! ? 1 : -1) as SessionMetadata[];
return allResults.sort((a, b) => (a.time!! < b.time!! ? 1 : -1)) as SessionMetadata[];
});
}

Expand Down
99 changes: 78 additions & 21 deletions app/server/tests/db/sessionStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@ describe("generate friendly id", () => {
});

describe("SessionStore handles duplicate sessions", () => {
const mockRedis = (savedHmGetValues: Record<string, (string | null)[]>, savedHGetValues: Record<string, string> = {}) => {
const mockRedis = (
savedHmGetValues: Record<string, (string | null)[]>,
savedHGetValues: Record<string, string> = {}
) => {
return {
hmget: jest.fn().mockImplementation(async (sessionKey: string) => {
const key = sessionKey.split(":").slice(-1)[0];
Expand All @@ -217,7 +220,13 @@ describe("SessionStore handles duplicate sessions", () => {

it("Filters out earlier duplicate sessions if removeDuplicates is true", async () => {
const savedValues = {
time: ["2023-10-25 11:10:01", "2023-10-25 11:10:02", "2023-10-25 11:10:03", "2023-10-25 11:10:04", "2023-10-25 11:10:05"],
time: [
"2023-10-25 11:10:01",
"2023-10-25 11:10:02",
"2023-10-25 11:10:03",
"2023-10-25 11:10:04",
"2023-10-25 11:10:05"
],
label: [null, null, null, null, null],
friendly: ["good dog", "bad cat", "devious chaffinch", "happy bat", "sad owl"],
hash: ["123", "234", "567", "234", "123"]
Expand All @@ -235,15 +244,27 @@ describe("SessionStore handles duplicate sessions", () => {

// results should be ordered chrono desc
expect(result).toStrictEqual([
{id: "mno", time: "2023-10-25 11:10:05", label: null, friendlyId: "sad owl"},
{id: "jkl", time: "2023-10-25 11:10:04", label: null, friendlyId: "happy bat"},
{id: "ghi", time: "2023-10-25 11:10:03", label: null, friendlyId: "devious chaffinch"}
{
id: "mno", time: "2023-10-25 11:10:05", label: null, friendlyId: "sad owl"
},
{
id: "jkl", time: "2023-10-25 11:10:04", label: null, friendlyId: "happy bat"
},
{
id: "ghi", time: "2023-10-25 11:10:03", label: null, friendlyId: "devious chaffinch"
}
]);
});

it("Does not filter out earlier duplicate sessions if removeDuplicates is false", async () => {
const savedValues = {
time: ["2023-10-25 11:10:01", "2023-10-25 11:10:02", "2023-10-25 11:10:03", "2023-10-25 11:10:04", "2023-10-25 11:10:05"],
time: [
"2023-10-25 11:10:01",
"2023-10-25 11:10:02",
"2023-10-25 11:10:03",
"2023-10-25 11:10:04",
"2023-10-25 11:10:05"
],
label: [null, null, null, null, null],
friendly: ["good dog", "bad cat", "devious chaffinch", "happy bat", "sad owl"],
hash: ["123", "234", "567", "234", "123"]
Expand All @@ -260,17 +281,33 @@ describe("SessionStore handles duplicate sessions", () => {
expect(redis.hmget).toHaveBeenNthCalledWith(4, `${sessionKeyPrefix}hash`, ...ids);

expect(result).toStrictEqual([
{id: "mno", time: "2023-10-25 11:10:05", label: null, friendlyId: "sad owl"},
{id: "jkl", time: "2023-10-25 11:10:04", label: null, friendlyId: "happy bat"},
{id: "ghi", time: "2023-10-25 11:10:03", label: null, friendlyId: "devious chaffinch"},
{id: "def", time: "2023-10-25 11:10:02", label: null, friendlyId: "bad cat"},
{id: "abc", time: "2023-10-25 11:10:01", label: null, friendlyId: "good dog"}
{
id: "mno", time: "2023-10-25 11:10:05", label: null, friendlyId: "sad owl"
},
{
id: "jkl", time: "2023-10-25 11:10:04", label: null, friendlyId: "happy bat"
},
{
id: "ghi", time: "2023-10-25 11:10:03", label: null, friendlyId: "devious chaffinch"
},
{
id: "def", time: "2023-10-25 11:10:02", label: null, friendlyId: "bad cat"
},
{
id: "abc", time: "2023-10-25 11:10:01", label: null, friendlyId: "good dog"
}
]);
});

it("returns duplicates if they have labels", async () => {
const savedValues = {
time: ["2023-10-25 11:10:01", "2023-10-25 11:10:02", "2023-10-25 11:10:03", "2023-10-25 11:10:04", "2023-10-25 11:10:05"],
time: [
"2023-10-25 11:10:01",
"2023-10-25 11:10:02",
"2023-10-25 11:10:03",
"2023-10-25 11:10:04",
"2023-10-25 11:10:05"
],
label: ["oldest session", null, null, "newer session", null],
friendly: ["good dog", "bad cat", "devious chaffinch", "happy bat", "sad owl"],
hash: ["123", "234", "567", "234", "123"]
Expand All @@ -281,10 +318,18 @@ describe("SessionStore handles duplicate sessions", () => {
const result = await sut.getSessionsMetadata(ids, true);

expect(result).toStrictEqual([
{id: "mno", time: "2023-10-25 11:10:05", label: null, friendlyId: "sad owl"},
{id: "jkl", time: "2023-10-25 11:10:04", label: "newer session", friendlyId: "happy bat"},
{id: "ghi", time: "2023-10-25 11:10:03", label: null, friendlyId: "devious chaffinch"},
{id: "abc", time: "2023-10-25 11:10:01", label: "oldest session", friendlyId: "good dog"}
{
id: "mno", time: "2023-10-25 11:10:05", label: null, friendlyId: "sad owl"
},
{
id: "jkl", time: "2023-10-25 11:10:04", label: "newer session", friendlyId: "happy bat"
},
{
id: "ghi", time: "2023-10-25 11:10:03", label: null, friendlyId: "devious chaffinch"
},
{
id: "abc", time: "2023-10-25 11:10:01", label: "oldest session", friendlyId: "good dog"
}
]);
});

Expand All @@ -297,13 +342,19 @@ describe("SessionStore handles duplicate sessions", () => {
const sessionContent2Hash = md5(sessionContent2);

const savedValues = {
time: ["2023-10-25 11:10:01", "2023-10-25 11:10:02", "2023-10-25 11:10:03", "2023-10-25 11:10:04", "2023-10-25 11:10:05"],
time: [
"2023-10-25 11:10:01",
"2023-10-25 11:10:02",
"2023-10-25 11:10:03",
"2023-10-25 11:10:04",
"2023-10-25 11:10:05"
],
label: [null, null, null, null, null],
friendly: ["good dog", "bad cat", "devious chaffinch", "happy bat", "sad owl"],
hash: [sessionContent1Hash, null, "567", sessionContent2Hash, null]
};

const redis = mockRedis(savedValues, {def: sessionContent2, mno: sessionContent1});
const redis = mockRedis(savedValues, { def: sessionContent2, mno: sessionContent1 });
const sut = new SessionStore(redis, "Test Course", "testApp");
const ids = ["abc", "def", "ghi", "jkl", "mno"];
const result = await sut.getSessionsMetadata(ids, true);
Expand All @@ -316,9 +367,15 @@ describe("SessionStore handles duplicate sessions", () => {

// results should be ordered chrono desc
expect(result).toStrictEqual([
{id: "mno", time: "2023-10-25 11:10:05", label: null, friendlyId: "sad owl"},
{id: "jkl", time: "2023-10-25 11:10:04", label: null, friendlyId: "happy bat"},
{id: "ghi", time: "2023-10-25 11:10:03", label: null, friendlyId: "devious chaffinch"}
{
id: "mno", time: "2023-10-25 11:10:05", label: null, friendlyId: "sad owl"
},
{
id: "jkl", time: "2023-10-25 11:10:04", label: null, friendlyId: "happy bat"
},
{
id: "ghi", time: "2023-10-25 11:10:03", label: null, friendlyId: "devious chaffinch"
}
]);
});
});
Expand Down

0 comments on commit 26f5001

Please sign in to comment.