Skip to content

Commit

Permalink
refactor: export redis scripts as single object
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-yarmosh committed Dec 20, 2023
1 parent fb241d7 commit 8ec786c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
4 changes: 1 addition & 3 deletions probes-stats/known-probes.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
[
{
"ip": "exampleIp",
"city": "exampleCity",
"country": "exampleCountry",
"note": "exampleNote"
"city": "exampleCity"
}
]
4 changes: 2 additions & 2 deletions src/lib/redis/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
type RedisFunctions,
} from 'redis';
import { scopedLogger } from '../logger.js';
import { count, recordResult, markFinished, type RedisScripts } from './scripts.js';
import { scripts, type RedisScripts } from './scripts.js';

const logger = scopedLogger('redis-client');

Expand All @@ -26,7 +26,7 @@ const createRedisClient = async (options?: RedisClientOptions): Promise<RedisCli
...options,
database: 0,
name: 'non-persistent',
scripts: { count, recordResult, markFinished },
scripts,
});

client
Expand Down
4 changes: 2 additions & 2 deletions src/lib/redis/persistent-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
type RedisFunctions,
} from 'redis';
import { scopedLogger } from '../logger.js';
import { count, recordResult, markFinished, type RedisScripts } from './scripts.js';
import { scripts, type RedisScripts } from './scripts.js';

const logger = scopedLogger('persistent-redis-client');

Expand All @@ -26,7 +26,7 @@ export const createPersistentRedisClient = async (options?: RedisClientOptions):
...options,
database: 1,
name: 'persistent',
scripts: { count, recordResult, markFinished },
scripts,
});

client
Expand Down
12 changes: 7 additions & 5 deletions src/lib/redis/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type CountScript = {
SHA1: string;
};

export type RecordResultScript = {
type RecordResultScript = {
NUMBER_OF_KEYS: number;
SCRIPT: string;
transformArguments (measurementId: string, testId: string, data: MeasurementResultMessage['result']): string[];
Expand All @@ -19,7 +19,7 @@ export type RecordResultScript = {
SHA1: string;
};

export type MarkFinishedScript = {
type MarkFinishedScript = {
NUMBER_OF_KEYS: number;
SCRIPT: string;
transformArguments (measurementId: string): string[];
Expand All @@ -34,7 +34,7 @@ export type RedisScripts = {
markFinished: MarkFinishedScript;
};

export const count: CountScript = defineScript({
const count: CountScript = defineScript({
NUMBER_OF_KEYS: 1,
SCRIPT: `
local cursor = 0
Expand All @@ -55,7 +55,7 @@ export const count: CountScript = defineScript({
},
});

export const recordResult: RecordResultScript = defineScript({
const recordResult: RecordResultScript = defineScript({
NUMBER_OF_KEYS: 4,
SCRIPT: `
local measurementId = KEYS[1]
Expand Down Expand Up @@ -88,7 +88,7 @@ export const recordResult: RecordResultScript = defineScript({
},
});

export const markFinished: MarkFinishedScript = defineScript({
const markFinished: MarkFinishedScript = defineScript({
NUMBER_OF_KEYS: 1,
SCRIPT: `
local measurementId = KEYS[1]
Expand All @@ -106,3 +106,5 @@ export const markFinished: MarkFinishedScript = defineScript({
return null;
},
});

export const scripts: RedisScripts = { count, recordResult, markFinished };
11 changes: 9 additions & 2 deletions test/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import {
populateNockCitiesList,
} from './utils/populate-static-files.js';
import chaiOas from './plugins/oas/index.js';
import { initRedisClient } from '../src/lib/redis/client.js';
import { initPersistentRedisClient } from '../src/lib/redis/persistent-client.js';
import { getRedisClient, initRedisClient } from '../src/lib/redis/client.js';
import { getPersistentRedisClient, initPersistentRedisClient } from '../src/lib/redis/persistent-client.js';
import { client as sql } from '../src/lib/sql/client.js';

const dbConfig = config.get<{ connection: { database: string, host: string } }>('db');
Expand Down Expand Up @@ -46,6 +46,13 @@ before(async () => {
await populateNockCitiesList();
});

after(async () => {
const redisClient = getRedisClient();
await redisClient.flushDb();
const persistentRedisClient = await getPersistentRedisClient();
await persistentRedisClient.flushDb();
});

const dropAllTables = async (sql: Knex) => {
const allTables = (await sql('information_schema.tables')
.whereRaw(`table_schema = database()`)
Expand Down

0 comments on commit 8ec786c

Please sign in to comment.