Skip to content

Commit

Permalink
simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
siiptuo committed Nov 27, 2024
1 parent e1d6836 commit 3f654f8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
18 changes: 7 additions & 11 deletions backend/src/routes/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class FileRoutes {
const repo = file instanceof RegularFile ? this.fileRepo : this.modelFileRepo;
const versions = await repo.find({
select,
where: { s3key: s3Key(file), tombstoneReason: IsNull() },
where: { filename: file.filename, tombstoneReason: IsNull() },
order: { createdAt: "DESC" },
});
res.send(versions);
Expand Down Expand Up @@ -222,7 +222,7 @@ export class FileRoutes {
);
return qb
.leftJoinAndSelect("file.site", "site")
.where("regexp_replace(s3key, '.+/', '') = :filename", { filename: basename(file.s3key) }) // eslint-disable-line quotes
.where("file.filename = :filename", { filename: file.filename })
.getOne();
};
const existingFile = await findFileByName(isModel);
Expand Down Expand Up @@ -375,7 +375,7 @@ export class FileRoutes {
// Hack to prevent loading of model files when instrument is selected without product
if (isModel && (query.instrument || query.instrumentPid)) qb.andWhere("1 = 0");

if (query.filename) qb.andWhere("regexp_replace(s3key, '.+/', '') IN (:...filename)", query); // eslint-disable-line quotes
if (query.filename) qb.andWhere("filename IN (:...filename)", query);
if (query.releasedBefore) qb.andWhere("file.updatedAt < :releasedBefore", query);
if (query.updatedAtFrom) qb.andWhere("file.updatedAt >= :updatedAtFrom", query);
if (query.updatedAtTo) qb.andWhere("file.updatedAt <= :updatedAtTo", query);
Expand Down Expand Up @@ -506,7 +506,7 @@ export class FileRoutes {

async fetchValidVersions(queryRunner: QueryRunner, file: File) {
return await queryRunner.manager.find(RegularFile, {
where: { filename: s3Key(file), tombstoneReason: IsNull() },
where: { filename: file.filename, tombstoneReason: IsNull() },
relations: { product: true, site: true },
order: { createdAt: "DESC" },
});
Expand Down Expand Up @@ -576,11 +576,7 @@ function addCommonFilters(qb: any, query: any) {
}

function isValidFilename(file: any) {
const [date, site] = basename(file.s3key).split(".")[0].split("_");
return file.measurementDate.replace(/-/g, "") == date && (file.site == site || typeof file.site == "object");
}

function s3Key(file: File) {
// Handle legacy filenames with 'legacy/' prefix.
return Raw((alias) => `regexp_replace(${alias}, '.+/', '') = :filename`, { filename: file.filename });
if (!file.filename) return false;
const [date, site] = file.filename.split(".")[0].split("_");
return file.measurementDate.replace(/-/g, "") == date && file.site == site;
}
1 change: 1 addition & 0 deletions backend/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ async function createServer(): Promise<void> {
err,
}),
);
console.error(err);
if (!res.headersSent) {
delete err.params;
const status = err.status || 500;
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/integration/sequential/sites.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async function putFile(data: { site: string; product: string; measurementDate: s
product: data.product,
createdAt: "2020-02-20T10:56:19.382Z",
updatedAt: "2020-02-20T10:56:19.382Z",
s3key: s3key,
filename: s3key,
checksum: crypto.randomBytes(20).toString("hex"),
size: 12200657,
format: "HDF5 (NetCDF4)",
Expand Down

0 comments on commit 3f654f8

Please sign in to comment.