chart added

This commit is contained in:
Bukovenszki Tamás 2017-11-01 22:25:45 +01:00
parent 363ac5898a
commit 54dba1b009
6 changed files with 362 additions and 27 deletions

View File

@ -7,6 +7,8 @@ using System.Net;
using System.Web;
using System.Web.Mvc;
using ResoursesManager.Models;
using ResoursesManager.ViewModels;
using System.Data.Entity.Core.Objects;
namespace ResoursesManager.Controllers
{
@ -15,9 +17,40 @@ namespace ResoursesManager.Controllers
private ApplicationDbContext db = new ApplicationDbContext();
// GET: Statistics
public ActionResult Index()
public ActionResult Index(StatisticType? byTypeType, int? byResourceId, SubscribeType? subscribeType, int? subscribeId)
{
return View(db.Statistic.ToList());
if (byTypeType == null)
{
byTypeType = StatisticType.Reserv;
}
if (byResourceId == null)
{
byResourceId = db.Resources.FirstOrDefault().Id;
}
if (subscribeType == null)
{
subscribeType = SubscribeType.ByUser;
}
if (subscribeId == null)
{
subscribeId = db.Resources.FirstOrDefault().Id;
}
var model = new StatisticViewModel()
{
Log = db.Statistic.ToList()
};
model.ByType = db.Statistic.Where(k => k.StatisticType == byTypeType /*&& k.DateTime > DateTime.Today.AddDays(-30.0)*/).GroupBy(l => DbFunctions.TruncateTime(l.DateTime)).ToDictionary(j => (DateTime)j.Key, j => j.Count());
model.ByResource = db.Statistic.Where(k => k.ResourceId == byResourceId /*&& k.DateTime > DateTime.Today.AddDays(-30.0)*/).GroupBy(l => l.ResourceId).ToDictionary(j => j.Key, j => j.Count());
model.ByTypeCircle = db.Statistic.GroupBy(l => l.StatisticType).ToDictionary(j => j.Key, j => j.Count());
var subscibedUser = db.Resources.FirstOrDefault(k => k.Id == subscribeId).Users.Count;
var allUsers = db.Users.Count();
model.Subscribe = new Dictionary<string, int>();
model.Subscribe.Add("Subscribed user", subscibedUser);
model.Subscribe.Add("Unsubscibed user", allUsers - subscibedUser);
return View(model);
}
protected override void Dispose(bool disposing)

View File

@ -6,6 +6,7 @@ using Microsoft.AspNet.Identity.EntityFramework;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.ModelConfiguration.Conventions;
using System;
namespace ResoursesManager.Models
{
@ -66,8 +67,37 @@ namespace ResoursesManager.Models
{
protected override void Seed(ApplicationDbContext context)
{
List<Resource> defaults = new List<Resource>();
string userRole = "User";
string userName = "user{0}@email.hu";
//Role létrehozása
var roleStore = new RoleStore<IdentityRole>(context);
var roleManager = new RoleManager<IdentityRole>(roleStore);
var role = new IdentityRole
{
Name = userRole
};
roleManager.Create(role);
var store = new UserStore<ApplicationUser>(context);
var manager = new UserManager<ApplicationUser>(store);
List<ApplicationUser> users = new List<ApplicationUser>();
for (int i = 0; i < 10; i++)
{
//User létrehozása
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
var user = new ApplicationUser
{
UserName = string.Format(userName, i),
Email = string.Format(userName, i)
};
users.Add(user);
userManager.Create(user, "P@ssword1");
userManager.AddToRole(user.Id, userRole);
}
List<Resource> defaults = new List<Resource>();
defaults.Add(new Resource() { Name = "Kihangosító", AssetTag = "ASDF654", Description = "A kedvenc kihangosítónk.", TimeLimit = 1, ImagePath = "~/App_Data/ResourceImages/bluetooth.jpg" });
defaults.Add(new Resource() { Name = "Laptop", AssetTag = "GFDS5412", Description = "Ez egy szép piros laptop", TimeLimit = 24, ImagePath = "~/App_Data/ResourceImages/laptop.jpg" });
defaults.Add(new Resource() { Name = "Piros Telefon", AssetTag = "ASDHGD5423", Description = "Ez a vészhívó telefon", ImagePath = "~/App_Data/ResourceImages/phone.jpg" });
@ -80,12 +110,40 @@ namespace ResoursesManager.Models
defaults.Add(new Resource() { Name = "IPhone", AssetTag = "GDSG3453", Description = "Ez egy iPhone", ImagePath = "~/App_Data/ResourceImages/mobile.jpg" });
defaults.Add(new Resource() { Name = "Epson Projektor", AssetTag = "BFR435", Description = "Az Epson projektor", TimeLimit = 8, ImagePath = "~/App_Data/ResourceImages/projector.jpg" });
defaults.Add(new Resource() { Name = "Egyéb", AssetTag = "HFG345", Description = "Ez valami egyéb gyári képpel.", ImagePath = "~/App_Data/ResourceImages/DefaultResource.png" });
foreach (var item in defaults)
{
context.Resources.Add(item);
}
Random r = new Random();
for (int i = 0; i < 100; i++)
{
var date = DateTime.Now.AddMinutes(r.Next(0, 43800));
var model = new Reservation()
{
ResourceId = r.Next(0, defaults.Count - 1),
Resource = defaults[r.Next(0,defaults.Count-1)],
Begining = date,
End = date.AddHours(r.Next(0, 10)),
User = manager.FindByName(string.Format(userName, r.Next(0, users.Count-1))).Id
};
context.Reservations.Add(model);
}
for (int i = 0; i < 1000; i++)
{
var model = new StatisticModel()
{
Id = i,
ResourceId = r.Next(0, defaults.Count - 1),
ReservationId = 1,
StatisticType = (StatisticType)r.Next(0, Enum.GetNames(typeof(StatisticType)).Length - 1),
DateTime = DateTime.Now.AddMinutes(r.Next(0, 43800) * (-1)),
UserId = manager.FindByName(string.Format(userName, r.Next(0, users.Count-1))).Id
};
context.Statistic.Add(model);
}
base.Seed(context);
}
}

View File

@ -234,6 +234,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Startup.cs" />
<Compile Include="ViewModels\ImageViewModel.cs" />
<Compile Include="ViewModels\StatisticViewModel.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="amcharts\amcharts.js" />

View File

@ -0,0 +1,21 @@
using ResoursesManager.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ResoursesManager.ViewModels
{
public enum SubscribeType
{
ByUser, ByResource
}
public class StatisticViewModel
{
public Dictionary<DateTime,int> ByType { get; set; }
public Dictionary<int, int> ByResource{ get; set; }
public Dictionary<StatisticType, int> ByTypeCircle { get; set; }
public Dictionary<string, int> Subscribe { get; set; }
public List<StatisticModel> Log { get; set; }
}
}

View File

@ -1,35 +1,163 @@
@model IEnumerable<ResoursesManager.Models.StatisticModel>
@model ResoursesManager.ViewModels.StatisticViewModel
@{
ViewBag.Title = "Index";
ViewBag.Title = "Statisztika";
}
<h2>Index</h2>
<!-- Styles -->
<style>
#chartdiv, #chartdiv2, #chartdiv3, #chartdiv4 {
width: 100%;
height: 500px;
font-size: 11px;
}
</style>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<!-- Resources -->
<script src="~/amcharts/amcharts.js"></script>
<script src="~/amcharts/pie.js"></script>
<script src="~/amcharts/serial.js"></script>
<script src="~/amcharts/plugins/export/export.min.js"></script>
<link rel="stylesheet" href="~/amcharts/plugins/export/export.css" type="text/css" media="all" />
<script src="~/amcharts/themes/light.js"></script>
<!-- Chart code -->
<script>
var chart = AmCharts.makeChart( "chartdiv", {
"type": "serial",
"theme": "light",
"dataProvider": [@Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model.ByType))],
"valueAxes": [ {
"gridColor": "#FFFFFF",
"gridAlpha": 0.2,
"dashLength": 0
} ],
"gridAboveGraphs": true,
"startDuration": 1,
"graphs": [ {
"balloonText": "[[category]]: <b>[[value]]</b>",
"fillAlphas": 0.8,
"lineAlpha": 0.2,
"type": "column",
"valueField": "visits"
} ],
"chartCursor": {
"categoryBalloonEnabled": false,
"cursorAlpha": 0,
"zoomable": false
},
"categoryField": "country",
"categoryAxis": {
"gridPosition": "start",
"gridAlpha": 0,
"tickPosition": "start",
"tickLength": 20
},
"export": {
"enabled": false
}
} );
</script>
<script>
var chart2 = AmCharts.makeChart( "chartdiv2", {
"type": "serial",
"theme": "light",
"dataProvider": [@Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model.ByResource))],
"valueAxes": [ {
"gridColor": "#FFFFFF",
"gridAlpha": 0.2,
"dashLength": 0
} ],
"gridAboveGraphs": true,
"startDuration": 1,
"graphs": [ {
"balloonText": "[[category]]: <b>[[value]]</b>",
"fillAlphas": 0.8,
"lineAlpha": 0.2,
"type": "column",
"valueField": "visits"
} ],
"chartCursor": {
"categoryBalloonEnabled": false,
"cursorAlpha": 0,
"zoomable": false
},
"categoryField": "country",
"categoryAxis": {
"gridPosition": "start",
"gridAlpha": 0,
"tickPosition": "start",
"tickLength": 20
},
"export": {
"enabled": false
}
} );
</script>
<script>
var chart3 = AmCharts.makeChart( "chartdiv3", {
"type": "pie",
"theme": "light",
"dataProvider": [@Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model.ByTypeCircle))],
"valueField": "litres",
"titleField": "country",
"balloon":{
"fixedPosition":true
},
"export": {
"enabled": false
}
} );
</script>
<script>
var chart4 = AmCharts.makeChart( "chartdiv4", {
"type": "pie",
"theme": "light",
"dataProvider": [@Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model.Subscribe))],
"valueField": "litres",
"titleField": "country",
"balloon":{
"fixedPosition":true
},
"export": {
"enabled": false
}
} );
</script>
<h2>Statisztika</h2>
<!-- HTML -->
<h3>Tevékenységek száma erőforrás szerint</h3>
<div id="chartdiv"></div>
<h3>Erőforrás szerint időben csoportosítva</h3>
<div id="chartdiv2"></div>
<h3>Tevékenységek aránya</h3>
<div id="chartdiv3"></div>
<h3>Felíratkozások aránya</h3>
<div id="chartdiv4"></div>
<h2>Napló</h2>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.UserId)
@Html.DisplayName("UserId")
</th>
<th>
@Html.DisplayNameFor(model => model.ResourceId)
@Html.DisplayName("ResourceId")
</th>
<th>
@Html.DisplayNameFor(model => model.ReservationId)
@Html.DisplayName("ReservationId")
</th>
<th>
@Html.DisplayNameFor(model => model.StatisticType)
@Html.DisplayName("StatisticType")
</th>
<th>
@Html.DisplayNameFor(model => model.DateTime)
@Html.DisplayName("DateTime")
</th>
<th></th>
</tr>
@foreach (var item in Model) {
@foreach (var item in Model.Log) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserId)
@ -46,12 +174,14 @@
<td>
@Html.DisplayFor(modelItem => item.DateTime)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
<p>json</p>
@Newtonsoft.Json.JsonConvert.SerializeObject(Model.ByType)
<p>-</p>
@Newtonsoft.Json.JsonConvert.SerializeObject(Model.ByResource)
<p>-</p>
@Newtonsoft.Json.JsonConvert.SerializeObject(Model.ByTypeCircle)
<p>-</p>
@Newtonsoft.Json.JsonConvert.SerializeObject(Model.Subscribe)

