Former-commit-id: 6982abb8c7aa6088f6c7da1d22ddc1d4cd414614
Former-commit-id: 9adc7cf05677909ed8042e0f0c41ad2c901253a8
1.0
wanggang 4 years ago
parent 0ce13a736c
commit 2768abe4e8

@ -1,26 +1,35 @@
using Infrastructure.Extensions;
using Application.Domain.Entities;
using Infrastructure.Data;
using Infrastructure.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace Platform.Apis
namespace Platform.Api.Api
{
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]/[action]")]
[ApiController]
[Authorize]
public class InfluxController : ControllerBase
{
private readonly IConfiguration _cfg;
private readonly IHttpClientFactory _httpClientFactory;
private readonly IRepository<IoTData> _dataRepo;
public InfluxController(IConfiguration cfg, IHttpClientFactory httpClientFactory)
public InfluxController(IConfiguration cfg, IHttpClientFactory httpClientFactory, IRepository<IoTData> dataRepo)
{
this._cfg = cfg;
this._httpClientFactory = httpClientFactory;
this._dataRepo = dataRepo;
}
[HttpPost]
@ -52,5 +61,63 @@ namespace Platform.Apis
{
return Encoding.UTF8.GetString(Convert.FromBase64String(value));
}
[HttpPost]
public async Task<IActionResult> History([FromBody] QueryDataModel model)
{
try
{
var data = this._dataRepo.ReadOnlyTable()
.Where(o => o.IoTDevice.Number == model.Number && o.Key == model.Key)
.FirstOrDefault();
model.Query = data;
var format = "yyyy-MM-dd 00:00:00";
var where = $" from data where DeviceNumber = '{model.Number}' ";
var timezone = " tz('Asia/Shanghai')";
if (model.Start.HasValue)
{
where += $"and time >= '{model.Start.Value.ToString(format)}' ";
}
if (model.End.HasValue)
{
where += $"and time < '{model.End.Value.ToString(format)}' ";
}
var query = $"select count({model.Key}) {where} {timezone}";
var result = await this.QueryAsync(query);
model.TotalCount = Convert.ToInt32(result["results"][0]["series"][0]["values"][0][1].ToString());
//
query = $"select {model.Key} {where} order by time desc limit {model.PageSize} offset {(model.PageIndex - 1) * model.PageSize} {timezone}";
result = await this.QueryAsync(query);
var list = result["results"][0]["series"][0]["values"]
.Select(o => new IoTData {
Timestamp = ((DateTimeOffset)o[0]).ToUnixTimeMilliseconds(),
Value = o[1].ToString()
})
.ToList();
model.List.AddRange(list);
return Ok(model);
}
catch (Exception ex)
{
ex.PrintStack();
return Problem(ex.Message);
}
}
private async Task<JObject> QueryAsync(string sql, string db = "iot")
{
var connectionString = this._cfg.GetConnectionStringValues("InfluxDB");
var server = connectionString["url"];
var url = $"{server}/query";
var data = new Dictionary<string, string> {
{ "db",db},
{ "q",sql }
};
using var content = new FormUrlEncodedContent(data);
var client = _httpClientFactory.CreateClient();
var result = await client.PostAsync(url, content).Result.Content.ReadAsStringAsync();
return JObject.Parse(result);
}
}
}

@ -0,0 +1,18 @@
using Application.Domain.Entities;
using Infrastructure.Application;
using System;
using System.ComponentModel.DataAnnotations;
namespace Platform.Api.Api
{
public class QueryDataModel : PagedListModel<IoTData>
{
[Required]
public string Number { get; set; }
[Required]
public string Key { get; set; }
public DateTime? Start { get; set; }
public DateTime? End { get; set; }
}
}

@ -159,6 +159,8 @@
</div>
</div>
</div>
<script src="lib/moment.js/moment.min.js"></script>
<script src="lib/moment.js/locale/zh-cn.js"></script>
<script src="~/lib/jquery/jquery.min.js"></script>
<script src="~/lib/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="~/lib/admin-lte/js/adminlte.min.js"></script>

