Skip to content

Commit

Permalink
Merge pull request #700 from NoodleOfDeath/dev
Browse files Browse the repository at this point in the history
~api
  • Loading branch information
NoodleOfDeath authored Oct 16, 2023
2 parents e571445 + 964dc76 commit 8ad2df6
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DROP VIEW IF EXISTS summary_media_view;
DROP MATERIALIZED VIEW IF EXISTS summary_media_view;

CREATE MATERIALIZED VIEW summary_media_view AS
SELECT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DROP VIEW IF EXISTS summary_sentiment_view;
DROP MATERIALIZED VIEW IF EXISTS summary_sentiment_view;

CREATE MATERIALIZED VIEW summary_sentiment_view AS
SELECT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DROP VIEW IF EXISTS summary_translation_view;
DROP MATERIALIZED VIEW IF EXISTS summary_translation_view;

CREATE MATERIALIZED VIEW summary_translation_view AS
SELECT
Expand Down
44 changes: 28 additions & 16 deletions src/server/src/api/v1/schema/resources/summary/Summary.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ function parseTimeInterval(str: string) {
}
}

function applyFilter(
filter = '',
matchType: 'any' | 'all' = 'any'
function buildFilter(
filter = '',
matchType: 'any' | 'exact' = 'exact'
) {
const categories: string[] = [];
const excludedCategories: string[] = [];
Expand All @@ -109,7 +109,6 @@ function applyFilter(
const [_, exclude, prefix, prefixValues] = match;
const pf = prefixValues.split(',');
if (/^(?:source|src|pub(lisher)?)$/i.test(prefix)) {
console.log(match);
if (exclude) {
excludedPublishers.push(...pf);
} else {
Expand Down Expand Up @@ -141,24 +140,25 @@ function applyFilter(
query = timeMatches[1];
}
}
const matches =
query.replace(/\s\s+/g, ' ')
.replace(/[-+*|=<>.^$!?(){}[\]\\]/g, ($0) => `\\${$0}`)
.matchAll(/(['"])(.+?)\1|\b([\S]+)\b/gm);
if (matches) {
const subqueries = [...matches].map((match) => (match[1] ? match[2] : match[3]).replace(/['"]/g, ($0) => `\\${$0}`));
if (matchType === 'all') {
//
} else {
query = query
.replace(/\s\s+/g, ' ')
.replace(/[-+*|=<>.^$!?(){}[\]\\]/g, ($0) => `\\${$0}`);
if (matchType === 'exact') {
parts.push(`(?:(?:^|\\y)${query.replace(/['"]/g, ($0) => `\\${$0}`)}(?:\\y|$))`);
} else {
const matches = query.matchAll(/(['"])(.+?)\1|\b([\S]+)\b/gm);
if (matches) {
const subqueries = [...matches].map((m) => (m[1] ? m[2] : m[3]).replace(/['"]/g, ($0) => `\\${$0}`));
parts.push(...subqueries.map((subquery) => `(?:(?:^|\\y)${subquery}(?:\\y|$))`));
}
}
}
const regex = parts.join('|');
return {
categories,
excludedCategories,
excludedPublishers,
filter: parts.join('|'),
filter: regex.length > 2 ? regex : '',
interval,
publishers,
};
Expand Down Expand Up @@ -281,7 +281,7 @@ export class Summary extends Post<SummaryAttributes, SummaryCreationAttributes>
publishers,
interval: pastInterval,
filter: query,
} = applyFilter(filter, matchType);
} = buildFilter(filter, 'exact');

const startDate = parseDate(start) ? parseDate(start) : end !== undefined ? new Date(0) : undefined;
const endDate = parseDate(end) ? parseDate(end) : start !== undefined ? new Date() : undefined;
Expand Down Expand Up @@ -338,7 +338,7 @@ export class Summary extends Post<SummaryAttributes, SummaryCreationAttributes>
const siblings: PublicSummaryGroup[] = [];
const fetch = async (previousRecords: PublicSummaryGroup[] = []) => {

const records = ((await this.store.query(QUERIES[queryKey], {
let records = ((await this.store.query(QUERIES[queryKey], {
nest: true,
replacements,
type: QueryTypes.SELECT,
Expand All @@ -352,6 +352,18 @@ export class Summary extends Post<SummaryAttributes, SummaryCreationAttributes>
};
}

// If exact search fails search partial words
if (records.rows.length === 0) {
const { filter: partialFilter } = buildFilter(filter, 'any');
replacements.filter = partialFilter;
records = ((await this.store.query(QUERIES[queryKey], {
nest: true,
replacements,
type: QueryTypes.SELECT,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
})) as BulkMetadataResponse<PublicSummaryGroup, { sentiment: number }>[])[0];
}

if (filter || records.rows.length < replacements.limit) {
return records;
}
Expand Down
27 changes: 27 additions & 0 deletions src/server/tests/publisher.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'dotenv/config';

import {
describe,
expect,
jest,
test,
} from '@jest/globals';

import { DBService, S3Service } from '../src/services';
import { Publisher } from '../src/api/v1/schema';

jest.setTimeout(30_000);

describe('publisher', () => {

test('sample', async () => {
await DBService.prepare();
await Publisher.prepare();
const publishers = await Publisher.findAll();
for (const publisher of publishers) {
const icon = await S3Service.download(publisher.icon);
console.log(icon);
}
});

});

0 comments on commit 8ad2df6

Please sign in to comment.