電站詳細資訊 - 歷史資料優化

This commit is contained in:
cesar liu 2022-05-14 01:49:37 +08:00
parent 56cee0ae8a
commit 1e2a74900d
9 changed files with 171 additions and 40 deletions

View File

@ -3292,6 +3292,7 @@ namespace SolarPower.Controllers
//檢查資料表是否有該欄位
var check_inv_col = await powerStationRepository.CheckInvTableColumn(powerStation.SiteDB, powerStation.Code);
var check_sensor_avg_col = await powerStationRepository.CheckSensorAvgTableColumn(powerStation.SiteDB, powerStation.Code);
var check_station_col = await powerStationRepository.CheckStationTableColumn(powerStation.SiteDB, powerStation.Code);
if (check_inv_col != 1)
{
@ -3303,6 +3304,11 @@ namespace SolarPower.Controllers
await powerStationRepository.AlertColSensoravgTableColumn(powerStation.SiteDB, powerStation.Code);
}
if (check_station_col == 0)
{
await powerStationRepository. AlertColStationTableColumn(powerStation.SiteDB, powerStation.Code);
}
await powerStationRepository.StationStatus(Id, 1);
await powerStationRepository.StationStatus(Id, 1, powerStation.SiteDB + ".");
apiResult.Msg = "電站啟用成功";

View File

