summaryrefslogtreecommitdiff
path: root/analyze.go
blob: fdf284265001c453f668e0b130ef97c98778c574 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// analyze.go contains all data analysis
package main

import (
	"fmt"
)

// Data for Month
type Month struct {
	Month int
	MonthName string
	Year int
	Days []int

	Earnings float64

	Workvalue float64
	Workhours float64
	//Workdays int
	Tasks []int
	Projects []int
}

var analysis []Month


//Analyze all tasks and PAyments 
func MakeAnalysis() {
	fmt.Println("Making Analysis")

	tids := GetTaskIds()
	alltasks := GetSelectedTasks(tids)

	for _,ta := range alltasks {
		year,monthn,day := ta.Start.Local().Date()
		month := int(monthn)
		monthstr := fmt.Sprintf("%v",monthn)
		exist, exid := MonthExists(year,month)
		dur := ta.Duration()
		val := ta.Money()

		if exist {
			if !isElement(day,analysis[exid].Days){
				analysis[exid].Days = append(analysis[exid].Days,day)
			}
			analysis[exid].Workvalue += val
			analysis[exid].Workhours += dur
			analysis[exid].Tasks = append(analysis[exid].Tasks,ta.Id)
			if !isElement(ta.Projectid,analysis[exid].Projects){
				analysis[exid].Projects = append(analysis[exid].Projects,ta.Projectid)
			}
		}else{
			analysis = append(analysis,Month{
				month,
				monthstr,
				year,
				[]int{day},

				0.0,  //Earning

				val,  // Workvalue
				dur,  //Workhours
				[]int{ta.Id},[]int{ta.Projectid}})
		}

	}
	pays := GetAllPayments()
	for _,py := range pays {
		year,monthn,_ := py.Date.Local().Date()
		month := int(monthn)
		monthstr := fmt.Sprintf("%v",monthn)
		exist, exid := MonthExists(year,month)

		if exist {
			analysis[exid].Earnings += py.Amount
		}else{
			analysis = append(analysis,Month{
				month,
				monthstr,
				year,
				[]int{},

				py.Amount,  //Earning

				0.0,  //Workvalue
				0.0,  //Workhours
				[]int{},
				[]int{}})
		}
	}
	//fmt.Println("Length:",len(analysis))
	ShowFullAnalysis()
}


//Show full analysis
func ShowFullAnalysis() {
	fmt.Println(frame("Full Analysis",true))
	for _,mo := range analysis {
		cnt, hr, dur := AnalyzeTasks(GetSelectedTasks(mo.Tasks))

		fmt.Println(sli,mo.Year,mo.MonthName)
		fmt.Printf("%s    Workhours: %.2f[h]\n",nli,mo.Workhours)
		fmt.Printf("%s     Earnings: %.2f[€]\n",nli,mo.Earnings)
		fmt.Printf("%s    Workvalue: %.2f[€]\n",nli,mo.Workvalue)
		fmt.Printf("%s     Projects: %v\n",nli,len(mo.Projects))
		fmt.Printf("%s         Days: %v\n",nli,len(mo.Days))
		fmt.Printf("%s        Tasks: %v Hours: %.2f[€]\n",nli,cnt, hr)
		fmt.Printf("%s               %s\n",nli,dur)
		fmt.Printf("%s  Hourly Rate: %.2f[€/h]\n",nli,mo.Earnings/mo.Workhours)
		fmt.Printf("%s Productivity: %.2f[h/d]\n",nli,mo.Workhours/float64(len(mo.Days)))

		
		fmt.Println(nli)
	}
	fmt.Println(frame("",false))
}


// Test if analysis slice conatins the month already and return its id if
func MonthExists(year, month int) (exist bool, id int) {
	for i,mo := range analysis {
		if mo.Month == month && mo.Year == year {
			return true,i
		}
	}
	return false,-1
}