Browse Source

get_segments_intersected + createSegment

pull/25/head
Alexey Velikiy 3 years ago
parent
commit
c5c0e7c762
  1. 18
      client/src/components/hastic_pod/index.ts
  2. 8
      server/src/api/segments.rs
  3. 35
      server/src/services/data_service.rs

18
client/src/components/hastic_pod/index.ts

@ -9,18 +9,18 @@ export type UpdateDataCallback = (range: TimeRange) => Promise<{
timeserie: LineTimeSerie[],
segments: Segment[]
}>;
export type UpdateSegmentCallback = (segment: Segment) => Promise<SegmentId>;
export type CreateSegmentCallback = (segment: Segment) => Promise<SegmentId>;
export class HasticPod extends LinePod {
private _udc: UpdateDataCallback;
private _usc: UpdateSegmentCallback;
private _csc: CreateSegmentCallback;
private _ctrlKeyIsDown: boolean;
private _ctrlBrush: boolean;
constructor(
el: HTMLElement, udc: UpdateDataCallback, usc: UpdateSegmentCallback,
el: HTMLElement, udc: UpdateDataCallback, csc: CreateSegmentCallback,
private _segmentSet: SegmentsSet<Segment>
) {
super(el, undefined, {
@ -32,7 +32,7 @@ export class HasticPod extends LinePod {
}
});
this._usc = usc;
this._csc = csc;
this._ctrlKeyIsDown = false;
this._ctrlBrush = false;
@ -114,14 +114,18 @@ export class HasticPod extends LinePod {
}
protected async addSegment(from: number, to: number) {
// TODO: implement
// TODO: persistance of the label
const id = this.getNewTempSegmentId();
from = Math.floor(from);
to = Math.ceil(to);
if (from < to) {
const t = from;
from = to;
to = t;
}
const segment = new Segment(id, from, to);
const storedId = await this._usc(segment);
const storedId = await this._csc(segment);
segment.id = storedId;
this._segmentSet.addSegment(segment);

8
server/src/api/segments.rs

@ -42,13 +42,12 @@ pub mod filters {
mod handlers {
use hastic::services::data_service;
use super::models::{ Db, ListOptions, CreateResponse };
use super::models::{CreateResponse, Db, ListOptions};
use crate::api::BadQuery;
use crate::api::API;
pub async fn list(opts: ListOptions, db: Db) -> Result<impl warp::Reply, warp::Rejection> {
// Just return a JSON array of todos, applying the limit and offset.
match db.read().get_segments(opts.from, opts.to) {
match db.read().get_segments_intersected(opts.from, opts.to) {
Ok(segments) => Ok(API::json(&segments)),
Err(e) => Err(warp::reject::custom(BadQuery)),
}
@ -58,7 +57,6 @@ mod handlers {
segment: data_service::Segment,
db: Db,
) -> Result<impl warp::Reply, warp::Rejection> {
// Just return a JSON array of todos, applying the limit and offset.
match db.write().insert_segment(&segment) {
Ok(id) => Ok(API::json(&CreateResponse { id })),
Err(e) => {
@ -84,7 +82,7 @@ mod models {
pub to: u64,
}
#[derive(Debug, Serialize )]
#[derive(Debug, Serialize)]
pub struct CreateResponse {
pub id: SegmentId,
}

35
server/src/services/data_service.rs

@ -2,6 +2,7 @@ use rusqlite::{params, Connection};
use serde::{Deserialize, Serialize};
use std::rc::Rc;
use std::sync::{Arc, Mutex};
use std::iter::repeat_with;
@ -36,7 +37,7 @@ impl DataService {
)?;
Ok(DataService {
connection: Arc::new(Mutex::new(conn))
connection: Arc::new(Mutex::new(conn)),
})
}
@ -45,6 +46,11 @@ impl DataService {
let id: SegmentId = repeat_with(fastrand::alphanumeric)
.take(ID_LENGTH)
.collect();
{
// merging
// TODO extract intersected ids
}
// TODO: merge with other segments
self.connection.lock().unwrap().execute(
"INSERT INTO segment (id, start, end) VALUES (?1, ?2, ?3)",
@ -53,7 +59,7 @@ impl DataService {
Ok(id)
}
pub fn get_segments(&self, from: u64, to: u64) -> anyhow::Result<Vec<Segment>> {
pub fn get_segments_inside(&self, from: u64, to: u64) -> anyhow::Result<Vec<Segment>> {
let conn = self.connection.lock().unwrap();
let mut stmt =
conn.prepare("SELECT id, start, end FROM segment WHERE ?1 < start AND end < ?2")?;
@ -70,4 +76,29 @@ impl DataService {
.collect();
Ok(res)
}
pub fn get_segments_intersected(&self, from: u64, to: u64) -> anyhow::Result<Vec<Segment>> {
let conn = self.connection.lock().unwrap();
let mut stmt = conn.prepare("SELECT id, start, end FROM segment
WHERE (start <= ?1 and ?1 <= end) OR
(start <= ?2 AND ?2 <= end) OR
(?1 <= start AND start <= ?2) OR
(?1 <= end AND end <= ?2) ")?;
let res = stmt
.query_map(params![from, to], |row| {
Ok(Segment {
id: row.get(0)?,
from: row.get(1)?,
to: row.get(2)?,
})
})?
.map(|e| e.unwrap())
.collect();
Ok(res)
}
pub fn delete_segments(&self, ids: &Vec<SegmentId>) {
let v = Rc::new(ids);
}
}

Loading…
Cancel
Save