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 path from 'path'; |
|
||||||
import * as fs from 'fs'; |
|
||||||
|
|
||||||
|
import * as _ from 'lodash'; |
||||||
|
|
||||||
export function getLabeledSegments(id: AnalyticUnit.AnalyticUnitId) { |
type SegmentId = string; |
||||||
let filename = path.join(SEGMENTS_PATH, `${id}_labeled.json`); |
|
||||||
|
|
||||||
if(!fs.existsSync(filename)) { |
type Segment = { |
||||||
return []; |
id?: SegmentId, |
||||||
} else { |
from: number, |
||||||
let segments = getJsonDataSync(filename); |
to: number, |
||||||
for(let segment of segments) { |
labeled: boolean |
||||||
if(segment.labeled === undefined) { |
|
||||||
segment.labeled = false; |
|
||||||
} |
|
||||||
} |
|
||||||
return segments; |
|
||||||
} |
|
||||||
} |
} |
||||||
|
|
||||||
export function getPredictedSegments(id: AnalyticUnit.AnalyticUnitId) { |
let db = makeDBQ(Collection.SEGMENTS); |
||||||
let filename = path.join(SEGMENTS_PATH, `${id}_segments.json`); |
|
||||||
|
|
||||||
let jsonData; |
export function getLabeledSegments(id: AnalyticUnitId) { |
||||||
try { |
return
|
||||||
jsonData = getJsonDataSync(filename); |
|
||||||
} catch(e) { |
|
||||||
console.error(e.message); |
|
||||||
jsonData = []; |
|
||||||
} |
|
||||||
return jsonData; |
|
||||||
} |
} |
||||||
|
|
||||||
export function saveSegments(id: AnalyticUnit.AnalyticUnitId, segments) { |
export function getPredictedSegments(id: AnalyticUnitId) { |
||||||
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 function saveSegments(id: AnalyticUnitId, segments: Segment[]) { |
||||||
// Set status
|
|
||||||
let info = await AnalyticUnit.findById(id); |
} |
||||||
let segments = getLabeledSegments(id); |
|
||||||
|
|
||||||
let nextId = info.nextId; |
export async function insertSegments(id: AnalyticUnitId, addedSegments: Segment[], labeled: boolean) { |
||||||
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