Skip to content

Commit

Permalink
eka toimiva demo
Browse files Browse the repository at this point in the history
  • Loading branch information
ArttuKuikka committed Mar 2, 2024
1 parent 50d0724 commit c059c2d
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 95 deletions.
123 changes: 69 additions & 54 deletions Controllers/AanestysController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Newtonsoft.Json.Linq;
using RuokalistaServer.Data;
using RuokalistaServer.Models;
using System.Text.Json;

namespace RuokalistaServer.Controllers
{
Expand Down Expand Up @@ -31,6 +32,71 @@ public IActionResult Tulokset(string? isApp = "false")
return View();
}

[HttpGet]
[Route("api/v1/Aanestys/ProsenttiTuloksetTietyllaAikavalilla")]
public IActionResult ProsenttiTuloksetTietyllaAikavalilla(string start, string? end)
{
var startDate = DateTime.Parse(start);
var endDate = DateTime.Now;
if (end != null && end != "")
{
endDate = DateTime.Parse(end);
}
var startWeek = System.Globalization.ISOWeek.GetWeekOfYear(startDate);
var endWeek = System.Globalization.ISOWeek.GetWeekOfYear(endDate);


var ruokalistaObjects = db.Ruokalista.Where(x => x.Year >= startDate.Year && x.WeekId >= startWeek && x.Year <= endDate.Year && x.WeekId <= endWeek);

var returnList = new List<ProsenttiTulos>();
foreach (var r in ruokalistaObjects)
{

var prosentit = new ViikonProsentit
{
maanantai = 0,
tiistai = 0,
keskiviikko = 0,
torstai = 0,
perjantai = 0
};

var VV = db.Votes.FirstOrDefault(x => x.ruokalistaId == r.Id);
if (VV != null)
{
prosentit.maanantai = CalculateFoodScore(VV.level1_votes_maanantai, VV.level2_votes_maanantai, VV.level3_votes_maanantai, VV.level4_votes_maanantai);
prosentit.tiistai = CalculateFoodScore(VV.level1_votes_tiistai, VV.level2_votes_tiistai, VV.level3_votes_tiistai, VV.level4_votes_tiistai);
prosentit.keskiviikko = CalculateFoodScore(VV.level1_votes_keskiviikko, VV.level2_votes_keskiviikko, VV.level3_votes_keskiviikko, VV.level4_votes_keskiviikko);
prosentit.torstai = CalculateFoodScore(VV.level1_votes_torstai, VV.level2_votes_torstai, VV.level3_votes_torstai, VV.level4_votes_torstai);
prosentit.perjantai = CalculateFoodScore(VV.level1_votes_perjantai, VV.level2_votes_perjantai, VV.level3_votes_perjantai, VV.level4_votes_perjantai);
}



var rObj = new ProsenttiTulos
{
ruokalista = r,
prosentit = prosentit,

};
returnList.Add(rObj);
}

return Ok(JsonConvert.SerializeObject(returnList));
}

private double CalculateFoodScore(int level1, int level2, int level3, int level4)
{
var yhteensa = level1 + level2 + level3 + level4;
if(yhteensa == 0)
{
return 0;
}

return (1 * ((double)level4 / yhteensa)) + (0.75 * ((double)level3 / yhteensa)) + (0.50 * ((double)level2 / yhteensa)) + (0.25 * ((double)level1 / yhteensa));

}



[HttpGet]
Expand Down Expand Up @@ -252,63 +318,12 @@ public async Task<IActionResult> Aanesta(int taso)
return Ok("aanestys onnistui");
}

[Authorize]
public async Task<IActionResult> Tulostaulu()

