Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ATLDEV-330 Show violations by user #76

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,9 @@ enum State {
@Ignore
TimesheetEntry[] getEntriesDesc();

@Ignore
TimesheetEntry[] getEntriesAsc();

@Ignore
int calculateViolations(int monitoring_period, int required_hours);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import org.catrobat.jira.timesheet.activeobjects.Timesheet;
import org.catrobat.jira.timesheet.activeobjects.TimesheetEntry;
import org.joda.time.DateTime;

import java.time.LocalDateTime;
import java.util.*;

public class TimesheetImpl {
Expand Down Expand Up @@ -63,4 +65,54 @@ public final TimesheetEntry[] getEntriesDesc() {
Arrays.sort(entries, Comparator.comparing(TimesheetEntry::getBeginDate).reversed());
return entries;
}

public final TimesheetEntry[] getEntriesAsc() {
TimesheetEntry[] entries = timesheet.getEntries();
Arrays.sort(entries, Comparator.comparing(TimesheetEntry::getBeginDate));
return entries;
}


public final int calculateViolations(int monitoring_period, int required_hours) {
TimesheetEntry[] entries = getEntriesAsc();
//TODO: add some checks here
if(entries.length == 0 || monitoring_period == 0) {
return 0;
}
DateTime first_entry_date = new DateTime(entries[0].getBeginDate());
DateTime last_entry_date = new DateTime(entries[entries.length - 1].getBeginDate());

DateTime start = first_entry_date.withDayOfMonth(1).plusMonths(1).withTimeAtStartOfDay();
DateTime end = last_entry_date.withDayOfMonth(1).withTimeAtStartOfDay();

DateTime start_t = start.minusMillis(1);
DateTime end_t = start.plusMonths(monitoring_period);

int required_minutes = required_hours * 60;
int violations = 0;
int i = 0;
DateTime date;

while(end_t.isBefore(end) || end_t.isEqual(end)) {
int minutes = 0;

do {
date = new DateTime(entries[i].getBeginDate());
if(date.isAfter(start_t) && date.isBefore(end_t)) {
minutes += entries[i].getDurationMinutes();
}
i++;
}
while(date.isBefore(end_t));
i--;

if(minutes < required_minutes) {
violations++;
}

start_t = end_t.minusMillis(1);
end_t = end_t.plusMonths(monitoring_period);
}
return violations;
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/catrobat/jira/timesheet/rest/UserRest.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ private List<JsonUserInformation> timesheetsToJson(List<Timesheet> timesheetList
jsonUserInformation.setHoursPerMonitoringPeriod(timesheetEntryService.getHours(timesheet, cur_interval.getKey(), cur_interval.getValue()));
jsonUserInformation.setHoursPerLastMonitoringPeriod(timesheetEntryService.getHours(timesheet, last_interval.getKey(), last_interval.getValue()));

jsonUserInformation.setViolations(timesheet.calculateViolations(monitoringService.getMonitoring().getPeriod(),monitoringService.getMonitoring().getRequiredHours()));

TimesheetEntry latestInactiveEntry = timesheetEntryService.getLatestInactiveEntry(timesheet);
if (latestInactiveEntry != null && (timesheet.getState() == Timesheet.State.INACTIVE
|| timesheet.getState() == Timesheet.State.INACTIVE_OFFLINE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public class JsonUserInformation {
private int timesheetID;
@XmlElement
private boolean isEnabled;
@XmlElement
private int violations;

public JsonUserInformation (Timesheet timesheet) {

Expand Down Expand Up @@ -205,4 +207,12 @@ public boolean isEnabled() {
public void setEnabled(boolean enabled) {
isEnabled = enabled;
}

public int getViolations() {
return violations;
}

public void setViolations(int violations) {
this.violations = violations;
}
}
1 change: 1 addition & 0 deletions src/main/resources/js/user_information.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ AJS.toInit(function () {
"</td><td headers='ti-hours-per-half-year' class='hours-half-year'>" + userInformation[i].hoursPerHalfYear +
"</td><td headers='ti-hours-per-monitoring-period' class='hours-half-year'>" + userInformation[i].hoursPerMonitoringPeriod +
"</td><td headers='ti-hours-per-last-monitoring-period' class='hours-half-year'>" + userInformation[i].hoursPerLastMonitoringPeriod +
"</td><td headers='ti-violations' class='violation'>" + userInformation[i].violations +
"</td><td headers='ti-first-entry-date' class='latest-date'>" + firstEntryDate +
"</td><td headers='ti-latest-entry-date' class='latest-date'>" + latestEntryDate +
"</td><td headers='ti-latest-entry-description' class='latest-description'>" + userInformation[i].latestEntryDescription +
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/user_information.vm
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
<th id="ti-hours-per-half-year">Hours last Half Year</th>
<th id="ti-hours-per-monitoring-period">Hours ($monitoringPeriod)</th>
<th id="ti-hours-per-last-monitoring-period">Hours ($lastMonitoringPeriod)</th>
<th id="ti-violations">Violations</th>
<th id="ti-first-entry-date">Date of the First Entry</th>
<th id="ti-latest-entry-date">Date of the Latest Entry</th>
<th id="ti-latest-entry-description">Description of the Latest Entry</th>
Expand Down Expand Up @@ -151,6 +152,7 @@
<th id="ti-hours-per-half-year">Hours last Half Year</th>
<th id="ti-hours-per-monitoring-period">Hours ($monitoringPeriod)</th>
<th id="ti-hours-per-last-monitoring-period">Hours ($lastMonitoringPeriod)</th>
<th id="ti-violations">Violations</th>
<th id="ti-first-entry-date">Date of the First Entry</th>
<th id="ti-latest-entry-date">Date of the Latest Entry</th>
<th id="ti-latest-entry-description">Description of the Latest Entry</th>
Expand Down Expand Up @@ -181,6 +183,7 @@
<th id="ti-hours-per-half-year">Hours last Half Year</th>
<th id="ti-hours-per-monitoring-period">Hours ($monitoringPeriod)</th>
<th id="ti-hours-per-last-monitoring-period">Hours ($lastMonitoringPeriod)</th>
<th id="ti-violations">Violations</th>
<th id="ti-first-entry-date">Date of the First Entry</th>
<th id="ti-latest-entry-date">Date of the Latest Entry</th>
<th id="ti-latest-entry-description">Description of the Latest Entry</th>
Expand Down Expand Up @@ -211,6 +214,7 @@
<th id="ti-hours-per-half-year">Hours last Half Year</th>
<th id="ti-hours-per-monitoring-period">Hours ($monitoringPeriod)</th>
<th id="ti-hours-per-last-monitoring-period">Hours ($lastMonitoringPeriod)</th>
<th id="ti-violations">Violations</th>
<th id="ti-first-entry-date">Date of the First Entry</th>
<th id="ti-latest-entry-date">Date of the Latest Entry</th>
<th id="ti-latest-entry-description">Description of the Latest Entry</th>
Expand Down Expand Up @@ -241,6 +245,7 @@
<th id="ti-hours-per-half-year">Hours last Half Year</th>
<th id="ti-hours-per-monitoring-period">Hours ($monitoringPeriod)</th>
<th id="ti-hours-per-last-monitoring-period">Hours ($lastMonitoringPeriod)</th>
<th id="ti-violations">Violations</th>
<th id="ti-first-entry-date">Date of the First Entry</th>
<th id="ti-latest-entry-date">Date of the Latest Entry</th>
<th id="ti-latest-entry-description">Description of the Latest Entry</th>
Expand Down Expand Up @@ -271,6 +276,7 @@
<th id="ti-hours-per-half-year">Hours last Half Year</th>
<th id="ti-hours-per-monitoring-period">Hours ($monitoringPeriod)</th>
<th id="ti-hours-per-last-monitoring-period">Hours ($lastMonitoringPeriod)</th>
<th id="ti-violations">Violations</th>
<th id="ti-first-entry-date">Date of the First Entry</th>
<th id="ti-latest-entry-date">Date of the Latest Entry</th>
<th id="ti-latest-entry-description">Description of the Latest Entry</th>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,116 @@ public void testCheckParamsPause() throws Exception {
pairProgrammingUserName, teamroom);
}

@Test
public void TestCalculateViolations() throws ServiceException {

Timesheet sheet = createTestTimesheet();
Category category = createTestCategory();
Team team = createTestTeam();

Date inactiveEnd = Date.from(ZonedDateTime.now().minusMonths(6).toInstant());

Date begin = Date.from(ZonedDateTime.now().toInstant());
Date end = Date.from(ZonedDateTime.now().plusHours(1).toInstant());
String desc = "Debugged this thingy...";
int pause = 0;
boolean isGoogleDocImport = false;
String jiraTicketID = "ATLDEV-287";
String pairProgrammingUserName = "TestUser";
boolean teamroom = false;

service.add(sheet, begin, end, category, desc, pause, team, isGoogleDocImport, inactiveEnd, jiraTicketID, pairProgrammingUserName, teamroom);

begin = Date.from(ZonedDateTime.now().minusMonths(1).toInstant());
end = Date.from(ZonedDateTime.now().minusMonths(1).plusHours(1).toInstant());

service.add(sheet, begin, end, category, desc, pause, team, isGoogleDocImport, inactiveEnd, jiraTicketID, pairProgrammingUserName, teamroom);

begin = Date.from(ZonedDateTime.now().minusMonths(2).toInstant());
end = Date.from(ZonedDateTime.now().minusMonths(2).plusHours(1).toInstant());

service.add(sheet, begin, end, category, desc, pause, team, isGoogleDocImport, inactiveEnd, jiraTicketID, pairProgrammingUserName, teamroom);

begin = Date.from(ZonedDateTime.now().minusMonths(3).toInstant());
end = Date.from(ZonedDateTime.now().minusMonths(3).plusHours(1).toInstant());

service.add(sheet, begin, end, category, desc, pause, team, isGoogleDocImport, inactiveEnd, jiraTicketID, pairProgrammingUserName, teamroom);

int monitoring_period = 1;
int required_hours = 25;
int result = sheet.calculateViolations(monitoring_period, required_hours);
Assert.assertEquals(2, result);
}

@Test
public void TestCalculateViolationsOneEntry() throws ServiceException {

Timesheet sheet = createTestTimesheet();
Category category = createTestCategory();
Team team = createTestTeam();

Date inactiveEnd = Date.from(ZonedDateTime.now().minusMonths(6).toInstant());

Date begin = Date.from(ZonedDateTime.now().toInstant());
Date end = Date.from(ZonedDateTime.now().plusHours(1).toInstant());
String desc = "Debugged this thingy...";
int pause = 0;
boolean isGoogleDocImport = false;
String jiraTicketID = "ATLDEV-287";
String pairProgrammingUserName = "TestUser";
boolean teamroom = false;

service.add(sheet, begin, end, category, desc, pause, team, isGoogleDocImport, inactiveEnd, jiraTicketID, pairProgrammingUserName, teamroom);

int monitoring_period = 1;
int required_hours = 25;
int result = sheet.calculateViolations(monitoring_period, required_hours);
Assert.assertEquals(0, result);
}

@Test
public void TestCalculateViolationsNoTimeBetween() throws ServiceException {

Timesheet sheet = createTestTimesheet();
Category category = createTestCategory();
Team team = createTestTeam();

Date inactiveEnd = Date.from(ZonedDateTime.now().minusMonths(6).toInstant());

Date begin = Date.from(ZonedDateTime.now().toInstant());
Date end = Date.from(ZonedDateTime.now().plusHours(1).toInstant());
String desc = "Debugged this thingy...";
int pause = 0;
boolean isGoogleDocImport = false;
String jiraTicketID = "ATLDEV-287";
String pairProgrammingUserName = "TestUser";
boolean teamroom = false;

service.add(sheet, begin, end, category, desc, pause, team, isGoogleDocImport, inactiveEnd, jiraTicketID, pairProgrammingUserName, teamroom);

end = begin;
begin = Date.from(ZonedDateTime.now().minusHours(1).toInstant());

service.add(sheet, begin, end, category, desc, pause, team, isGoogleDocImport, inactiveEnd, jiraTicketID, pairProgrammingUserName, teamroom);

int monitoring_period = 1;
int required_hours = 25;
int result = sheet.calculateViolations(monitoring_period, required_hours);
Assert.assertEquals(0, result);
}

@Test
public void TestCalculateViolationsNoMonitoring() {

Timesheet sheet = createTestTimesheet();

int monitoring_period = 0;
int required_hours = 25;
int result = sheet.calculateViolations(monitoring_period, required_hours);
Assert.assertEquals(0, result);
}



public static class MyDatabaseUpdater implements DatabaseUpdater {

Expand Down