@ -595,6 +595,11 @@ namespace SolarPower.Repository.Implement
}
}
/// <summary>
/// 電站資訊 - history - 上方圖表與總計
/// </summary>
/// <param name="post"></param>
/// <returns></returns>
public async Task<List<HistoryTable>> GethistoryTable(HistorySent post)
{
List<HistoryTable> result;
@ -618,7 +623,7 @@ namespace SolarPower.Repository.Implement
pyrdb = "sensor_history_hour";
timeGroup = "%Y-%m-%d %H:%M:%S";
range = $" WHERE DATE_FORMAT(V.TIMESTAMP,'%Y-%m-%d') = '{post.HistoryRange}'";
range2 = $" AND DATE_FORMAT(C.TIMESTAMP,'%Y-%m-%d') = '{post.HistoryRange}'";
range2 = $" AND DATE_FORMAT(c.TIMESTAMP,'%Y-%m-%d') = '{post.HistoryRange}'";
break;
case 1:
@ -627,7 +632,8 @@ namespace SolarPower.Repository.Implement
usedb = "power_station_history_day";
pyrdb = "sensor_history_day";
timeGroup = "%Y-%m-%d";
range = $" WHERE DATE_FORMAT(V.TIMESTAMP,'%Y-%m') = '{post.HistoryRange}'";
//range = $" WHERE DATE_FORMAT(V.TIMESTAMP,'%Y-%m') = '{post.HistoryRange}'";
range = $" and DATE_FORMAT(c.TIMESTAMP,'%Y-%m') = '{post.HistoryRange}'"; // edit by jiahao @2022-05-13
//range2 = $" DATE_FORMAT(C.TIMESTAMP,'%Y-%m') = '{post.HistoryRange}'";
break;
@ -637,11 +643,19 @@ namespace SolarPower.Repository.Implement
usedb = "power_station_history_month";
pyrdb = "sensor_history_month";
timeGroup = "%Y-%m";
range = $" WHERE DATE_FORMAT(V.TIMESTAMP,'%Y') = '{post.HistoryRange}'";
//range = $" WHERE DATE_FORMAT(V.TIMESTAMP,'%Y') = '{post.HistoryRange}'";
range = $" and DATE_FORMAT(c.TIMESTAMP,'%Y') = '{post.HistoryRange}'"; // edit by jiahao @2022-05-13
//range2 = $" DATE_FORMAT(C.TIMESTAMP,'%Y') = '{post.HistoryRange}'";
break;
case 3:
select = "DATE_FORMAT(V.TIMESTAMP,'%Y') AS TIMESTAMP, MAX(KWH) AS KWH, SUM(SOLARHOUR) AS SOLARHOUR, SUM(SOLARHOUR-PSValue) AS DiffSOLARHOUR, AVG(PR) AS PR, AVG(Irradiance) AS Irradiance, AVG(Temp) AS Temp ,AVG(KWHKWP) AS KWHKWP ";
//select = "DATE_FORMAT(V.TIMESTAMP,'%Y') AS TIMESTAMP, MAX(KWH) AS KWH, SUM(SOLARHOUR) AS SOLARHOUR, SUM(SOLARHOUR-PSValue) AS DiffSOLARHOUR, AVG(PR) AS PR, AVG(Irradiance) AS Irradiance, AVG(Temp) AS Temp ,AVG(KWHKWP) AS KWHKWP ";
//kwh = "C.TOTALKWH";
//usedb = "power_station_history_month";
//pyrdb = "sensor_history_month";
//timeGroup = "%Y-%m";
//range = "";
//out_group = " GROUP BY DATE_FORMAT(V.TIMESTAMP,'%Y')";
select = "DATE_FORMAT(c.TIMESTAMP,'%Y') AS TIMESTAMP, MAX(KWH) AS KWH, SUM(SOLARHOUR) AS SOLARHOUR, 0 AS DiffSOLARHOUR, AVG(PR) AS PR, AVG(Irradiance) AS Irradiance, AVG(Temp) AS Temp ,AVG(KWHKWP) AS KWHKWP ";
kwh = "C.TOTALKWH";
usedb = "power_station_history_month";
pyrdb = "sensor_history_month";
@ -650,36 +664,80 @@ namespace SolarPower.Repository.Implement
out_group = " GROUP BY DATE_FORMAT(V.TIMESTAMP,'%Y')";
break;
}
string sql = string.Empty;
try
{
var sql = @$"SELECT {select}
FROM
(
SELECT B.*, IFNULL(A.SOLARHOUR,0) AS PSValue
FROM
(
SELECT DENSE_RANK() OVER(ORDER BY C.TIMESTAMP) AS ROWID
, (DENSE_RANK() OVER(ORDER BY C.TIMESTAMP)) - 1 AS PreROWID,
C.TIMESTAMP,{kwh} AS KWH,C.SOLARHOUR,C.PR,P.Irradiance,P.Temperature AS Temp
FROM {usedb} C
LEFT JOIN {pyrdb} P ON DATE_FORMAT(P.TIMESTAMP,'{timeGroup}') = DATE_FORMAT(C.TIMESTAMP,'{timeGroup}') AND P.PowerStationId = C.PowerStationId
WHERE C.PowerStationId = {post.PowerstationId}{range2} GROUP BY C.TIMESTAMP ORDER BY C.TIMESTAMP
) A
RIGHT OUTER JOIN
(
SELECT DENSE_RANK() OVER(ORDER BY C.TIMESTAMP) AS ROWID
, (DENSE_RANK() OVER(ORDER BY C.TIMESTAMP)) - 1 AS PreROWID,
C.TIMESTAMP,{kwh} AS KWH,C.SOLARHOUR,C.PR,P.Irradiance,P.Temperature AS Temp ,C.KWHKWP,P.IrrDay
FROM {usedb} C
LEFT JOIN {pyrdb} P ON DATE_FORMAT(P.TIMESTAMP,'{timeGroup}') = DATE_FORMAT(C.TIMESTAMP,'{timeGroup}') AND P.PowerStationId = C.PowerStationId
WHERE C.PowerStationId = {post.PowerstationId}{range2} GROUP BY C.TIMESTAMP ORDER BY C.TIMESTAMP
) B
ON A.ROWID = B.PreROWID
) V {range} {out_group}" ;
switch (post.GroupType)
{
case 0: // today - hour
sql = $@" SELECT C.TIMESTAMP, C.KWH AS KWH,C.SOLARHOUR, C.PR,
P.Irradiance, P.Temperature AS Temp , C.KWHKWP, P.IrrDay, 0 AS DiffSOLARHOUR
FROM power_station_history_hour C
LEFT JOIN sensor_history_hour P ON P.`TIMESTAMP` =C.`TIMESTAMP` AND P.PowerStationId = C.PowerStationId
WHERE C.PowerStationId = {post.PowerstationId}{range2} "; break;
case 1: // day
case 2: // month
sql = $@" SELECT C.TIMESTAMP, {kwh} AS KWH, C.SOLARHOUR, C.PR, P.Irradiance, P.Temperature AS Temp ,C.KWHKWP, P.IrrDay, 0 AS DiffSOLARHOUR
FROM {usedb} C
LEFT JOIN {pyrdb} P ON DATE_FORMAT(P.TIMESTAMP,'{timeGroup}') = DATE_FORMAT(C.TIMESTAMP,'{timeGroup}') AND P.PowerStationId = C.PowerStationId
WHERE C.PowerStationId = {post.PowerstationId}{range}"; break;
case 3: // yeat
//sql = $@"SELECT left(c.`TIMESTAMP`, 4) AS `TIMESTAMP`, round(avg(monthKWH), 3) AS KWH, SUM(SOLARHOUR) AS SOLARHOUR, 0 AS DiffSOLARHOUR, AVG(PR) AS PR, round(AVG(KWHKWP), 3) AS KWHKWP
// FROM power_station_history_month C
// LEFT JOIN sensor_history_month P ON P.`TIMESTAMP` = C.`TIMESTAMP` AND P.PowerStationId = C.PowerStationId
// WHERE C.PowerStationId = C.PowerStationId = {post.PowerstationId}
// GROUP BY left(c.`TIMESTAMP`, 4) ORDER BY C.`TIMESTAMP` ";
sql = $@"
SELECT left(c.`TIMESTAMP`, 4) AS `TIMESTAMP`, sum(monthKWH) AS KWH, SUM(SOLARHOUR) AS SOLARHOUR, 0 AS DiffSOLARHOUR, AVG(PR) AS PR,
AVG(Irradiance) AS Irradiance, AVG(Temperature) AS Temp ,AVG(KWHKWP) AS KWHKWP
FROM power_station_history_month C
LEFT JOIN sensor_history_month P ON P.`TIMESTAMP` = C.`TIMESTAMP` AND P.PowerStationId = C.PowerStationId
WHERE C.PowerStationId = {post.PowerstationId}
GROUP BY left(c.`TIMESTAMP`, 4) ORDER BY C.`TIMESTAMP` "; break;
}
//if (post.GroupType == 0)
//{
//}
//else
//{
// sql = $@" SELECT C.TIMESTAMP, {kwh} AS KWH, C.SOLARHOUR, C.PR, P.Irradiance, P.Temperature AS Temp ,C.KWHKWP, P.IrrDay, 0 AS DiffSOLARHOUR
// FROM {usedb} C
// LEFT JOIN {pyrdb} P ON DATE_FORMAT(P.TIMESTAMP,'{timeGroup}') = DATE_FORMAT(C.TIMESTAMP,'{timeGroup}') AND P.PowerStationId = C.PowerStationId
// WHERE C.PowerStationId = {post.PowerstationId}{range}";
// /*
// sql = @$"SELECT {select}
// FROM
// (
// SELECT B.*, IFNULL(A.SOLARHOUR,0) AS PSValue
// FROM
// (
// SELECT DENSE_RANK() OVER(ORDER BY C.TIMESTAMP) AS ROWID
// , (DENSE_RANK() OVER(ORDER BY C.TIMESTAMP)) - 1 AS PreROWID,
// C.TIMESTAMP,{kwh} AS KWH,C.SOLARHOUR,C.PR,P.Irradiance,P.Temperature AS Temp
// FROM {usedb} C
// LEFT JOIN {pyrdb} P ON DATE_FORMAT(P.TIMESTAMP,'{timeGroup}') = DATE_FORMAT(C.TIMESTAMP,'{timeGroup}') AND P.PowerStationId = C.PowerStationId
// WHERE C.PowerStationId = {post.PowerstationId}{range2} GROUP BY C.TIMESTAMP ORDER BY C.TIMESTAMP
// ) A
// RIGHT OUTER JOIN
// (
// SELECT DENSE_RANK() OVER(ORDER BY C.TIMESTAMP) AS ROWID
// , (DENSE_RANK() OVER(ORDER BY C.TIMESTAMP)) - 1 AS PreROWID,
// C.TIMESTAMP,{kwh} AS KWH,C.SOLARHOUR,C.PR,P.Irradiance,P.Temperature AS Temp ,C.KWHKWP,P.IrrDay
// FROM {usedb} C
// LEFT JOIN {pyrdb} P ON DATE_FORMAT(P.TIMESTAMP,'{timeGroup}') = DATE_FORMAT(C.TIMESTAMP,'{timeGroup}') AND P.PowerStationId = C.PowerStationId
// WHERE C.PowerStationId = {post.PowerstationId}{range2} GROUP BY C.TIMESTAMP ORDER BY C.TIMESTAMP
// ) B
// ON A.ROWID = B.PreROWID
// ) V {range} {out_group}";
// */
//}
result = (await conn.QueryAsync<HistoryTable>(sql)).ToList();
}
catch (Exception exception)
catch (Exception exception)
{
throw exception;
}

