summaryrefslogtreecommitdiff
path: root/analyze.go
blob: 8fb56aae4a862ba4592fa275da16d08fdad8699d (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// 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
}

// Yearly Data
type Year struct {
	Year int
	Months []int
	Days []int

	Earnings float64

	Workvalue float64
	Workhours float64

	Tasks []int
	Projects []int
}

var analysis []Month
var yearly   []Year

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

	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))
	for _,mo := range analysis {
		exist,exid := YearExists(mo.Year)

		if exist{
			yearly[exid].Months = append(yearly[exid].Months,mo.Month)
			yearly[exid].Days = append(yearly[exid].Days,mo.Days...)
			//yearly[exid].Days = AddNewElement(yearly[exid].Days,mo.Days)

			yearly[exid].Earnings += mo.Earnings

			yearly[exid].Workvalue += mo.Workvalue
			yearly[exid].Workhours += mo.Workhours

			yearly[exid].Tasks = AddNewElement(yearly[exid].Tasks,mo.Tasks)
			yearly[exid].Projects = AddNewElement(yearly[exid].Projects,mo.Projects)
		}else{
			yearly = append(yearly,Year{
				mo.Year,
				[]int{mo.Month},
				mo.Days,

				mo.Earnings,

				mo.Workvalue,
				mo.Workhours,

				mo.Tasks,
				mo.Projects})
		}
	}

	ShowMonthlyAnalysis()
	ShowYearlyAnalysis()
}


//Show Yearly analysis
func ShowYearlyAnalysis() {
	fmt.Println(frame("Yearly Analysis",true))
	for _,yr := range yearly {
		cnt, hr, dur := AnalyzeTasks(GetSelectedTasks(yr.Tasks))

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

		fmt.Println(nli)
	}
	fmt.Println(frame("",false))
}
//Show Mothly analysis
func ShowMonthlyAnalysis() {
	fmt.Println(frame("Monthly 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[h]\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
}

// Test if yearly slice conatins the year already and return its id if
func YearExists(year int) (exist bool, id int) {
	for i,yr := range yearly {
		if yr.Year == year {
			return true,i
		}
	}
	return false,-1
}