|
| 1 | +import { CommanderStatic } from "commander"; |
| 2 | +import { log } from "console"; |
| 3 | +import * as fs from "fs"; |
| 4 | +import { isArray } from "lodash"; |
| 5 | +import * as path from "path"; |
| 6 | +import { EventAnalyticsClient } from "../../api/sdk"; |
| 7 | +import { retry } from "../../api/utils"; |
| 8 | +import { errorLog, getColor, getSdk, homeDirLog, proxyLog, serviceCredentialLog, verboseLog } from "./command-utils"; |
| 9 | + |
| 10 | +const color = getColor("blue"); |
| 11 | + |
| 12 | +export default (program: CommanderStatic) => { |
| 13 | + program |
| 14 | + .command("event-analytics") |
| 15 | + .alias("ea") |
| 16 | + .option("-m, --mode [count|filter|duplicate|top]", `mode see ${color("@ Additional Documentation")}`, "top") |
| 17 | + .option("-f, --file <file>", `events file`) |
| 18 | + .option("-o, --output <output>", `result ${color("mode")}.ea.mdsp.json`) |
| 19 | + .option( |
| 20 | + "-t, --type [timeseries|event]", |
| 21 | + `event analytics can be used on both timeseries (with string properties as event names) and event formats`, |
| 22 | + "event" |
| 23 | + ) |
| 24 | + .option("-p, --property <property>", `property name used for grouping and counting`, "description") |
| 25 | + .option("-l, --filterlist <filterlist>", `filter events`, "[]") |
| 26 | + .option("-x, --top <top>", "number of events (for top mode)", "10") |
| 27 | + .option("-s, --split <split>", "split interval (for count, duplicate)", "5000") |
| 28 | + .option("-y, --retry <number>", "retry attempts before giving up", "3") |
| 29 | + .option("-p, --passkey <passkey>", `passkey`) |
| 30 | + .option("-v, --verbose", "verbose output") |
| 31 | + .description(`${color("analyze mindsphere events @")}`) |
| 32 | + .action((options) => { |
| 33 | + (async () => { |
| 34 | + try { |
| 35 | + checkRequiredParameters(options); |
| 36 | + const sdk = getSdk(options); |
| 37 | + homeDirLog(options.verbose, color); |
| 38 | + proxyLog(options.verbose, color); |
| 39 | + const fileName = path.resolve(options.file); |
| 40 | + const data = fs.readFileSync(fileName).toString(); |
| 41 | + const events = JSON.parse(data); |
| 42 | + |
| 43 | + let newEvents: any[] = events; |
| 44 | + |
| 45 | + if (options.type === "event") { |
| 46 | + newEvents = []; |
| 47 | + |
| 48 | + events.events.forEach((element: any) => { |
| 49 | + const newObject: any = { _time: element.timestamp }; |
| 50 | + newObject[options.property] = element[options.property]; |
| 51 | + newEvents.push(newObject); |
| 52 | + }); |
| 53 | + } |
| 54 | + const ea = sdk.GetEventAnalyticsClient(); |
| 55 | + |
| 56 | + const output = options.output || `${options.mode}.ea.mdsp.json`; |
| 57 | + |
| 58 | + switch (options.mode) { |
| 59 | + case "top": |
| 60 | + await getTopEvents(ea, options, newEvents, output); |
| 61 | + break; |
| 62 | + |
| 63 | + case "count": |
| 64 | + await countEvents(ea, options, newEvents, output); |
| 65 | + break; |
| 66 | + |
| 67 | + case "duplicate": |
| 68 | + await removeDuplicates(ea, options, newEvents, output); |
| 69 | + break; |
| 70 | + |
| 71 | + case "filter": |
| 72 | + await filterEvents(ea, options, newEvents, output); |
| 73 | + break; |
| 74 | + |
| 75 | + default: |
| 76 | + throw Error(`no such option: ${options.mode}`); |
| 77 | + } |
| 78 | + } catch (err) { |
| 79 | + errorLog(err, options.verbose); |
| 80 | + } |
| 81 | + })(); |
| 82 | + }) |
| 83 | + .on("--help", () => { |
| 84 | + log("\n Examples:\n"); |
| 85 | + log( |
| 86 | + ` mc event-analytics --mode top --file events.json --property description \t\t find the top 10 events in events.json` |
| 87 | + ); |
| 88 | + log( |
| 89 | + ` mc event-analytics --mode duplicate --file events.json --property description --split 5000 \t\t remove all duplicate events` |
| 90 | + ); |
| 91 | + log("\n Additional Documentation:\n"); |
| 92 | + log( |
| 93 | + ` ${color( |
| 94 | + "https://developer.mindsphere.io/apis/analytics-eventanalytics/api-eventanalytics-overview.html" |
| 95 | + )}` |
| 96 | + ); |
| 97 | + |
| 98 | + log( |
| 99 | + ` ${color( |
| 100 | + "https://developer.mindsphere.io/apis/analytics-eventanalytics/api-eventanalytics-samples.html" |
| 101 | + )}` |
| 102 | + ); |
| 103 | + |
| 104 | + serviceCredentialLog(color); |
| 105 | + }); |
| 106 | +}; |
| 107 | + |
| 108 | +async function getTopEvents(ea: EventAnalyticsClient, options: any, newEvents: any[], output: any) { |
| 109 | + const result = await retry(options.retry, () => |
| 110 | + ea.FindTopEvents({ |
| 111 | + numberOfTopPositionsRequired: parseInt(options.top), |
| 112 | + eventsMetadata: { eventTextPropertyName: options.property }, |
| 113 | + events: newEvents, |
| 114 | + }) |
| 115 | + ); |
| 116 | + fs.writeFileSync(output, JSON.stringify(result, null, 2)); |
| 117 | + verboseLog(JSON.stringify(result, null, 2), options.verbose); |
| 118 | + console.log(`wrote results to ${color(output)}`); |
| 119 | +} |
| 120 | + |
| 121 | +async function countEvents(ea: EventAnalyticsClient, options: any, newEvents: any[], output: any) { |
| 122 | + const result = await retry(options.retry, () => |
| 123 | + ea.CountEvents({ |
| 124 | + eventsMetadata: { |
| 125 | + eventTextPropertyName: options.property, |
| 126 | + splitInterval: parseInt(options.split), |
| 127 | + }, |
| 128 | + events: newEvents, |
| 129 | + }) |
| 130 | + ); |
| 131 | + fs.writeFileSync(output, JSON.stringify(result, null, 2)); |
| 132 | + verboseLog(JSON.stringify(result, null, 2), options.verbose); |
| 133 | + console.log(`wrote results to ${color(output)}`); |
| 134 | +} |
| 135 | + |
| 136 | +async function removeDuplicates(ea: EventAnalyticsClient, options: any, newEvents: any[], output: any) { |
| 137 | + const result = await retry(options.retry, () => |
| 138 | + ea.RemoveDuplicateEvents({ |
| 139 | + eventsMetadata: { |
| 140 | + eventTextPropertyName: options.property, |
| 141 | + splitInterval: parseInt(options.split), |
| 142 | + }, |
| 143 | + events: newEvents, |
| 144 | + }) |
| 145 | + ); |
| 146 | + fs.writeFileSync(output, JSON.stringify(result, null, 2)); |
| 147 | + verboseLog(JSON.stringify(result, null, 2), options.verbose); |
| 148 | + console.log(`wrote results to ${color(output)}`); |
| 149 | +} |
| 150 | + |
| 151 | +async function filterEvents(ea: EventAnalyticsClient, options: any, newEvents: any[], output: any) { |
| 152 | + const resultFilter = await retry(options.retry, () => |
| 153 | + ea.FilterEvents({ |
| 154 | + eventsMetadata: { |
| 155 | + eventTextPropertyName: options.property, |
| 156 | + }, |
| 157 | + events: newEvents, |
| 158 | + filterList: options.filterlist ? JSON.parse(options.filterlist) : [], |
| 159 | + }) |
| 160 | + ); |
| 161 | + fs.writeFileSync(output, JSON.stringify(resultFilter, null, 2)); |
| 162 | + verboseLog(JSON.stringify(resultFilter, null, 2), options.verbose); |
| 163 | + console.log(`wrote results to ${color(output)}`); |
| 164 | +} |
| 165 | + |
| 166 | +function checkRequiredParameters(options: any) { |
| 167 | + !["timeseries", "event"].includes(options.type) && |
| 168 | + errorLog(`invalid type ${options.type}; type must be timeseries or event`, true); |
| 169 | + |
| 170 | + if (options.filterlist) { |
| 171 | + try { |
| 172 | + const obj = JSON.parse(options.filterlist); |
| 173 | + if (!isArray(obj)) { |
| 174 | + errorLog( |
| 175 | + "invalid filter, you have to pass an array with event names. example --filterlist '[\"Flow to low\"]' ", |
| 176 | + true |
| 177 | + ); |
| 178 | + } |
| 179 | + } catch (error) { |
| 180 | + errorLog( |
| 181 | + "invalid filter, you have to pass an array with event names. example --filterlist '[\"Flow to low\"]' ", |
| 182 | + true |
| 183 | + ); |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments