Sleep and Steps

- added sleep
- added steps
- bertter DB filling
This commit is contained in:
2018-09-23 18:39:59 +02:00
parent c807c474c4
commit a1019dd62e
17 changed files with 386 additions and 69 deletions

4
.gitignore vendored
View File

@@ -258,4 +258,6 @@ paket-files/
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
*.pyc
SeniorAssistant/SeniorAssistant/wwwroot/*

View File

@@ -2,6 +2,7 @@
namespace SeniorAssistant.Controllers
{
[ApiExplorerSettings(IgnoreApi = true)]
public class HomeController : Controller
{
[Route("")]
@@ -17,5 +18,23 @@ namespace SeniorAssistant.Controllers
{
return View();
}
[Route("Sleep")]
public IActionResult Sleep()
{
return View();
}
[Route("Step")]
public IActionResult Step()
{
return View();
}
[Route("{User}")]
public IActionResult SingleUser(string user)
{
return View("user", user);
}
}
}

View File

@@ -0,0 +1,21 @@
using Microsoft.AspNetCore.Mvc;
using SeniorAssistant.Models;
namespace SeniorAssistant.Controllers.Services
{
[Route("api/[controller]")]
public class HeartbeatController : CrudTimeController<Heartbeat>
{ }
[Route("api/[controller]")]
public class SleepController : CrudTimeController<Sleep>
{ }
[Route("api/[controller]")]
public class StepController : CrudTimeController<Step>
{ }
[Route("api/[controller]")]
public class UserController : CrudController<User>
{ }
}

View File

@@ -11,6 +11,8 @@ namespace SeniorAssistant.Controllers.Services
public class CrudTimeController<TEntity> : BaseController
where TEntity : class, IHasTime
{
static readonly object Empty = new { };
[HttpGet]
public async Task<IEnumerable<TEntity>> Read() => await Db.GetTable<TEntity>().ToListAsync();
@@ -18,31 +20,54 @@ namespace SeniorAssistant.Controllers.Services
public async Task<IEnumerable<TEntity>> Read(string username) => await Db.GetTable<TEntity>().Where(e => e.Username.Equals(username)).ToListAsync();
[HttpGet("{username}/{date:regex((today|\\d{{4}}-\\d{{2}}-\\d{{2}}))}/{hour:range(0, 23)?}")]
public async Task<IEnumerable<TEntity>> Read(string username, string date, int hour = -1)
{
DateTime time = (date.Equals("today") ? DateTime.Now : DateTime.ParseExact(date, "yyyy-MM-dd", null) );
public async Task<IEnumerable<TEntity>> Read(string username, string date, int hour = -1) => await Read(username, date, date, hour);
return await Db.GetTable<TEntity>().Where(e =>
e.Username.Equals(username) &&
(time.Year == 0 || e.Time.Year == time.Year) &&
(time.Month == 0 || e.Time.Month == time.Month) &&
(time.Day == 0 || e.Time.Day == time.Day) &&
(hour < 0 || e.Time.Hour == hour)
).ToListAsync();
[HttpGet("{username}/{from:regex((today|\\d{{4}}-\\d{{2}}-\\d{{2}}))}/{to:regex((today|\\d{{4}}-\\d{{2}}-\\d{{2}}))}/{hour:range(0, 23)?}")]
public async Task<IEnumerable<TEntity>> Read(string username, string from, string to, int hour = -1)
{
try
{
DateTime dateFrom = (from.Equals("today") ? DateTime.Now : DateTime.ParseExact(from, "yyyy-MM-dd", null));
DateTime dateTo = (to.Equals("today") ? DateTime.Now : DateTime.ParseExact(to, "yyyy-MM-dd", null));
return await Db.GetTable<TEntity>().Where(e => e.Username.Equals(username) && dateFrom.Date<=e.Time.Date && dateTo.Date>=e.Time.Date && (hour < 0 || e.Time.Hour == hour)).ToListAsync();
}
catch
{
return new List<TEntity>();
}
}
[HttpGet("{username}/last/{hour:min(1)}")]
public async Task<IEnumerable<TEntity>> Read(string username, int hour)
{
DateTime date = DateTime.Now.AddHours(-hour);
return await Db.GetTable<TEntity>().Where(e => e.Username.Equals(username) && date <= e.Time).ToListAsync();
}
[NonAction]
public async Task<TEntity> Read(string username, DateTime date) => await Db.GetTable<TEntity>().FirstOrDefaultAsync(e => e.Username.Equals(username) && date == e.Time);
[HttpPost]
public async Task Create([FromBody]TEntity item) => await Db.InsertAsync(item);
[HttpPut]
public async Task<object> Update([FromBody]TEntity item)
{
var e = await Read(item.Username, item.Time);
if (e == null)
{
await Create(item);
}
else
{
await Db.UpdateAsync(item);
}
return Empty;
}
/*
[HttpPost("{username}")]
public async Task Create([FromBody]TEntity item) => await Db.InsertAsync(item);
[HttpPut("{username}/{date}/{time}")]
public async Task Update(string username, [FromBody]TEntity item)
{
item.Username = username;
await Db.UpdateAsync(item);
}
[HttpDelete("{username}")]
public async Task Delete(string username) => await Db.GetTable<TEntity>().Where(c => c.Username.Equals(username)).DeleteAsync();
*/

