oc-front/lib/models/workflow.dart

627 lines
19 KiB
Dart
Raw Normal View History

2024-08-08 08:42:32 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_colorpicker/flutter_colorpicker.dart';
import 'package:flutter_flow_chart/flutter_flow_chart.dart';
import 'package:oc_front/core/models/workspace_local.dart';
import 'package:oc_front/models/abstract.dart';
2024-08-22 15:46:16 +02:00
import 'package:oc_front/models/logs.dart';
2024-08-30 12:52:32 +02:00
import 'package:oc_front/models/response.dart';
2024-08-08 08:42:32 +02:00
import 'package:oc_front/models/search.dart';
2024-08-22 15:46:16 +02:00
class Check extends SerializerDeserializer<Check> {
bool is_available = false;
Check({
this.is_available = false,
});
@override deserialize(dynamic json) {
try { json = json as Map<String, dynamic>;
} catch (e) { return Check(); }
return Check(
is_available: json.containsKey("is_available") ? json["is_available"] : false,
);
}
@override Map<String, dynamic> serialize() {
return {
"is_available": is_available,
};
}
}
2024-08-08 08:42:32 +02:00
class WorkflowExecutions extends SerializerDeserializer<WorkflowExecutions> {
List<WorkflowExecution> executions = [];
String? executionData;
int? status;
String? workflowId;
WorkflowExecutions({
this.executions = const [],
});
@override deserialize(dynamic json) {
try { json = json as List<dynamic>;
} catch (e) { return WorkflowExecutions(); }
return WorkflowExecutions(
executions: fromListJson(json, WorkflowExecution()),
);
}
@override Map<String, dynamic> serialize() {
return {};
}
}
class WorkflowExecution extends SerializerDeserializer<WorkflowExecution> {
String? id;
String? name;
String? executionData;
String? endDate;
int? status;
String? workflowId;
2024-08-22 15:46:16 +02:00
List<Log>? logs;
2024-08-08 08:42:32 +02:00
WorkflowExecution({
this.id,
this.executionData,
this.status,
this.workflowId,
this.name,
this.endDate,
});
@override deserialize(dynamic json) {
try { json = json as Map<String, dynamic>;
} catch (e) { return WorkflowExecution(); }
return WorkflowExecution(
id: json.containsKey("id") ? json["id"] : "",
endDate: json.containsKey("end_date") ? json["end_date"] : "",
executionData: json.containsKey("execution_date") ? json["execution_date"] : "",
status: json.containsKey("status") ? json["status"] : 1,
workflowId: json.containsKey("workflow_id") ? json["workflow_id"] : "",
name: json.containsKey("name") ? json["name"] : "",
);
}
@override Map<String, dynamic> serialize() {
return {
"id": id,
"name": name,
"end_date": endDate,
"execution_data": executionData,
"status": status,
"workflow_id": workflowId,
};
}
}
2024-08-30 12:52:32 +02:00
class Workflow extends SerializerDeserializer<Workflow> implements ShallowData {
2024-08-08 08:42:32 +02:00
String? id;
String? name;
List<dynamic> data;
List<dynamic> datacenter;
List<dynamic> storage;
List<dynamic> processing;
List<dynamic> workflows;
Graph? graph;
Scheduler? schedule;
2024-08-22 15:46:16 +02:00
bool scheduleActive = false;
2024-08-27 15:38:21 +02:00
List<dynamic> shared;
2024-08-08 08:42:32 +02:00
Workflow({
this.id,
this.name = "",
this.data = const [],
this.datacenter = const [],
this.storage = const [],
this.processing = const [],
this.workflows = const [],
this.graph,
this.schedule,
2024-08-22 15:46:16 +02:00
this.scheduleActive = false,
2024-08-27 15:38:21 +02:00
this.shared = const [],
2024-08-08 08:42:32 +02:00
});
2024-08-30 12:52:32 +02:00
@override String getID() => id ?? "";
@override String getName() => name ?? "";
@override String getDescription() => "";
2024-08-08 08:42:32 +02:00
@override deserialize(dynamic json) {
try { json = json as Map<String, dynamic>;
} catch (e) { return Workflow(); }
return Workflow(
id: json.containsKey("id") ? json["id"] : "",
name: json.containsKey("name") ? json["name"] : "",
workflows: json.containsKey("workflows") ? json["workflows"] : [],
processing: json.containsKey("processings") ? json["processings"] : [],
datacenter: json.containsKey("datacenters") ? json["datacenters"] : [],
data: json.containsKey("datas") ? json["datas"] : [],
2024-08-22 15:46:16 +02:00
scheduleActive: json.containsKey("schedule_active") ? json["schedule_active"] : false,
2024-08-08 08:42:32 +02:00
storage: json.containsKey("storages") ? json["storages"] : [],
2024-08-27 15:38:21 +02:00
shared: json.containsKey("shared") ? json["shared"] : [],
2024-08-08 08:42:32 +02:00
graph: json.containsKey("graph") ? Graph().deserialize(json["graph"]) : null,
schedule: json.containsKey("schedule") ? Scheduler().deserialize(json["schedule"]) : null,
);
}
@override Map<String, dynamic> serialize() {
var obj = {
"id": id,
"name": name,
"datas": data,
"datacenters" : datacenter,
"storages": storage,
"processings": processing,
"workflows": workflows,
2024-08-22 15:46:16 +02:00
"schedule_active": scheduleActive,
2024-08-08 08:42:32 +02:00
"schedule": schedule?.serialize(),
};
if (graph != null) {
obj["graph"] = graph!.serialize();
}
return obj;
}
void fromDashboard(Map<String, dynamic> j) {
id = j["id"];
name = j["name"];
2024-08-22 15:46:16 +02:00
scheduleActive = j["schedule_active"];
2024-08-08 08:42:32 +02:00
if (j.containsKey("graph")) {
graph = Graph();
graph!.fromDashboard(j["graph"]);
}
if (j.containsKey("schedule")) {
schedule = Scheduler();
schedule!.fromDashboard(j["schedule"]);
}
}
Map<String, dynamic> toDashboard() {
return {
"id": id,
"name": name,
"graph": graph?.toDashboard(),
2024-08-22 15:46:16 +02:00
"schedule_active": scheduleActive,
2024-08-08 08:42:32 +02:00
"schedule": schedule?.toDashboard(),
2024-08-27 15:38:21 +02:00
"shared": shared,
2024-08-08 08:42:32 +02:00
};
}
}
class Scheduler extends SerializerDeserializer<Scheduler> {
String? id;
String? name;
String? cron;
DateTime? start;
DateTime? end;
2024-08-22 15:46:16 +02:00
int? mode;
2024-08-08 08:42:32 +02:00
Scheduler({
this.id,
this.name,
this.cron,
this.start,
2024-08-22 15:46:16 +02:00
this.end,
this.mode,
2024-08-08 08:42:32 +02:00
});
void fromDashboard(Map<String, dynamic> j) {
id = j["id"];
name = j["name"];
cron = j["cron"];
2024-08-30 12:52:32 +02:00
mode =j["mode"];
2024-08-27 15:38:21 +02:00
try {
2024-08-30 12:52:32 +02:00
start = j["start"] != null ? DateTime.parse(j["start"]) : DateTime.now().add( const Duration(minutes: 1)).toUtc();
if (start == DateTime.utc(0)) {
start = DateTime.now().add( const Duration(minutes: 1)).toUtc();
}
2024-08-27 15:38:21 +02:00
if (j.containsKey("end") && j["end"] != null) {
end = DateTime.parse(j["end"]);
}
2024-08-30 12:52:32 +02:00
2024-08-27 15:38:21 +02:00
} catch (e) {}
2024-08-08 08:42:32 +02:00
}
Map<String, dynamic> toDashboard() {
return {
"id": id,
"name": name,
"cron": cron,
2024-08-30 12:52:32 +02:00
"mode": mode ?? 1,
2024-08-08 08:42:32 +02:00
"start": start?.toIso8601String(),
"end": end?.toIso8601String(),
};
}
@override deserialize(dynamic json) {
try { json = json as Map<String, dynamic>;
} catch (e) { return Scheduler(); }
return Scheduler(
id: json.containsKey("id") ? json["id"] : null,
name: json.containsKey("name") ? json["name"] : "",
cron: json.containsKey("cron") ? json["cron"] : "",
2024-08-22 15:46:16 +02:00
mode: json.containsKey("mode") ? json["mode"] : "",
2024-08-27 15:38:21 +02:00
start: json.containsKey("start") && json["start"] != null ? DateTime.parse(json["start"]) : null,
end: json.containsKey("end") && json["end"] != null ? DateTime.parse(json["end"]) : null,
2024-08-08 08:42:32 +02:00
);
}
2024-08-30 12:52:32 +02:00
@override Map<String, dynamic> serialize() {
try {
return {
"id": id,
"name": name,
"cron": cron ?? "",
"mode": mode ?? 1,
"start": start?.toIso8601String(),
"end": end?.toIso8601String(),
};
} catch (e) {
return {
"id": id,
"name": name,
"cron": cron ?? "",
"start": start?.toIso8601String(),
"end": end?.toIso8601String(),
};
}
}
2024-08-08 08:42:32 +02:00
}
2024-08-30 12:52:32 +02:00
2024-08-08 08:42:32 +02:00
class Graph extends SerializerDeserializer<Graph> {
double zoom;
Map<String, GraphItem> items = {};
List<GraphLink> links = [];
Graph({
this.zoom = 1,
this.items = const {},
this.links = const [],
});
void fromDashboard(Map<String, dynamic> j) {
items = {};
for (var el in (j["elements"] as Map<dynamic, dynamic>).values) {
var d = GraphItem();
d.fromDashboard(el as Map<String, dynamic>);
items[d.id ?? ""] = d;
}
links = (j["arrows"] as List<dynamic>).map( (el) {
var d = GraphLink();
d.fromDashboard(el);
return d;
}).toList();
j["zoom"] = zoom;
}
Map<String, dynamic> toDashboard() {
List<Map<String, dynamic>> elements = [];
List<Map<String, dynamic>> arrows = [];
for (var el in items.values) {
elements.add(el.toDashboard());
}
for (var l in links) {
arrows.add(l.toDashboard());
}
return {
"zoom": zoom,
"elements": elements,
"arrows": arrows,
};
}
@override deserialize(dynamic json) {
try { json = json as Map<String, dynamic>;
} catch (e) { return Graph(); }
return Graph(
zoom: json.containsKey("zoom") ? double.parse(json["zoom"].toString()) : 0,
links: json.containsKey("links") ? fromListJson(json["links"], GraphLink()) : [],
items: json.containsKey("items") ? fromMapJson<GraphItem>(json["items"], GraphItem()) : {},
);
}
@override Map<String, dynamic> serialize() => {
"zoom": zoom,
"items": toMapJson(items),
"links": toListJson(links),
};
}
class GraphLink extends SerializerDeserializer<GraphLink> {
Position? source;
Position? destination;
GraphLinkStyle? style;
GraphLink({
this.source,
this.destination,
this.style,
});
void fromDashboard(Map<String, dynamic> j) {
source = Position(id: j["from"]["id"], x: j["from"]["x"], y: j["from"]["y"]);
destination = Position(id: j["to"]["id"], x: j["to"]["x"], y: j["to"]["y"]);
style = GraphLinkStyle();
style!.fromDashboard(j["params"]);
}
Map<String, dynamic> toDashboard() {
return {
"from": {
"id": source?.id ?? "",
"x" : source?.x ?? 0,
"y": source?.y ?? 0,
},
"to": {
"id": destination?.id ?? "",
"x" : destination?.x ?? 0,
"y": destination?.y ?? 0,
},
"params": style?.toDashboard(),
};
}
@override deserialize(dynamic json) {
try { json = json as Map<String, dynamic>;
} catch (e) { return GraphLink(); }
return GraphLink(
source: json.containsKey("source") ? Position().deserialize(json["source"]) : null,
destination: json.containsKey("destination") ? Position().deserialize(json["destination"]) : null,
style: json.containsKey("style") ? GraphLinkStyle().deserialize(json["style"]) : null,
);
}
@override Map<String, dynamic> serialize() {
var obj = <String, dynamic>{};
if (source != null) {
obj["source"] = source!.serialize();
}
if (destination != null) {
obj["destination"] = destination!.serialize();
}
if (style != null) {
obj["style"] = style!.serialize();
}
return obj;
}
}
class GraphLinkStyle extends SerializerDeserializer<GraphLinkStyle> {
Color color = Colors.black;
double stroke = 1;
double tension = 23;
double headRadius = 10;
double dashWidth = 0;
double dashSpace = 0;
double startArrowWidth = 1;
double endArrowWidth = 10;
Position? startArrow;
Position? endArrow;
ArrowStyle arrowStyle = ArrowStyle.curve;
ArrowDirection arrowDirection = ArrowDirection.forward;
GraphLinkStyle({
this.color = Colors.black,
this.stroke = 1,
this.tension = 23,
this.headRadius = 10,
this.dashWidth = 0,
this.dashSpace = 0,
this.startArrowWidth = 10,
this.endArrowWidth = 10,
this.startArrow,
this.endArrow,
this.arrowStyle = ArrowStyle.curve,
this.arrowDirection = ArrowDirection.forward
});
void fromDashboard(Map<String, dynamic> j) {
dashSpace = j["dash_space"];
dashWidth = j["dash_width"];
endArrowWidth = j["backward_arrow_width"];
startArrowWidth = j["forward_arrow_width"];
stroke = j["thickness"];
arrowDirection = ArrowDirection.values.firstWhere((element) => element.index == j["direction"]);
headRadius = j["head_radius"];
color = Color(j["color"]);
arrowStyle = ArrowStyle.values.firstWhere((element) => element.index == j["arrow_style"]);
tension = j["tension"];
startArrow = Position(x: j["start_arrow_position_x"], y: j["start_arrow_position_y"]);
endArrow = Position(x: j["end_arrow_position_x"], y: j["end_arrow_position_y"]);
}
Map<String, dynamic> toDashboard() {
return {
"dash_space": dashSpace,
"dash_width": dashWidth,
"backward_arrow_width": endArrowWidth,
"forward_arrow_width": startArrowWidth,
"thickness": stroke,
"direction": arrowDirection.index,
"head_radius": headRadius,
"tail_length": 25.0,
"color": int.parse(color.toHexString(), radix: 16),
"arrow_style" : arrowStyle.index,
"tension" : tension,
"start_arrow_position_x": startArrow?.x ?? 0,
"start_arrow_position_y": startArrow?.y ?? 0,
"end_arrow_position_x": endArrow?.x ?? 0,
"end_arrow_position_y": endArrow?.y ?? 0,
};
}
@override deserialize(dynamic json) {
try { json = json as Map<String, dynamic>;
} catch (e) { return GraphLinkStyle(); }
return GraphLinkStyle(
color: json.containsKey("color") ? Color(json["color"] as int) : Colors.black,
stroke: json.containsKey("stroke") ? double.parse(json["stroke"].toString()) : 0,
tension: json.containsKey("tension") ? double.parse(json["tension"].toString()) : 0,
headRadius: json.containsKey("head_radius") ? double.parse(json["head_radius"].toString()) : 0,
dashWidth: json.containsKey("dash_width") ? double.parse(json["dash_width"].toString()) : 0,
dashSpace: json.containsKey("dash_space") ? double.parse(json["dash_space"].toString()) : 0,
startArrow: json.containsKey("start_arrow") ? Position().deserialize(json["start_arrow"]) : null,
endArrow: json.containsKey("end_arrow") ? Position().deserialize(json["end_arrow"]) : null,
arrowDirection: json.containsKey("arrow_direction") ? ArrowDirection.values.firstWhere((el) => el.index == json["arrow_direction"]) : ArrowDirection.forward,
arrowStyle: json.containsKey("arrow_style") ? ArrowStyle.values.firstWhere((el) => el.index == json["arrow_style"]) : ArrowStyle.curve,
startArrowWidth: json.containsKey("start_arrow_width") ? double.parse(json["start_arrow_width"].toString()) : 0,
endArrowWidth: json.containsKey("end_arrow_width") ? double.parse(json["end_arrow_width"].toString()) : 0,
);
}
@override Map<String, dynamic> serialize() {
var obj = <String, dynamic> {
"color" : int.parse(color.toHexString(), radix: 16),
"stroke" : stroke,
"tension" : tension,
"head_radius" : headRadius,
"dash_width" : dashWidth,
"dash_space" : dashSpace,
"arrow_direction" : arrowDirection.index,
"arrow_style" : arrowStyle.index,
"start_arrow_width" : startArrowWidth,
"end_arrow_width" : endArrowWidth,
};
if (startArrow != null) {
obj["start_arrow"] = startArrow!.serialize();
}
if (endArrow != null) {
obj["end_arrow"] = endArrow!.serialize();
}
return obj;
}
}
class GraphItem extends SerializerDeserializer<GraphItem> {
String? id;
double? width;
double? height;
Position? position;
DataItem? data;
ProcessingItem? processing;
StorageItem? storage;
DataCenterItem? datacenter;
WorkflowItem? workflow;
GraphItem({
this.id,
this.width,
this.height,
this.position,
this.data,
this.processing,
this.storage,
this.datacenter,
this.workflow,
});
void fromDashboard(Map<String, dynamic> j) {
id = j["id"];
position = Position(x: j["x"], y: j["y"]);
width = j["width"];
height = j["height"];
var abs = WorkspaceLocal.getItem(j["element"]?["id"] ?? "", true) as AbstractItem<FlowData>?;
if (abs != null) {
if (abs.topic == "data") {
data = DataItem().deserialize(abs.serialize());
data!.model = ResourceModel().deserialize(j["element"]["resource_model"]);
2024-08-30 12:52:32 +02:00
} else if (abs.topic == "processing") {
2024-08-08 08:42:32 +02:00
processing = ProcessingItem().deserialize(abs.serialize());
processing!.model = ResourceModel().deserialize(j["element"]["resource_model"]);
2024-08-30 12:52:32 +02:00
} else if (abs.topic == "datacenter") {
2024-08-08 08:42:32 +02:00
datacenter = DataCenterItem().deserialize(abs.serialize());
datacenter!.model = ResourceModel().deserialize(j["element"]["resource_model"]);
2024-08-30 12:52:32 +02:00
} else if (abs.topic == "storage") {
2024-08-08 08:42:32 +02:00
storage = StorageItem().deserialize(abs.serialize());
storage!.model = ResourceModel().deserialize(j["element"]["resource_model"]);
2024-08-30 12:52:32 +02:00
} else if (abs.topic == "workflow") {
2024-08-08 08:42:32 +02:00
workflow = WorkflowItem().deserialize(abs.serialize());
workflow!.model = ResourceModel().deserialize(j["element"]["resource_model"]);
2024-08-30 12:52:32 +02:00
} else {
datacenter = null;
data = null;
processing = null;
storage = null;
workflow = null;
2024-08-08 08:42:32 +02:00
}
2024-08-30 12:52:32 +02:00
} else {
datacenter = null;
data = null;
processing = null;
storage = null;
workflow = null;
}
2024-08-08 08:42:32 +02:00
}
Map<String, dynamic> toDashboard() {
Map<String, dynamic> element = {};
for(var el in [data, processing, storage, datacenter, workflow]) {
if (el != null && el.getID() != "") {
element = el.serialize();
break;
}
}
return {
"id": id,
"x": (position?.x ?? 0),
"y": (position?.y ?? 0),
"width": width ?? 0,
"height": height ?? 0,
"element": element,
};
}
@override deserialize(dynamic json) {
try { json = json as Map<String, dynamic>;
} catch (e) { return GraphItem(); }
return GraphItem(
id: json.containsKey("id") ? json["id"] : null,
width: json.containsKey("width") ? double.parse(json["width"].toString()) : null,
height: json.containsKey("height") ? double.parse(json["height"].toString()) : null,
position: json.containsKey("position") ? Position().deserialize(json["position"]) : null,
data: json.containsKey("data") ? DataItem().deserialize(json["data"]) : null,
processing: json.containsKey("processing") ? ProcessingItem().deserialize(json["processing"]) : null,
storage: json.containsKey("storage") ? StorageItem().deserialize(json["storage"]) : null,
datacenter: json.containsKey("datacenter") ? DataCenterItem().deserialize(json["datacenter"]) : null,
workflow: json.containsKey("workflow") ? WorkflowItem().deserialize(json["workflow"]) : null,
);
}
@override Map<String, dynamic> serialize() {
return {
"id": id,
"width": width,
"height": height,
"data": data?.serialize(),
"processing": processing?.serialize(),
"storage": storage?.serialize(),
"datacenter": datacenter?.serialize(),
"workflow": workflow?.serialize(),
"position": position?.serialize(),
};
}
}
class Position extends SerializerDeserializer<Position> {
String? id;
double? x;
double? y;
Position({
this.id,
this.x,
this.y,
});
@override deserialize(dynamic json) {
try { json = json as Map<String, dynamic>;
} catch (e) { return Position(); }
return Position(
id: json.containsKey("id") ? json["id"] : null,
x: json.containsKey("x") ? double.parse(json["x"].toString()) : null,
y: json.containsKey("y") ? double.parse(json["y"].toString()) : null,
);
}
@override Map<String, dynamic> serialize() => {
"id": id,
2024-08-30 12:52:32 +02:00
"x": x ,
2024-08-08 08:42:32 +02:00
"y": y,
};
}