Compare commits

...

30 Commits

Author SHA1 Message Date
108000207 d022e72ff1 [update] need to try execute_batch for optimize 2022-08-27 16:51:37 +08:00
108000207 1e82af9ee2 [update] use stream type as api 2022-08-25 16:31:01 +08:00
108000207 327e57e72f [update] api for get_csv, fix bugs for large file 2022-08-24 17:42:53 +08:00
108000207 5d5bf0ee51 [update] differential done 2022-08-23 17:51:21 +08:00
108000207 cf83afc8f3 [update] integral functional done 2022-08-23 17:02:23 +08:00
108000207 c8d0248da1 [update] log functional done 2022-08-23 15:32:00 +08:00
108000207 830fde89a3 [update] support multi "***" and export csv 2022-08-23 13:48:17 +08:00
108000207 fcc76c835c [update] sql get DB and build dataFrame 2022-08-23 11:07:01 +08:00
peterlu14 2937043f78 Merge branch 'feature/project_setting' into release/v1.0.0_project 2022-05-06 10:53:29 +08:00
peterlu14 1f65ad0f11 [update] fix update project 2022-04-29 17:17:03 +08:00
peterlu14 18c61c0996 [update] add new feature project api 2022-04-27 16:10:40 +08:00
peterlu14 451d246952 [update] init_setting 2022-04-15 17:47:39 +08:00
peterlu14 15d6c2cda6 [update] add meta getattrbyparent 2022-04-15 17:47:21 +08:00
peterlu14 5ad6683b81 [update] remove sd_card column 2021-10-19 10:45:30 +08:00
Jordan Hsu a646844b9a Merge branch 'master' into release/for_elite 2021-10-18 17:48:53 +08:00
Jordan Hsu 363d8aac11 [fix] delete bug 2021-10-18 17:47:56 +08:00
peterlu14 a7bea5288c [update] recover route device/update/:id 2021-09-23 18:31:08 +08:00
Jordan Hsu 97993827b2 [update] api for checking if mini have data 2021-09-10 20:10:08 +08:00
peterlu14 46b6918031 [update] babel src build copy python file to dist & port fix 2021-09-01 17:52:24 +08:00
peterlu14 df09a4c92b [update] add update sd card api 2021-08-30 18:47:53 +08:00
peterlu14 f4ac57c8f8 Merge branch 'release/0826_merge_spike' 2021-08-30 14:37:19 +08:00
peterlu14 64c6b20a8f [update] fix delete file error 2021-08-30 14:36:44 +08:00
peterlu14 8a65f20142 Merge remote-tracking branch 'origin/feature/spike_detect' 2021-08-27 17:46:59 +08:00
peterlu14 5605bcbe68 [update] remove wrong index 2021-08-20 19:59:32 +08:00
peterlu14 6bb37fd48d [update] fix sub_signal 2021-08-20 18:09:34 +08:00
peterlu14 64a6698611 [update] execute python file path 2021-08-20 16:55:03 +08:00
peterlu14 70a4f448e9 [update] change output data delimiter 2021-08-20 16:17:21 +08:00
peterlu14 ddd4524407 [update] fix start_index < 0 2021-08-20 16:06:42 +08:00
peterlu14 92a2fc8a26 [update] add spikeDetect all & partial 2021-08-20 13:27:37 +08:00
peterlu14 af999c8b33 [update] spikeDetect 2021-08-20 11:38:25 +08:00
23 changed files with 57446 additions and 17 deletions
+1 -1
View File
@@ -7,7 +7,7 @@
"scripts": {
"start": "gulp nodemon",
"dev": "gulp",
"build": "babel src -d dist",
"build": "babel src -d dist --copy-files",
"production": "node dist/app.js",
"test": "jest",
"link": "eslint .",
+1 -1
View File
@@ -25,7 +25,7 @@ export const DB = {
export const pgDB = {
host: '127.0.0.1', // 服务器地址
port: 54321, // 数据库端口号
port: 5432, // 数据库端口号
username: 'biopro', // 数据库用户名
password: 'BioProControlBox', // 数据库密码
database: 'postgres' // 数据库名称
+57
View File
@@ -0,0 +1,57 @@
import RecordingDataMeta from '../../models/file/recording_data_meta'
import childProcess from 'child_process'
import path from 'path'
const recordingDataMeta = new RecordingDataMeta()
export const spikeDetect = async (ctx, next) => {
const request = ctx.request.body
const id = request.id
const channel = request.channel
const cutOffFreq = request.cut_off_freq
const threshold = request.threshold
const waveForm = request.waveForm
const retDataType = request.ret_data_type
// console.log(id, channel, cutOffFreq, threshold, waveForm)
const result = await recordingDataMeta.getByID(id)
const channels = JSON.parse(result[0].channels)
const sampleRate = 8e5 / result[0].parameter_set.ADC_CLOCK / channels.length
const timeDuration = result[0].time_duration / 1e6
// console.log(sampleRate, timeDuration)
const process = childProcess.spawnSync('python3', [
path.join(__dirname, '/spikeDetect.py'),
'meta_id=' + id,
'channel=' + channel,
'cut_off_freq=' + cutOffFreq,
'threshold=' + threshold,
'waveform=' + waveForm,
'sample_rate=' + sampleRate,
'time_duration=' + timeDuration,
'ret_data_type=' + retDataType
], { maxBuffer: 2 * 1024 * 1024 * 1024 })
// console.log('out', process.stdout.toString())
console.log('err', process.stderr.toString())
ctx.body = process.stdout.toString()
// const process = child_process.execSync('python3 /Users/lubokai/Desktop/bioproapiserver/src/controllers/analysis/test.py 1089 0')
// console.log(process)
// process.stdout.on('data', (data) => {
// _data += data.toString()
// })
// process.stderr.on('data', (data) => {
// console.error(data.toString())
// })
// process.on('exit', (code) => {
// console.log(_data)
// console.log(`Child exited with code ${code}`)
// })
next()
}
+110
View File
@@ -0,0 +1,110 @@
import sys
import ast
import psycopg2
import numpy as np
import pandas as pd
from math import ceil, floor
from json import loads, dumps
from scipy.fft import fft, fftfreq, rfft, rfftfreq, irfft, ifft
if __name__ == '__main__':
params = {}
for input in sys.argv[1:]: #Now we're going to iterate over argv[1:] (argv[0] is the program name)
param = input.split("=") #Get what's left of the '='
params[param[0]] = param[1]
meta_id = params['meta_id']
channel = params['channel']
cut_off_freq = int(params['cut_off_freq'])
threshold = ast.literal_eval('[' + params['threshold'] + ']')
waveform = ast.literal_eval('[' + params['waveform'] + ']')
SAMPLE_RATE = int(params['sample_rate'])
DURATION = ceil(float(params['time_duration']))
DELTA_TIME = 1e6 / SAMPLE_RATE
N = SAMPLE_RATE * DURATION
ret_data_type = params['ret_data_type']
# print(meta_id, channel, cut_off_freq, threshold, waveForm, SAMPLE_RATE, DURATION, N)
conn = psycopg2.connect(database="postgres", user="biopro", password="BioProControlBox", host="127.0.0.1", port="5432")
cur = conn.cursor()
sql_str = 'SELECT data FROM "public"."' + str(channel) + '_recording_data_raws" WHERE parent = ' + meta_id + 'ORDER BY id ASC'
cur.execute(sql_str)
ret = None
try:
ret = cur.fetchall()
except BaseException as e:
print(e)
exit
finally:
time = np.array([], dtype=int)
data = np.array([])
for sub_data in ret:
for split_data in sub_data[0].split('"***"')[:-1]:
value = np.array(split_data.split(' '))
time = np.append(time, value[::2].astype(np.int))
data = np.append(data, value[1::2].astype(np.int))
# print(len(time), len(data))
xf = rfftfreq(N, 1 / SAMPLE_RATE)
points_per_freq = len(xf) / (SAMPLE_RATE / 2)
target_idx = int(points_per_freq * cut_off_freq)
# print(xf[target_idx])
yf = rfft(data)
yf[:target_idx] = 0
new_sig = np.round(irfft(yf, len(data)),3)
# print(time[-1])
# print(len(datas))
# print(len(yf),yf)
if threshold[0] == 'below':
th = new_sig < threshold[1]
elif threshold[0] == 'over':
th = new_sig > threshold[1]
elif threshold[0] == 'auto':
th = new_sig
threshold_edges = np.convolve([1, -1], th, mode='same')
thresholded_edge_indices = np.where(threshold_edges==1)[0]
filter_array = []
for idx, point in enumerate(thresholded_edge_indices):
if idx > 0:
if time[thresholded_edge_indices[idx]] - int(waveform[2]) >= time[thresholded_edge_indices[idx-1]]:
filter_array.append(True)
elif time[thresholded_edge_indices[idx]] - int(waveform[1]) >= time[thresholded_edge_indices[idx-1]]:
filter_array.append(True)
else:
filter_array.append(False)
else:
if time[thresholded_edge_indices[idx]] - int(waveform[1]) < 0:
filter_array.append(False)
else:
filter_array.append(True)
timemark_list = thresholded_edge_indices[filter_array]
if ret_data_type == 'all':
print(' '.join(str(x) for x in time))
print(' '.join(str(x) for x in new_sig))
print(' '.join(str(x) for x in timemark_list))
elif ret_data_type == 'partial':
return_sub_signal_lists = {}
for timemark in timemark_list:
start_index = timemark - floor(int(waveform[1] / DELTA_TIME))
end_index = timemark + ceil(int(waveform[2] / DELTA_TIME))
subSignal = new_sig[start_index: end_index + 2]
return_sub_signal_lists[str(timemark)] = ' '.join(str(x) for x in subSignal)
print(dumps(return_sub_signal_lists))
conn.commit()
conn.close()
exit
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+142
View File
@@ -0,0 +1,142 @@
import psycopg2
import pandas as pd
import numpy as np
from os.path import exists
import sys
import os
import time
def log(df, channel):
# TODO: not sure need use 32 or 64? => 64, since large integral will cause overflow
df[f'{channel}_log'] = np.log10(np.absolute(df[channel]))
# df[f'{channel}_log'] = np.log10(np.absolute(df[channel].astype(np.int64)))
return df
def integral(df, x_channel, y_channel, pre_last_x, pre_last_y, integral_result):
offset = df[x_channel]
offset = offset.shift(periods=1)
offset[0] = pre_last_x
x_diff = df[x_channel] - offset
# x_diff = df[x_channel].astype(np.int64) - offset.astype(np.int64)
# print('x_diff\n', x_diff)
offset = df[y_channel]
offset = offset.shift(periods=1)
offset[0] = pre_last_y
y_avg = (offset + df[y_channel]) / 2
# y_avg = (offset.astype(np.int64) + df[y_channel].astype(np.int64)) / 2
# print('y_avg:', y_avg)
# print('y_avg: ', y_avg)
local_integral = x_diff * y_avg
# TODO: if user don't need, then can remove it
df[f'{x_channel}_{y_channel}_integral_delta'] = local_integral
local_integral[0] += integral_result
df[f'{x_channel}_{y_channel}_integral'] = local_integral.cumsum()
# print(df)
pre_last_x = df[x_channel].iat[-1]
pre_last_y = df[y_channel].iat[-1]
integral_result = df[f'{x_channel}_{y_channel}_integral'].iat[-1]
# print('next run info: ', pre_last_x, pre_last_y, integral_result)
return df, pre_last_x, pre_last_y, integral_result
def differential(df, x_channel, y_channel, pre_last_x, pre_last_y):
offset = df[x_channel]
offset = offset.shift(periods=1)
offset[0] = pre_last_x
x_diff = df[x_channel] - offset
# x_diff = df[x_channel].astype(np.int64) - offset.astype(np.int64)
offset = df[y_channel]
offset = offset.shift(periods=1)
offset[0] = pre_last_y
y_diff = df[y_channel] - offset
# y_diff = df[y_channel].astype(np.int64) - offset.astype(np.int64)
local_diff = y_diff / x_diff
# TODO: if user don't need, then can remove it
df[f'{x_channel}_{y_channel}_diff'] = local_diff
pre_last_x = df[x_channel].iat[-1]
pre_last_y = df[y_channel].iat[-1]
return df, pre_last_x, pre_last_y
def download(id, mode, x_channel, y_channel):
# TODO: can change different chunck size
pages_chunck_size = 250
sql_total = 0
conn_start = time.time()
# TODO: need the check port id
conn = psycopg2.connect(database="postgres", user="biopro", password="BioProControlBox", host="127.0.0.1", port="54321")
print('conn time: ', time.time() - conn_start)
# conn = psycopg2.connect(database="postgres", user="biopro", password="BioProControlBox", host="127.0.0.1", port="5432")
cur = conn.cursor()
# recording_data_metas: meta info to get the raw_data table
sql_str = f'select "raw_data" from "recording_data_metas" WHERE id = {id}'
first_sql = time.time()
cur.execute(sql_str)
print('first sql time: ', time.time() - first_sql)
raw_data_list = None
try:
raw_data_list = cur.fetchall()[0][0]
channels = list(raw_data_list.keys())
df_all = pd.DataFrame()
# df = pd.DataFrame()
df_time = []
pre_last_x = 0
pre_last_y = 0
integral_result = 0
for raw_data_pages in range(len(raw_data_list[channels[0]])):
df = pd.DataFrame()
for channel in channels:
sql_str = f'select "data" from "{channel}_recording_data_raws" WHERE id = {raw_data_list[channel][raw_data_pages]}'
sql_start = time.time()
cur.execute(sql_str)
sql_total += (time.time() - sql_start)
data_value = cur.fetchall()[0][0].replace('"***"', ' ')
data_value = data_value.split(' ')[:-1]
if df.empty:
df_time = data_value[::2]
df[channel] = data_value
df = df.iloc[lambda x: x.index % 2 == 1].reset_index(drop=True)
df.insert(0, 'Time', df_time)
# transfer dataframe from string to number
cols = df.columns
df[cols] = df[cols].apply(pd.to_numeric, errors='coerce')
if mode == 'Log':
df = log(df, x_channel)
elif mode == 'Integral':
df, pre_last_x, pre_last_y, integral_result = integral(df, x_channel, y_channel, pre_last_x, pre_last_y, integral_result)
elif mode == 'Differential':
df, pre_last_x, pre_last_y = differential(df, x_channel, y_channel, pre_last_x, pre_last_y)
df_all = pd.concat([df_all, df], ignore_index=True, sort=False)
if ~raw_data_pages and (raw_data_pages % pages_chunck_size == 0 or raw_data_pages == len(raw_data_list[channels[0]]) - 1):
if not(exists(f"./src/csv/{id}.csv")):
df.to_csv(f"./src/csv/{id}.csv", mode='a', index=False, header=True)
else:
df.to_csv(f"./src/csv/{id}.csv", mode='a', index=False, header=False)
""" if not(exists(f"./{id}.csv")):
df_all.to_csv(f"./{id}.csv", mode='a', index=False, header=True)
else:
df_all.to_csv(f"./{id}.csv", mode='a', index=False, header=False) """
df_all = pd.DataFrame()
print('total sql: ', sql_total)
except BaseException as e:
print(e)
exit
return
if __name__ == '__main__':
params = {}
for input in sys.argv[1:]: #Now we're going to iterate over argv[1:] (argv[0] is the program name)
param = input.split("=") #Get what's left of the '='
params[param[0]] = param[1]
id = params['id']
mode = params['mode']
x_channel = params['x_channel']
y_channel = params['y_channel']
# print('download_functional init at main: ', id, mode, x_channel, y_channel)
start_time = time.time()
download(id, mode, x_channel, y_channel)
# download(408, '', 'Time', '0')
# download(408, '', 'Time', '0')
# download(405, 'Integral', 'Time', '0')
# download(405, 'Integral', 'Time', '0')
# download(408, 'Integral', 'Time', '0')
print('total: ', time.time() - start_time)
# download(1262, 'differential', '1', '0')
# 1262/differential/1/0
@@ -48,6 +48,16 @@ export const getByParent = async (ctx, next) => {
next()
}
export const getAttrByParent = async (ctx, next) => {
const type = ctx.params.type
const parent = ctx.params.parent
const attr = ctx.params.attr
const result = await recordingDataMeta.getByAttrParent(type, parent, attr)
ctx.body = result
next()
}
export const getCountByParent = async (ctx, next) => {
const type = ctx.params.type
const parent = ctx.params.parent
@@ -61,6 +61,15 @@ export const getByID = async (ctx, next) => {
next()
}
export const getExistOrNotByID = async (ctx, next) => {
const index = ctx.params.channel
const id = ctx.params.id
const result = await recordingDataMini.getExistOrNotByID(id, index)
ctx.body = result
next()
}
export const getByIDs = async (ctx, next) => {
const index = ctx.params.channel
const id = ctx.params.id
@@ -1,7 +1,12 @@
import RecordingDataRaw from '../../models/file/recording_data_raw'
import RecordingDataMeta from '../../models/file/recording_data_meta'
import * as auth from '../auth'
import childProcess from 'child_process'
import path from 'path'
import { createReadStream } from 'fs'
const recordingDataRaw = new RecordingDataRaw()
const recordingDataMeta = new RecordingDataMeta()
export const create = async (ctx, next) => {
const index = ctx.params.channel
@@ -25,6 +30,16 @@ export const update = async (ctx, next) => {
next()
}
export const updateSdData = async (ctx, next) => {
const index = ctx.params.channel
const data = ctx.request.body
const id = ctx.params.id
const result = await recordingDataRaw.updateSdData(data, id, index)
ctx.body = result
next()
}
export const updateByIDs = async (ctx, next) => {
const index = ctx.params.channel
const data = ctx.request.body
@@ -152,3 +167,37 @@ export const getAll = async (ctx, next) => {
next()
}
export const getCSV = async (ctx, next) => {
const id = ctx.params.id
const mode = ctx.params.mode
const x_channel = ctx.params.x_channel
const y_channel = ctx.params.y_channel
// console.log('getCSV: ', id, mode, x_channel, y_channel)
// const result = await recordingDataRaw.getCSV(id, mode, x_channel, y_channel)
const process = childProcess.spawnSync('python', [
path.join(__dirname, '/download_functional.py'),
'id=' + id,
'mode=' + mode,
'x_channel=' + x_channel,
'y_channel=' + y_channel,
], { maxBuffer: 2 * 1024 * 1024 * 1024 })
// console.log('process: ', process)
// process.stdout.on('data', (data) => {
// conosle.log('data', data)
// })
const result = await recordingDataMeta.getByID(id)
// console.log('result: ', result)
// console.log('result: ', result[0]['dataValues']['name'])
var file_name = id + '.csv'
// ctx.body = fs.stdout.toString()
// console.log('process.stdout.toString(): ', process.stdout.toString())
// console.log('process.stderr.toString(): ', process.stderr.toString())
ctx.type = 'stream'
ctx.attachment(result[0]['dataValues']['name']+'.csv')
ctx.body = createReadStream(path.resolve(__dirname, '../../csv', file_name))
// need to remove the csv file after download, since using 'append' for set header one time
const fs = require('fs')
fs.unlinkSync(path.resolve(__dirname, '../../csv', file_name))
next()
}
+60
View File
@@ -0,0 +1,60 @@
time 0 1 2 3 time_0_integral
0 0 -73922 -836265 -848970 0 0.000000e+00
1 10500 -84368 -834380 -848548 0 -8.310225e+08
2 21000 -84389 -834380 -848126 0 -1.716997e+09
3 31500 -84332 -835637 -847704 0 -2.602782e+09
4 42500 -84166 -835637 -847283 0 -3.529521e+09
... ... ... ... ... .. ...
1462 15390500 -45761 -449806 -466674 0 -9.716838e+11
1463 15401000 -45833 -449806 -466445 0 -9.721646e+11
1464 15411500 -45790 -451063 -466216 0 -9.726457e+11
1465 15422500 -45638 -449806 -465988 0 -9.731485e+11
1466 15432500 -45674 -449806 -465759 0 -9.736051e+11
[1467 rows x 6 columns]
next run info: 15432500 -45674 -973605066500.0
time 0 1 2 3 time_0_integral
0 15443000 -45689 -448549 -465531 0 -9.740847e+11
1 15453500 -45689 -448549 -465531 0 -9.745645e+11
2 15464000 -45631 -449178 -465303 0 -9.750439e+11
3 15474500 -45581 -447921 -465075 0 -9.755227e+11
4 15485500 -45646 -447921 -464847 0 -9.760245e+11
... ... ... ... ... .. ...
1443 30720000 -24971 -248122 -259749 0 -1.497382e+12
1444 30730500 -24920 -247950 -259624 0 -1.497644e+12
1445 30741000 -24920 -248381 -259624 0 -1.497905e+12
1446 30751500 -24791 -248036 -259500 0 -1.498166e+12
1447 30762000 -24856 -247346 -259376 0 -1.498427e+12
[1448 rows x 6 columns]
next run info: 30762000 -24856 -1498426872750.0
time 0 1 2 3 time_0_integral
0 30772500 -24913 -247669 -259251 0 -1.498688e+12
1 30783000 -24848 -247669 -259127 0 -1.498949e+12
2 30794000 -24805 -247669 -259003 0 -1.499222e+12
3 30804000 -24805 -247454 -259003 0 -1.499471e+12
4 30814500 -24870 -247238 -258879 0 -1.499731e+12
... ... ... ... ... .. ...
1456 46091500 -13496 -134943 -147002 0 -1.783935e+12
1457 46102000 -13510 -134921 -146934 0 -1.784077e+12
1458 46112500 -13510 -134123 -146934 0 -1.784219e+12
1459 46123000 -13597 -134339 -146866 0 -1.784361e+12
1460 46134000 -13525 -134598 -146799 0 -1.784510e+12
[1461 rows x 6 columns]
next run info: 46134000 -13525 -1784510350500.0
time 0 1 2 3 time_0_integral
0 46144500 -13366 -134210 -146732 0 -1.784652e+12
1 46154500 -13460 -134210 -146665 0 -1.784786e+12
2 46165500 -13503 -134016 -146597 0 -1.784934e+12
3 46176000 -13503 -134102 -146597 0 -1.785076e+12
4 46186500 -13431 -134167 -146530 0 -1.785217e+12
.. ... ... ... ... .. ...
583 52277500 -10487 -105193 -117883 0 -1.858083e+12
584 52288000 -10573 -105042 -117830 0 -1.858194e+12
585 52298500 -10595 -105106 -117778 0 -1.858305e+12
586 52309000 -10494 -105085 -117725 0 -1.858415e+12
587 52319500 -10523 -105085 -117672 0 -1.858526e+12
[588 rows x 6 columns]
next run info: 52319500 -10523 -1858525771000.0
+5 -1
View File
@@ -8,6 +8,8 @@ import * as task from './controller/task'
import * as recordingDataMeta from './file/recording_data_meta'
import * as recordingDataRaw from './file/recording_data_raw'
import * as recordingDataMini from './file/recording_data_mini'
import * as analysis from './analysis'
import * as project from './project/project'
export default {
upload,
@@ -19,5 +21,7 @@ export default {
task,
recordingDataMeta,
recordingDataRaw,
recordingDataMini
recordingDataMini,
analysis,
project
}
+62
View File
@@ -0,0 +1,62 @@
import ProjectModal from '../../models/project/project'
const project = new ProjectModal()
export const create = async (ctx, next) => {
const data = ctx.request.body
console.log('create', data)
const result = await project.create(data)
ctx.body = result
next()
}
export const update = async (ctx, next) => {
const data = ctx.request.body
const id = ctx.params.id
const result = await project.update(data, id)
ctx.body = result
next()
}
export const updateByAttr = async (ctx, next) => {
const attr = ctx.params.attr
const value = ctx.params.value
const result = await project.updateByAttr(attr, value)
ctx.body = result
next()
}
export const clearDeleted = async (ctx, next) => {
const result = await project.clearDeleted()
ctx.body = result
next()
}
export const getByID = async (ctx, next) => {
const index = ctx.params.channel
const id = ctx.params.id
const result = await project.getByID(id, index)
ctx.body = result
next()
}
export const getByAttr = async (ctx, next) => {
const attr = ctx.params.attr
const value = ctx.params.value
const result = await project.getByAttr(attr, value)
ctx.body = result
next()
}
export const getAll = async (ctx, next) => {
const result = await project.getAll()
ctx.body = result
next()
}
+7 -6
View File
@@ -27,6 +27,7 @@ db.controllers = require('./schema/controller.model')(sequelize, Sequelize)
db.tasks = require('./schema/task.model')(sequelize, Sequelize)
db.devices = require('./schema/device.model')(sequelize, Sequelize)
db.collections = require('./schema/collection.model')(sequelize, Sequelize)
db.projects = require('./schema/project.model')(sequelize, Sequelize)
db.recording_data_metas = require('./schema/recording_data_meta.model')(sequelize, Sequelize)
for (let i = 0; i < 32; i++) {
db[i + '_recording_data_raws'] = require('./schema/recording_data_raw.model')(sequelize, Sequelize, i)
@@ -34,12 +35,12 @@ for (let i = 0; i < 32; i++) {
for (let i = 0; i < 32; i++) {
db[i + '_recording_data_minis'] = require('./schema/recording_data_mini.model')(sequelize, Sequelize, i)
}
for (let i = 256; i < 288; i++) {
db[i + '_recording_data_raws'] = require('./schema/recording_data_raw.model')(sequelize, Sequelize, i)
}
for (let i = 256; i < 288; i++) {
db[i + '_recording_data_minis'] = require('./schema/recording_data_mini.model')(sequelize, Sequelize, i)
}
// for (let i = 256; i < 288; i++) {
// db[i + '_recording_data_raws'] = require('./schema/recording_data_raw.model')(sequelize, Sequelize, i)
// }
// for (let i = 256; i < 288; i++) {
// db[i + '_recording_data_minis'] = require('./schema/recording_data_mini.model')(sequelize, Sequelize, i)
// }
db.sequelize.sync()
+47
View File
@@ -0,0 +1,47 @@
module.exports = (sequelize, Sequelize) => {
const Project = sequelize.define('project', {
name: {
type: Sequelize.STRING
},
desc: {
type: Sequelize.STRING
},
task: {
type: Sequelize.JSONB,
defaultValue: {}
},
device: {
type: Sequelize.JSONB,
defaultValue: {}
},
uuid: {
type: Sequelize.STRING,
defaultValue: ''
},
user_auth: {
type: Sequelize.JSONB,
defaultValue: {
owner: [],
maintainer: [],
guest: []
}
},
deleted: {
type: Sequelize.BOOLEAN,
defaultValue: false
},
created_at: {
type: Sequelize.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
},
updated_at: {
type: Sequelize.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
}
},
{
timestamps: false
})
return Project
}
+25 -4
View File
@@ -62,9 +62,9 @@ class RecordingDataMeta {
recordingDataRaw.clearDeleted(channel)
}
if (meta.mini_data[channel] != null) {
await recordingDataMini.updateByIDs({ deleted: true }, meta.mini_data[channel]['10'], channel)
await recordingDataMini.updateByIDs({ deleted: true }, meta.mini_data[channel]['100'], channel)
await recordingDataMini.updateByIDs({ deleted: true }, meta.mini_data[channel]['100'], channel)
for (const miniID in meta.mini_data[channel]) {
await recordingDataMini.updateByIDs({ deleted: true }, meta.mini_data[channel][miniID], channel)
}
recordingDataMini.clearDeleted(channel)
}
}
@@ -98,7 +98,28 @@ class RecordingDataMeta {
return ret
}
async getCountByParent (type, parent) {
async getByAttrParent(type, parent, attr) {
const attrList = attr.split('-')
const collectionType = type + '::jsonb'
const parentOp = {}
parentOp[collectionType] = {
[Op.contains]: '[' + parent + ']'
}
const ret = await db.recording_data_metas.findAll(
{
where: {
parent: parentOp
},
attributes: attrList,
order: [
['id', 'DESC']
],
}
)
return ret
}
async getCountByParent(type, parent) {
const collectionType = type + '::jsonb'
const parentOp = {}
parentOp[collectionType] = {
+13 -2
View File
@@ -31,11 +31,11 @@ class RecordingDataMini {
return ret
}
async clearDeleted (index) {
async clearDeleted (index, where = { deleted: true }) {
const ret = await db[index + '_recording_data_minis'].destroy(
{
where: {
deleted: true
...where
}
}
)
@@ -69,6 +69,17 @@ class RecordingDataMini {
return ret
}
async getExistOrNotByID (id, index) {
const ret = await db[index + '_recording_data_minis'].findOne(
{
where: {
id: id
}
}
).then(token => !(token.data_mean === ''))
return ret
}
async getByIDs (id, index) {
const _id = JSON.parse('[' + id + ']')
const ret = await db[index + '_recording_data_minis'].findAllWithStream(
+7 -2
View File
@@ -20,6 +20,11 @@ class RecordingDataRaw {
return ret
}
async updateSdData (req, id, index) {
const ret = await db.sequelize.query(`UPDATE "public"."${index}_recording_data_raws" SET sd_data = sd_data || '${req.data}' WHERE id = ${String(id)}`)
return ret
}
async updateByIDs (req, id, index) {
const ret = await db[index + '_recording_data_raws'].update(
req,
@@ -32,11 +37,11 @@ class RecordingDataRaw {
return ret
}
async clearDeleted (index) {
async clearDeleted (index, where = { deleted: true }) {
const ret = await db[index + '_recording_data_raws'].destroy(
{
where: {
deleted: true
...where
}
}
)
+97
View File
@@ -0,0 +1,97 @@
const db = require('../../db/database')
// const Op = db.Sequelize.Op
class ProjectModal {
async create (req) {
return db.projects.create(req)
}
async update (req, id) {
if (id != null) {
const ret = await db.projects.update(
req,
{
where: {
id: id
}
}
)
return ret
} else {
const ret = await db.projects.update(
req,
{
where: {}
}
)
return ret
}
}
async updateByAttr (req, attr, value) {
const whereObj = {}
const attrList = attr.split('-')
const valueList = value.split('-')
attrList.forEach((key, idx) => {
whereObj[key] = valueList[idx]
})
const ret = await db.projects.update(
req,
{
where: whereObj
}
)
return ret
}
async clearDeleted () {
const ret = await db.projects.destroy(
{
where: {
deleted: true
}
}
)
return ret
}
async getByID (id) {
const ret = await db.projects.findAll(
{
where: {
id: id
}
}
)
return ret
}
async getByAttr (attr, value) {
const attrList = attr.split('-')
const valueList = value.split('-')
const whereObj = {}
attrList.forEach((key, idx) => {
whereObj[key] = valueList[idx]
})
const ret = await db.projects.findAll(
{
where: whereObj
}
)
return ret
}
async getAll () {
const ret = await db.projects.findAll({
order: [
['created_at', 'ASC']
]
})
return ret
}
}
export default ProjectModal
+17
View File
@@ -39,6 +39,7 @@ export default router
.get('/api/file/meta/get_by_id/:id', controllers.recordingDataMeta.getByID)
.get('/api/file/meta/get_by_path/:path', controllers.recordingDataMeta.getByPath)
.get('/api/file/meta/get_by_parent/:type/:parent', controllers.recordingDataMeta.getByParent)
.get('/api/file/meta/get_attr_by_parent/:type/:parent/:attr', controllers.recordingDataMeta.getAttrByParent)
.get('/api/file/meta/get_count_by_parent/:type/:parent', controllers.recordingDataMeta.getCountByParent)
.get('/api/file/meta/get_by_parent_with_limit/:type/:parent/:limit/:offset/:order', controllers.recordingDataMeta.getByParentWithLimit)
.get('/api/file/meta/get_by_attr_parent_with_limit/:type/:parent/:limit/:offset/:order/:attr', controllers.recordingDataMeta.getByAttrsParentWithLimit)
@@ -49,6 +50,7 @@ export default router
// raw
.post('/api/file/raw/create/:channel', controllers.recordingDataRaw.create)
.post('/api/file/raw/update/:channel/:id', controllers.recordingDataRaw.update)
.post('/api/file/raw/update_sd_data/:channel/:id', controllers.recordingDataRaw.updateSdData)
.get('/api/file/raw/clear_deleted/:channel/:id', controllers.recordingDataRaw.clearDeleted)
.get('/api/file/raw/get_by_id/:channel/:id', controllers.recordingDataRaw.getByID)
.get('/api/file/raw/get_by_ids/:channel/:id', controllers.recordingDataRaw.getByIDs)
@@ -57,12 +59,14 @@ export default router
.get('/api/file/raw/get_attr_by_id/:channel/:id/:attr', controllers.recordingDataRaw.getAttrByID)
.get('/api/file/raw/get_attr_by_ids/:channel/:id/:attr', controllers.recordingDataRaw.getAttrByIDs)
.get('/api/file/raw/get_attr_by_parent/:channel/:parent/:attr', controllers.recordingDataRaw.getAttrByParent)
.get('/api/file/raw/get_csv/:id/:mode/:x_channel/:y_channel', controllers.recordingDataRaw.getCSV)
// mini
.post('/api/file/mini/create/:channel', controllers.recordingDataMini.create)
.post('/api/file/mini/update/:channel/:id', controllers.recordingDataMini.update)
.get('/api/file/mini/clear_deleted/:channel/:id', controllers.recordingDataMini.clearDeleted)
.get('/api/file/mini/get_by_id/:channel/:id', controllers.recordingDataMini.getByID)
.get('/api/file/mini/get_exist_or_not_by_id/:channel/:id', controllers.recordingDataMini.getExistOrNotByID)
.get('/api/file/mini/get_by_parent/:channel/:parent', controllers.recordingDataMini.getByParent)
.get('/api/file/mini/get_attr_by_id/:channel/:id/:attr', controllers.recordingDataMini.getAttrByID)
.get('/api/file/mini/get_attr_by_parent/:channel/:parent/:attr', controllers.recordingDataMini.getAttrByParent)
@@ -71,6 +75,7 @@ export default router
// device
.post('/api/device/create', controllers.device.create)
.post('/api/device/update/:id', controllers.device.update)
.post('/api/device/update/all', controllers.device.update)
.post('/api/device/update_by_id/:id', controllers.device.updateByID)
.post('/api/device/update_by_mac/:mac', controllers.device.updateByMac)
@@ -106,3 +111,15 @@ export default router
.post('/api/task/update', controllers.task.update)
.post('/api/task/update_by_id/:id', controllers.task.updateByID)
// analyze
.post('/api/analysis/spike_detect', controllers.analysis.spikeDetect)
// project
.post('/api/project/create', controllers.project.create)
.post('/api/project/update/:id', controllers.project.update)
.post('/api/project/update_by_attr/:attr/:value', controllers.project.updateByAttr)
.post('/api/project/update/all', controllers.project.update)
.get('/api/project/clear_deleted/:controller_id', controllers.project.clearDeleted)
.get('/api/project/get_by_id/:id', controllers.project.getByID)
.get('/api/project/get_by_attr/:attr/:value', controllers.project.getByAttr)
.get('/api/project/get/all', controllers.project.getAll)
+3
View File
@@ -0,0 +1,3 @@
# Elite: 53
# Neulive: 101 or 95
ssh -NL 54321:localhost:5432 pi@192.168.2.1