Skip to content

Commit

Permalink
feat: support daily cost
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 committed Jan 23, 2024
1 parent fcc8c73 commit 459984e
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 148 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 4.2.6 on 2024-01-23 02:45

from decimal import Decimal

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("proxy", "0025_alter_occupancyconfig_color"),
]

operations = [
migrations.AlterModelOptions(
name="userproxynodeoccupancy",
options={"verbose_name": "用户占用记录", "verbose_name_plural": "用户占用记录"},
),
migrations.AddField(
model_name="relaynode",
name="enlarge_scale",
field=models.DecimalField(
decimal_places=1,
default=Decimal("1.0"),
max_digits=10,
verbose_name="倍率",
),
),
]
17 changes: 11 additions & 6 deletions apps/proxy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,21 @@ class BaseNodeModel(BaseModel):
cost_price = models.DecimalField(
max_digits=10, decimal_places=2, verbose_name="成本", default=0
)
enlarge_scale = models.DecimalField(
"倍率",
default=Decimal("1.0"),
decimal_places=1,
max_digits=10,
)

class Meta:
abstract = True

@classmethod
def calc_all_cost_price(cls):
aggs = cls.objects.all().aggregate(cost_price=models.Sum("cost_price"))
return aggs["cost_price"] or 0


class ProxyNode(BaseNodeModel, SequenceMixin):
NODE_TYPE_SS = "ss"
Expand Down Expand Up @@ -194,12 +205,6 @@ class ProxyNode(BaseNodeModel, SequenceMixin):
)
used_traffic = models.BigIntegerField("已用流量(单位字节)", default=0)
total_traffic = models.BigIntegerField("总流量(单位字节)", default=settings.GB)
enlarge_scale = models.DecimalField(
"倍率",
default=Decimal("1.0"),
decimal_places=1,
max_digits=10,
)
enable_direct = models.BooleanField("允许直连", default=True)
enable_udp = models.BooleanField("是否开启UDP 转发", default=True)
xray_grpc_port = models.IntegerField("xray grpc port", default=23456)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 4.2.6 on 2024-01-23 02:45

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("sspanel", "0021_alter_ticketmessage_options_and_more"),
]

operations = [
migrations.AlterField(
model_name="moneycode",
name="number",
field=models.DecimalField(
blank=True,
decimal_places=2,
default=10,
max_digits=10,
null=True,
verbose_name="金额",
),
),
migrations.AlterField(
model_name="moneycode",
name="user",
field=models.CharField(
blank=True, max_length=128, null=True, verbose_name="使用人"
),
),
]
8 changes: 0 additions & 8 deletions apps/sspanel/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,14 +756,6 @@ class Meta:
verbose_name_plural = "充值码"
ordering = ("isused",)

def clean(self):
# 保证充值码不会重复
code_length = len(self.code or "")
if 0 < code_length < 12:
self.code = "{}{}".format(self.code, get_long_random_string())
else:
self.code = get_long_random_string()

def __str__(self):
return self.code