@ -0,0 +1,118 @@
<template>
<views-shared-layout>
<div class="row">
<div class="col-sm-12">
<div class="card">
<div class="card-header">
<div class="form-horizontal query">
<div class="row">
<div class="col-sm-3">
<div class="form-group row">
<label class="col-sm-3 col-form-label">开始:</label>
<div class="col-sm-9">
<a-date-picker :model="moment(model.start, dateFormat)" :format="dateFormat" @change="onChange1" />
</div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group row">
<label class="col-sm-3 col-form-label">结束:</label>
<div class="col-sm-9">
<a-date-picker :model="moment(model.end, dateFormat)" :format="dateFormat" @change="onChange2" />
</div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group row">
<div class="col-sm-12">
<button class="btn btn-primary" v-on:click="query">查询</button>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="card-body">
<table class="table">
<tr>
<th>#</th>
<th>属性名称</th>
<th>属性值</th>
<th>时间</th>
</tr>
<tr v-for="(item,i) in model.list">
<td>{{i+1}}</td>
<td>{{model.query.name}}</td>
<td>{{getValue(item.value)}}</td>
<td>{{toDate(item.timestamp)}}</td>
</tr>
</table>
</div>
<div class="card-footer">
<views-shared-pagination :index.sync="model.pageIndex" :size.sync="model.pageSize" :total="model.totalCount" :callback="load" />
</div>
</div>
</div>
</div>
</views-shared-layout>
</template>
<script>
export default {
data: function () {
return {
title: '历史数据',
url: config.service('platform/api/v1/Influx/History'),
dateFormat:'YYYY/MM/DD',
model: {
number: this.$route.query.number,
key: this.$route.query.key,
pageIndex: parseInt(this.$route.query.pageIndex) || 1,
pageSize: parseInt(this.$route.query.pageSize) || 20,
}
}
},
mounted: function () {
this.subscribe();
this.load();
},
beforeDestroy: function () {
PubSub.unsubscribes(this.events);
},
methods: {
query: function () {
this.model.pageIndex = 1;
this.load();
},
load: function () {
var vm = this;
this.model.list = [];
axios.post(this.url, this.model).then(function (response) {
vm.model = response.data;
});
},
subscribe: function () {
var vm = this;
PubSub.subscribes(this.events, function (method, data) {
parent.load();
});
},
toDate: function (value) {
return moment(value).format('YYYY-MM-DD HH:mm:ss.SSS');
},
getValue(value) {
var output;
if (this.model.query.enumValues) {
return JSON.parse(this.model.query.enumValues)[value] || value;
}
return value;
},
onChange1: function (date, dateString) {
this.model.start = dateString;
},
onChange2: function (date, dateString) {
this.model.end = dateString;
}
}
}
</script>

@ -13,7 +13,9 @@
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h3 class="card-title">{{ getName(data) }}</h3>
<h3 class="card-title">{{ getName(data) }}
<router-link :to="'/components/views/areas/default/data.vue?number='+model.number+'&key='+data.key">历史数据</router-link>
</h3>
<ul role="tablist" class="nav nav-tabs card-tools">
<li class="nav-item"><a href="javascript:;" data-toggle="tab" class="nav-link active" @click="tabClick($event,data.key,'1d',data.name)">1</a></li>
<li class="nav-item"><a href="javascript:;" data-toggle="tab" class="nav-link" @click="tabClick($event,data.key,'7d',data.name)">1</a></li>

@ -58,10 +58,12 @@
},
watch: {
currentIndex: function (val) {
this.$emit('update:index', val);
this.callback();
},
currentSize: function (val) {
this.currentIndex = 1;
this.$emit('update:size', val);
this.callback();
}
},

@ -43,6 +43,8 @@
<p><i class="icon fa fa-ban"></i> 警告! Javascript处于禁用状态</p>
</div>
</noscript>
<script src="lib/moment.js/moment.min.js"></script>
<script src="lib/moment.js/locale/zh-cn.js"></script>
<script src="lib/lodash/lodash.min.js"></script>
<script src="lib/jquery/jquery.min.js"></script>
<script src="lib/bootstrap/js/bootstrap.bundle.min.js"></script>

@ -21,7 +21,6 @@ axios.interceptors.response.use(function (response) {
return response;
}, function (error) {
loading.hide();
console.error(error);
if (error.response) {
if (error.response.status === 401) {
if (store.state.token.refreshToken && error.config.url.indexOf('refreshToken') > -1) {
@ -36,7 +35,7 @@ axios.interceptors.response.use(function (response) {
})
.catch(function (error) {
if (error.response.status === 401) {
store.commit('logout', response.data);
store.commit('logout');
console.log('refreshToken 已过期');
router.push('components/views/areas/default/login.vue');
}
@ -46,7 +45,7 @@ axios.interceptors.response.use(function (response) {
});
}
else {
store.commit('logout', response.data);
store.commit('logout');
router.push('components/views/areas/default/login.vue');
}
}

Loading…
Cancel
Save