public IActionResult Tulostaulu()
{
var model = new TulostauluModel();

var lista = new List<(string, double)>();

foreach(var week in db.Ruokalista)
{
var viikonVotet = db.Votes.FirstOrDefault(x => x.ruokalistaId == week.Id);
if (viikonVotet == null) continue;

var maanantai_yhteensa = viikonVotet.level4_votes_maanantai + viikonVotet.level3_votes_maanantai + viikonVotet.level2_votes_maanantai + viikonVotet.level1_votes_maanantai;
if(maanantai_yhteensa != 0)
{
lista.Add((week.Maanantai, (1 * ((double)viikonVotet.level4_votes_maanantai / maanantai_yhteensa)) + (0.75 * ((double)viikonVotet.level3_votes_maanantai / maanantai_yhteensa)) + (0.50 * ((double)viikonVotet.level2_votes_maanantai / maanantai_yhteensa)) + (0.25 * ((double)viikonVotet.level1_votes_maanantai / maanantai_yhteensa))));
}

var tiistai_yhteensa = viikonVotet.level4_votes_tiistai + viikonVotet.level3_votes_tiistai + viikonVotet.level2_votes_tiistai + viikonVotet.level1_votes_tiistai;
if(tiistai_yhteensa != 0)
{
lista.Add((week.Tiistai, (1 * ((double)viikonVotet.level4_votes_tiistai / tiistai_yhteensa)) + (0.75 * ((double)viikonVotet.level3_votes_tiistai / tiistai_yhteensa)) + (0.50 * ((double)viikonVotet.level2_votes_tiistai / tiistai_yhteensa)) + (0.25 * ((double)viikonVotet.level1_votes_tiistai / tiistai_yhteensa))));
}

var keskiviikko_yhteensa = viikonVotet.level4_votes_keskiviikko + viikonVotet.level3_votes_keskiviikko + viikonVotet.level2_votes_keskiviikko + viikonVotet.level1_votes_keskiviikko;
if(keskiviikko_yhteensa != 0)
{
lista.Add((week.Keskiviikko, (1 * ((double)viikonVotet.level4_votes_keskiviikko / keskiviikko_yhteensa)) + (0.75 * ((double)viikonVotet.level3_votes_keskiviikko / keskiviikko_yhteensa)) + (0.50 * ((double)viikonVotet.level2_votes_keskiviikko / keskiviikko_yhteensa)) + (0.25 * ((double)viikonVotet.level1_votes_keskiviikko / keskiviikko_yhteensa))));
}

var torstai_yhteensa = viikonVotet.level4_votes_torstai + viikonVotet.level3_votes_torstai + viikonVotet.level2_votes_torstai + viikonVotet.level1_votes_torstai;
if(torstai_yhteensa != 0)
{
lista.Add((week.Torstai, (1 * ((double)viikonVotet.level4_votes_torstai / torstai_yhteensa)) + (0.75 * ((double)viikonVotet.level3_votes_torstai / torstai_yhteensa)) + (0.50 * ((double)viikonVotet.level2_votes_torstai / torstai_yhteensa)) + (0.25 * ((double)viikonVotet.level1_votes_torstai / torstai_yhteensa))));
}

var perjantai_yhteensa = viikonVotet.level4_votes_perjantai + viikonVotet.level3_votes_perjantai + viikonVotet.level2_votes_perjantai + viikonVotet.level1_votes_perjantai;
if(perjantai_yhteensa != 0)
{
lista.Add((week.Perjantai, (1 * ((double)viikonVotet.level4_votes_perjantai / perjantai_yhteensa)) + (0.75 * ((double)viikonVotet.level3_votes_perjantai / perjantai_yhteensa)) + (0.50 * ((double)viikonVotet.level2_votes_perjantai / perjantai_yhteensa)) + (0.25 * ((double)viikonVotet.level1_votes_perjantai / perjantai_yhteensa))));
}
}


lista = lista.OrderBy(x => x.Item2).ToList();
lista.Reverse();


var dataStr = "[{x: " + lista.First().Item2.ToString().Replace(',', '.') + ", y: '" + lista.First().Item1 + "'}";
foreach(var item in lista.Skip(1))
{
dataStr += ", {x: " + item.Item2.ToString().Replace(',', '.') + ", y: '" + item.Item1 + "'}";
}
dataStr += "]";
model.data = dataStr;

return View(model);
return View();
}
}
}
17 changes: 17 additions & 0 deletions Models/ProsenttiTulos.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace RuokalistaServer.Models
{
public class ProsenttiTulos
{
public Ruokalista ruokalista { get; set; }
public ViikonProsentit prosentit { get; set; }
}

public class ViikonProsentit
{
public double maanantai { get; set; }
public double tiistai { get; set; }
public double keskiviikko { get; set; }
public double torstai { get; set; }
public double perjantai { get; set; }
}
}
9 changes: 0 additions & 9 deletions Models/TulostauluModel.cs

This file was deleted.