Expand Down
129 changes: 77 additions & 52 deletions apps/sspanel/static/sspanel/js/sspanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,11 @@ var genLineChart = function (id, config_data) {
{
label: config_data.title,
data: config_data.data,
fill: false,
tension: 0.2,
backgroundColor: getRandomColor(),
borderColor: getRandomColor,
borderWidth: 1,
borderColor: getRandomColor(),
borderWidth: 2,
},
],
},
Expand All @@ -166,40 +168,20 @@ var genLineChart = function (id, config_data) {
mode: "nearest",
intersect: true,
},
title: {
display: true,
text: config_data.title,
},
elements: {
point: {
radius: 2,
},
},
responsive: true,
maintainAspectRatio: false,
legend: {
display: true,
},
scales: {
xAxes: [
{
display: true,
scaleLabel: {
display: true,
labelString: config_data.x_label,
plugins: {
tooltip: {
// only show value
callbacks: {
label: function (context) {
if (context.parsed.y == 0) {
return " 0";
}
return " " + context.parsed.y;
},
},
],
yAxes: [
{
display: true,
ticks: { beginAtZero: true },
scaleLabel: {
display: true,
labelString: config_data.y_label,
},
},
],
},
},
},
});
Expand All @@ -222,7 +204,9 @@ var genBarChart = function (id, config_data) {
data: config_data.data,
backgroundColor: getRandomColor(),
borderColor: getRandomColor,
borderWidth: 1,
borderWidth: 2,
tension: 0.2,
fill: false,
},
],
},
Expand All @@ -242,29 +226,70 @@ var genBarChart = function (id, config_data) {
},
responsive: true,
maintainAspectRatio: false,
legend: {
display: true,
},
scales: {
xAxes: [
{
display: true,
scaleLabel: {
display: true,
labelString: config_data.x_label,
plugins: {
tooltip: {
// only show value
callbacks: {
label: function (context) {
if (context.parsed.y == 0) {
return " 0";
}
return " " + context.parsed.y;
},
},
],
yAxes: [
{
display: true,
ticks: { beginAtZero: true },
scaleLabel: {
display: true,
labelString: config_data.y_label,
},
},
},
});
return myChart;
};

var genLineChartWithDataList = function (id, config_data_list) {
let chartStatus = Chart.getChart(id);
if (chartStatus != undefined) {
chartStatus.destroy();
}

var datasets = [];
for (let i = 0; i < config_data_list.length; i++) {
var config_data = config_data_list[i];
datasets.push({
label: config_data.title,
data: config_data.data,
fill: false,
tension: 0.2,
backgroundColor: getRandomColor(),
borderColor: getRandomColor(),
borderWidth: 2,
});
}

var ctx = document.getElementById(id);
var myChart = new Chart(ctx, {
type: "line",
data: {
labels: config_data.labels,
datasets: datasets,
},
options: {
hover: {
mode: "nearest",
intersect: true,
},
responsive: true,
maintainAspectRatio: false,
plugins: {
tooltip: {
// only show value
callbacks: {
label: function (context) {
if (context.parsed.y == 0) {
return " 0";
}
return " " + context.parsed.y;
},
},
],
},
},
},
});
Expand Down
1 change: 1 addition & 0 deletions apps/stats/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class DailyStatsAdmin(admin.ModelAdmin):
"checkin_user_count",
"order_count",
"order_amount",
"cost_amount",
"total_used_traffic",
]
list_per_page = 10
Expand Down
24 changes: 24 additions & 0 deletions apps/stats/migrations/0003_dailystats_cost_amount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 4.2.6 on 2024-01-23 02:45

from decimal import Decimal

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("stats", "0002_alter_dailystats_id"),
]

operations = [
migrations.AddField(
model_name="dailystats",
name="cost_amount",
field=models.DecimalField(
decimal_places=2,
default=Decimal("0"),
max_digits=10,
verbose_name="订单总成本",
),
),
]
9 changes: 9 additions & 0 deletions apps/stats/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ class DailyStats(models.Model):
max_digits=10,
default=decimal.Decimal(0),
)
cost_amount = models.DecimalField(
verbose_name="订单总成本",
decimal_places=2,
max_digits=10,
default=decimal.Decimal(0),
)

total_used_traffic = models.BigIntegerField(verbose_name="总流量GB", default=0)

Expand Down Expand Up @@ -52,6 +58,9 @@ def create_or_update_stats(cls, dt: pendulum.DateTime):
log.order_amount = decimal.Decimal(sm.UserOrder.get_success_order_amount(dt))

log.total_used_traffic = pm.UserTrafficLog.calc_traffic_by_datetime(dt)
log.cost_amount = (
pm.ProxyNode.calc_all_cost_price() + pm.RelayNode.calc_all_cost_price()
) / 30
log.save()
return log

Expand Down
Loading

0 comments on commit 459984e

Please sign in to comment.