From df2e4b72d3f7cfc64a3650feb1e5e7ea28a7171b Mon Sep 17 00:00:00 2001 From: Sebastian Date: Tue, 1 Sep 2020 10:41:24 +0200 Subject: [PATCH] Improve javascript error handling. --- .../Scripting/JintScriptEngine.cs | 8 ++++++-- .../Contents/ContentDomainObject.cs | 2 +- .../Contents/ContentSchedulerGrain.cs | 1 + .../UsageTracking/ApiStatsSummary.cs | 9 ++++++++- .../UsageTracking/ApiUsageTracker.cs | 9 ++++++++- .../Statistics/Models/CallsUsageDtoDto.cs | 12 ++++++++++++ .../UsageTracking/ApiUsageTrackerTests.cs | 11 ++++++++++- .../cards/api-calls-summary-card.component.html | 4 ++-- .../pages/cards/api-calls-summary-card.component.ts | 4 ++-- .../cards/api-traffic-summary-card.component.html | 4 ++-- .../cards/api-traffic-summary-card.component.ts | 4 ++-- .../fields/types/string-validation.component.ts | 2 +- frontend/app/shared/services/usages.service.spec.ts | 4 +++- frontend/app/shared/services/usages.service.ts | 4 ++++ 14 files changed, 62 insertions(+), 16 deletions(-) diff --git a/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/JintScriptEngine.cs b/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/JintScriptEngine.cs index 4a1a94ac4..67e0b3f98 100644 --- a/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/JintScriptEngine.cs +++ b/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/JintScriptEngine.cs @@ -184,11 +184,15 @@ namespace Squidex.Domain.Apps.Core.Scripting } catch (JavaScriptException ex) { - throw new ValidationException(T.Get("common.jsError", new { error = ex.Message })); + throw new ValidationException(T.Get("common.jsError", new { message = ex.Message })); } catch (ParserException ex) { - throw new ValidationException(T.Get("common.jsError", new { error = ex.Message })); + throw new ValidationException(T.Get("common.jsError", new { message = ex.Message })); + } + catch + { + throw new ValidationException(T.Get("common.jsError", new { message = "RuntimeError" })); } } } diff --git a/backend/src/Squidex.Domain.Apps.Entities/Contents/ContentDomainObject.cs b/backend/src/Squidex.Domain.Apps.Entities/Contents/ContentDomainObject.cs index a720b1bc7..5c44d4b12 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Contents/ContentDomainObject.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Contents/ContentDomainObject.cs @@ -172,7 +172,7 @@ namespace Squidex.Domain.Apps.Entities.Contents } catch (Exception) { - if (Snapshot.ScheduleJob?.Id == c.JobId) + if (Snapshot.ScheduleJob != null && Snapshot.ScheduleJob.Id == c.JobId) { CancelChangeStatus(c); } diff --git a/backend/src/Squidex.Domain.Apps.Entities/Contents/ContentSchedulerGrain.cs b/backend/src/Squidex.Domain.Apps.Entities/Contents/ContentSchedulerGrain.cs index 8e7240300..d50659cf1 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Contents/ContentSchedulerGrain.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Contents/ContentSchedulerGrain.cs @@ -69,6 +69,7 @@ namespace Squidex.Domain.Apps.Entities.Contents return contentRepository.QueryScheduledWithoutDataAsync(now, content => { + return Task.CompletedTask; return Dispatch(async () => { try diff --git a/backend/src/Squidex.Infrastructure/UsageTracking/ApiStatsSummary.cs b/backend/src/Squidex.Infrastructure/UsageTracking/ApiStatsSummary.cs index 292245cde..525ad4bf6 100644 --- a/backend/src/Squidex.Infrastructure/UsageTracking/ApiStatsSummary.cs +++ b/backend/src/Squidex.Infrastructure/UsageTracking/ApiStatsSummary.cs @@ -13,13 +13,20 @@ namespace Squidex.Infrastructure.UsageTracking public long TotalBytes { get; } + public long MonthCalls { get; } + + public long MonthBytes { get; } + public double AverageElapsedMs { get; } - public ApiStatsSummary(long totalCalls, double averageElapsedMs, long totalBytes) + public ApiStatsSummary(double averageElapsedMs, long totalCalls, long totalBytes, long monthCalls, long monthBytes) { TotalCalls = totalCalls; TotalBytes = totalBytes; + MonthCalls = monthCalls; + MonthBytes = monthBytes; + AverageElapsedMs = averageElapsedMs; } } diff --git a/backend/src/Squidex.Infrastructure/UsageTracking/ApiUsageTracker.cs b/backend/src/Squidex.Infrastructure/UsageTracking/ApiUsageTracker.cs index 9a4334d7f..a17ebdfc2 100644 --- a/backend/src/Squidex.Infrastructure/UsageTracking/ApiUsageTracker.cs +++ b/backend/src/Squidex.Infrastructure/UsageTracking/ApiUsageTracker.cs @@ -90,7 +90,14 @@ namespace Squidex.Infrastructure.UsageTracking var summaryElapsedAvg = CalculateAverage(summaryCalls, summaryElapsed); - var summary = new ApiStatsSummary(summaryCalls, summaryElapsedAvg, summaryBytes); + var monthStats = await usageTracker.GetForMonthAsync(apiKey, DateTime.Today, null); + + var summary = new ApiStatsSummary( + summaryElapsedAvg, + summaryCalls, + summaryBytes, + monthStats.GetInt64(CounterTotalCalls), + monthStats.GetInt64(CounterTotalBytes)); return (summary, details); } diff --git a/backend/src/Squidex/Areas/Api/Controllers/Statistics/Models/CallsUsageDtoDto.cs b/backend/src/Squidex/Areas/Api/Controllers/Statistics/Models/CallsUsageDtoDto.cs index 47e536676..9cdffd8a9 100644 --- a/backend/src/Squidex/Areas/Api/Controllers/Statistics/Models/CallsUsageDtoDto.cs +++ b/backend/src/Squidex/Areas/Api/Controllers/Statistics/Models/CallsUsageDtoDto.cs @@ -27,6 +27,16 @@ namespace Squidex.Areas.Api.Controllers.Statistics.Models /// public long TotalBytes { get; set; } + /// + /// The total number of API calls this month. + /// + public long MonthCalls { get; set; } + + /// + /// The total number of bytes transferred this month. + /// + public long MonthBytes { get; set; } + /// /// The amount of calls that will block the app. /// @@ -63,6 +73,8 @@ namespace Squidex.Areas.Api.Controllers.Statistics.Models AllowedCalls = plan.MaxApiCalls, TotalBytes = summary.TotalBytes, TotalCalls = summary.TotalCalls, + MonthBytes = summary.MonthBytes, + MonthCalls = summary.MonthCalls, Details = details.ToDictionary(x => x.Key, x => x.Value.Select(CallsUsagePerDateDto.FromStats).ToArray()) }; } diff --git a/backend/tests/Squidex.Infrastructure.Tests/UsageTracking/ApiUsageTrackerTests.cs b/backend/tests/Squidex.Infrastructure.Tests/UsageTracking/ApiUsageTrackerTests.cs index 512d5b6a3..1acb5b2af 100644 --- a/backend/tests/Squidex.Infrastructure.Tests/UsageTracking/ApiUsageTrackerTests.cs +++ b/backend/tests/Squidex.Infrastructure.Tests/UsageTracking/ApiUsageTrackerTests.cs @@ -106,6 +106,15 @@ namespace Squidex.Infrastructure.UsageTracking } }; + var forMonth = new Counters + { + [ApiUsageTracker.CounterTotalCalls] = 120, + [ApiUsageTracker.CounterTotalBytes] = 400 + }; + + A.CallTo(() => usageTracker.GetForMonthAsync($"{key}_API", DateTime.Today, null)) + .Returns(forMonth); + A.CallTo(() => usageTracker.QueryAsync($"{key}_API", dateFrom, dateTo)) .Returns(counters); @@ -131,7 +140,7 @@ namespace Squidex.Infrastructure.UsageTracking } }); - summary.Should().BeEquivalentTo(new ApiStatsSummary(15, 20, 3728)); + summary.Should().BeEquivalentTo(new ApiStatsSummary(20, 15, 3728, 120, 400)); } private static Counters Counters(long calls, long elapsed, long bytes) diff --git a/frontend/app/features/dashboard/pages/cards/api-calls-summary-card.component.html b/frontend/app/features/dashboard/pages/cards/api-calls-summary-card.component.html index 8bf16926d..5630b9cc3 100644 --- a/frontend/app/features/dashboard/pages/cards/api-calls-summary-card.component.html +++ b/frontend/app/features/dashboard/pages/cards/api-calls-summary-card.component.html @@ -1,10 +1,10 @@
{{ 'dashboard.apiCallsSummaryCard' | sqxTranslate }}
-
+
{{ 'dashboard.currentMonthLabel' | sqxTranslate }}
-
{{callsTotal | sqxKNumber}}
+
{{callsMonth | sqxKNumber}}
{{ 'dashboard.apiCallsLimitLabel' | sqxTranslate }}: {{callsAllowed | sqxKNumber}} diff --git a/frontend/app/features/dashboard/pages/cards/api-calls-summary-card.component.ts b/frontend/app/features/dashboard/pages/cards/api-calls-summary-card.component.ts index c55d8de80..b7336382e 100644 --- a/frontend/app/features/dashboard/pages/cards/api-calls-summary-card.component.ts +++ b/frontend/app/features/dashboard/pages/cards/api-calls-summary-card.component.ts @@ -24,12 +24,12 @@ export class ApiCallsSummaryCardComponent implements OnChanges { @Input() public usage: CallsUsageDto; - public callsTotal = 0; + public callsMonth = 0; public callsAllowed = 0; public ngOnChanges() { if (this.usage) { - this.callsTotal = this.usage.totalCalls; + this.callsMonth = this.usage.monthCalls; this.callsAllowed = this.usage.allowedCalls; } } diff --git a/frontend/app/features/dashboard/pages/cards/api-traffic-summary-card.component.html b/frontend/app/features/dashboard/pages/cards/api-traffic-summary-card.component.html index 213b90b1d..d1671a0f6 100644 --- a/frontend/app/features/dashboard/pages/cards/api-traffic-summary-card.component.html +++ b/frontend/app/features/dashboard/pages/cards/api-traffic-summary-card.component.html @@ -1,10 +1,10 @@
{{ 'dashboard.trafficHeader' | sqxTranslate }}
-
+
{{ 'dashboard.currentMonthLabel' | sqxTranslate }}
-
{{bytesTotal | sqxFileSize}}
+
{{bytesMonth | sqxFileSize}}
{{ 'dashboard.trafficLimitLabel' | sqxTranslate }}: {{bytesAllowed | sqxFileSize}} diff --git a/frontend/app/features/dashboard/pages/cards/api-traffic-summary-card.component.ts b/frontend/app/features/dashboard/pages/cards/api-traffic-summary-card.component.ts index ace8340d0..e3057759c 100644 --- a/frontend/app/features/dashboard/pages/cards/api-traffic-summary-card.component.ts +++ b/frontend/app/features/dashboard/pages/cards/api-traffic-summary-card.component.ts @@ -25,12 +25,12 @@ export class ApiTrafficSummaryCardComponent implements OnChanges { @Input() public usage: CallsUsageDto; - public bytesTotal = 0; + public bytesMonth = 0; public bytesAllowed = 0; public ngOnChanges() { if (this.usage) { - this.bytesTotal = this.usage.totalBytes; + this.bytesMonth = this.usage.monthBytes; this.bytesAllowed = this.usage.allowedBytes; } } diff --git a/frontend/app/features/schemas/pages/schema/fields/types/string-validation.component.ts b/frontend/app/features/schemas/pages/schema/fields/types/string-validation.component.ts index 1494e7107..0f05f422c 100644 --- a/frontend/app/features/schemas/pages/schema/fields/types/string-validation.component.ts +++ b/frontend/app/features/schemas/pages/schema/fields/types/string-validation.component.ts @@ -7,7 +7,7 @@ import { Component, Input, OnChanges, OnInit } from '@angular/core'; import { FormControl, FormGroup } from '@angular/forms'; -import { fadeAnimation, FieldDto, hasNoValue$, hasValue$, ModalModel, PatternDto, ResourceOwner, RootFieldDto, StringFieldPropertiesDto, Types, value$, STRING_CONTENT_TYPES } from '@app/shared'; +import { fadeAnimation, FieldDto, hasNoValue$, hasValue$, ModalModel, PatternDto, ResourceOwner, RootFieldDto, StringFieldPropertiesDto, STRING_CONTENT_TYPES, Types, value$ } from '@app/shared'; import { Observable } from 'rxjs'; @Component({ diff --git a/frontend/app/shared/services/usages.service.spec.ts b/frontend/app/shared/services/usages.service.spec.ts index dd8decbc7..9025b9455 100644 --- a/frontend/app/shared/services/usages.service.spec.ts +++ b/frontend/app/shared/services/usages.service.spec.ts @@ -46,6 +46,8 @@ describe('UsagesService', () => { blockingCalls: 200, totalBytes: 1024, totalCalls: 40, + monthCalls: 5120, + monthBytes: 256, averageElapsedMs: 12.4, details: { category1: [ @@ -66,7 +68,7 @@ describe('UsagesService', () => { }); expect(usages!).toEqual( - new CallsUsageDto(512, 100, 200, 1024, 40, 12.4, { + new CallsUsageDto(512, 100, 200, 1024, 40, 256, 5120, 12.4, { category1: [ new CallsUsagePerDateDto(DateTime.parseISO('2017-10-12'), 10, 130, 12.3), new CallsUsagePerDateDto(DateTime.parseISO('2017-10-13'), 13, 170, 33.3) diff --git a/frontend/app/shared/services/usages.service.ts b/frontend/app/shared/services/usages.service.ts index d79d944ea..b0e98e4bc 100644 --- a/frontend/app/shared/services/usages.service.ts +++ b/frontend/app/shared/services/usages.service.ts @@ -18,6 +18,8 @@ export class CallsUsageDto { public readonly blockingCalls: number, public readonly totalBytes: number, public readonly totalCalls: number, + public readonly monthBytes: number, + public readonly monthCalls: number, public readonly averageElapsedMs: number, public readonly details: { [category: string]: ReadonlyArray } ) { @@ -102,6 +104,8 @@ export class UsagesService { body.blockingCalls, body.totalBytes, body.totalCalls, + body.monthBytes, + body.monthCalls, body.averageElapsedMs, details);