View File

@ -4795,6 +4795,13 @@ namespace SolarPower.Repository.Implement
}
}
/// <summary>
/// 電站資訊 - history - 下方表格
/// </summary>
/// <param name="date"></param>
/// <param name="searchType"></param>
/// <param name="entities"></param>
/// <returns></returns>
public async Task<List<PowerStationHistory>> GetPowerStationHistory(string date, byte searchType, List<int> entities)
{
List<PowerStationHistory> result;
@ -5926,6 +5933,38 @@ namespace SolarPower.Repository.Implement
}
}
public async Task<int> CheckStationTableColumn(string dbname, string code)
{
using (IDbConnection conn = this._databaseHelper.GetConnection())
{
conn.Open();
var resule = 0;
try
{
// var sql = $@"SHOW INDEX from '{dbname}'.s{code}01_station where column_name <> 'ID';";
var sql = $@"SELECT COUNT(1) index_count --, TABLE_NAME, GROUP_CONCAT(DISTINCT(index_name) SEPARATOR ',\n ') indexes
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = '{dbname}' and table_name = 's{code}01_station'
AND INDEX_NAME != 'primary'
GROUP BY TABLE_NAME ;";
resule = await conn.QueryFirstOrDefaultAsync<int>(sql);
}
catch (Exception exception)
{
throw exception;
}
finally
{
conn.Close();
}
return resule;
}
}
public async Task AlertColInvTableColumn(string dbname, string code)
{
using (IDbConnection conn = this._databaseHelper.GetConnection())
@ -5996,5 +6035,28 @@ namespace SolarPower.Repository.Implement
}
}
public async Task AlertColStationTableColumn(string dbname, string code)
{
using (IDbConnection conn = this._databaseHelper.GetConnection())
{
conn.Open();
try
{
var sql = $@" CREATE INDEX idx_s{code}01_station_TIMESTAMP ON {dbname}.s{code}01_station (`TIMESTAMP` DESC);";
await conn.ExecuteAsync(sql);
}
catch (Exception exception)
{
throw exception;
}
finally
{
conn.Close();
}
}
}
}
}