View File

@@ -1,9 +0,0 @@
using Microsoft.AspNetCore.Mvc;
using SeniorAssistant.Models;
namespace SeniorAssistant.Controllers.Services
{
[Route("api/[controller]")]
public class HeartbeatController : CrudTimeController<Heartbeat>
{ }
}

View File

@@ -1,9 +0,0 @@
using Microsoft.AspNetCore.Mvc;
using SeniorAssistant.Models;
namespace SeniorAssistant.Controllers.Services
{
[Route("api/[controller]")]
public class UserController : CrudController<User>
{ }
}

View File

@@ -0,0 +1,21 @@
using LinqToDB.Mapping;
using System;
namespace SeniorAssistant.Models
{
public class Sleep : IHasTime
{
[PrimaryKey]
[NotNull]
public string Username { get; set; }
[PrimaryKey]
[NotNull]
public DateTime Time { get; set; }
[Association(ThisKey = nameof(Username), OtherKey = nameof(User.Username), CanBeNull = false)]
public User user { get; set; }
public long Value { get; set; }
}
}

View File

@@ -0,0 +1,21 @@
using LinqToDB.Mapping;
using System;
namespace SeniorAssistant.Models
{
public class Step : IHasTime
{
[PrimaryKey]
[NotNull]
public string Username { get; set; }
[PrimaryKey]
[NotNull]
public DateTime Time { get; set; }
[Association(ThisKey = nameof(Username), OtherKey = nameof(User.Username), CanBeNull = false)]
public User user { get; set; }
public long Value { get; set; }
}
}

View File

@@ -2,9 +2,13 @@
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iis": {
"applicationUrl": "http://localhost/SeniorAssistant",
"sslPort": 0
},
"iisExpress": {
"applicationUrl": "http://localhost:50361",
"sslPort": 44348
"sslPort": 44387
}
},
"profiles": {
@@ -27,6 +31,9 @@
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}"
},
"Snapshot Debugger (servizio app di Azure)": {
"commandName": "Snapshot Debugger (servizio app di Azure)"
}
}
}

View File

@@ -17,6 +17,7 @@
<PackageReference Include="Microsoft.Data.Sqlite" Version="2.1.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.0.1985401" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="3.0.0" />
</ItemGroup>
</Project>

View File

