Skip to content

Commit

Permalink
table filter view
Browse files Browse the repository at this point in the history
  • Loading branch information
張百寬 authored and 張百寬 committed Sep 21, 2023
1 parent 28c07a0 commit a756810
Show file tree
Hide file tree
Showing 16 changed files with 316 additions and 225 deletions.
Binary file modified build/23afcf4cc1673bfa5f8629cb500d8482.cache.dill.track.dill
Binary file not shown.
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:web_dashboard/view_model/auth_manager.dart';
import 'package:web_dashboard/view_model/theme_manager.dart';
import 'package:web_dashboard/views/pages/dashboard/consumption_report_dashboard_view.dart';
import 'package:web_dashboard/views/pages/dashboard_config/dashboard_config_view.dart';
import 'package:web_dashboard/views/pages/data_table/daily_scda_data_table_view_model.dart';
import 'package:web_dashboard/views/pages/data_table/daily_scda_data_table_view.dart';
import 'package:web_dashboard/views/pages/data_table/device_error_report_view.dart';
import 'package:provider/provider.dart';
import 'package:firebase_core/firebase_core.dart';
Expand Down
51 changes: 0 additions & 51 deletions lib/models/data/config.dart

This file was deleted.

1 change: 0 additions & 1 deletion lib/models/data/filter_data_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ class LayerFilterData<T>{
layerLabel: layerLabel,
layerIndex: layerIndex,
layerSelectedIndex: layerIndex,

);
}
56 changes: 53 additions & 3 deletions lib/models/repo/config_repo_model.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import 'package:flutter/material.dart';
import 'package:web_dashboard/db/db_config.dart';
import 'package:web_dashboard/models/base_repo.dart';
import 'package:web_dashboard/models/data/config.dart';

class ConfigRepoModel implements RepoModel{

Expand Down Expand Up @@ -57,4 +55,56 @@ class ConfigRepoModel implements RepoModel{
'price_rate_voltage_type': priceRateVoltageType?.value,
};
}
}
}

enum RateSectionType{
twoStep('兩階段', 'two_section'), threeStep('三階段', 'three_section'), non('non', 'non');
final String label;
final String value;
const RateSectionType(this.label, this.value);
factory RateSectionType.fromValue(String value){
switch(value){
case 'two_section':
return RateSectionType.twoStep;
case 'three_section':
return RateSectionType.threeStep;
default:
return RateSectionType.non;
}
}
}

enum RateType{
fixed('fix 固定時段', 'fix'), dynamic('Dynamic 動態時段', 'dynamic'), non('non', 'non');
final String label;
final String value;
const RateType(this.label, this.value);
factory RateType.fromValue(String value){
switch(value){
case 'fix':
return RateType.fixed;
case 'dynamic':
return RateType.dynamic;
default:
return RateType.non;
}
}
}

enum RateVoltageType{
hv('高壓電 HV', 'hv'), uhv('超高壓電 UHV', 'uhv'), non('non', 'non');
final String label;
final String value;
const RateVoltageType(this.label, this.value);
factory RateVoltageType.fromValue(String value){
switch(value){
case 'hv':
return RateVoltageType.hv;
case 'uhv':
return RateVoltageType.uhv;
default:
return RateVoltageType.non;
}
}
}

