-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/development' into 7085-ix-status…
…es-revamp-dev
- Loading branch information
Showing
66 changed files
with
1,323 additions
and
321 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
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
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
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
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
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
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 |
---|---|---|
|
@@ -3,9 +3,7 @@ name: eslint | |
on: | ||
push: | ||
branches: | ||
- production | ||
- development | ||
- staging | ||
pull_request: | ||
|
||
concurrency: | ||
|
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
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 |
---|---|---|
|
@@ -3,9 +3,7 @@ name: check-types | |
on: | ||
push: | ||
branches: | ||
- production | ||
- development | ||
- staging | ||
pull_request: | ||
|
||
concurrency: | ||
|
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 |
---|---|---|
|
@@ -3,9 +3,7 @@ name: Unit tests | |
on: | ||
push: | ||
branches: | ||
- production | ||
- development | ||
- staging | ||
pull_request: | ||
|
||
concurrency: | ||
|
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { FilesDataSource } from './contracts/FilesDataSource'; | ||
import { FileStorage } from './contracts/FileStorage'; | ||
import { URLAttachment } from './model/URLAttachment'; | ||
|
||
function filterFilesInStorage(files: string[]) { | ||
return files.filter( | ||
file => | ||
!file.includes('/log/') && !file.includes('/segmentation/') && !file.includes('index.html') | ||
); | ||
} | ||
|
||
export class FilesHealthCheck { | ||
// eslint-disable-next-line class-methods-use-this | ||
private onMissingInDBCB: (filename: string) => void = () => {}; | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
private onMissingInStorageCB: (fileDTO: { _id: string; filename: string }) => void = () => {}; | ||
|
||
private fileStorage: FileStorage; | ||
|
||
private filesDS: FilesDataSource; | ||
|
||
constructor(fileStorage: FileStorage, filesDS: FilesDataSource) { | ||
this.fileStorage = fileStorage; | ||
this.filesDS = filesDS; | ||
} | ||
|
||
async execute() { | ||
const allFilesInDb = await this.filesDS.getAll().all(); | ||
const allFilesInStorage = await this.fileStorage.list(); | ||
const filteredFilesInStorage = new Set(filterFilesInStorage(allFilesInStorage)); | ||
let missingInStorage = 0; | ||
const missingInStorageList: string[] = []; | ||
const missingInDbList: string[] = []; | ||
const countInStorage = filteredFilesInStorage.size; | ||
let countInDb = 0; | ||
|
||
allFilesInDb.forEach(file => { | ||
countInDb += 1; | ||
const existsInStorage = filteredFilesInStorage.delete(this.fileStorage.getPath(file)); | ||
|
||
if (!existsInStorage && !(file instanceof URLAttachment)) { | ||
missingInStorage += 1; | ||
missingInStorageList.push(this.fileStorage.getPath(file)); | ||
this.onMissingInStorageCB({ _id: file.id, filename: file.filename }); | ||
} | ||
}); | ||
|
||
filteredFilesInStorage.forEach(file => { | ||
missingInDbList.push(file); | ||
this.onMissingInDBCB(file); | ||
}); | ||
|
||
return { | ||
missingInStorageList, | ||
missingInStorage, | ||
missingInDbList, | ||
missingInDb: filteredFilesInStorage.size, | ||
countInDb, | ||
countInStorage, | ||
}; | ||
} | ||
|
||
onMissingInDB(cb: (filename: string) => void) { | ||
this.onMissingInDBCB = cb; | ||
} | ||
|
||
onMissingInStorage(cb: (fileDTO: { _id: string; filename: string }) => void) { | ||
this.onMissingInStorageCB = cb; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { UwaziFile } from '../model/UwaziFile'; | ||
|
||
export interface FileStorage { | ||
list(): Promise<string[]>; | ||
getPath(file: UwaziFile): string; | ||
} |
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,3 +1,7 @@ | ||
import { ResultSet } from 'api/common.v2/contracts/ResultSet'; | ||
import { UwaziFile } from '../model/UwaziFile'; | ||
|
||
export interface FilesDataSource { | ||
filesExistForEntities(files: { entity: string; _id: string }[]): Promise<boolean>; | ||
getAll(): ResultSet<UwaziFile>; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// import { OptionalId } from 'mongodb'; | ||
|
||
import { FileDBOType } from './schemas/filesTypes'; | ||
import { UwaziFile } from '../model/UwaziFile'; | ||
import { Document } from '../model/Document'; | ||
import { URLAttachment } from '../model/URLAttachment'; | ||
import { Attachment } from '../model/Attachment'; | ||
import { CustomUpload } from '../model/CustomUpload'; | ||
|
||
export const FileMappers = { | ||
// toDBO(file: UwaziFile): OptionalId<FileDBOType> { | ||
// return { | ||
// filename: file.filename, | ||
// entity: file.entity, | ||
// type: 'document', | ||
// totalPages: file.totalPages, | ||
// }; | ||
// }, | ||
|
||
toModel(fileDBO: FileDBOType): UwaziFile { | ||
if (fileDBO.type === 'attachment' && fileDBO.url) { | ||
return new URLAttachment( | ||
fileDBO._id.toString(), | ||
fileDBO.entity, | ||
fileDBO.totalPages, | ||
fileDBO.url | ||
); | ||
} | ||
if (fileDBO.type === 'attachment') { | ||
return new Attachment( | ||
fileDBO._id.toString(), | ||
fileDBO.entity, | ||
fileDBO.totalPages, | ||
fileDBO.filename | ||
); | ||
} | ||
|
||
if (fileDBO.type === 'custom') { | ||
return new CustomUpload( | ||
fileDBO._id.toString(), | ||
fileDBO.entity, | ||
fileDBO.totalPages, | ||
fileDBO.filename | ||
); | ||
} | ||
return new Document( | ||
fileDBO._id.toString(), | ||
fileDBO.entity, | ||
fileDBO.totalPages, | ||
fileDBO.filename | ||
); | ||
}, | ||
}; |
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
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
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
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { _Object, ListObjectsV2Command, S3Client } from '@aws-sdk/client-s3'; | ||
import { config } from 'api/config'; | ||
import { Tenant } from 'api/tenants/tenantContext'; | ||
import path from 'path'; | ||
import { FileStorage } from '../contracts/FileStorage'; | ||
import { Attachment } from '../model/Attachment'; | ||
import { UwaziFile } from '../model/UwaziFile'; | ||
import { URLAttachment } from '../model/URLAttachment'; | ||
import { CustomUpload } from '../model/CustomUpload'; | ||
|
||
export class S3FileStorage implements FileStorage { | ||
private s3Client: S3Client; | ||
|
||
private tenant: Tenant; | ||
|
||
constructor(s3Client: S3Client, tenant: Tenant) { | ||
this.s3Client = s3Client; | ||
this.tenant = tenant; | ||
} | ||
|
||
getPath(file: UwaziFile): string { | ||
if (file instanceof Attachment) { | ||
return path.join(this.tenant.attachments, file.filename); | ||
} | ||
if (file instanceof CustomUpload) { | ||
return path.join(this.tenant.customUploads, file.filename); | ||
} | ||
if (file instanceof URLAttachment) { | ||
return 'not implemented'; | ||
} | ||
return path.join(this.tenant.uploadedDocuments, file.filename); | ||
} | ||
|
||
async list(): Promise<string[]> { | ||
const objects: _Object[] = []; | ||
const requestNext = async (token?: string) => { | ||
const response = await this.s3Client.send( | ||
new ListObjectsV2Command({ | ||
Bucket: config.s3.bucket, | ||
Prefix: `${this.tenant.name}/`, | ||
ContinuationToken: token, | ||
MaxKeys: config.s3.batchSize, | ||
}) | ||
); | ||
objects.push(...(response.Contents || [])); | ||
return response.NextContinuationToken; | ||
}; | ||
|
||
let continuationToken = await requestNext(); | ||
while (continuationToken) { | ||
// eslint-disable-next-line no-await-in-loop | ||
continuationToken = await requestNext(continuationToken); | ||
} | ||
|
||
return objects.map(c => c.Key!); | ||
} | ||
} |
Oops, something went wrong.