/
plan.go
297 lines (269 loc) · 8.34 KB
/
plan.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// SPDX-License-Identifier: Apache-2.0
package cmd
import (
"bytes"
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/rgolangh/calgo/internal/google_calendar"
"github.com/spf13/cobra"
"google.golang.org/api/calendar/v3"
"log"
"time"
)
var (
interactive bool
meetingsTime time.Duration
focusTime time.Duration
focusEventDuration time.Duration
tasks time.Duration
)
type Slot struct {
StartTime time.Time
EndTime time.Time
}
type Focus struct {
Duration time.Duration
Title string `default:"Focus Time"`
}
type Meeting struct {
Duration time.Duration
Title string
Description string
Attendees []*calendar.EventAttendee
}
type Plan struct {
eventInserter func(event *calendar.Event) (*calendar.Event, error)
calendarId string
// target date of plan, either today or future
date time.Time
events Events
overallFocusTime time.Duration
focusDuration time.Duration
slots []Slot
focuses []Focus
meetings []Meeting
}
func newPlan(calId string, service *calendar.Service) *Plan {
events, err := service.Events.List(calendarID).
ShowDeleted(false).
SingleEvents(true).
TimeMin(time.Now().Format(time.RFC3339)).
TimeMax(endOfDay(time.Now()).Format(time.RFC3339)).
OrderBy("startTime").
Do()
if err != nil {
log.Fatalf("Unable to retrieve next ten of the user's events: %v", err)
}
eventInserter := func(event *calendar.Event) (*calendar.Event, error) {
return service.Events.Insert(calendarID, event).Do()
}
plannedEvents := newEvents()
plannedEvents.addAll(events.Items)
return &Plan{
date: time.Now(),
eventInserter: eventInserter,
calendarId: calId,
overallFocusTime: focusTime,
focusDuration: focusEventDuration,
events: plannedEvents,
}
}
func (p *Plan) plan() error {
for focusDurationToAdd := p.overallFocusTime; focusDurationToAdd >= p.focusDuration; focusDurationToAdd -= p.focusDuration {
slot, err := p.findNextSlot()
if err != nil {
log.Printf("failed finding slot %v\n", err)
continue
}
log.Printf("found a slot on %v\n", slot.Format(time.Kitchen))
p.events.insert(newFocusEvent(slot, p.focusDuration))
}
return nil
}
func (p *Plan) commit() error {
if interactive {
var commit bool
err := survey.AskOne(&survey.Confirm{Message: "Commit changes to the calendar?", Default: true}, &commit)
if err != nil {
return err
}
if !commit {
return nil
}
}
for _, newEvent := range p.getAddedEvents() {
_, err := p.eventInserter(newEvent)
if err != nil {
log.Fatalf("Unable to create event. %v\n", err)
}
}
return nil
}
func (p *Plan) String() string {
buf := bytes.NewBufferString("")
fmt.Fprintf(buf, "\n\nPlan for %v %d events\n", p.date.Format(time.RFC822), p.events.Len())
for e := p.events.Front(); e != nil; e = e.Next() {
v := e.Value.(*calendar.Event)
fmt.Fprintf(buf, eventString(v))
}
return buf.String()
}
// planCmd represents the plan command
var planCmd = &cobra.Command{
Use: "plan",
Short: "Plan your day",
Long: `Plan your day, add meetings, focus times, and break time.`,
Example: `$ calgo plan --focus-time 5h --tasks 1 --break 1
#
dd/mm/yy, today, 0 meetings
[focus time] duration(minutes) for each interval?(45): 50
[focus time] optional event name?(focus time): create calgo
✔ done
[meeting 1] event name? discuss new requirements
[meeting 1] duration?(50m):
[meeting 1] attendants (tab to autocomplete, enter twice to done):
rgo(tab) - rgolan@redhat.com
`,
RunE: func(cmd *cobra.Command, args []string) error {
srv := google_calendar.Service()
//focuses := surveyFocus()
//meetings := surveyMeetings()
plan := newPlan(calendarID, srv)
err := plan.plan()
if err != nil {
return err
}
log.Println(plan)
plan.commit()
return nil
},
Args: func(cmd *cobra.Command, args []string) error {
if focusTime < focusEventDuration {
return fmt.Errorf("--focus-time (%s) must be greater then or equal to --focus-event-duration (%s)", focusTime, focusEventDuration)
}
return nil
},
}
func surveyFocus() []Focus {
focuses := []Focus{}
var duration = time.Minute * 45
var title = "Focus Time"
// prevent 0, make sure = < focusTime
survey.AskOne(&survey.Input{Message: "duration(minutes) for each focus slot?", Default: "45m"}, &duration)
survey.AskOne(&survey.Input{Message: "focus time title?", Default: "Focus Time"}, &title)
var nFocuses = int(focusTime / duration)
for i := 0; i < nFocuses; i++ {
focuses = append(focuses, Focus{
Duration: duration,
Title: title,
})
}
return focuses
}
func surveyMeetings() []Meeting {
if meetingsTime == 0 {
return nil
}
meetings := []Meeting{}
var duration = time.Minute * 45
var title = ""
// prevent 0, make sure = < focusTime
survey.AskOne(&survey.Input{Message: "duration in minutes for each meeting?", Default: "45m"}, &duration)
survey.AskOne(&survey.Input{Message: "meeting title?"}, &title)
var nFocuses = int(meetingsTime / duration)
for i := 0; i < nFocuses; i++ {
meetings = append(meetings, Meeting{
Duration: duration,
Title: title,
})
}
return meetings
}
func newFocusEvent(startTime time.Time, duration time.Duration) *calendar.Event {
return &calendar.Event{
Summary: "Focus Time",
Description: "Focus Time",
EventType: "focusTime",
Start: &calendar.EventDateTime{
DateTime: startTime.Format(time.RFC3339),
},
End: &calendar.EventDateTime{
DateTime: startTime.Add(duration).Format(time.RFC3339),
},
//Attendees: []*calendar.EventAttendee{
// &calendar.EventAttendee{Email:"lpage@example.com"},
// &calendar.EventAttendee{Email:"sbrin@example.com"},
//},
}
}
func (p *Plan) getAddedEvents() []*calendar.Event {
events := []*calendar.Event{}
for elm := p.events.Front(); elm != nil; elm = elm.Next() {
e := elm.Value.(*calendar.Event)
// events with no ID are new ones, not added yet.
if e.Id == "" {
events = append(events, e)
}
}
return events
}
// find the next slot to add an event
func (p *Plan) findNextSlot() (time.Time, error) {
// walk the list of events, which are sorted by start time, measure
// a free period of the wanted duration by starting a point in time, now for
// today, or start of day if in future.
// measure from that markpoint to next meeting start date , test if it can
// contain the period.
// TODO handle overlapping meetings to calculate the available periods.
// currently naively assuming there is no overlap.
// advanced the markpoint to the end of current meeting and start again
var markpoint time.Time
y, m, d := p.date.Date()
y1, m1, d1 := time.Now().Date()
if y == y1 && m == m1 && d == d1 {
s := startOfDay(time.Now())
// it maybe that the day already started and we want to plan. if now
// is later the startofday then use it.
if time.Now().After(s) {
markpoint = time.Now()
} else {
markpoint = s
}
} else {
markpoint = startOfDay(p.date)
}
for elm := p.events.Front(); elm != nil; elm = elm.Next() {
nextEvent := elm.Value.(*calendar.Event)
nextEventStartTime, err := time.Parse(time.RFC3339, nextEvent.Start.DateTime)
if err != nil {
return markpoint, err
}
if markpoint.Add(p.focusDuration).Before(nextEventStartTime) {
return markpoint, nil
}
nextEventEndTime, err := time.Parse(time.RFC3339, nextEvent.End.DateTime)
if err != nil {
return markpoint, err
}
markpoint = nextEventEndTime
if elm.Next() == nil {
// last element
if markpoint.Add(p.focusDuration).Before(endOfDay(p.date)) {
return markpoint, nil
}
}
}
log.Println("couldn't find a slot")
return markpoint, fmt.Errorf("couldn't not find a slot")
}
func init() {
planCmd.Flags().BoolVar(&interactive, "interactive", true, "Ask before committing changes, ask optional inputs")
planCmd.Flags().DurationVar(&focusTime, "focus-time", time.Minute*45, "desired overall focus time duration (e.g 45m, 1h20m)")
planCmd.Flags().DurationVar(&focusEventDuration, "focus-event-duration", time.Minute*45, "desired focus time per event. An overall focus time is devided to events (e.g 45m, 1h20m)")
planCmd.Flags().DurationVar(&meetingsTime, "meetings", 0, "desired meetings overall time duration (e.g 1h30m")
planCmd.Flags().DurationVar(&tasks, "break", time.Hour, "desired break time duration (e.g 1h)")
rootCmd.AddCommand(planCmd)
}
var eventFormat = func(title, startTime, endTime string) string {
return fmt.Sprintf("%17s: %s", startTime+"-"+endTime, title)
}