-
Notifications
You must be signed in to change notification settings - Fork 534
/
Copy pathhistory_utils.dart
159 lines (145 loc) · 4.5 KB
/
history_utils.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import 'package:apidash/models/models.dart';
import 'package:apidash/consts.dart';
import 'convert_utils.dart';
DateTime stripTime(DateTime dateTime) {
return DateTime(
dateTime.year,
dateTime.month,
dateTime.day,
dateTime.hour,
dateTime.minute,
dateTime.second,
);
}
RequestModel getRequestModelFromHistoryModel(HistoryRequestModel model) {
return RequestModel(
id: model.historyId,
apiType: model.metaData.apiType,
name: model.metaData.name,
responseStatus: model.httpResponseModel.statusCode,
message: kResponseCodeReasons[model.httpResponseModel.statusCode],
httpRequestModel: model.httpRequestModel,
httpResponseModel: model.httpResponseModel,
);
}
String getHistoryRequestName(HistoryMetaModel model) {
if (model.name.isNotEmpty) {
return model.name;
} else {
return model.url;
}
}
String getHistoryRequestKey(HistoryMetaModel model) {
String timeStamp = humanizeDate(model.timeStamp);
if (model.name.isNotEmpty) {
return model.name + model.method.name + timeStamp;
} else {
return model.url + model.method.name + timeStamp;
}
}
String? getLatestRequestId(
Map<DateTime, List<HistoryMetaModel>> temporalGroups) {
if (temporalGroups.isEmpty) {
return null;
}
List<DateTime> keys = temporalGroups.keys.toList();
keys.sort((a, b) => b.compareTo(a));
return temporalGroups[keys.first]!.first.historyId;
}
DateTime getDateTimeKey(List<DateTime> keys, DateTime currentKey) {
if (keys.isEmpty) return currentKey;
for (DateTime key in keys) {
if (key.year == currentKey.year &&
key.month == currentKey.month &&
key.day == currentKey.day) {
return key;
}
}
return stripTime(currentKey);
}
Map<DateTime, List<HistoryMetaModel>> getTemporalGroups(
List<HistoryMetaModel>? models) {
Map<DateTime, List<HistoryMetaModel>> temporalGroups = {};
if (models?.isEmpty ?? true) {
return temporalGroups;
}
for (HistoryMetaModel model in models!) {
List<DateTime> existingKeys = temporalGroups.keys.toList();
DateTime key = getDateTimeKey(existingKeys, model.timeStamp);
if (existingKeys.contains(key)) {
temporalGroups[key]!.add(model);
} else {
temporalGroups[stripTime(key)] = [model];
}
}
temporalGroups.forEach((key, value) {
value.sort((a, b) => b.timeStamp.compareTo(a.timeStamp));
});
return temporalGroups;
}
Map<String, List<HistoryMetaModel>> getRequestGroups(
List<HistoryMetaModel>? models) {
Map<String, List<HistoryMetaModel>> historyGroups = {};
if (models?.isEmpty ?? true) {
return historyGroups;
}
for (HistoryMetaModel model in models!) {
String key = getHistoryRequestKey(model);
if (historyGroups.containsKey(key)) {
historyGroups[key]!.add(model);
} else {
historyGroups[key] = [model];
}
}
historyGroups.forEach((key, value) {
value.sort((a, b) => b.timeStamp.compareTo(a.timeStamp));
});
return historyGroups;
}
List<HistoryMetaModel> getRequestGroup(
List<HistoryMetaModel>? models, HistoryMetaModel? selectedModel) {
List<HistoryMetaModel> requestGroup = [];
if (selectedModel == null || (models?.isEmpty ?? true)) {
return requestGroup;
}
String selectedModelKey = getHistoryRequestKey(selectedModel);
for (HistoryMetaModel model in models!) {
String key = getHistoryRequestKey(model);
if (key == selectedModelKey) {
requestGroup.add(model);
}
}
requestGroup.sort((a, b) => b.timeStamp.compareTo(a.timeStamp));
return requestGroup;
}
int calculateOptimalBatchSize(int totalRecords) {
if (totalRecords < 100) return 50;
if (totalRecords < 500) return 80;
if (totalRecords < 5000) return 100;
return 500;
}
List<List<String>> createBatches(List<String> items, int batchSize) {
return List.generate(
(items.length / batchSize).ceil(),
(index) => items.sublist(
index * batchSize,
(index * batchSize + batchSize).clamp(0, items.length),
),
);
}
DateTime? getRetentionDate(HistoryRetentionPeriod? retentionPeriod) {
DateTime now = DateTime.now();
DateTime today = stripTime(now);
switch (retentionPeriod) {
case HistoryRetentionPeriod.fiveSeconds:
return today.subtract(const Duration(seconds: 5));
case HistoryRetentionPeriod.oneWeek:
return today.subtract(const Duration(days: 7));
case HistoryRetentionPeriod.oneMonth:
return today.subtract(const Duration(days: 30));
case HistoryRetentionPeriod.threeMonths:
return today.subtract(const Duration(days: 90));
default:
return null;
}
}