{
   "templateGroup": {
      "uuid": "twitter-analytics",
      "name": "Twitter Analytics",
      "description": "Analyzes data from Twitter",
      "ruleTemplates": [
         {
            "uuid": "popular-tweets-analysis",
            "name": "Popular Tweets Analysis",
            "description": "Analyzes real time tweets and display popular tweets for the given query",
            "type": "template",
            "instanceCount": "one",
            "script": "var count = validateCount('${userInputForcount}');\n\n/**\n * Validates Tweet Count \n */\nfunction validateCount(input) {\n\tif (!isNaN(input) && input <= 100) {\n\t   return input;\n\t}\n\tthrow 'Number of tweets per page can not exceed 100';\n}\n\nvar pollingInterval = validatePollingInterval('${userInputForpollingInterval}');\n\n/**\n * Validates Polling Interval \n */\nfunction validatePollingInterval(input) {\n\tif (!isNaN(input)) {\n\t   return input;\n\t}\n\tthrow 'Polling Interval should be a number';\n}\n",
            "templates": [
               {
                  "type": "siddhiApp",
                  "content": "@App:name('TwitterAnalytics') \n@App:description('Twitter Analytics using Twitter Extension') \n\n@source(type='twitter',  consumer.key='${consumerKey}', consumer.secret='${consumerSecret}', \naccess.token ='${accessToken}', access.token.secret='${accessTokenSecret}', mode=\"streaming\",  track='${track}',  language = '${language}',  @map(type='keyvalue', fail.on.missing.attribute='false', @attributes(created_at = 'createdAt', id = 'tweetId' , text = 'text',location = 'user.location', hashtags = 'hashtags' , mentions = 'userMentions',trackwords = 'track.words')))\n\ndefine stream Tweetstream(created_at string, id long, text string, location string, hashtags string, mentions string,trackwords string);\n--define stream Tweetstream(created_at string, id long, text string, location string, hashtags string, mentions string);\n\ndefine stream Sentimentstream(count long,id long, timestamp string,CDate string, TweetID string, value int,country string, hashtags string, mentions string);\ndefine stream SentimentTypestream(count long,id long ,timestamp string,CDate string, TweetID string, value int, type string, country string , hashtags string, mentions string);\n\n@source(type='twitter' , consumer.key='${consumerKey}', consumer.secret='${consumerSecret}', \naccess.token ='${accessToken}', access.token.secret='${accessTokenSecret}', mode='polling', count = '${count}', query = '${query}', polling.interval = '${pollingInterval}', result.type = 'popular', @map(type='keyvalue', fail.on.missing.attribute='false', @attributes(createdAt = 'createdAt', id = 'tweetId', text= 'text')))\n\ndefine stream TweetStream(createdAt string, id long, text string);\n\n@Store(type=\"rdbms\",datasource=\"Twitter_Analytics\")\ndefine aggregation TweetAggregation from SentimentTypestream select count() as tweetCount, avg(value) as avgEmotionsIndex aggregate by timestamp every sec ... year;\n\n@Store(type=\"rdbms\",datasource=\"Twitter_Analytics\", field.length=\"mentions:8000\")\ndefine table sentiment(count long,id long, timestamp string,CDate string, TweetID string, value int, type string, country string, hashtags string, mentions string);\n\n@Store(type=\"rdbms\",datasource=\"Twitter_Analytics\")\ndefine table textCloud(id long, timestamp long, words string);\n\n@Store(type=\"rdbms\",datasource=\"Twitter_Analytics\")\ndefine table hashtagCloud(id long, timestamp long, hashtag string);\n\n@Store(type=\"rdbms\",datasource=\"Twitter_Analytics\")\ndefine table mentionCloud(id long, timestamp long, mention string);\n\n@Store(type=\"rdbms\",datasource=\"Twitter_Analytics\")\ndefine table hashTag(id long, trackwords string);\n\n@Store(type=\"rdbms\",datasource=\"Twitter_Analytics\")\ndefine table PopularTweet (timestamp long, TweetID string);\n\n\n--Sentiment Analysis\nfrom Tweetstream\nselect count() as count,currentTimeMillis() as id,time:utcTimestamp() as timestamp,created_at as CDate,convert(id,'string') as TweetID, sentiment:getRate(text) as value,ifThenElse(location is null,'undefined',find:country(location))  as country, hashtags, mentions\ninsert into Sentimentstream;\n\nfrom Sentimentstream[value<0]\nselect count, id,timestamp,CDate,TweetID, value,'negative' as type,country, hashtags, mentions\ninsert into SentimentTypestream;\n\nfrom Sentimentstream[value>=0]\nselect count, id,timestamp,CDate,TweetID, value, ifThenElse(value>0,'positive','neutral') as type,country, hashtags, mentions\ninsert into SentimentTypestream;\n\n@info(name='query1')\nfrom SentimentTypestream\nselect *\ninsert into sentiment;\n\nfrom Tweetstream\nselect currentTimeMillis() as id, trackwords\ninsert into Outputstream;\n\nfrom Outputstream\nselect *\ninsert into hashTag;\n\n--Words for text cloud\n@info(name='Filtering words for text cloud')\nfrom Tweetstream#text:tokenize(text)\ninsert all events into textStream;\n\nfrom textStream[filter:word(token)]\nselect count() as id, currentTimeMillis() as timestamp,token as words\ninsert into Tokens;\n\n@info(name='inserting into database')\nfrom Tokens\nselect *\ninsert into textCloud;\n\n--Words for hashtag cloud\n@info(name='Filtering words for hashtag cloud')\nfrom Tweetstream#entity:tokenize(hashtags)\nselect count() as id, currentTimeMillis() as timestamp, token as hashtag\ninsert all events into hashtagstream;\n\n@info(name='inserting into hashtag cloud table')\nfrom hashtagstream\nselect *\ninsert into hashtagCloud;\n\n--Words for mention cloud\n@info(name='Filtering words for mention cloud')\nfrom Tweetstream#entity:tokenize(mentions)\nselect count() as id, currentTimeMillis() as timestamp, token as mention\ninsert all events into mentionstream;\n\n@info(name='inserting into mention table')\nfrom mentionstream\nselect *\ninsert into mentionCloud;\n\n@info(name='fetching popular tweets')\nfrom TweetStream\nselect currentTimeMillis() as timestamp,convert(id,'string') as TweetID\ninsert into PopularStream;\n\n@info(name='inserting into popular Tweets table')\nfrom PopularStream\nselect *\ninsert into PopularTweet;"
               }
            ],
            "properties": {
               "consumerKey": {
                  "fieldName": "Consumer Key",
                  "description": "API key to access the created Twitter app",
                  "defaultValue": ""
               },
               "consumerSecret": {
                  "fieldName": "Consumer Secret",
                  "description": "API secret to access the created Twitter app",
                  "defaultValue": ""
               },
               "accessToken": {
                  "fieldName": "Access Token",
                  "description": "Used to make API requests on behalf of your account",
                  "defaultValue": ""
               },
               "accessTokenSecret": {
                  "fieldName": "Access Token Secret",
                  "description": "Used to make API requests on behalf of your account",
                  "defaultValue": ""
               },
               "track": {
                  "fieldName": "Filtering Keywords",
                  "description": "Tweets that have these keywords will be filtered",
                  "defaultValue": "Amazon,Google"
               },
               "language": {
                  "fieldName": "Filtering Language",
                  "description": "Language by which, tweets are filtered",
                  "defaultValue": "en"
               },
               "query": {
                  "fieldName": "Query",
                  "description": "Tweets are filtered, when matching to the given Query",
                  "defaultValue": "#Amazon"
               },
               "userInputForcount": {
                  "fieldName": "Count",
                  "description": "Number of tweets per page, up to a maximum of 100",
                  "defaultValue": "100"
               },
               "userInputForpollingInterval": {
                  "fieldName": "Polling Interval",
                  "description": "Period of time (in seconds) to poll tweets periodically",
                  "defaultValue": "300"
               }
            }
         }
      ]
   }
}