1 change: 1 addition & 0 deletions lib/models/repo/sum_consumption_repo_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class SumOfElectricityConsumptionDataModel implements RepoModel{

@override
SumOfElectricityConsumptionDataModel fromJson(Map<String, dynamic> json) {

try{
var s = json['_source'];
return SumOfElectricityConsumptionDataModel(
Expand Down
12 changes: 7 additions & 5 deletions lib/models/search/search_node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,17 @@ class SearchTreeNode<T>{
}
}

SearchTreeNode? searchTree(List<String> indexes, {int index = 0}){
SearchTreeNode? searchTree(List<String> indexes, {int index = 0, bool matchAll = false}){
if(indexes.isEmpty) return null;
if(indexes.length == index){
return this;
}
String target = indexes[index];
SearchTreeNode? node = _searchTree(target);
if(node != null){
return node.searchTree(indexes, index: index + 1);
if(!matchAll){
String target = indexes[index];
SearchTreeNode? node = _searchTree(target);
if(node != null){
return node.searchTree(indexes, index: index + 1, matchAll: matchAll);
}
}
// debugPrint('search result empty');
return null;
Expand Down
1 change: 0 additions & 1 deletion lib/view_model/dashboard/dashboard_config_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import 'package:web_dashboard/db/elastic_search.dart';
import 'package:web_dashboard/models/data/config.dart';
import 'package:web_dashboard/models/data/state.dart';
import 'package:web_dashboard/models/repo/config_repo_model.dart';
import 'package:web_dashboard/view_model/base_view_model.dart';
Expand Down
29 changes: 6 additions & 23 deletions lib/view_model/dashboard/electricity_consumption_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class ElectricityConsumptionDashboardViewModel extends BaseViewModel {

List<SumOfElectricityConsumptionDataModel> _sumOfConsumptionDataList = [];
List<DeviceErrorReportModel> _deviceErrorReportList = [];
// List<ElectricityConsumptionDataModel> _electricityConsumptionDataList = [];

set setFilterOrder(List<LayerFilterData<String>> value){
filterOrder = value;
Expand Down Expand Up @@ -75,8 +74,6 @@ class ElectricityConsumptionDashboardViewModel extends BaseViewModel {
return r as ConsumptionSearchNode;
}



List<SearchTreeNode> getTimeGroupDataSource(SearchTreeNode? tree, DateTime startTime, int days){
List<SearchTreeNode> timeGroupDataSource = [];
for(int i=0;i<days;i++){
Expand All @@ -101,7 +98,7 @@ class ElectricityConsumptionDashboardViewModel extends BaseViewModel {
List<SearchTreeNode> get lastWeekSumOfElectricityConsumptionDataList
=> getTimeGroupDataSource(consumptionDataGroupSearchTree, targetDateTime.subtract(const Duration(days: 7)), 7);

Future<List<SumOfElectricityConsumptionDataModel>> getSumConsumptionDataByTime(DateTime startTime, DateTime endTime, String? tagId) async{
Future<List<SumOfElectricityConsumptionDataModel>> getSumConsumptionDataByTime(DateTime startTime, DateTime endTime) async{
Map<String, dynamic> query = {};
int maxResult = 9999;
Map<String, Map<String,String>> targetDateTime = {
Expand All @@ -112,7 +109,7 @@ class ElectricityConsumptionDashboardViewModel extends BaseViewModel {
}
};
List queryConditionList = [
{"match": {DBConfig.tagIdId : tagId}},
// {"match": {DBConfig.tagIdId : tagId}},
{"range": targetDateTime}
];
List sortList = [{DBConfig.dateTimeId: {"order": "desc"}}];
Expand All @@ -127,30 +124,16 @@ class ElectricityConsumptionDashboardViewModel extends BaseViewModel {
Future<void> init() async{
_deviceErrorReportList = [];
filterSearchTreeNode = FilterSearchTreeNode.buildTree(data: filterOrder);
// debugPrint(filterSearchTreeNode!.toList().toString());
setLoadingState(LoadingState.loading);
try{
var deviceIndexList = (await ElasticSearchClient.deviceClient().search()).map((e) => e.device?.tagId ?? "").toSet().toList();

_sumOfConsumptionDataList = [];

for(String? tagId in deviceIndexList){
var result = await getSumConsumptionDataByTime(
targetDateTime.subtract(const Duration(days: 14)),
targetDateTime.add(const Duration(hours: 23, minutes: 59, seconds: 59)),
tagId
);
if(result.isNotEmpty){
_sumOfConsumptionDataList.addAll(result);
}else{
debugPrint("no device data for $tagId");
}
}
_sumOfConsumptionDataList = await getSumConsumptionDataByTime(
targetDateTime.subtract(const Duration(days: 14)),
targetDateTime.add(const Duration(hours: 23, minutes: 59, seconds: 59)),
);
consumptionDataGroupSearchTree = ConsumptionSearchNode.buildTree(
data: _sumOfConsumptionDataList,
indexes: [DBConfig.dateTimeId, ...filterOrder.map((e) => e.layerIndex).toList()]
);

_deviceErrorReportList = await ElasticSearchClient.errorReportClient().search();
}catch(e){
debugPrint(e.toString());
Expand Down
125 changes: 96 additions & 29 deletions lib/view_model/data_table/daily_scada_data_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,49 +1,116 @@
import 'package:flutter/material.dart';
import 'package:web_dashboard/db/db_config.dart';
import 'package:web_dashboard/db/elastic_search.dart';
import 'package:web_dashboard/models/repo/consumption_repo_model.dart';
import 'package:web_dashboard/models/data/filter_data_class.dart';
import 'package:web_dashboard/models/data/state.dart';
import 'package:web_dashboard/models/repo/sum_consumption_repo_model.dart';
import 'package:web_dashboard/models/search/consumption_search_node.dart';
import 'package:web_dashboard/models/search/filter_search_node.dart';
import 'package:web_dashboard/view_model/base_view_model.dart';

class DailyScadaDataViewModel extends BaseViewModel{
// view model data
List<SumOfElectricityConsumptionDataModel> dataList = [];
List<SumOfElectricityConsumptionDataModel> _dataList = [];
List<SumOfElectricityConsumptionDataModel> get dataList{
if(filterSearchTreeNode != null && consumptionDataGroupSearchTree != null){
var searchList = filterSearchTreeNode!.levelList();
// consumptionDataGroupSearchTree!.searchTree(searchList, matchAll: true)!.printTree();
List<SumOfElectricityConsumptionDataModel> result = [];
if(searchList.length == 1) {
return _dataList;
}
// get time label
var timeNode = consumptionDataGroupSearchTree!.searchTree(['all', 'datetime']);
for (var node in timeNode!.children) {
// debugPrint("node: ${node.index}");
result.addAll(node.searchTree(searchList)!.toList() as List<SumOfElectricityConsumptionDataModel>);
}
return result;
}
return _dataList;
}
DateTime _searchStartTime = DateTime.now();
DateTime _searchEndTime = DateTime.now();
DateTime get searchStartTime => DateTime(_searchStartTime.year, _searchStartTime.month, _searchStartTime.day - 7, 0, 0 , 0);
DateTime get searchEndTime => DateTime(_searchEndTime.year, _searchEndTime.month, _searchEndTime.day, 23, 59, 59);

@override
Future<void> init() async {
setLoadingState(LoadingState.loading);
dataList = await ElasticSearchClient.sumOfConsumptionClient().search(
query: {
"size": 9999,
"query": {
"bool": {
"must": [
{
"range": {
"datetime": {
"time_zone": "+08:00",
"gte": DateTime.now().subtract(const Duration(days: 14)).toIso8601String(),
"lte": DateTime.now().add(const Duration(hours: 23, minutes: 59, seconds: 59)).toIso8601String()
}
}
},
]
}
},
"sort": [
ConsumptionSearchNode? consumptionDataGroupSearchTree;
FilterSearchTreeNode? filterSearchTreeNode;
List<LayerFilterData<String>> filterOrder = [
LayerFilterData.init(layerLabel: "L1 廠區", layerIndex: DBConfig.locId),
LayerFilterData.init(layerLabel: "L2 建築", layerIndex: DBConfig.buildingId),
LayerFilterData.init(layerLabel: "L3 產線類別",layerIndex: DBConfig.lineTypeId),
LayerFilterData.init(layerLabel: "L4 用電部門", layerIndex: DBConfig.departmentId),
LayerFilterData.init(layerLabel: "L5 設備部門",layerIndex: DBConfig.assetTypeId),
LayerFilterData.init(layerLabel: "L6 設備編號",layerIndex: DBConfig.tagIdId),
];

void checkSearchTimeOrder(){
if(_searchStartTime.isAfter(_searchEndTime)){
DateTime temp = _searchStartTime;
_searchStartTime = _searchEndTime;
_searchEndTime = temp;
}
}

set searchStartTime(DateTime newTime){
_searchStartTime = newTime;
checkSearchTimeOrder();
init();
notifyListeners();
}

set searchEndTime(DateTime newTime){
_searchEndTime = newTime;
checkSearchTimeOrder();
init();
notifyListeners();
}

Map<String, dynamic> get queryObject => {
"size": 9999,
"query": {
"bool": {
"must": [
{
"datetime": {
"order": "desc"
"range": {
"datetime": {
"time_zone": "+08:00",
"gte": searchStartTime.toIso8601String(),
"lte": searchEndTime.toIso8601String(),
}
}
}
},
]
}
);
},
"sort": [
{
"datetime": { "order": "desc"}
}
]
};



@override
Future<void> init() async {
setLoadingState(LoadingState.loading);
_dataList = await ElasticSearchClient.sumOfConsumptionClient().search(
query: queryObject
);
consumptionDataGroupSearchTree = ConsumptionSearchNode.buildTree(
data: dataList,
indexes: [DBConfig.dateTimeId, ...filterOrder.map((e) => e.layerIndex).toList()]
);
// consumptionDataGroupSearchTree!.printTree();
filterSearchTreeNode = FilterSearchTreeNode.buildTree(data: filterOrder);
setLoadingState(LoadingState.error);
}
// view model getter and setter
List<SumOfElectricityConsumptionDataModel> get dataSource => dataList;
set updateDeviceErrorReportData(List<SumOfElectricityConsumptionDataModel> value){
dataList = value;
_dataList = value;
notifyListeners();
}
}
Loading

0 comments on commit a756810

Please sign in to comment.