2021-05-28 12:04:50 +05:00
|
|
|
|
import { useRef, useLayoutEffect, useState, useEffect } from 'react'
|
2021-05-19 16:05:01 +05:00
|
|
|
|
import {
|
|
|
|
|
Button,
|
|
|
|
|
DatePicker,
|
|
|
|
|
Row,
|
|
|
|
|
Col,
|
2021-05-28 12:04:50 +05:00
|
|
|
|
Tooltip} from 'antd'
|
2021-05-19 16:05:01 +05:00
|
|
|
|
import { useParams } from 'react-router-dom'
|
|
|
|
|
import { DataService } from '../services/api'
|
2021-05-28 12:04:50 +05:00
|
|
|
|
import {generateUUID} from '../services/UidGenerator'
|
|
|
|
|
import { ArchiveColumn } from '../components/ArchiveColumn'
|
|
|
|
|
import moment from 'moment'
|
2021-08-20 10:49:20 +05:00
|
|
|
|
import { notify } from "../components/factory"
|
2021-06-02 11:38:27 +05:00
|
|
|
|
import LoaderPortal from '../components/LoaderPortal'
|
2021-05-12 17:53:35 +05:00
|
|
|
|
|
2021-07-20 16:15:44 +05:00
|
|
|
|
const { RangePicker } = DatePicker
|
2021-05-19 16:05:01 +05:00
|
|
|
|
|
2021-05-28 12:04:50 +05:00
|
|
|
|
const SaveObject = (key, obj) => {
|
|
|
|
|
let json = JSON.stringify(obj)
|
|
|
|
|
localStorage.setItem(key, json)
|
2021-05-19 16:05:01 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 12:04:50 +05:00
|
|
|
|
const LoadObject = (key) => {
|
|
|
|
|
let json = localStorage.getItem(key)
|
|
|
|
|
return json ? JSON.parse(json) : null
|
2021-05-19 16:05:01 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 12:04:50 +05:00
|
|
|
|
export default function Archive() {
|
2021-05-19 16:05:01 +05:00
|
|
|
|
let { id } = useParams();
|
|
|
|
|
const [saubData, setSaubData] = useState([])
|
2021-05-28 12:04:50 +05:00
|
|
|
|
const [chartsCfgs, setChartsCfgs] = useState([])
|
|
|
|
|
const [rangeDate, setRangeDate] = useState([moment().subtract(3,'hours'), moment()])
|
2021-06-02 11:38:27 +05:00
|
|
|
|
const [geometry, setGeometry] = useState({ratioRest:1, ratio1st:1, wRest:.5, w1st:.5})
|
|
|
|
|
const [loader, setLoader] = useState(false)
|
2021-05-28 12:04:50 +05:00
|
|
|
|
const chartsCfgsKey = 'chartsCfgs'
|
|
|
|
|
const chartsContainerRef = useRef();
|
2021-05-19 16:05:01 +05:00
|
|
|
|
|
|
|
|
|
const handleReceiveDataSaub = (data) => {
|
2021-05-28 12:04:50 +05:00
|
|
|
|
if (data)
|
2021-05-19 16:05:01 +05:00
|
|
|
|
setSaubData(data)
|
2021-05-28 12:04:50 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const onAddChart = () => {
|
|
|
|
|
let newChartCfgs = [...chartsCfgs, {id: generateUUID(), yDisplay: false, aspectRatio:1}]
|
|
|
|
|
setChartsCfgs(newChartCfgs)
|
2021-05-19 16:05:01 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 12:04:50 +05:00
|
|
|
|
const onRemoveChart = (id) => {
|
|
|
|
|
let newChartCfgs = chartsCfgs.filter(cfg => cfg.id !== id )
|
|
|
|
|
setChartsCfgs(newChartCfgs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const onSaveConfig = ()=>{
|
|
|
|
|
SaveObject(chartsCfgsKey, chartsCfgs)
|
|
|
|
|
}
|
2021-05-19 16:05:01 +05:00
|
|
|
|
|
2021-05-28 12:04:50 +05:00
|
|
|
|
const onChangeRange = (range) => {
|
|
|
|
|
setRangeDate(range)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useLayoutEffect(()=>{
|
|
|
|
|
if(chartsContainerRef.current && chartsCfgs?.length){
|
|
|
|
|
let w = chartsContainerRef.current.offsetWidth //1792
|
|
|
|
|
w = w > 0 ? w : 1792
|
|
|
|
|
let ot = chartsContainerRef.current.offsetTop
|
|
|
|
|
let ph = chartsContainerRef.current.offsetParent.offsetHeight
|
|
|
|
|
let h = ph - ot - 32 //761
|
|
|
|
|
h = h > 0 ? h : 761
|
|
|
|
|
|
|
|
|
|
let chartsCount = chartsCfgs.length
|
|
|
|
|
|
|
|
|
|
let labelLenght = 8
|
|
|
|
|
let borderWidth = 8
|
|
|
|
|
let wRest = Math.floor((w - labelLenght)/chartsCount) - borderWidth
|
|
|
|
|
let w1st = wRest + labelLenght
|
|
|
|
|
|
|
|
|
|
let ratio1st = w1st/h
|
|
|
|
|
let ratioRest = wRest/h
|
|
|
|
|
|
|
|
|
|
setGeometry({ratio1st, ratioRest, w1st, wRest})
|
2021-05-19 16:05:01 +05:00
|
|
|
|
}
|
2021-05-28 12:04:50 +05:00
|
|
|
|
},[chartsContainerRef, chartsCfgs])
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
let cfgs = LoadObject(chartsCfgsKey)
|
|
|
|
|
if(cfgs)
|
|
|
|
|
setChartsCfgs(cfgs)
|
|
|
|
|
},[])
|
2021-05-19 16:05:01 +05:00
|
|
|
|
|
2021-05-28 12:04:50 +05:00
|
|
|
|
useEffect(()=>{
|
|
|
|
|
SaveObject(chartsCfgsKey, chartsCfgs)
|
|
|
|
|
},[chartsCfgs])
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
let interval = (rangeDate[1] - rangeDate[0]) / 1000
|
|
|
|
|
let startDate = rangeDate[0].toISOString()
|
|
|
|
|
|
2021-06-02 11:38:27 +05:00
|
|
|
|
setLoader(true)
|
2021-05-28 12:04:50 +05:00
|
|
|
|
DataService.getData(id, startDate, interval, 2048)
|
|
|
|
|
.then(handleReceiveDataSaub)
|
2021-05-31 15:21:37 +05:00
|
|
|
|
.catch(error => {
|
|
|
|
|
notify(`Не удалось загрузить данные по скважине (${id}) c ${rangeDate[0]} по ${rangeDate[1]}`, 'error')
|
|
|
|
|
console.error(error)
|
|
|
|
|
})
|
2021-06-02 11:38:27 +05:00
|
|
|
|
.finally(()=>setLoader(false))
|
2021-05-28 12:04:50 +05:00
|
|
|
|
}, [id, rangeDate]);
|
|
|
|
|
|
|
|
|
|
let charts = null
|
|
|
|
|
if(chartsCfgs.length > 0){
|
|
|
|
|
chartsCfgs[0].yDisplay = true
|
|
|
|
|
|
|
|
|
|
charts = chartsCfgs.map((cfg, i) =>
|
|
|
|
|
<Col flex={`${i===0 ? geometry.w1st : geometry.wRest}px`}
|
|
|
|
|
key={cfg.id}>
|
|
|
|
|
<ArchiveColumn
|
|
|
|
|
data={saubData}
|
|
|
|
|
rangeDate={rangeDate}
|
|
|
|
|
chartRatio={i===0 ? geometry.ratio1st : geometry.ratioRest}
|
|
|
|
|
onRemoveChart={onRemoveChart}
|
|
|
|
|
onSaveConfig={onSaveConfig}
|
|
|
|
|
config={cfg}/>
|
|
|
|
|
</Col>)
|
|
|
|
|
}
|
2021-05-19 16:05:01 +05:00
|
|
|
|
|
|
|
|
|
return (<>
|
2021-05-28 12:04:50 +05:00
|
|
|
|
<Tooltip title="Добавить график">
|
|
|
|
|
<Button
|
|
|
|
|
type="primary"
|
|
|
|
|
onClick={onAddChart}
|
|
|
|
|
disabled={chartsCfgs.length >= 6}>
|
|
|
|
|
+
|
|
|
|
|
</Button>
|
|
|
|
|
</Tooltip>
|
2021-08-17 13:01:13 +05:00
|
|
|
|
<RangePicker
|
|
|
|
|
showTime
|
|
|
|
|
allowClear={false}
|
|
|
|
|
onChange = {onChangeRange}
|
|
|
|
|
value = {rangeDate}
|
|
|
|
|
/>
|
2021-06-02 11:38:27 +05:00
|
|
|
|
<LoaderPortal show={loader}>
|
|
|
|
|
<Row ref={chartsContainerRef}>
|
|
|
|
|
{charts}
|
|
|
|
|
</Row>
|
|
|
|
|
</LoaderPortal>
|
2021-05-19 16:05:01 +05:00
|
|
|
|
</>)
|
2021-05-13 13:50:25 +05:00
|
|
|
|
}
|