View File

@ -610,9 +610,12 @@ namespace SolarPower.Repository.Interface
Task<Check4table> Check4table(string code, string dbname);
Task<int> CheckInvTableColumn(string dbname, string code);
Task<int> CheckSensorAvgTableColumn(string dbname, string code);
Task<int> CheckStationTableColumn(string dbname, string code);
Task AlertColInvTableColumn(string dbname, string code);
Task AlertColSensoravgTableColumn(string dbname, string code);
Task AlertColStationTableColumn(string dbname, string code);
Task<List<PowerStation>> GetAllNoStatusAsync();
}
}

View File

@ -2445,8 +2445,7 @@
var today_format = today.toISOString().slice(0, 10).replace(/-/g, "-");
$('#DateGet').val(today_format);
historyRange = $('#DateGet').val();
}
}
var send_data = {
GroupType: groupType,
@ -2489,6 +2488,7 @@
kwhALL += val.kwh;
//diffSOLARHOURALL += val.diffSOLARHOUR;
irradianceALL += val.irradiance;
irr_day = val.irrDay;
prALL = val.pr;
tempALL += val.temp;
@ -2555,11 +2555,7 @@
listsolarhour.push(parseFloat(val.solarhour.toFixed(2)));
listpr.push(parseFloat(val.pr.toFixed(2)));
listkwhkwp.push(parseFloat(val.kwhkwp.toFixed(2)));
}
}
}
})
@ -2802,7 +2798,8 @@
'<td>' + TimestampALL + '</td>' +
'<td>' + kwhALL.toFixed(2) + '</td>' +
'<td>' + kwhkwp + '</td>' +
'<td>' + irradianceALL.toFixed(2) + '</td>' +
//'<td>' + irradianceALL.toFixed(2) + '</td>' + // 改成累積日照 by jiahao 2022-04-30 00:444
'<td>' + irr_day.toFixed(2) + '</td>' +
'<td>' + prALL.toFixed(2) + '</td>' +
'<td>' + tempALL.toFixed(2) + '</td>' +
'</tr>');

View File

@ -60,7 +60,7 @@
<th>時間</th>
<th>發電量(kWh)</th>
<th>有效日照時數</th>
<th>日照度(kWh/m2)</th>
<th>累積日照量(W/m2)</th>
<th>PR(%)</th>
<th>溫度(℃)</th>
</tr>

View File

@ -14,6 +14,10 @@ namespace solarApp.Model
public double PR { get; set; }
public double SOLARHOUR { get; set; }
public double kwhkwp { get; set; }
/// <summary>
/// 寫入 DB 時間
/// </summary>
public string insertTime { get; set; }
}
public class raw_station_day

View File

@ -28,7 +28,7 @@ namespace solarApp.Service
{
conn.Open();
string sql = @"select id , FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') reportdate, siteid, round(KWH, 3) KWH, round(TODAYKWH, 3) TODAYKWH,
round(TOTALKWH, 3)TOTALKWH, round(PR, 3) PR, round(SOLARHOUR, 3) SOLARHOUR , round(kwhkwp, 3) kwhkwp
round(TOTALKWH, 3)TOTALKWH, round(PR, 3) PR, round(SOLARHOUR, 3) SOLARHOUR , round(kwhkwp, 3) kwhkwp , insertTime
from " + siteDB + ".s" + siteID + @"_station
where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @reportDate";
try

View File

@ -189,6 +189,7 @@ namespace solarApp
string date1 = dtselect_station1.Value.AddDays(-7).ToString("yyyy-MM-dd");
gv_fic_station_raw.DataSource = "";
gv_fic_station_day.DataSource = "";
//gv_fic_station_raw.column
try
{
gv_fic_station_raw.DataSource = stationSvc.get_station_raw(date2, lbSiteDB_inv.Text, lbSiteID_inv.Text);