Alexey Velikiy
6 years ago
8 changed files with 142 additions and 189 deletions
@ -1,76 +1,36 @@ |
|||||||
import * as AnalyticUnit from './analytic_unit_model'; |
import { AnalyticUnitId } from './analytic_unit_model'; |
||||||
|
|
||||||
|
import { Collection, makeDBQ } from '../services/data_service'; |
||||||
|
|
||||||
|
|
||||||
import * as _ from 'lodash'; |
import * as _ from 'lodash'; |
||||||
|
|
||||||
import * as path from 'path'; |
type SegmentId = string; |
||||||
import * as fs from 'fs'; |
|
||||||
|
|
||||||
|
type Segment = { |
||||||
|
id?: SegmentId, |
||||||
|
from: number, |
||||||
|
to: number, |
||||||
|
labeled: boolean |
||||||
|
} |
||||||
|
|
||||||
export function getLabeledSegments(id: AnalyticUnit.AnalyticUnitId) { |
let db = makeDBQ(Collection.SEGMENTS); |
||||||
let filename = path.join(SEGMENTS_PATH, `${id}_labeled.json`); |
|
||||||
|
|
||||||
if(!fs.existsSync(filename)) { |
export function getLabeledSegments(id: AnalyticUnitId) { |
||||||
return []; |
return
|
||||||
} else { |
|
||||||
let segments = getJsonDataSync(filename); |
|
||||||
for(let segment of segments) { |
|
||||||
if(segment.labeled === undefined) { |
|
||||||
segment.labeled = false; |
|
||||||
} |
|
||||||
} |
|
||||||
return segments; |
|
||||||
} |
|
||||||
} |
} |
||||||
|
|
||||||
export function getPredictedSegments(id: AnalyticUnit.AnalyticUnitId) { |
export function getPredictedSegments(id: AnalyticUnitId) { |
||||||
let filename = path.join(SEGMENTS_PATH, `${id}_segments.json`); |
|
||||||
|
|
||||||
let jsonData; |
|
||||||
try { |
|
||||||
jsonData = getJsonDataSync(filename); |
|
||||||
} catch(e) { |
|
||||||
console.error(e.message); |
|
||||||
jsonData = []; |
|
||||||
} |
|
||||||
return jsonData; |
|
||||||
} |
} |
||||||
|
|
||||||
export function saveSegments(id: AnalyticUnit.AnalyticUnitId, segments) { |
export function saveSegments(id: AnalyticUnitId, segments: Segment[]) { |
||||||
let filename = path.join(SEGMENTS_PATH, `${id}_labeled.json`); |
|
||||||
|
|
||||||
try { |
|
||||||
return writeJsonDataSync(filename, _.uniqBy(segments, 'start')); |
|
||||||
} catch(e) { |
|
||||||
console.error(e.message); |
|
||||||
throw new Error('Can`t write to db'); |
|
||||||
} |
|
||||||
} |
} |
||||||
|
|
||||||
export async function insertSegments(id: AnalyticUnit.AnalyticUnitId, addedSegments, labeled: boolean) { |
export async function insertSegments(id: AnalyticUnitId, addedSegments: Segment[], labeled: boolean) { |
||||||
// Set status
|
|
||||||
let info = await AnalyticUnit.findById(id); |
|
||||||
let segments = getLabeledSegments(id); |
|
||||||
|
|
||||||
let nextId = info.nextId; |
|
||||||
let addedIds = [] |
|
||||||
for (let segment of addedSegments) { |
|
||||||
segment.id = nextId; |
|
||||||
segment.labeled = labeled; |
|
||||||
addedIds.push(nextId); |
|
||||||
nextId++; |
|
||||||
segments.push(segment); |
|
||||||
} |
|
||||||
info.nextId = nextId; |
|
||||||
saveSegments(id, segments); |
|
||||||
await AnalyticUnit.update(id, info); |
|
||||||
return addedIds; |
|
||||||
} |
} |
||||||
|
|
||||||
export function removeSegments(id: AnalyticUnit.AnalyticUnitId, removedSegments) { |
export function removeSegments(idsToRemove: SegmentId[]) { |
||||||
let segments = getLabeledSegments(id); |
|
||||||
for (let segmentId of removedSegments) { |
|
||||||
segments = segments.filter(el => el.id !== segmentId); |
|
||||||
} |
|
||||||
saveSegments(id, segments); |
|
||||||
} |
} |
||||||
|
Loading…
Reference in new issue