-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #470 from COS301-SE-2023/dev/version_control_resource
Dev/version control resource -> codecov
- Loading branch information
Showing
49 changed files
with
2,813 additions
and
281 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,172 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
import { InjectRepository } from "@nestjs/typeorm"; | ||
import { Repository } from "typeorm"; | ||
import { Diff } from "./entities/diffs.entity"; | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { Repository } from 'typeorm'; | ||
import { Diff } from './entities/diffs.entity'; | ||
import 'dotenv/config'; | ||
import * as CryptoJS from 'crypto-js'; | ||
import { MarkdownFileDTO } from '../markdown_files/dto/markdown_file.dto'; | ||
import { DiffDTO } from './dto/diffs.dto'; | ||
|
||
@Injectable() | ||
export class DiffsService { | ||
constructor( | ||
@InjectRepository(Diff) | ||
private diffRepository: Repository<Diff>, | ||
) {} | ||
|
||
///===----------------------------------------------------- | ||
|
||
async getDiff( | ||
diffDTO: DiffDTO, | ||
nextDiffID: number, | ||
) { | ||
const diff = | ||
await this.diffRepository.findOne({ | ||
where: { | ||
MarkdownID: diffDTO.MarkdownID, | ||
S3DiffID: nextDiffID, | ||
}, | ||
}); | ||
|
||
return diff; | ||
} | ||
|
||
///===----------------------------------------------------- | ||
|
||
async updateDiff( | ||
diffDTO: DiffDTO, | ||
nextDiffID: number, | ||
) { | ||
// const diff = | ||
// await this.diffRepository.findOne({ | ||
// where: { | ||
// MarkdownID: diffDTO.MarkdownID, | ||
// S3DiffID: nextDiffID, | ||
// }, | ||
// }); | ||
|
||
const diff = await this.getDiff( | ||
diffDTO, | ||
nextDiffID, | ||
); | ||
|
||
diff.LastModified = new Date(); | ||
diff.HasBeenUsed = true; | ||
await this.diffRepository.save(diff); | ||
} | ||
|
||
///===----------------------------------------------------- | ||
|
||
async createDiffs( | ||
markdownFileDTO: MarkdownFileDTO, | ||
snapshotIDs: string[], | ||
) { | ||
const diffRecords = []; | ||
let snapshotIndex = 0; | ||
for ( | ||
let i = 0; | ||
i < parseInt(process.env.MAX_DIFFS); | ||
i++ | ||
) { | ||
const diffID = CryptoJS.SHA256( | ||
markdownFileDTO.UserID.toString() + | ||
new Date().getTime().toString() + | ||
i.toString(), | ||
).toString(); | ||
|
||
if ( | ||
i % | ||
parseInt( | ||
process.env.DIFFS_PER_SNAPSHOT, | ||
) === | ||
0 && | ||
i !== 0 | ||
) { | ||
snapshotIndex++; | ||
} | ||
|
||
diffRecords.push({ | ||
DiffID: diffID, | ||
MarkdownID: markdownFileDTO.MarkdownID, | ||
UserID: markdownFileDTO.UserID, | ||
S3DiffID: i, | ||
HasBeenUsed: false, | ||
SnapshotID: snapshotIDs[snapshotIndex], | ||
}); | ||
} | ||
await this.diffRepository.insert(diffRecords); | ||
} | ||
|
||
///===----------------------------------------------------- | ||
|
||
async deleteDiffs( | ||
markdownFileDTO: MarkdownFileDTO, | ||
) { | ||
await this.diffRepository.delete({ | ||
MarkdownID: markdownFileDTO.MarkdownID, | ||
}); | ||
} | ||
|
||
///===----------------------------------------------------- | ||
|
||
async resetDiffs( | ||
markdownID: string, | ||
snapshotID: string, | ||
) { | ||
await this.diffRepository.update( | ||
{ | ||
MarkdownID: markdownID, | ||
SnapshotID: snapshotID, | ||
}, | ||
{ | ||
HasBeenUsed: false, | ||
}, | ||
); | ||
} | ||
|
||
///===----------------------------------------------------- | ||
|
||
async getAllDiffs(markdownID: string) { | ||
return await this.diffRepository.find({ | ||
where: { | ||
MarkdownID: markdownID, | ||
HasBeenUsed: true, | ||
}, | ||
}); | ||
} | ||
|
||
///===----------------------------------------------------- | ||
|
||
getLogicalIndex( | ||
s3Index: number, | ||
nextDiffID: number, | ||
arr_len: number, | ||
): number { | ||
return ( | ||
(s3Index - nextDiffID + arr_len) % arr_len | ||
); | ||
} | ||
|
||
///===----------------------------------------------------- | ||
|
||
async getLogicalDiffOrder( | ||
diffDTOs: DiffDTO[], | ||
nextDiffID: number, | ||
) { | ||
const arrLength = parseInt( | ||
process.env.MAX_DIFFS, | ||
); | ||
const logicalOrder: DiffDTO[] = new Array( | ||
arrLength, | ||
).fill(0); | ||
for (let idx = 0; idx < arrLength; idx++) { | ||
const logicalIndex = this.getLogicalIndex( | ||
diffDTOs[idx].S3DiffID, | ||
nextDiffID, | ||
arrLength, | ||
); | ||
logicalOrder[logicalIndex] = diffDTOs[idx]; | ||
} | ||
return logicalOrder; | ||
} | ||
} |
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,23 @@ | ||
export class DiffDTO { | ||
DiffID: string; | ||
MarkdownID: string; | ||
UserID: number; | ||
DisplayID: number; | ||
S3DiffID: number; | ||
LastModified: Date; | ||
Content: string; | ||
SnapshotPayload: string; | ||
SnapshotID: string; | ||
|
||
constructor() { | ||
this.DiffID = undefined; | ||
this.MarkdownID = undefined; | ||
this.UserID = undefined; | ||
this.DisplayID = undefined; | ||
this.S3DiffID = undefined; | ||
this.LastModified = undefined; | ||
this.Content = undefined; | ||
this.SnapshotPayload = undefined; | ||
this.SnapshotID = undefined; | ||
} | ||
} |
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
Oops, something went wrong.