klesh commented on code in PR #3813: URL: https://github.com/apache/incubator-devlake/pull/3813#discussion_r1035795046
########## plugins/helper/api_collector_with_state.go: ########## @@ -0,0 +1,98 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package helper + +import ( + "github.com/apache/incubator-devlake/errors" + "github.com/apache/incubator-devlake/models" + "github.com/apache/incubator-devlake/plugins/core/dal" + "gorm.io/gorm" + "time" +) + +// ApiCollectorStateManager save collector state in framework table +type ApiCollectorStateManager struct { + RawDataSubTaskArgs + *ApiCollector + LatestState models.CollectorLatestState + StartFrom *time.Time Review Comment: How about `CreatedDateAfter`? ########## plugins/jira/impl/impl.go: ########## @@ -168,9 +168,9 @@ func (plugin Jira) PrepareTaskData(taskCtx core.TaskContext, options map[string] return nil, errors.Default.Wrap(err, "unable to get Jira connection") } - var since time.Time - if op.Since != "" { - since, err = time.Parse("2006-01-02T15:04:05Z", op.Since) + var startFrom time.Time + if op.StartFrom != "" { Review Comment: `CreatedDateAfter` ########## plugins/helper/api_collector_with_state.go: ########## @@ -0,0 +1,98 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package helper + +import ( + "github.com/apache/incubator-devlake/errors" + "github.com/apache/incubator-devlake/models" + "github.com/apache/incubator-devlake/plugins/core/dal" + "gorm.io/gorm" + "time" +) + +// ApiCollectorStateManager save collector state in framework table +type ApiCollectorStateManager struct { + RawDataSubTaskArgs + *ApiCollector + LatestState models.CollectorLatestState + StartFrom *time.Time + ExecuteStart time.Time +} + +// NewApiCollectorWithState create a new ApiCollectorStateManager +func NewApiCollectorWithState(args RawDataSubTaskArgs, startFrom *time.Time) (*ApiCollectorStateManager, errors.Error) { + db := args.Ctx.GetDal() + + rawDataSubTask, err := NewRawDataSubTask(args) + if err != nil { + return nil, errors.Default.Wrap(err, "Couldn't resolve raw subtask args") + } + latestState := models.CollectorLatestState{} + err = db.First(&latestState, dal.Where(`raw_data_table = ? AND raw_data_params = ?`, rawDataSubTask.table, rawDataSubTask.params)) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + latestState = models.CollectorLatestState{ + RawDataTable: rawDataSubTask.table, + RawDataParams: rawDataSubTask.params, + } + } else { + return nil, errors.Default.Wrap(err, "failed to load JiraLatestCollectorMeta") + } + } + return &ApiCollectorStateManager{ + RawDataSubTaskArgs: args, + LatestState: latestState, + StartFrom: startFrom, + ExecuteStart: time.Now(), + }, nil +} + +// CanIncrementCollect return if the old data can support collect incrementally. +// only when latest collection is success && +// (m.LatestState.StartFrom == nil means all data have been collected || +// StartFrom at this time exists and later than in the LatestState) +// if StartFrom at this time not exists, collect incrementally only when "m.LatestState.StartFrom == nil" +func (m ApiCollectorStateManager) CanIncrementCollect() bool { + return m.LatestState.LatestSuccessStart != nil && + (m.LatestState.StartFrom == nil || m.StartFrom != nil && m.StartFrom.After(*m.LatestState.StartFrom)) +} + +// InitCollector init the embedded collector +func (m *ApiCollectorStateManager) InitCollector(args ApiCollectorArgs) (err errors.Error) { + args.RawDataSubTaskArgs = m.RawDataSubTaskArgs + m.ApiCollector, err = NewApiCollector(args) + return err +} + +// Execute the embedded collector and record execute state +func (m ApiCollectorStateManager) Execute() errors.Error { + executeErr := m.ApiCollector.Execute() + + db := m.Ctx.GetDal() + m.LatestState.LatestSuccessStart = &m.ExecuteStart + m.LatestState.StartFrom = m.StartFrom + saveErr := db.CreateOrUpdate(&m.LatestState) Review Comment: Shouldn't we save this only when `executeErr == nil`? ########## plugins/jira/jira.go: ########## @@ -32,12 +32,12 @@ func main() { boardId := cmd.Flags().Uint64P("board", "b", 0, "jira board id") _ = cmd.MarkFlagRequired("connection") _ = cmd.MarkFlagRequired("board") - since := cmd.Flags().StringP("since", "s", "", "collect data that are updated after specified time, ie 2006-05-06T07:08:09Z") + startFrom := cmd.Flags().StringP("start_from", "s", "", "collect data that are updated after specified time, ie 2006-05-06T07:08:09Z") cmd.Run = func(c *cobra.Command, args []string) { runner.DirectRun(c, args, PluginEntry, map[string]interface{}{ "connectionId": *connectionId, "boardId": *boardId, - "since": *since, + "startFrom": *startFrom, Review Comment: Same as above ########## plugins/jira/jira.go: ########## @@ -32,12 +32,12 @@ func main() { boardId := cmd.Flags().Uint64P("board", "b", 0, "jira board id") _ = cmd.MarkFlagRequired("connection") _ = cmd.MarkFlagRequired("board") - since := cmd.Flags().StringP("since", "s", "", "collect data that are updated after specified time, ie 2006-05-06T07:08:09Z") + startFrom := cmd.Flags().StringP("start_from", "s", "", "collect data that are updated after specified time, ie 2006-05-06T07:08:09Z") Review Comment: Same as above -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
