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

import * as AnalyticUnit from './analytic_unit_model';
7 years ago
import * as _ from 'lodash';
6 years ago
import * as path from 'path';
import * as fs from 'fs';
export function getLabeledSegments(id: AnalyticUnit.AnalyticUnitId) {
6 years ago
let filename = path.join(SEGMENTS_PATH, `${id}_labeled.json`);
7 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;
}
}
return segments;
7 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;
}
export function saveSegments(id: AnalyticUnit.AnalyticUnitId, segments) {
6 years ago
let filename = path.join(SEGMENTS_PATH, `${id}_labeled.json`);
7 years ago
try {
return writeJsonDataSync(filename, _.uniqBy(segments, 'start'));
7 years ago
} catch(e) {
console.error(e.message);
throw new Error('Can`t write to db');
}
}
export async function insertSegments(id: AnalyticUnit.AnalyticUnitId, addedSegments, labeled: boolean) {
7 years ago
// Set status
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);
await AnalyticUnit.update(id, info);
7 years ago
return addedIds;
}
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
}