View File

@ -772,3 +772,95 @@ Parameter name: fileName
at System.Web.Mvc.Controller.File(String fileName, String contentType, String fileDownloadName)
at System.Web.Mvc.Controller.File(String fileName, String contentType)
at ResoursesManager.Controllers.ResourcesController.GetImage(String path) in C:\Users\blaketillman159\Source\Repos\thesis\ResoursesManager\ResoursesManager\Controllers\ResourcesController.cs:line 32
INFO 2017-11-01 11:22:08,443 56575ms ReservationsController Create - Új Foglalás: , user: admin@admin.hu, kezdõIdõpont: 02/11/2017 12:00:00, befejezõIdõpont: 02/11/2017 13:00:00
ERROR 2017-11-01 11:22:08,529 56661ms NotificationHelper Send - Save notification message
System.NullReferenceException: Object reference not set to an instance of an object.
at ResoursesManager.Helpers.NotificationHelper.Send(List`1 users, String title, String message, String img, String url) in C:\Users\blaketillman159\Source\Repos\thesis\ResoursesManager\ResoursesManager\Helpers\NotificationHelper.cs:line 63
INFO 2017-11-01 11:22:51,330 99462ms ReservationsController Create - Új Foglalás: , user: admin@admin.hu, kezdõIdõpont: 03/11/2017 08:00:00, befejezõIdõpont: 03/11/2017 10:00:00
ERROR 2017-11-01 11:22:51,362 99494ms NotificationHelper Send - Save notification message
System.NullReferenceException: Object reference not set to an instance of an object.
at ResoursesManager.Helpers.NotificationHelper.Send(List`1 users, String title, String message, String img, String url) in C:\Users\blaketillman159\Source\Repos\thesis\ResoursesManager\ResoursesManager\Helpers\NotificationHelper.cs:line 63
INFO 2017-11-01 11:23:06,521 114653ms ReservationsController Create - Új Foglalás: , user: admin@admin.hu, kezdõIdõpont: 06/11/2017 10:00:00, befejezõIdõpont: 06/11/2017 11:00:00
ERROR 2017-11-01 11:23:06,555 114687ms NotificationHelper Send - Save notification message
System.NullReferenceException: Object reference not set to an instance of an object.
at ResoursesManager.Helpers.NotificationHelper.Send(List`1 users, String title, String message, String img, String url) in C:\Users\blaketillman159\Source\Repos\thesis\ResoursesManager\ResoursesManager\Helpers\NotificationHelper.cs:line 63
INFO 2017-11-01 11:26:20,524 308656ms ReservationsController Edit - Új foglalás: , user: admin@admin.hu, kezdõIdõpont: 06/11/2017 11:00:00, befejezõIdõpont: 06/11/2017 12:00:00
ERROR 2017-11-01 11:26:20,567 308699ms NotificationHelper Send - Save notification message
System.NullReferenceException: Object reference not set to an instance of an object.
at ResoursesManager.Helpers.NotificationHelper.Send(List`1 users, String title, String message, String img, String url) in C:\Users\blaketillman159\Source\Repos\thesis\ResoursesManager\ResoursesManager\Helpers\NotificationHelper.cs:line 63
ERROR 2017-11-01 15:44:35,632 35083ms MvcApplication Application_Start - Admin User, Role create
System.Data.DataException: An exception occurred while initializing the database. See the InnerException for details. ---> System.Data.Entity.Infrastructure.DbUpdateException: Unable to determine the principal end of the 'ResoursesManager.Models.Resource_Reservation' relationship. Multiple added entities may have the same primary key. ---> System.Data.Entity.Core.UpdateException: Unable to determine the principal end of the 'ResoursesManager.Models.Resource_Reservation' relationship. Multiple added entities may have the same primary key.
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.RegisterEntityReferentialConstraints(IEntityStateEntry stateEntry, Boolean currentValues)
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.RegisterReferentialConstraints(IEntityStateEntry stateEntry)
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.PullModifiedEntriesFromStateManager()
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.ProduceCommands()
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.<Update>b__2(UpdateTranslator ut)
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T noChangesResult, Func`2 updateFunction)
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update()
at System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__35()
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction)
at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass2a.<SaveChangesInternal>b__27()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
--- End of inner exception stack trace ---
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at System.Data.Entity.CreateDatabaseIfNotExists`1.InitializeDatabase(TContext context)
at System.Data.Entity.Internal.InternalContext.<>c__DisplayClassf`1.<CreateInitializationAction>b__e()
at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
--- End of inner exception stack trace ---
at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
at System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c)
at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
at System.Linq.Queryable.Any[TSource](IQueryable`1 source, Expression`1 predicate)
at ResoursesManager.MvcApplication.Application_Start() in C:\Users\blaketillman159\Source\Repos\thesis\ResoursesManager\ResoursesManager\Global.asax.cs:line 44
ERROR 2017-11-01 15:57:13,431 198616ms MvcApplication Application_Start - Admin User, Role create
System.Data.DataException: An exception occurred while initializing the database. See the InnerException for details. ---> System.Data.Entity.Infrastructure.DbUpdateException: Unable to determine the principal end of the 'ResoursesManager.Models.Resource_Reservation' relationship. Multiple added entities may have the same primary key. ---> System.Data.Entity.Core.UpdateException: Unable to determine the principal end of the 'ResoursesManager.Models.Resource_Reservation' relationship. Multiple added entities may have the same primary key.
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.RegisterEntityReferentialConstraints(IEntityStateEntry stateEntry, Boolean currentValues)
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.RegisterReferentialConstraints(IEntityStateEntry stateEntry)
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.PullModifiedEntriesFromStateManager()
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.ProduceCommands()
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.<Update>b__2(UpdateTranslator ut)
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T noChangesResult, Func`2 updateFunction)
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update()
at System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__35()
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction)
at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass2a.<SaveChangesInternal>b__27()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
--- End of inner exception stack trace ---
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at System.Data.Entity.CreateDatabaseIfNotExists`1.InitializeDatabase(TContext context)
at System.Data.Entity.Internal.InternalContext.<>c__DisplayClassf`1.<CreateInitializationAction>b__e()
at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
--- End of inner exception stack trace ---
at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
at System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c)
at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
at System.Linq.Queryable.Any[TSource](IQueryable`1 source, Expression`1 predicate)
at ResoursesManager.MvcApplication.Application_Start() in C:\Users\blaketillman159\Source\Repos\thesis\ResoursesManager\ResoursesManager\Global.asax.cs:line 45