Skip to content
Snippets Groups Projects
Commit 4c7f421e authored by doodlezucc's avatar doodlezucc
Browse files

move fetch statistics to separate class

parent 68df2013
No related branches found
No related tags found
No related merge requests found
......@@ -11,7 +11,7 @@ import 'element_type.dart';
import 'lecture.dart';
typedef RequestOptions = (String path, String method, Json params);
typedef JsonWithCookies = (Json json, List<Cookie> cookies);
typedef RequestResult = (Json json, List<Cookie> cookies, FetchStats stats);
class UntisFetch {
static const String apiBase = '/WebUntis/api/public';
......@@ -64,10 +64,10 @@ class UntisFetch {
return response;
}
Future<JsonWithCookies> _requestJson(RequestOptions options) async {
Future<RequestResult> _requestJson(RequestOptions options) async {
final (_, String method, _) = options;
final before = DateTime.now();
final onStart = DateTime.now();
final response = await _request(method, _makeUri(options));
final cookies = response.cookies;
......@@ -79,30 +79,19 @@ class UntisFetch {
}
}
final afterDownloadStart = DateTime.now();
final responseTime = afterDownloadStart.difference(before);
final onDownloadStart = DateTime.now();
final responseString = await utf8.decodeStream(response);
final afterDownloadEnd = DateTime.now();
final downloadTime = afterDownloadEnd.difference(afterDownloadStart);
final onDownloadEnd = DateTime.now();
final bodyJson = jsonDecode(responseString);
final afterDecode = DateTime.now();
final decodeTime = afterDecode.difference(afterDownloadEnd);
final onDecodeEnd = DateTime.now();
final timeStats = {
'Ping': responseTime,
'Download': downloadTime,
'JSON': decodeTime,
};
final stats =
FetchStats(onStart, onDownloadStart, onDownloadEnd, onDecodeEnd);
print(timeStats.entries
.map((e) => '${e.key}: ${e.value.inMilliseconds}ms')
.join(', '));
return (bodyJson as Json, resultCookies);
return (bodyJson as Json, resultCookies, stats);
}
Future<Json> requestOptions(RequestOptions options) async {
......@@ -112,9 +101,10 @@ class UntisFetch {
final methodPadded = method.padRight(8);
print('$methodPadded $uri');
final (result, cookies) = await compute(_requestJson, options);
final (result, cookies, stats) = await compute(_requestJson, options);
_sessionCookies.addAll(cookies);
print('$stats');
return result;
}
......@@ -127,3 +117,34 @@ class UntisFetch {
return await requestOptions(options);
}
}
class FetchStats {
final DateTime onStart;
final DateTime onDownloadStart;
final DateTime onDownloadEnd;
final DateTime onDecodeEnd;
FetchStats(
this.onStart,
this.onDownloadStart,
this.onDownloadEnd,
this.onDecodeEnd,
);
@override
String toString() {
final responseTime = onDownloadStart.difference(onStart);
final downloadTime = onDownloadEnd.difference(onDownloadStart);
final decodeTime = onDecodeEnd.difference(onDownloadEnd);
final timeStats = {
'Ping': responseTime,
'Download': downloadTime,
'JSON': decodeTime,
};
return timeStats.entries
.map((e) => '${e.key}: ${e.value.inMilliseconds}ms')
.join(', ');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment