Skip to content

Commit

Permalink
🐛 fix(AppVisit): correct data usage and optimize code (#482)
Browse files Browse the repository at this point in the history
  • Loading branch information
capdiem authored Jan 15, 2025
1 parent 4c6d5b5 commit 1b6f86f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
@GenAppVisitCard(UvTitle, _uva)
</MCol>
<MCol Cols="12" Sm="6" Md="6" Lg="3" Xl="12">
@GenAppVisitCard(UvTitle, _uvta)
@GenAppVisitCard(UvtTitle, _uvta)
</MCol>
<MCol Cols="12" Sm="6" Md="6" Lg="3" Xl="12">
@GenAppVisitCard(PvTitle, _pva)
Expand Down Expand Up @@ -133,7 +133,7 @@
</span>
<span class="@color">
<MIcon Style="color: inherit;" Small>@icon</MIcon>
<span>@(rate.ToString("P"))%</span>
<span>@(rate.ToString("P", ZhCN))</span>
</span>
</div>
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the Apache License. See LICENSE.txt in the project root for license information.

using System.Globalization;
using System.Timers;
using GraphQL.Client.Http;
using GraphQL.Client.Serializer.SystemTextJson;
Expand All @@ -20,6 +21,8 @@ public partial class AppVisitAnalysis : TscComponentBase
private const string Top10UvtTitle = UvtTitle + " Top10";
private const string Top10PvTitle = PvTitle + " Top10";

private static readonly CultureInfo ZhCN = new("zh-CN");

private GraphQLHttpClient _graphClient = null!;
private AppVisit? _uva;
private AppVisit? _uvta;
Expand Down Expand Up @@ -137,20 +140,51 @@ private async Task RefreshAppVisitHourAsync()
{
var response = await _graphClient.SendQueryAsync<CubeData<AppVisitHourItem>>(GetAppVisitHourQuery());

var x = response.Data.Items.Select(u => u.AppVisitHour.TimeKey).ToArray();
var tuv = response.Data.Items.Select(u => u.AppVisitHour.Tuv).ToArray();
var yuv = response.Data.Items.Select(u => u.AppVisitHour.Yuv).ToArray();
var suv = response.Data.Items.Select(u => u.AppVisitHour.Suv).ToArray();
var tuvt = response.Data.Items.Select(u => u.AppVisitHour.Tuvt).ToArray();
var yuvt = response.Data.Items.Select(u => u.AppVisitHour.Yuvt).ToArray();
var suvt = response.Data.Items.Select(u => u.AppVisitHour.Suvt).ToArray();
var tpv = response.Data.Items.Select(u => u.AppVisitHour.Tpv).ToArray();
var ypv = response.Data.Items.Select(u => u.AppVisitHour.Ypv).ToArray();
var spv = response.Data.Items.Select(u => u.AppVisitHour.Spv).ToArray();
var currentHour = DateTime.UtcNow.Add(CurrentTimeZone.BaseUtcOffset).Hour;

var x = new List<string>();
var tuv = new List<int>();
var yuv = new List<int>();
var suv = new List<int>();
var tuvt = new List<int>();
var yuvt = new List<int>();
var suvt = new List<int>();
var tpv = new List<int>();
var ypv = new List<int>();
var spv = new List<int>();

foreach (var item in response.Data.Items)
{
x.Add(item.AppVisitHour.TimeKey);
if (item.AppVisitHour.TimeNum <= currentHour)
{
tuv.Add(item.AppVisitHour.Tuv);
}

yuv.Add(item.AppVisitHour.Yuv);
suv.Add(item.AppVisitHour.Suv);

if (item.AppVisitHour.TimeNum <= currentHour)
{
tuvt.Add(item.AppVisitHour.Tuvt);
}

yuvt.Add(item.AppVisitHour.Yuvt);
suvt.Add(item.AppVisitHour.Suvt);

if (item.AppVisitHour.TimeNum <= currentHour)
{
tpv.Add(item.AppVisitHour.Tpv);
}

ypv.Add(item.AppVisitHour.Ypv);
spv.Add(item.AppVisitHour.Spv);
}

_uvOption = GetSharedHourOption(UvTitle, x, tuv, yuv, suv);
_uvtOption = GetSharedHourOption(UvtTitle, x, tuvt, yuvt, suvt);
_pvOption = GetSharedHourOption(PvTitle, x, tpv, ypv, spv);

StateHasChanged();
}

Expand Down Expand Up @@ -178,7 +212,6 @@ private async Task<object> RefreshAppVisitPageAsync(AppVisitType type)
return GetSharedAppVisitPageOption(keys, data, max);
}


private async Task RefreshAppVisitDataAsync()
{
_uva = await GetAppVisitAsync(AppVisitType.Uv);
Expand Down Expand Up @@ -239,7 +272,7 @@ private void UpdateMapOption(object data, int max)
StateHasChanged();
}

private static object GetSharedHourOption(string title, string[] x, int[] t, int[] y, int[] s)
private static object GetSharedHourOption(string title, List<string> x, List<int> t, List<int> y, List<int> s)
{
return new
{
Expand Down Expand Up @@ -340,13 +373,19 @@ private static object GetSharedAppVisitPageOption(string[] keys, object[] data,
{
type = "category",
data = keys,
axisLabel = new
{
overflow = "break",
width = 250
}
},
series = new[]
{
new
{
name = "数量",
type = "bar",
barMinWidth = "50%",
data = data,
label = new
{
Expand All @@ -369,8 +408,8 @@ private static GraphQLHttpRequest GetAppVisitQuery(AppVisitType type)
""",
AppVisitType.Uvt => """
data1: uvta
data2: yesterdayuvrate
data3: seventhuvrate
data2: yesterdayuvtrate
data3: seventhuvtrate
""",
AppVisitType.Pv => """
data1: pva
Expand Down Expand Up @@ -473,7 +512,7 @@ private GraphQLHttpRequest GetAppVisitPageQuery(AppVisitType type)
filter = $"path:{{notIn:[{string.Join(",", _urls.Select(x => $"\"{x}\""))}]}}";
}

var r = new GraphQLHttpRequest(
return new GraphQLHttpRequest(
$$"""
query {
cube(limit:10) {
Expand All @@ -485,10 +524,6 @@ private GraphQLHttpRequest GetAppVisitPageQuery(AppVisitType type)
}
}
""");

Console.Out.WriteLine("query: " + r.Query);

return r;
}

// TODO: Test this method, like value 0 or there has more effective way to implement this method
Expand Down

0 comments on commit 1b6f86f

Please sign in to comment.