This repository has been archived by the owner on Nov 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathestimeoprazo.go
169 lines (148 loc) · 4.13 KB
/
estimeoprazo.go
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
package estimeoprazo
import (
"encoding/json"
"io"
"math"
"math/rand"
"net/http"
"sort"
"strconv"
"sync"
"time"
)
type Config struct {
MinTasks float64
MaxTasks float64
MinSplitTasks float64
MaxSplitTasks float64
MinTasksDone float64
MaxTasksDone float64
Simulations int
}
type ForecastResult struct {
Likelihood int
Weeks float64
}
func TotalStories(config Config) float64 {
return math.Ceil(getRandBeetween(config.MinTasks, config.MaxTasks) * (getRand()*float64(config.MaxSplitTasks-config.MinSplitTasks) + float64(config.MinSplitTasks)))
}
func EndWeekStories(totalStories float64, config Config) float64 {
return math.Max(0, totalStories-getRandBeetween(config.MinTasksDone, config.MaxTasksDone))
}
func getRand() float64 {
rand.Seed(time.Now().UTC().UnixNano())
return rand.Float64()
}
func WeeksToZero(config Config, wg *sync.WaitGroup, weeks *sort.Float64Slice) float64 {
defer wg.Done()
totalStories := TotalStories(config)
week := 1
endWeekStories := EndWeekStories(totalStories, config)
for endWeekStories > 0 {
week = week + 1
endWeekStories = EndWeekStories(endWeekStories, config)
}
*weeks = append(*weeks, float64(week))
return float64(week)
}
func getRandBeetween(min, max float64) float64 {
rand.Seed(time.Now().UTC().UnixNano())
return rand.Float64()*(max-min) + min
}
func Percentile(numbers sort.Float64Slice, l int, n int) float64 {
i := l*n/100 - 1
return numbers[i]
}
func Forecast(config Config) []ForecastResult {
var wg sync.WaitGroup
var weeks sort.Float64Slice
for i := 0; i <= config.Simulations; i++ {
wg.Add(1)
go WeeksToZero(config, &wg, &weeks)
}
wg.Wait()
sort.Sort(weeks)
l := len(weeks)
result := []ForecastResult{
{5, Percentile(weeks, l, 5)},
{10, Percentile(weeks, l, 10)},
{15, Percentile(weeks, l, 15)},
{20, Percentile(weeks, l, 20)},
{25, Percentile(weeks, l, 25)},
{30, Percentile(weeks, l, 30)},
{35, Percentile(weeks, l, 35)},
{40, Percentile(weeks, l, 40)},
{45, Percentile(weeks, l, 45)},
{50, Percentile(weeks, l, 50)},
{55, Percentile(weeks, l, 55)},
{60, Percentile(weeks, l, 60)},
{65, Percentile(weeks, l, 55)},
{70, Percentile(weeks, l, 70)},
{75, Percentile(weeks, l, 75)},
{80, Percentile(weeks, l, 80)},
{85, Percentile(weeks, l, 85)},
{90, Percentile(weeks, l, 90)},
{95, Percentile(weeks, l, 95)},
{100, Percentile(weeks, l, 100)},
}
return result
}
func HandleIndex(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
var config = Config{}
config.MinTasks = filterInputToFloat(r.FormValue("MinTasks"))
config.MaxTasks = filterInputToFloat(r.FormValue("MaxTasks"))
config.MinSplitTasks = filterInputToFloat(r.FormValue("MinSplitTasks"))
config.MaxSplitTasks = filterInputToFloat(r.FormValue("MaxSplitTasks"))
config.MinTasksDone = filterInputToFloat(r.FormValue("MinTasksDone"))
config.MaxTasksDone = filterInputToFloat(r.FormValue("MaxTasksDone"))
config.Simulations = 1000
w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
result, _ := json.Marshal(Forecast(config))
io.WriteString(w, string(result))
}
func filterInputToFloat(value string) float64 {
result, err := strconv.ParseFloat(value, 64)
if err != nil {
panic("Error converting value")
}
return result
}
/**
* @api {post} /estimeoprazo Run Monte Carlo Simulations
* @apiVersion 1.0.0
* @apiName EstimePrazo
* @apiGroup GoServices
*
* @apiParam (parameters) {String} MinTasks Minimum of tasks to complete
* @apiParam (parameters) {String} MaxTasks Maximum of tasks to complete
* @apiParam (parameters) {String} MinSplitTasks Split tasks mininum value
* @apiParam (parameters) {String} MaxSplitTasks Split tasks maxium value
* @apiParam (parameters) {String} MinTasksDone Minimum of tasks done
* @apiParam (parameters) {String} MaxTasksDone Maximum of tasks done
*
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
*[
* {
* "Likelihood": 85,
* "Weeks": 5
* },
* {
* "Likelihood": 90,
* "Weeks": 5
* },
* {
* "Likelihood": 95,
* "Weeks": 6
* },
* {
* "Likelihood": 100,
* "Weeks": 10
* }
*]
*/
func init() {
http.HandleFunc("/", HandleIndex)
}