import { ActionTypes , LineBreak } from '../constants' ; import * as enums from '../enums' ; import { Dispatch } from 'redux' ; import * as AWS from 'aws-sdk' ; import * as stream from 'stream' ; import { LogGroup , LogStream , Settings , DownloadJob } from '../common-interfaces' ; import { safeFilter } from '../utils' ; import voca from 'voca' ; export interface ShowWindowContent { type : ActionTypes . SHOW_WINDOW_CONTENT ; windowContent : enums . WindowContent ; } export interface RequestLogGroups { type : ActionTypes . REQUEST_LOG_GROUPS ; } export interface ReceiveLogGroups { type : ActionTypes . RECEIVE_LOG_GROUPS ; logGroups : LogGroup [ ] ; lastModified : Date ; } export interface ErrorLogGroups { type : ActionTypes . ERROR_LOG_GROUPS ; errorMessage : string ; } export interface SelectLogGroup { type : ActionTypes . SELECT_LOG_GROUP ; selectedName ? : string ; } export interface RequestLogStreams { type : ActionTypes . REQUEST_LOG_STREAMS ; } export interface ReceiveLogStreams { type : ActionTypes . RECEIVE_LOG_STREAMS ; logStreams : LogStream [ ] ; lastModified : Date ; } export interface ErrorLogStreams { type : ActionTypes . ERROR_LOG_STREAMS ; errorMessage : string ; } export interface SelectLogStream { type : ActionTypes . SELECT_LOG_STREAM ; selectedName ? : string ; } export interface RequestLogText { type : ActionTypes . REQUEST_LOG_TEXT ; } export interface ReceiveLogText { type : ActionTypes . RECEIVE_LOG_TEXT ; text : string ; } export interface ErrorLogText { type : ActionTypes . ERROR_LOG_TEXT ; errorMessage : string ; } export interface RequestLogEvents { type : ActionTypes . REQUEST_LOG_EVENTS ; job : DownloadJob ; } export interface ProgressLogEvents { type : ActionTypes . PROGRESS_LOG_EVENTS ; job : DownloadJob ; } export interface ReceiveLogEvents { type : ActionTypes . RECEIVE_LOG_EVENTS ; job : DownloadJob ; } export interface ErrorLogEvents { type : ActionTypes . ERROR_LOG_EVENTS ; job : DownloadJob ; } export interface SetDateRange { type : ActionTypes . SET_DATERANGE ; startDate : Date ; endDate : Date ; } export interface SaveSettings { type : ActionTypes . SAVE_SETTINGS ; settings : Settings ; lastModified : Date ; } export interface ReceiveSettings { type : ActionTypes . RECEIVE_SETTINGS ; settings : Settings ; lastModified : Date ; } export interface ShowMessage { type : ActionTypes . SHOW_MESSAGE ; message : string ; } export interface HideMessage { type : ActionTypes . HIDE_MESSAGE ; } export type WindowAction = ShowWindowContent ; export type LogGroupAction = RequestLogGroups | ReceiveLogGroups | ErrorLogGroups | SelectLogGroup ; export type LogStreamAction = RequestLogStreams | ReceiveLogStreams | ErrorLogStreams | SelectLogStream ; export type LogTextAction = RequestLogText | ReceiveLogText | ErrorLogText ; export type LogEventAction = RequestLogEvents | ProgressLogEvents | ReceiveLogEvents | ErrorLogEvents ; export type DateRangeAction = SetDateRange ; export type SettingsAction = SaveSettings | ReceiveSettings ; export type MessageAction = ShowMessage | HideMessage ; export function showWindowContent ( windowContent ) { return { type : ActionTypes . SHOW_WINDOW_CONTENT , windowContent : windowContent , } ; } export function requestLogGroups ( ) { return { type : ActionTypes . REQUEST_LOG_GROUPS , } ; } export function receiveLogGroups ( logGroups , lastModified ) { return { type : ActionTypes . RECEIVE_LOG_GROUPS , logGroups : logGroups , lastModified : lastModified , } ; } export function errorLogGroups ( errorMessage ) { return { type : ActionTypes . ERROR_LOG_GROUPS , errorMessage : errorMessage , } ; } export function fetchLogGroups ( settings , getCloudWatchLogGroups ) { return ( dispatch ) => { if ( ! settings . region || ! settings . awsAccessKeyId || ! settings . awsSecretAccessKey ) { return ; } getCloudWatchLogGroups ( ( time ) => dispatch ( requestLogGroups ( ) ) , ( time , err ) => dispatch ( errorLogGroups ( err . message ) ) , ( time , logGroups ) => dispatch ( receiveLogGroups ( logGroups , time ) ) ) ; } ; } export function selectLogGroup ( selectedName ? ) { return { type : ActionTypes . SELECT_LOG_GROUP , selectedName : selectedName , } ; } export function requestLogStreams ( ) { return { type : ActionTypes . REQUEST_LOG_STREAMS , } ; } export function receiveLogStreams ( logStreams , lastModified ) { return { type : ActionTypes . RECEIVE_LOG_STREAMS , logStreams : logStreams , lastModified : lastModified , } ; } export function errorLogStreams ( errorMessage ) { return { type : ActionTypes . ERROR_LOG_STREAMS , errorMessage : errorMessage , } ; } export function fetchLogStreams ( settings , logGroupName , getCloudWatchLogStreams ) { return ( dispatch ) => { getCloudWatchLogStreams ( ( time ) => dispatch ( requestLogStreams ( ) ) , ( time , err ) => dispatch ( errorLogStreams ( err . message ) ) , ( time , logStreams ) => dispatch ( receiveLogStreams ( logStreams , time ) ) ) ; } ; } export function selectLogStream ( selectedName ? ) { return { type : ActionTypes . SELECT_LOG_STREAM , selectedName : selectedName , } ; } export function setDateRange ( startDate , endDate ) { return { type : ActionTypes . SET_DATERANGE , startDate , endDate , } ; } const trimmer = ( settings ) => ( settings . lineBreak === LineBreak . NO_MODIFICATION ) ? line => line : line => voca . trimRight ( line , '\r\n' ) ; const separator = ( lineBreak ) => { switch ( lineBreak ) { case LineBreak . LF : return '\n' ; case LineBreak . CRLF : return '\r\n' ; case LineBreak . NO_MODIFICATION : default : return '' ; } } ; export function requestLogText ( ) { return { type : ActionTypes . REQUEST_LOG_TEXT , } ; } export function receiveLogText ( text ) { return { type : ActionTypes . RECEIVE_LOG_TEXT , text , } ; } export function errorLogText ( errorMessage ) { return { type : ActionTypes . ERROR_LOG_TEXT , errorMessage , } ; } export function fetchLogText ( settings , getCloudWatchLogsEvents ) { let logs = '' ; return ( dispatch ) => { dispatch ( requestLogText ( ) ) ; let callbackData = ( data ) => { if ( ! data . events ) { return ; } let sep = separator ( settings . lineBreak ) ; let messages = data . events . filter ( e => e . message != null ) . map ( e => e . message ! ) . map ( trimmer ( settings ) ) . join ( sep ) ; logs = logs + messages + sep ; } ; let callbackError = ( err ) => dispatch ( errorLogText ( err . message ) ) ; let callbackEnd = ( ) => dispatch ( receiveLogText ( logs ) ) ; getCloudWatchLogsEvents ( callbackData , callbackError , callbackEnd ) ; } ; } export function requestLogEvents ( job ) { return { type : ActionTypes . REQUEST_LOG_EVENTS , job , } ; } export function progressLogEvents ( job ) { return { type : ActionTypes . PROGRESS_LOG_EVENTS , job , } ; } export function receiveLogEvents ( job ) { return { type : ActionTypes . RECEIVE_LOG_EVENTS , job , } ; } export function errorLogEvents ( job ) { return { type : ActionTypes . ERROR_LOG_EVENTS , job , } ; } export function downloadLogs ( settings , fileChooser , job , getCloudWatchLogsEvents , filter = ( line ) => line ) { return ( dispatch ) => { let choosed = fileChooser ( ) ; if ( ! choosed ) { return ; } const out = choosed ; dispatch ( requestLogEvents ( job ) ) ; let callbackData = ( data , progress ) => { if ( ! data . events ) { return ; } let sep = separator ( settings . lineBreak ) ; let messages = data . events . filter ( e => e . message != null ) . map ( e => e . message ! ) . map ( safeFilter ( filter ) ) . filter ( e => e !== undefined ) . map ( trimmer ( settings ) ) . join ( sep ) ; out . write ( messages ) ; out . write ( sep ) ; job = { ... job , progress : progress } ; dispatch ( progressLogEvents ( job ) ) ; } ; let callbackError = ( err ) => { out . end ( ) ; dispatch ( errorLogEvents ( job ) ) ; } ; let callbackEnd = ( ) => { out . end ( ) ; job = { ... job , progress : 1.0 } ; dispatch ( receiveLogEvents ( job ) ) ; } ; getCloudWatchLogsEvents ( callbackData , callbackError , callbackEnd ) ; } ; } export function saveSettings ( settings , lastModified , save ) { save ( settings ) ; return { type : ActionTypes . SAVE_SETTINGS , settings : settings , lastModified : lastModified , } ; } export function loadSettings ( load , now ) { return ( dispatch ) => { load ( ) . then ( ( settings ) => { dispatch ( receiveSettings ( settings , now ( ) ) ) ; } ) ; } ; } export function receiveSettings ( settings , lastModified ) { return { type : ActionTypes . RECEIVE_SETTINGS , settings : settings , lastModified : lastModified , } ; } export function showMessage ( message ) { return { type : ActionTypes . SHOW_MESSAGE , message : message , } ; } export function hideMessage ( ) { return { type : ActionTypes . HIDE_MESSAGE , } ; }