This repository has been archived by the owner on May 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for core-data endpoints
This commit adds support for the `event` and `reading` endpoints. ``` edgex-cli event add edgex-cli event count edgex-cli event list edgex-cli event rm edgex-cli reading count edgex-cli reading list ``` the `count` and `list` commands support the `-j` argument to return the raw json result. Signed-off-by: Siggi Skulason <siggi.skulason@canonical.com>
- Loading branch information
Siggi Skulason
committed
Oct 7, 2021
1 parent
fa2629c
commit dc49a0c
Showing
11 changed files
with
644 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
/* | ||
* Copyright (C) 2021 Canonical Ltd | ||
* | ||
* Licensed 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0' | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"text/tabwriter" | ||
"time" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var eventLimit, eventOffset int | ||
var eventDevice, eventProfile, eventSource string | ||
var eventAge int | ||
var numberOfReadings int | ||
|
||
func init() { | ||
eventCmd := initEventCommand() | ||
initListEventCommand(eventCmd) | ||
initCountEventCommand(eventCmd) | ||
initRmEventCommand(eventCmd) | ||
initAddEventCommand(eventCmd) | ||
} | ||
|
||
func initEventCommand() *cobra.Command { | ||
var cmd = &cobra.Command{ | ||
Use: "event", | ||
Short: "Add, remove and list events", | ||
Long: ``, | ||
SilenceUsage: true, | ||
} | ||
|
||
rootCmd.AddCommand(cmd) | ||
addFormatFlags(cmd) | ||
return cmd | ||
} | ||
|
||
func initListEventCommand(cmd *cobra.Command) { | ||
var listCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "List events", | ||
Long: `List all events, optionally specifying a limit and offset`, | ||
RunE: handleListEvents, | ||
SilenceUsage: true, | ||
} | ||
|
||
cmd.AddCommand(listCmd) | ||
addFormatFlags(listCmd) | ||
listCmd.Flags().IntVarP(&eventLimit, "limit", "l", 50, "The number of items to return. Specifying -1 will return all remaining items") | ||
listCmd.Flags().IntVarP(&eventOffset, "offset", "o", 0, "The number of items to skip") | ||
} | ||
|
||
func initCountEventCommand(cmd *cobra.Command) { | ||
var countCmd = &cobra.Command{ | ||
Use: "count", | ||
Short: "Count available events", | ||
Long: `Count the number of events in core data, optionally filtering by device name`, | ||
RunE: handleCountEvents, | ||
SilenceUsage: true, | ||
} | ||
|
||
countCmd.Flags().StringVarP(&eventDevice, "device", "e", "", "Device name") | ||
cmd.AddCommand(countCmd) | ||
addFormatFlags(countCmd) | ||
} | ||
|
||
func initRmEventCommand(cmd *cobra.Command) { | ||
var rmcmd = &cobra.Command{ | ||
Use: "rm", | ||
Short: "Remove events", | ||
Long: `Remove events, specifying either device name or maximum event age in milliseconds | ||
'edgex-cli event rm --device {devicename}' removes all events for the specified device | ||
'edgex-cli event rm --age {ms}' removes all events generated in the last {ms} milliseconds`, | ||
RunE: handleRmEvents, | ||
SilenceUsage: true, | ||
} | ||
|
||
rmcmd.Flags().StringVarP(&eventDevice, "device", "e", "", "Device name") | ||
rmcmd.Flags().IntVarP(&eventAge, "age", "a", 0, "Event age (in milliseconds)") | ||
cmd.AddCommand(rmcmd) | ||
} | ||
|
||
func initAddEventCommand(cmd *cobra.Command) { | ||
var addCmd = &cobra.Command{ | ||
Use: "add", | ||
Short: "Create an event", | ||
Long: `Create an event with a specified number of random readings`, | ||
RunE: handleAddEvents, | ||
SilenceUsage: true, | ||
} | ||
addCmd.Flags().StringVarP(&eventDevice, "device", "e", "", "Device name") | ||
addCmd.Flags().StringVarP(&eventProfile, "profile", "p", "", "Profile name") | ||
addCmd.Flags().StringVarP(&eventSource, "source", "s", "", "Event source name (ResourceName or CommandName)") | ||
addCmd.Flags().IntVarP(&numberOfReadings, "readings", "r", 0, "Number of sample readings to create") | ||
addCmd.MarkFlagRequired("device") | ||
addCmd.MarkFlagRequired("profile") | ||
addCmd.MarkFlagRequired("source") | ||
cmd.AddCommand(addCmd) | ||
} | ||
|
||
func handleAddEvents(cmd *cobra.Command, args []string) error { | ||
id, err := getCoreDataService().CreateEvent(eventDevice, eventProfile, eventSource, numberOfReadings) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println("Event " + id + " created") | ||
return nil | ||
} | ||
|
||
func handleRmEvents(cmd *cobra.Command, args []string) error { | ||
err := getCoreDataService().RemoveEvents(eventDevice, eventAge) | ||
return err | ||
} | ||
|
||
func handleCountEvents(cmd *cobra.Command, args []string) error { | ||
if json || verbose { | ||
var json, url string | ||
var err error | ||
|
||
if eventDevice != "" { | ||
json, url, err = getCoreDataService().CountEventsByDeviceJSON(eventDevice) | ||
} else { | ||
json, url, err = getCoreDataService().CountEventsJSON() | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
if verbose { | ||
fmt.Printf("Result:%sURL: %s\n", json, url) | ||
} else { | ||
fmt.Printf(json) | ||
} | ||
|
||
} else { | ||
count, err := getCoreDataService().CountEvents(eventDevice) | ||
if err != nil { | ||
return err | ||
} | ||
if eventDevice != "" { | ||
fmt.Printf("Total %s events: %v\n", eventDevice, count.Count) | ||
} else { | ||
fmt.Printf("Total events: %v\n", count.Count) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func handleListEvents(cmd *cobra.Command, args []string) error { | ||
events, err := getCoreDataService().ListAllEvents(eventOffset, eventLimit) | ||
if err != nil { | ||
return err | ||
} | ||
if len(events) == 0 { | ||
fmt.Println("No events available") | ||
return nil | ||
} | ||
|
||
if json || verbose { | ||
var json, url string | ||
|
||
json, url, err = getCoreDataService().ListAllEventsJSON(eventOffset, eventLimit) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if verbose { | ||
fmt.Printf("Result:%sURL: %s\n", json, url) | ||
} else { | ||
fmt.Printf(json) | ||
} | ||
|
||
} else { | ||
w := tabwriter.NewWriter(os.Stdout, 1, 1, 2, ' ', 0) | ||
fmt.Fprintln(w, "Origin\tDevice\tProfile\tSource") | ||
for _, event := range events { | ||
tm := time.Unix(0, event.Origin) | ||
sTime := tm.Format(time.RFC1123) | ||
fmt.Fprintf(w, "%s\t%s\t%s\t%v\n", | ||
sTime, event.DeviceName, event.ProfileName, event.SourceName) | ||
|
||
} | ||
w.Flush() | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.