88 changes: 56 additions & 32 deletions Views/Aanestys/Tulostaulu.cshtml
Original file line number Diff line number Diff line change
@@ -1,49 +1,73 @@
@using Newtonsoft.Json
@using RuokalistaServer.Models;
@model TulostauluModel
@{
Layout = "_Layout";
ViewData["Title"] = "Tulostaulu";
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.4.1/chart.min.js" integrity="sha512-L0Shl7nXXzIlBSUUPpxrokqq4ojqgZFQczTYlGjzONGTDAcLremjwaWv5A+EDLnxhQzY5xUZPWLOLqYRkY0Cbw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<h1>Tulostaulu</h1>

<script type="text/javascript" src="https://cdn.jsdelivr.net/jquery/latest/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" />

<link rel="stylesheet" href="/css/aanestys.css">
<script src="/js/aanestys.js"></script>
<link rel="stylesheet" href="/css/tulostaulu.css">
<script src="/js/tulostaulu.js"></script>


<div>
<canvas id="myChart"></canvas>

</div>
<div class="date-container">
<div class="title">
<h1>Tulostaulu</h1>

</div>
<div class="input-container">
<div>Aikaväli</div>
<input type="text" name="datetimes" />
</div>
</div>

<br />

<div id="contentBody">

</div>

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script type="text/javascript">
<script>
const ctx = document.getElementById('myChart');
var contentBody = document.getElementById('contentBody');
new Chart(ctx, {
type: 'bar',
data: {
datasets: [{
data: @Html.Raw(Model?.data)
}]
$('input[name="datetimes"]').daterangepicker({
"showDropdowns": true,
"minYear": 2022,
"showWeekNumbers": true,
ranges: {
'Viimeiset 7 päivää': [moment().subtract(6, 'days'), moment()],
'Viimeiset 30 päivää': [moment().subtract(29, 'days'), moment()],
'Tämä kuukausi': [moment().startOf('month'), moment().endOf('month')],
'Viime kuukausi': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
},
options: {
indexAxis: 'y',
elements: {
bar: {
borderWidth: 2,
}
},
responsive: true,
plugins: {
legend: {
display: false
},
title: {
display: true,
text: 'Ruokien äänestystulokset'
}
}
}
"alwaysShowCalendars": true,
"startDate": "02/25/2024",
"endDate": "03/02/2024",
"opens": "left"
}, function (start, end, label) {
create_tulostaulu(contentBody, start, end)
});
</script>
const today = new Date();
const dayOfWeek = today.getDay();
const diffMonday = today.getDate() - dayOfWeek + (dayOfWeek === 0 ? -6 : 1); // Adjust for Sunday
const diffFriday = diffMonday + 4;
const friday = new Date(today.setDate(diffFriday));
const monday = new Date(today.setDate(diffMonday));
create_tulostaulu(contentBody, monday, friday)
</script>
28 changes: 28 additions & 0 deletions wwwroot/css/tulostaulu.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.date-container {
display: flex;
align-items: center;
justify-content: space-between;
padding: 20px;
box-sizing: border-box;
}

.input-container {
text-align: left;
margin-right: 20px;
}

.title {
flex: 1;
margin-right: 20px;
}

input {
width: 100%;
box-sizing: border-box;
padding: 10px;
background-color: #262626;
color: #ffffff;
border-radius: 5px
}


39 changes: 39 additions & 0 deletions wwwroot/js/tulostaulu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function create_tulostaulu(contentBody, startTime, endTime) {
//muista että teet sellasen josta voi valita aikavälin (custom, tämäviikko, viimeviikko, tämä kuukausi, viime kuukausi, viimeiset kolme kuukautta, tämä vuosi, viime vuosi, koko aika ja custom)
//aikavälin keskiarvo
//toinen valinta että näyttää vaan tietyt päivät
//ruokien arvostelujen kehitys ajan funktiona
var startDate = new Date(startTime)
var endDate = new Date(endTime)
console.log("reguested for " + startDate.toISOString() + " - " + endDate.toISOString())

const formattedStartDate = formatDateForURL(startDate);
const formattedEndDate = formatDateForURL(endDate);

const url = `/api/v1/Aanestys/ProsenttiTuloksetTietyllaAikavalilla?start=${formattedStartDate}&end=${formattedEndDate}`;

//get all of the votes in procentage form

fetch(url)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
console.log('Data:', data);
// You can do something with the data here
})
.catch(error => {
console.error('Error:', error);
});

}


function formatDateForURL(date) {
const options = { year: 'numeric', month: '2-digit', day: '2-digit' };
const formattedDate = date.toLocaleDateString('fi-FI', options);
return formattedDate.replace(/\//g, '-');
}

0 comments on commit c059c2d

Please sign in to comment.