@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using LinqToDB;
using LinqToDB.DataProvider.SQLite;
using Microsoft.AspNetCore.Builder;
@@ -11,8 +10,9 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SeniorAssistant.Configuration;
using SeniorAssistant.Data;
using SeniorAssistant.Extensions;
using SeniorAssistant.Models;
using SeniorAssistant.Extensions;
using Swashbuckle.AspNetCore.Swagger;
namespace SeniorAssistant
{
@@ -31,6 +31,23 @@ namespace SeniorAssistant
{
services.AddMvc();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info
{
Version = "v1",
Title = "Senior REST APIs",
Description = "REST APIs for old people",
TermsOfService = "None"
});
// Se decommento ottengo un'eccezione quando ho un controller (es. CategoriesController) che estende un altro controller.
// Set the comments path for the Swagger JSON and UI.
//var basePath = AppContext.BaseDirectory;
//var xmlPath = Path.Combine(basePath, "REST.xml");
//c.IncludeXmlComments(xmlPath);
});
services.Configure<Kendo>(Configuration.GetSection("kendo"));
services.Configure<Theme>(Configuration.GetSection("theme"));
@@ -43,8 +60,8 @@ namespace SeniorAssistant
{
new MenuItem("User", "/"),
new MenuItem("Heartbeat", "/heartbeat"),
new MenuItem("Sleep"),
new MenuItem("Step")
new MenuItem("Sleep", "/sleep"),
new MenuItem("Step", "/step")
}
},
});
@@ -68,6 +85,15 @@ namespace SeniorAssistant
app.UseStaticFiles();
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/api/v1.json", "V1");
});
/*
Shortcut per:
@@ -88,38 +114,65 @@ namespace SeniorAssistant
{
using (var db = dataContext.Create())
{
const string baseUsername = "vecchio";
string[] users = { "Mario", "Giovanni", "Aldo", "Giacomo", "Marcello", "Filippo" };
db.CreateTableIfNotExists<Heartbeat>();
db.CreateTableIfNotExists<Sleep>();
db.CreateTableIfNotExists<Step>();
try
{
db.CreateTable<User>();
db.CreateTable<Heartbeat>();
const string baseUsername = "vecchio";
string[] users = { "Mario", "Giovanni", "Aldo", "Giacomo", "Marcello", "Filippo" };
int num = 0;
int count = 0;
foreach (string user in users)
{
db.InsertOrReplace(new User { Name = user, Username = baseUsername+(num==0?"":""+num) });
num++;
}
Random rnd = new Random();
for (int i=0; i<50; i++)
{
int random = rnd.Next(num);
string user = baseUsername + (random==0? "":""+random);
DateTime time = DateTime.Now.AddHours(rnd.Next(-24, +24));
int beat = rnd.Next(50, 90);
Heartbeat heart = new Heartbeat { Username = user, Time = time, Value = beat };
db.Insert(heart);
db.InsertOrReplace(new User { Name = user, Username = baseUsername + (count == 0 ? "" : "" + count) });
count++;
}
}
catch (SqliteException)
catch
{ }
Random rnd = new Random();
DateTime now = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
now = now.AddHours(DateTime.Now.Hour).AddMinutes(30);
double totalHours = 50;
try
{
// Do nothing
DateTime time = db.GetTable<Heartbeat>().MaxAsync(x => x.Time).Result;
TimeSpan span = now.Subtract(time);
totalHours = span.TotalHours;
}
catch { }
for (int i = 0; i<totalHours; i++)
{
DateTime time = now.AddHours(-i);
for (int num = 0; num < users.Length; num++)
{
string user = baseUsername + num;
if (time.Day != now.Day)
{
Sleep sleep = new Sleep() { Username = user, Time = time, Value = rnd.Next(6 * 3600000, 9 * 3600000) };
db.Insert(sleep);
}
if (rnd.Next(5) < 4)
{
Heartbeat heart = new Heartbeat() { Username = user, Time = time, Value = rnd.Next(50, 120) };
Step step = new Step() { Username = user, Time = time, Value = rnd.Next(100, 500) };
db.Insert(heart);
db.Insert(step);
}
}
if (time.Day != now.Day)
{
now = now.AddDays(-1);
}
}
}
}
}

View File

@@ -1,5 +1,4 @@
@model IEnumerable<Heartbeat>
@{
@{
ViewBag.Title = "Hello Razor";
}

View File

@@ -0,0 +1,42 @@
@{
ViewBag.Title = "Hello Razor";
}
<div id="grid"></div>
<script>
$(document).ready(function () {
var baseUrl = "@Url.Content("~/api/sleep/")";
$("#grid").kendoGrid({
dataSource: {
transport: {
read: { url: baseUrl, type: "GET" }
},
serverPaging: false,
serverSorting: false,
batch: false,
schema: {
model: {
id: "username",
fields: {
username: { type: "string", },
time: { type: "date" },
value: { type: "number" }
}
}
}
},
scrollable: true,
sortable: true,
filterable: true,
editable: false,
columns: [
{ field: "username", title: "Username" },
{ field: "time", title: "Date/Time", format: "{0:dd/MM/yyyy HH:mm}" },
{ field: "value", title: "Sleep Time", format: "{n2}", template: '${value/3600000}' }
]
});
})
</script>

View File

@@ -0,0 +1,42 @@
@{
ViewBag.Title = "Hello Razor";
}
<div id="grid"></div>
<script>
$(document).ready(function () {
var baseUrl = "@Url.Content("~/api/step/")";
$("#grid").kendoGrid({
dataSource: {
transport: {
read: { url: baseUrl, type: "GET" }
},
serverPaging: false,
serverSorting: false,
batch: false,
schema: {
model: {
id: "username",
fields: {
username: { type: "string", },
time: { type: "date" },
value: { type: "number" }
}
}
}
},
scrollable: true,
sortable: true,
filterable: true,
editable: false,
columns: [
{ field: "username", title: "Username" },
{ field: "time", title: "Date/Time", format: "{0:dd/MM/yyyy HH:mm}" },
{ field: "value", title: "Steps done" }
]
});
})
</script>

View File

@@ -0,0 +1,82 @@
@model string
@{
ViewBag.Title = "Hello Razor";
}
<div id="chart"></div>
<script>
var base_url = "@Url.Content("~/api/")";
$("#chart").kendoChart({
title: { text: "Visualizzazione attivita' di @Model" },
legend: { position: "bottom" },
dataSource: {
transport: {
read: {
url: base_url+"heartbeat/@Model/last/48",
type: "GET",
dataType: "json"
}
},
sort: {
field: "time",
dir: "asc"
}
},
seriesDefaults: {
type: "line",
style: "smooth"
},
series: [{
name: "Battito",
field: "value",
categoryField: "time",
format: "0:{dd HH:mm}"
}],
/*series: [{
type: "column",
field: "value",
categoryField: "time"
}, {
name: "Steps",
dataSource: {
transport: {
read: {
url: "/api/steps/",
dataType: "json"
}
},
sort: {
field: "time",
dir: "asc"
}
}
}],
/*
valueAxis: {
labels: {
format: "{0}%"
},
line: {
visible: false
},
axisCrossingValue: -10
},
categoryAxis: {
categories: [2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011],
majorGridLines: {
visible: false
},
labels: {
rotation: "auto"
}
},
tooltip: {
visible: true,
format: "{0}%",
template: "#= series.name #: #= value #"
}*/
});
</script>

Binary file not shown.