You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
1.9 KiB
77 lines
1.9 KiB
6 years ago
|
import * as AnalyticUnit from './analytic_unit_model';
|
||
|
|
||
7 years ago
|
|
||
7 years ago
|
import * as _ from 'lodash';
|
||
|
|
||
6 years ago
|
import * as path from 'path';
|
||
|
import * as fs from 'fs';
|
||
|
|
||
|
|
||
6 years ago
|
export function getLabeledSegments(id: AnalyticUnit.AnalyticUnitId) {
|
||
6 years ago
|
let filename = path.join(SEGMENTS_PATH, `${id}_labeled.json`);
|
||
7 years ago
|
|
||
6 years ago
|
if(!fs.existsSync(filename)) {
|
||
|
return [];
|
||
|
} else {
|
||
|
let segments = getJsonDataSync(filename);
|
||
|
for(let segment of segments) {
|
||
|
if(segment.labeled === undefined) {
|
||
7 years ago
|
segment.labeled = false;
|
||
|
}
|
||
|
}
|
||
6 years ago
|
return segments;
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
export function getPredictedSegments(id: AnalyticUnit.AnalyticUnitId) {
|
||
6 years ago
|
let filename = path.join(SEGMENTS_PATH, `${id}_segments.json`);
|
||
7 years ago
|
|
||
|
let jsonData;
|
||
|
try {
|
||
|
jsonData = getJsonDataSync(filename);
|
||
|
} catch(e) {
|
||
|
console.error(e.message);
|
||
|
jsonData = [];
|
||
|
}
|
||
|
return jsonData;
|
||
|
}
|
||
|
|
||
6 years ago
|
export function saveSegments(id: AnalyticUnit.AnalyticUnitId, segments) {
|
||
6 years ago
|
let filename = path.join(SEGMENTS_PATH, `${id}_labeled.json`);
|
||
7 years ago
|
|
||
|
try {
|
||
7 years ago
|
return writeJsonDataSync(filename, _.uniqBy(segments, 'start'));
|
||
7 years ago
|
} catch(e) {
|
||
|
console.error(e.message);
|
||
|
throw new Error('Can`t write to db');
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
export async function insertSegments(id: AnalyticUnit.AnalyticUnitId, addedSegments, labeled: boolean) {
|
||
7 years ago
|
// Set status
|
||
6 years ago
|
let info = await AnalyticUnit.findById(id);
|
||
6 years ago
|
let segments = getLabeledSegments(id);
|
||
7 years ago
|
|
||
6 years ago
|
let nextId = info.nextId;
|
||
7 years ago
|
let addedIds = []
|
||
|
for (let segment of addedSegments) {
|
||
|
segment.id = nextId;
|
||
|
segment.labeled = labeled;
|
||
|
addedIds.push(nextId);
|
||
|
nextId++;
|
||
|
segments.push(segment);
|
||
|
}
|
||
6 years ago
|
info.nextId = nextId;
|
||
|
saveSegments(id, segments);
|
||
6 years ago
|
await AnalyticUnit.update(id, info);
|
||
7 years ago
|
return addedIds;
|
||
|
}
|
||
|
|
||
6 years ago
|
export function removeSegments(id: AnalyticUnit.AnalyticUnitId, removedSegments) {
|
||
6 years ago
|
let segments = getLabeledSegments(id);
|
||
7 years ago
|
for (let segmentId of removedSegments) {
|
||
|
segments = segments.filter(el => el.id !== segmentId);
|
||
|
}
|
||
6 years ago
|
saveSegments(id, segments);
|
||
7 years ago
|
}
|